Skip to content

Commit

Permalink
* switch to datamapper 1.0.0
Browse files Browse the repository at this point in the history
* added execute_update method to the adapter which allows to excute SQL directly

* added new hsqldb jdbc driver in pom
  • Loading branch information
mkristian committed Jun 19, 2010
1 parent cbee9a3 commit 303a777
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 33 deletions.
18 changes: 16 additions & 2 deletions lib/dm-hibernate-adapter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
require 'java'
begin
require 'dm-hibernate-adapter_ext.jar'
rescue LoadError
warn "missing extension jar, may it is already in the parent classloader"
end
import 'de.saumya.jibernate.UpdateWork'
require 'slf4r'

if require 'dm-core'
Expand Down Expand Up @@ -176,6 +183,13 @@ def delete(resources)
resources.size
end

# extension to the adapter API

def execute_update(sql)
Hibernate.tx do |session|
session.do_work(UpdateWork.new(sql))
end
end

private

Expand Down Expand Up @@ -219,7 +233,7 @@ def handle_comparison(con, model)
# why does is not work: con.subject.child_key.get(con.value).first ???
value = con.subject.child_key.first.get(con.value.first) # value used in comparison
end
model_type = model.to_java_type(model.properties[subject.to_sym].type) # Java type of property (used in typecasting)
model_type = model.to_java_type(model.properties[subject.to_sym].class) # Java type of property (used in typecasting)
dialect = Hibernate.dialect # SQL dialect for current configuration

case con
Expand Down Expand Up @@ -249,7 +263,7 @@ def handle_comparison(con, model)
Restrictions.in(subject, cast_to_hibernate(value, model_type))
else
# XXX proper ordering?
arr = value.to_a
arr = value.is_a? Array ? value : value.to_a
lo = arr.first
hi = arr.last
if lo.nil? || hi.nil?
Expand Down
48 changes: 30 additions & 18 deletions lib/dm-hibernate-adapter/hibernate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,19 @@ def self.properties
end

def self.tx
session.begin_transaction
if block_given?
yield session
session.transaction.commit
s = nil
begin
s = session
s.begin_transaction
yield s
s.transaction.commit
rescue => e
s.transaction.rollback if s
raise e
end
else
raise "not supported"
end
end

Expand All @@ -79,7 +88,7 @@ def self.factory
end

def self.session
factory.current_session
factory.open_session
end

def self.config
Expand Down Expand Up @@ -267,14 +276,17 @@ def to_java_class_name
end

def hibernate!
# just make sure all the properties are there
# initialize join models and target keys
relationships.each do |property, relationship|
relationship.child_key
relationship.parent_key
relationship.through if relationship.respond_to?(:through)
relationship.via if relationship.respond_to?(:via)
end
unless mapped?
discriminator = nil

relationships.each do |property, relationship|
#load lazy child_keys properties
relationship.child_key if relationship.class == DataMapper::Associations::ManyToOne::Relationship
end

properties.each do |prop|
discriminator = add_java_property(prop) || discriminator
end
Expand Down Expand Up @@ -312,7 +324,7 @@ def hibernate_sigs
def add_java_property(prop)
@@logger.info("#{prop.model.name} gets property added #{prop.name}")
name = prop.name
type = prop.type
type = prop.class
return name if(type == DataMapper::Types::Discriminator)

column_name = prop.field
Expand Down Expand Up @@ -343,24 +355,24 @@ def add_java_property(prop)
#end
end
end
unless prop.required?.nil?
if prop.required?
annotation[javax.persistence.Column]["nullable"] = !prop.required?
end
unless prop.length.nil?
if (prop.respond_to?(:length) && !prop.length.nil?)
annotation[javax.persistence.Column]["length"] = java.lang.Integer.new(prop.length)
end
unless prop.scale.nil?
if (prop.respond_to?(:scale) && !prop.scale.nil?)
annotation[javax.persistence.Column]["scale"] = java.lang.Integer.new(prop.scale)
end
unless prop.precision.nil?
if (prop.respond_to?(:precision) && !prop.precision.nil?)
annotation[javax.persistence.Column]["precision"] = java.lang.Integer.new(prop.precision)
end

get_name = "get#{name.to_s.capitalize}"
set_name = "set#{name.to_s.capitalize}"

# TODO Time
if(type == ::Date)
if(type == DataMapper::Property::Date)
class_eval <<-EOT
def #{set_name.intern}(d)
attribute_set(:#{name}, d.nil? ? nil : Date.civil(d.year + 1900, d.month + 1, d.date))
Expand All @@ -374,7 +386,7 @@ def #{get_name.intern}
end
end
EOT
elsif(type == ::DateTime)
elsif(type == DataMapper::Property::DateTime)
class_eval <<-EOT
def #{set_name.intern}(d)
attribute_set(:#{name}, d.nil? ? nil : DateTime.civil(d.year + 1900, d.month + 1, d.date, d.hours, d.minutes, d.seconds))
Expand All @@ -388,10 +400,10 @@ def #{get_name.intern}
end
end
EOT
elsif(type == ::BigDecimal)
elsif(type.to_s == BigDecimal || type == DataMapper::Property::Decimal)
class_eval <<-EOT
def #{set_name.intern}(d)
attribute_set(:#{name}, d.nil? ? nil : BigDecimal.new(d.to_s))
attribute_set(:#{name}, d.nil? ? nil : #{type}.new(d.to_s))
end
EOT
class_eval <<-EOT
Expand Down
4 changes: 2 additions & 2 deletions pom-parent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
</plugins>
</build>
<properties>
<jruby.plugins.version>0.12.0</jruby.plugins.version>
<jruby.plugins.version>0.20.0-SNAPSHOT</jruby.plugins.version>
<jruby.version>1.5.0</jruby.version>
<dm.version>0.10.2</dm.version>
<dm.version>1.0.0</dm.version>
</properties>
</project>
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<version>0.1-SNAPSHOT</version>
<relativePath>pom-parent.xml</relativePath>
</parent>
<artifactId>jibernate</artifactId>
<packaging>gem</packaging>
<artifactId>dm-hibernate-adapter</artifactId>
<packaging>java-gem</packaging>

<dependencies>
<dependency>
Expand Down Expand Up @@ -54,13 +54,11 @@
<artifactId>derby</artifactId>
<version>10.5.3.0_1</version>
</dependency>
<!--
<dependency>
<groupId>hsqldb</groupId>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.0.0</version>
</dependency>
-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand All @@ -84,6 +82,7 @@
</dependencies>

<build>
<finalName>${project.artifactId}_ext</finalName>
<plugins>
<plugin>
<groupId>de.saumya.mojo</groupId>
Expand Down
40 changes: 35 additions & 5 deletions spec/abstract_adapter/adapter_shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ::Heffalump
property :number, Integer, :unique => true
property :striped, Boolean, :index => :big #[:big, :small]
property :weight, Float, :precision => 12, :unique_index => :usmall
property :distance, BigDecimal, :unique_index => :usamle #[:ubig, :usmall]
property :distance, Decimal, :unique_index => :usamle #[:ubig, :usmall]
property :birthdate, Date, :required => false, :field => "birth_date"
property :modified_at, DateTime, :required => false
property :expiration, Time, :required => false
Expand Down Expand Up @@ -55,9 +55,8 @@ class ::Heffalump

describe "property constraints set via annotations" do
it 'should obey required == true' do
lambda {
Heffalump.create(:color => 'peach', :alpha => nil)
}.should raise_error(NativeException)
h = Heffalump.create(:color => 'peach', :alpha => nil)
h.saved?.should be_false
end

it 'should obey length on not required' do
Expand Down Expand Up @@ -203,6 +202,10 @@ class ::Heffalump
@heffalump.save
Heffalump.get(*@heffalump.key).color.should == color
end

it 'should obey required == true' do
@heffalump.update(:alpha => nil).should be_false
end
end
else
it 'needs to support #update'
Expand All @@ -216,7 +219,7 @@ class ::Heffalump

it 'should not raise any errors' do
lambda {
@heffalump.destroy
@heffalump.destroy
}.should_not raise_error
end

Expand Down Expand Up @@ -481,4 +484,31 @@ class Group
admin_users.should == [@user]
end
end

before :all do
class ::Friend
include DataMapper::Resource

property :id, Serial
property :name, String

belongs_to :creator, "Friend"
end
Friend.auto_migrate!
end

describe "self referecing and direct sql" do

it 'should needs repository with execute method' do
repository.adapter.respond_to?( :execute_update).should be_true
end

it 'should create a self referencing enitity' do
repository.adapter.execute_update("insert into friends (id, name, creator_id) values(1, 'god', 1)")
Friend.all.size.should == 1
f = Friend.first
f.name.should == 'god'
f.creator.should == f
end
end
end
2 changes: 1 addition & 1 deletion spec/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ log4j.category.org.hibernate.SQL=warn
log4j.category.DataMapper=debug
log4j.category.DataMapper.Adapters.HibernateAdapter=warn
log4j.category.Hibernate=debug
log4j.category.Hibernate.Model=debug
log4j.category.Hibernate.Model=info

0 comments on commit 303a777

Please sign in to comment.