From 697f1e4c8fd5eaa64204ed070c8da1462f83285e Mon Sep 17 00:00:00 2001 From: Ola Bini Date: Fri, 29 Aug 2008 21:23:04 +0100 Subject: [PATCH] Make it possible to handle simple updates --- lib/ribs/db.rb | 1 + lib/ribs/definition.rb | 4 +-- lib/ribs/session.rb | 18 +++++------ .../org/jruby/ribs/EntityNameInterceptor.java | 30 +++++++++++++++++++ test/artist_spec.rb | 14 ++++++++- 5 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 src/java/org/jruby/ribs/EntityNameInterceptor.java diff --git a/lib/ribs/db.rb b/lib/ribs/db.rb index 4be8d53..d86197f 100644 --- a/lib/ribs/db.rb +++ b/lib/ribs/db.rb @@ -72,6 +72,7 @@ def create properties.set_property(key, value) end @configuration = Configuration.new.add_properties(properties) + @configuration.set_interceptor org.jruby.ribs.EntityNameInterceptor.new @mappings = @configuration.create_mappings reset_session_factory! end diff --git a/lib/ribs/definition.rb b/lib/ribs/definition.rb index c9ccbe7..a76c271 100644 --- a/lib/ribs/definition.rb +++ b/lib/ribs/definition.rb @@ -160,7 +160,7 @@ def define_ribs(on, options = {}) val.add_column(c) val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code) prop.value = val - + if (!rib.primary_keys.empty? && rib.primary_keys[c.name.downcase]) || c.name.downcase == 'id' pc.identifier_property = prop pc.identifier = val @@ -201,7 +201,7 @@ def #{downcased}=(value) def get_type_for_sql(name, code) case code when JTypes::VARCHAR - "java.lang.String" + "string" when JTypes::INTEGER "int" when JTypes::TIME diff --git a/lib/ribs/session.rb b/lib/ribs/session.rb index 6b944a9..7f1e1fa 100644 --- a/lib/ribs/session.rb +++ b/lib/ribs/session.rb @@ -30,12 +30,12 @@ def release @db.release(self) end - # LOW LEVEL - shouldn't be used + # LOW LEVEL - shouldn't be used except by Ribs def hibernate_session @hibernate_session end - # LOW LEVEL - shouldn't be used + # LOW LEVEL - shouldn't be used except by Ribs def find(entity_name, id) chk_conn if id == :all @@ -45,33 +45,33 @@ def find(entity_name, id) end end - # LOW LEVEL - shouldn't be used + # LOW LEVEL - shouldn't be used except by Ribs def save(obj) chk_conn tx = @hibernate_session.begin_transaction if obj.__ribs_meat.persistent - @hibernate_session.update(obj.class.ribs_metadata.persistent_class.entity_name, obj) + @hibernate_session.update(obj) else - @hibernate_session.save(obj.class.ribs_metadata.persistent_class.entity_name, obj) + @hibernate_session.save(obj) obj.__ribs_meat.persistent = true end tx.commit obj end - # LOW LEVEL - shouldn't be used + # LOW LEVEL - shouldn't be used except by Ribs def meta_data chk_conn @hibernate_session.connection.meta_data end - # LOW LEVEL - shouldn't be used + # LOW LEVEL - shouldn't be used except by Ribs def ddl(string) chk_conn execute(string) end - # LOW LEVEL - shouldn't be used + # LOW LEVEL - shouldn't be used except by Ribs def insert(template, *data) chk_conn conn = @hibernate_session.connection @@ -91,7 +91,7 @@ def insert(template, *data) stmt.close rescue nil end - # LOW LEVEL - shouldn't be used + # LOW LEVEL - shouldn't be used except by Ribs def select(string) chk_conn conn = @hibernate_session.connection diff --git a/src/java/org/jruby/ribs/EntityNameInterceptor.java b/src/java/org/jruby/ribs/EntityNameInterceptor.java new file mode 100644 index 0000000..3b453c7 --- /dev/null +++ b/src/java/org/jruby/ribs/EntityNameInterceptor.java @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2008, Ola Bini + * 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("::","."); + } +} diff --git a/test/artist_spec.rb b/test/artist_spec.rb index 603a19d..cfd4b91 100644 --- a/test/artist_spec.rb +++ b/test/artist_spec.rb @@ -62,5 +62,17 @@ class Artist 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