Permalink
Browse files

Make first parts of simple aliasing of columns work.

  • Loading branch information...
1 parent e633ac9 commit ee8849fd42718b54f6e94779445fcb3fa4bed721 @olabini committed Aug 22, 2008
View
@@ -2,6 +2,45 @@ module Ribs
class MetaData
attr_accessor :table
attr_accessor :persistent_class
+ attr_accessor :rib
+
+ def [](name)
+# $stderr.puts self.persistent_class.property_iterator.to_a.inspect
+ self.persistent_class.get_property(name.to_s) rescue nil
+ end
+
+ def properties
+ self.persistent_class.property_iterator.to_a.inject({}) do |h, value|
+ h[value.name] = value
+ h
+ end
+ end
+ end
+
+ class Rib
+ attr_accessor :table
+ attr_reader :columns
+ attr_reader :primary_keys
+ attr_reader :to_avoid
+
+ def initialize
+ @columns = { }
+ @primary_keys = { }
+ @to_avoid = []
+ end
+
+ def col(column, property = column, options = {})
+ @columns[column.to_s.downcase] = [property.to_s, options]
+ end
+
+ def primary_key(column, property = column, options = {})
+ @primary_keys[column.to_s.downcase] = property.to_s
+ @columns[column.to_s.downcase] = [property.to_s, options]
+ end
+
+ def avoid(*columns)
+ @to_avoid += columns.map{|s| s.to_s.downcase}
+ end
end
Table = org.hibernate.mapping.Table
@@ -24,17 +63,21 @@ def getRubyClass
end
class << self
- def define_ribs(on, options = {}, &block)
+ def define_ribs(on, options = {})
+ rib = Rib.new
+ yield rib if block_given?
+
define_metadata_on_class on
rm = on.ribs_metadata
+ rm.rib = rib
db = nil
with_session(options[:db] || :default) do |s|
db = s.db
m = s.meta_data
- name = table_name_for(on.name, m)
+ name = rib.table || table_name_for(on.name, m)
- tables = m.get_tables nil, nil, name, %w(TABLE VIEW ALIAS SYNONYM).to_java(:String)
+ tables = m.get_tables nil, nil, name.to_s, %w(TABLE VIEW ALIAS SYNONYM).to_java(:String)
if tables.next
table = Table.new(tables.get_string(3))
rm.table = table
@@ -69,22 +112,24 @@ def define_ribs(on, options = {}, &block)
rm.persistent_class = pc
table.column_iterator.each do |c|
- prop = Property.new
- prop.persistent_class = pc
- prop.name = c.name
- val = SimpleValue.new(table)
- val.add_column(c)
- val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
- prop.value = val
+ unless rib.to_avoid.include?(c.name.downcase)
+ prop = Property.new
+ prop.persistent_class = pc
+ prop.name = ((v=rib.columns[c.name.downcase]) && v[0]) || c.name
+ val = SimpleValue.new(table)
+ val.add_column(c)
+ val.type_name = get_type_for_sql(c.sql_type, c.sql_type_code)
+ prop.value = val
- if c.name.downcase == 'id'
- pc.identifier_property = prop
- pc.identifier = val
- else
- pc.add_property(prop)
+ if (!rib.primary_keys.empty? && rib.primary_keys[c.name.downcase]) || c.name.downcase == 'id'
+ pc.identifier_property = prop
+ pc.identifier = val
+ else
+ pc.add_property(prop)
+ end
+
+ define_meat_accessor(on, prop.name)
end
-
- define_meat_accessor(on, prop.name)
end
pc.create_primary_key
db.mappings.add_class(pc)
@@ -119,7 +164,14 @@ def get_type_for_sql(name, code)
"java.lang.String"
when JTypes::INTEGER
"int"
+ when JTypes::TIME
+ "java.sql.Time"
+ when JTypes::DATE
+ "java.sql.Date"
+ when JTypes::TIMESTAMP
+ "java.sql.Timestamp"
else
+ $stderr.puts [name, code].inspect
nil
end
end
View
@@ -38,8 +38,7 @@ def hibernate_session
# LOW LEVEL - shouldn't be used
def find(entity_name, id)
chk_conn
- case id
- when :all
+ if id == :all
@hibernate_session.create_criteria(entity_name).list.to_a
else
@hibernate_session.get(entity_name, java.lang.Integer.new(id))
@@ -30,7 +30,7 @@ private boolean isRubyProxy(Object o) {
public Getter getGetter(Class theClass, final String propertyName) throws PropertyNotFoundException {
return new Getter() {
public Object get(Object owner) throws HibernateException {
- Ruby runtime = null;
+ Ruby runtime = ((IRubyObject)owner).getRuntime();
IRubyObject rubyValue = ((IRubyObject)owner).callMethod(runtime.getCurrentContext(),propertyName.toLowerCase());
if(rubyValue instanceof RubyTime) {
return ((RubyTime)rubyValue).getJavaDate();
@@ -55,7 +55,7 @@ public String getMethodName() {
public Class getReturnType() {
return Object.class;
- }
+ }
};
}
@@ -76,7 +76,7 @@ public void set(Object target, Object value, SessionFactoryImplementor factory)
IRubyObject rubyObject = null;
if(value instanceof java.util.Date) {
long milisecs = ((java.util.Date)value).getTime();
- rubyObject = runtime.getClass("Time").newInstance(runtime.getCurrentContext(), new IRubyObject[]{runtime.newFixnum(milisecs)},Block.NULL_BLOCK);
+ rubyObject = RubyTime.newTime(runtime, milisecs);
} else {
rubyObject = JavaUtil.convertJavaToRuby(runtime, value);
}
View
@@ -0,0 +1 @@
+
@@ -0,0 +1,53 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
+
+# TRACK_ID INT NOT NULL,
+# title VARCHAR(255) NOT NULL,
+# filePath VARCHAR(255) NOT NULL,
+# playTime TIME,
+# added DATE,
+# volume INT NOT NULL,
+
+class Track
+ Ribs! do |rib|
+ rib.table = :DB_TRACK
+
+ rib.primary_key :TRACK_ID
+
+ rib.col :title, :track_title
+ rib.col :playTime, :time
+ rib.col :added, :date_added
+
+ rib.avoid :filePath
+ end
+end
+
+describe Track do
+ it "should be able to find things based on mapped primary key" do
+ track = Track.find(2)
+ track.track_id.should == 2
+ track.track_title.should == "flux"
+ track.time.should == Time.time_at(16,23,0)
+ track.date_added.should == Time.local(1983, 12, 13, 0,0,0)
+ track.volume.should == 13
+ end
+
+ it "should have property that wasn't named" do
+ prop = Track.ribs_metadata['VOLUME']
+ prop.name.should == 'VOLUME'
+ colarr = prop.column_iterator.to_a
+ colarr.length.should == 1
+ colarr[0].name.should == 'VOLUME'
+ end
+
+ it "shouldn't have property that's avoided" do
+ Track.ribs_metadata['filePath'].should be_nil
+ end
+
+ it "should have correct names for defined properties" do
+ props = Track.ribs_metadata.properties
+ # the primary keys aren't actually part of the properties
+ props.keys.sort.should == ['VOLUME', 'track_title', 'time', 'date_added'].sort
+ props.values.map { |p| p.column_iterator.to_a[0].name }.sort.should ==
+ ['VOLUME', 'TITLE', 'PLAYTIME', 'ADDED'].sort
+ end
+end
View
@@ -16,4 +16,6 @@ class Artist
it "should return correct value and type for name property" do
Artist.find(:all).map { |a| a.name }.sort.should == ["David Bowie","New Model Army","Public Image Ltd"]
end
+
+ it "should only have the appropriate methods defined"
end
View
@@ -0,0 +1 @@
+

0 comments on commit ee8849f

Please sign in to comment.