Skip to content

Commit

Permalink
Make it possible to handle simple updates
Browse files Browse the repository at this point in the history
  • Loading branch information
olabini committed Aug 29, 2008
1 parent 67ae64f commit 697f1e4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/ribs/db.rb
Expand Up @@ -72,6 +72,7 @@ def create
properties.set_property(key, value) properties.set_property(key, value)
end end
@configuration = Configuration.new.add_properties(properties) @configuration = Configuration.new.add_properties(properties)
@configuration.set_interceptor org.jruby.ribs.EntityNameInterceptor.new
@mappings = @configuration.create_mappings @mappings = @configuration.create_mappings
reset_session_factory! reset_session_factory!
end end
Expand Down
4 changes: 2 additions & 2 deletions lib/ribs/definition.rb
Expand Up @@ -160,7 +160,7 @@ def define_ribs(on, options = {})
val.add_column(c) val.add_column(c)
val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code) val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
prop.value = val prop.value = val

if (!rib.primary_keys.empty? && rib.primary_keys[c.name.downcase]) || c.name.downcase == 'id' if (!rib.primary_keys.empty? && rib.primary_keys[c.name.downcase]) || c.name.downcase == 'id'
pc.identifier_property = prop pc.identifier_property = prop
pc.identifier = val pc.identifier = val
Expand Down Expand Up @@ -201,7 +201,7 @@ def #{downcased}=(value)
def get_type_for_sql(name, code) def get_type_for_sql(name, code)
case code case code
when JTypes::VARCHAR when JTypes::VARCHAR
"java.lang.String" "string"
when JTypes::INTEGER when JTypes::INTEGER
"int" "int"
when JTypes::TIME when JTypes::TIME
Expand Down
18 changes: 9 additions & 9 deletions lib/ribs/session.rb
Expand Up @@ -30,12 +30,12 @@ def release
@db.release(self) @db.release(self)
end end


# LOW LEVEL - shouldn't be used # LOW LEVEL - shouldn't be used except by Ribs
def hibernate_session def hibernate_session
@hibernate_session @hibernate_session
end end


# LOW LEVEL - shouldn't be used # LOW LEVEL - shouldn't be used except by Ribs
def find(entity_name, id) def find(entity_name, id)
chk_conn chk_conn
if id == :all if id == :all
Expand All @@ -45,33 +45,33 @@ def find(entity_name, id)
end end
end end


# LOW LEVEL - shouldn't be used # LOW LEVEL - shouldn't be used except by Ribs
def save(obj) def save(obj)
chk_conn chk_conn
tx = @hibernate_session.begin_transaction tx = @hibernate_session.begin_transaction
if obj.__ribs_meat.persistent if obj.__ribs_meat.persistent
@hibernate_session.update(obj.class.ribs_metadata.persistent_class.entity_name, obj) @hibernate_session.update(obj)
else else
@hibernate_session.save(obj.class.ribs_metadata.persistent_class.entity_name, obj) @hibernate_session.save(obj)
obj.__ribs_meat.persistent = true obj.__ribs_meat.persistent = true
end end
tx.commit tx.commit
obj obj
end end


# LOW LEVEL - shouldn't be used # LOW LEVEL - shouldn't be used except by Ribs
def meta_data def meta_data
chk_conn chk_conn
@hibernate_session.connection.meta_data @hibernate_session.connection.meta_data
end end


# LOW LEVEL - shouldn't be used # LOW LEVEL - shouldn't be used except by Ribs
def ddl(string) def ddl(string)
chk_conn chk_conn
execute(string) execute(string)
end end


# LOW LEVEL - shouldn't be used # LOW LEVEL - shouldn't be used except by Ribs
def insert(template, *data) def insert(template, *data)
chk_conn chk_conn
conn = @hibernate_session.connection conn = @hibernate_session.connection
Expand All @@ -91,7 +91,7 @@ def insert(template, *data)
stmt.close rescue nil stmt.close rescue nil
end end


# LOW LEVEL - shouldn't be used # LOW LEVEL - shouldn't be used except by Ribs
def select(string) def select(string)
chk_conn chk_conn
conn = @hibernate_session.connection conn = @hibernate_session.connection
Expand Down
30 changes: 30 additions & 0 deletions src/java/org/jruby/ribs/EntityNameInterceptor.java
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2008, Ola Bini <ola.bini@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the distribution.
*
* Neither the name of the IT-Centrum, Karolinska Institutet, Sweden nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jruby.ribs;

import org.hibernate.EmptyInterceptor;
import org.jruby.runtime.builtin.IRubyObject;

public class EntityNameInterceptor extends EmptyInterceptor {
public String getEntityName(Object object) {
return ((IRubyObject)object).getType().getName().replace("::",".");
}
}
14 changes: 13 additions & 1 deletion test/artist_spec.rb
Expand Up @@ -62,5 +62,17 @@ class Artist
end end
end end


it "should be possible to update name property on existing bean" it "should be possible to update name property on existing bean" do
begin
artist = Artist.find(2)
artist.name = "U2"
Artist.find(2).name.should == "New Model Army"
artist.save
Artist.find(2).name.should == "U2"
ensure
reset_database!
end
end

it "should be possible to delete existing bean"
end end

0 comments on commit 697f1e4

Please sign in to comment.