Skip to content

Commit

Permalink
Make basic loading of entities work. Yay.
Browse files Browse the repository at this point in the history
  • Loading branch information
olabini committed Aug 22, 2008
1 parent 6a155e5 commit c045561
Show file tree
Hide file tree
Showing 18 changed files with 1,038 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
build
lib/ribs.jar
6 changes: 6 additions & 0 deletions Rakefile
Expand Up @@ -3,6 +3,10 @@ require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'spec/rake/spectask'

task :ant do
system('ant')
end

task :default => [:spec]
task :test => [:spec]

Expand All @@ -15,6 +19,8 @@ Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ["-fs", "--color"]
end

task :spec => [:ant]

desc 'Generate RDoc'
Rake::RDocTask.new do |task|
task.main = 'README'
Expand Down
40 changes: 40 additions & 0 deletions build.xml
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>

<project basedir="." default="jar" name="ribs">
<path id="build.classpath">
<fileset dir="lib" includes="*.jar" excludes="ribs*.jar"/>
</path>

<patternset id="java.src.pattern">
<include name="**/*.java"/>
</patternset>

<target name="prepare" description="Creates the directories needed for building">
<mkdir dir="build/classes"/>
</target>

<target name="compile" depends="prepare" description="Compile the source files for the project.">
<javac destdir="build/classes" debug="true" source="1.5" target="1.5">
<classpath refid="build.classpath"/>
<src path="src/java"/>
<patternset refid="java.src.pattern"/>
</javac>
</target>

<target name="jar" depends="compile" description="Create the ribs.jar file">
<jar destfile="lib/ribs.jar">
<fileset dir="build/classes">
<include name="**/*.class"/>
<include name="**/*.properties"/>
</fileset>
<manifest>
<attribute name="Built-By" value="${user.name}"/>
</manifest>
</jar>
</target>

<target name="clean" depends="prepare" description="clean almost everything">
<delete dir="build/classes"/>
<delete file="lib/ribs.jar" quiet="true"/>
</target>
</project>
Binary file added lib/jruby-complete.jar
Binary file not shown.
10 changes: 10 additions & 0 deletions lib/ribs.rb
Expand Up @@ -8,9 +8,12 @@
require 'slf4j-api-1.5.2.jar'
require 'slf4j-jdk14-1.5.2.jar'
require 'hibernate3.jar'
require 'ribs.jar'

require 'ribs/db'
require 'ribs/definition'
require 'ribs/session'
require 'ribs/meat'
require 'ribs/core_ext/time'

module Ribs
Expand All @@ -23,3 +26,10 @@ def with_session(from = :default)
end
end
end

module Kernel
def Ribs!(options = {}, &block)
options.merge!({:on => self, :db => :default, :from => nil})
Ribs::define_ribs(options[:on], options, &block)
end
end
18 changes: 18 additions & 0 deletions lib/ribs/core_ext/time.rb
@@ -0,0 +1,18 @@

class Time
JavaSqlDate = java.sql.Date
JavaSqlTime = java.sql.Time
JavaSqlTimestamp = java.sql.Timestamp

def to_java_sql_date
JavaSqlDate.new(self.to_i*1000)
end

def to_java_sql_time
JavaSqlTime.new(self.to_i*1000)
end

def to_java_sql_time_stamp
JavaSqlTimestamp.new(self.to_i*1000)
end
end
14 changes: 10 additions & 4 deletions lib/ribs/db.rb
Expand Up @@ -14,8 +14,7 @@ def define(name = :main)
register db

db.create
db.freeze


db
end

Expand Down Expand Up @@ -51,6 +50,7 @@ def get(name = nil)
attr_accessor :password
attr_accessor :properties
attr_accessor :default
attr_reader :mappings

def initialize(name = :main)
self.name = name
Expand All @@ -71,7 +71,13 @@ def create
self.properties.each do |key, value|
properties.set_property(key, value)
end
@session_factory = Configuration.new.add_properties(properties).build_session_factory
@configuration = Configuration.new.add_properties(properties)
@mappings = @configuration.create_mappings
reset_session_factory!
end

def reset_session_factory!
@session_factory = @configuration.build_session_factory
end

def session
Expand All @@ -88,7 +94,7 @@ def session

def release(session)
res = Thread.current[:ribs_db_sessions][self.object_id]
if res[0] == session
if res[0] == session.hibernate_session
res[1] -= 1
if res[1] == 0
res[0].close
Expand Down
159 changes: 159 additions & 0 deletions lib/ribs/definition.rb
@@ -0,0 +1,159 @@
module Ribs
class MetaData
attr_accessor :table
attr_accessor :persistent_class
end

Table = org.hibernate.mapping.Table
Column = org.hibernate.mapping.Column
Property = org.hibernate.mapping.Property
SimpleValue = org.hibernate.mapping.SimpleValue

class RubyRootClass < org.hibernate.mapping.RootClass
include org.jruby.ribs.WithRubyClass

attr_accessor :ruby_class

def initialize(*args)
super
end

def getRubyClass
@ruby_class
end
end

class << self
def define_ribs(on, options = {}, &block)
define_metadata_on_class on
rm = on.ribs_metadata

db = nil
with_session(options[:db] || :default) do |s|
db = s.db
m = s.meta_data
name = table_name_for(on.name, m)

tables = m.get_tables nil, nil, name, %w(TABLE VIEW ALIAS SYNONYM).to_java(:String)
if tables.next
table = Table.new(tables.get_string(3))
rm.table = table
c = tables.get_string(1)
table.catalog = c if c && c.length > 0
c = tables.get_string(2)
table.schema = c if c && c.length > 0

columns_rs = m.get_columns table.catalog, table.schema, table.name, nil

while columns_rs.next
c = Column.new(columns_rs.get_string(4))
c.default_value = columns_rs.get_string(13)
c.length = columns_rs.get_int(7)
c.nullable = columns_rs.get_string(18) == 'YES'
c.precision = columns_rs.get_int(10)
c.scale = columns_rs.get_int(9)
c.sql_type = columns_rs.get_string(6)
c.sql_type_code = java.lang.Integer.new(columns_rs.get_int(5))

table.add_column(c)
end
columns_rs.close rescue nil
tables.close rescue nil

pc = RubyRootClass.new
pc.ruby_class = on
pc.entity_name = on.name
pc.table = table
pc.add_tuplizer(org.hibernate.EntityMode::MAP, "org.jruby.ribs.RubyTuplizer")

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

if c.name.downcase == 'id'
pc.identifier_property = prop
pc.identifier = val
else
pc.add_property(prop)
end

define_meat_accessor(on, prop.name)
end
pc.create_primary_key
db.mappings.add_class(pc)
else
tables.close rescue nil
raise "No table found for: #{name}"
end
end

db.reset_session_factory!
end

JTypes = java.sql.Types

private
def define_meat_accessor(clazz, name)
clazz.class_eval <<CODE
def #{name}
self.__ribs_meat[:"#{name}"]
end
def #{name}=(value)
self.__ribs_meat[:"#{name}"] = value
end
CODE
end

def get_type_for_sql(name, code)
case code
when JTypes::VARCHAR
"java.lang.String"
when JTypes::INTEGER
"int"
else
nil
end
end

def table_name_for(str, metadata)
if metadata.stores_lower_case_identifiers
str.downcase
elsif metadata.stores_upper_case_identifiers
str.upcase!
else
str
end
end

def define_metadata_on_class(clazz)
clazz.instance_variable_set :@ribs_metadata, Ribs::MetaData.new
class << clazz
attr_reader :ribs_metadata

def find(id_or_sym)
Ribs.with_session do |s|
s.find(self.ribs_metadata.persistent_class.entity_name, id_or_sym)
end
end
end

clazz.class_eval do
def __ribs_meat
@__ribs_meat ||= Ribs::Meat.new(self)
end

def inspect
"#<#{self.class.name}: #{self.__ribs_meat.properties.inspect}>"
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/ribs/meat.rb
@@ -0,0 +1,18 @@
module Ribs
class Meat
attr_reader :properties

def initialize(inside)
@inside = inside
@properties = {}
end

def [](ix)
@properties[ix]
end

def []=(ix, value)
@properties[ix] = value
end
end
end

0 comments on commit c045561

Please sign in to comment.