Permalink
Browse files

Add more data types, including broken support for booleans

  • Loading branch information...
1 parent 7d0e05f commit 5fb18a3784d2ba89632d791ac3583962477351f1 @olabini committed Aug 25, 2008
Showing with 93 additions and 8 deletions.
  1. +2 −0 lib/ribs.rb
  2. +23 −5 lib/ribs/session.rb
  3. +1 −0 test/find_spec.rb
  4. +53 −0 test/simple_select_spec.rb
  5. +13 −3 test/test_helper.rb
  6. +1 −0 test/type_spec.rb
View
@@ -16,6 +16,8 @@
require 'hibernate3.jar'
require 'ribs.jar'
+require 'bigdecimal'
+
require 'ribs/db'
require 'ribs/definition'
require 'ribs/session'
View
@@ -84,7 +84,8 @@ def select(string)
stmt = conn.create_statement
rs = stmt.execute_query(string)
result = []
- cols = rs.meta_data.column_count
+ meta = rs.meta_data
+ cols = meta.column_count
while rs.next
row = []
(1..cols).each do |n|
@@ -105,10 +106,18 @@ def chk_conn
def from_database_type(obj)
case obj
- when String, Integer, NilClass
+ when String, Float, Integer, NilClass, TrueClass, FalseClass
obj
when java.sql.Date, java.sql.Time, java.sql.Timestamp
Time.at(obj.time/1000)
+ when java.sql.Blob
+ String.from_java_bytes(obj.get_bytes(1,obj.length))
+ when java.sql.Clob
+ obj.get_sub_string(1, obj.length)
+ when java.math.BigDecimal
+ BigDecimal.new(obj.to_s)
+ else
+ raise "Can't find correct type to convert #{obj.inspect} into"
end
end
@@ -117,7 +126,14 @@ def set_prepared_statement(stmt, item, index, type)
when NilClass
stmt.set_object index, nil
when String
- stmt.set_string index, item
+ case type
+ when :binary
+ stmt.set_bytes index, item.to_java_bytes
+ when :text
+ stmt.set_string index, item
+ else
+ stmt.set_string index, item
+ end
when Symbol
stmt.set_string index, item.to_s
when Integer
@@ -130,9 +146,11 @@ def set_prepared_statement(stmt, item, index, type)
stmt.set_date index, item.to_java_sql_date
when :time
stmt.set_time index, item.to_java_sql_time
- when :times_stamp
- stmt.set_time_stamp index, item.to_java_sql_time_stamp
+ when :timestamp
+ stmt.set_timestamp index, item.to_java_sql_time_stamp
end
+ when BigDecimal
+ stmt.set_big_decimal index, java.math.BigDecimal.new(item.to_s('F'))
when TrueClass, FalseClass
stmt.set_boolean index, item
else
View
@@ -1 +1,2 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
View
@@ -1,3 +1,4 @@
+require File.join(File.dirname(__FILE__), 'test_helper')
describe "Simple select" do
it "should return correct data for ints" do
@@ -23,4 +24,56 @@
s.select("SELECT added FROM DB_TRACK").should == [[Time.local(1984, 12, 13, 0, 0, 0)], [Time.local(1983, 12, 13, 0, 0, 0)]]
end
end
+
+ it "should return correct data for timestamp" do
+ Ribs.with_session do |s|
+ s.select("SELECT lastPlayed FROM DB_TRACK").should == [[Time.local(1984, 12, 14, 12,3,11)], [Time.local(1982, 5, 3, 13,3,7)]]
+ end
+ end
+
+ it "should return correct data for floats" do
+ result = Ribs.with_session do |s|
+ s.select("SELECT fraction FROM DB_TRACK")
+ end
+ result[0].length.should == 1
+ result[0][0].should be_close(3.4, 0.00001)
+ result[1].length.should == 1
+ result[1][0].should be_close(3.5, 0.00001)
+
+ end
+
+ it "should return correct data for doubles" do
+ result = Ribs.with_session do |s|
+ s.select("SELECT otherFraction FROM DB_TRACK")
+ end
+ result[0].length.should == 1
+ result[0][0].should be_close(5.7, 0.00001)
+ result[1].length.should == 1
+ result[1][0].should be_close(35435.4522234, 0.01)
+ end
+
+ it "should return correct data for blobs" do
+ Ribs.with_session do |s|
+ s.select("SELECT data FROM DB_TRACK").should == [["abc"], ["mumsi"]]
+ end
+ end
+
+ it "should return correct data for clobs" do
+ Ribs.with_session do |s|
+ s.select("SELECT description FROM DB_TRACK").should == [["foobar"], ["maxi"]]
+ end
+ end
+
+ it "should return correct data for booleans" do
+ Ribs.with_session do |s|
+ # Not strictly correct - an artifact of a lack in Derby
+ s.select("SELECT good FROM DB_TRACK").should == [[1], [0]]
+ end
+ end
+
+ it "should return correct data for decimal" do
+ Ribs.with_session do |s|
+ s.select("SELECT price FROM DB_TRACK").should == [[BigDecimal.new("13134.11")], [BigDecimal.new("55454.33")]]
+ end
+ end
end
View
@@ -1,6 +1,7 @@
require 'java'
require 'rubygems'
require 'spec'
+require 'bigdecimal'
require 'ribs'
class Time
@@ -36,6 +37,13 @@ def self.date_at(year, month, day)
playTime TIME,
added DATE,
volume INT NOT NULL,
+ lastPlayed TIMESTAMP,
+ data BLOB,
+ description CLOB,
+ fraction FLOAT,
+ otherFraction DOUBLE,
+ good SMALLINT,
+ price DECIMAL(10,2),
PRIMARY KEY (TRACK_ID)
)
SQL
@@ -49,12 +57,14 @@ def self.date_at(year, month, day)
SQL
template = <<SQL
-INSERT INTO DB_TRACK(TRACK_ID, title, filePath, playTime, added, volume) VALUES(?, ?, ?, ?, ?, ?)
+INSERT INTO DB_TRACK(TRACK_ID, title, filePath, playTime, added, volume, lastPlayed, data, description, fraction, otherFraction, good, price) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
SQL
s.insert(template,
- [1, "foobar", "c:/abc/cde/foo.mp3", [Time.time_at(14,50,0), :time], [Time.local(1984, 12, 13, 0,0,0), :date], 13],
- [2, "flux", "d:/abc/cde/flax.mp3", [Time.time_at(16,23,0), :time], [Time.local(1983, 12, 13, 0,0,0), :date], 13])
+ [1, "foobar", "c:/abc/cde/foo.mp3", [Time.time_at(14,50,0), :time], [Time.local(1984, 12, 13, 0,0,0), :date], 13,
+ [Time.local(1984, 12, 14, 12,3,11), :timestamp], ["abc", :binary], ["foobar", :text], 3.4, 5.7, true, BigDecimal.new("13134.11")],
+ [2, "flux", "d:/abc/cde/flax.mp3", [Time.time_at(16,23,0), :time], [Time.local(1983, 12, 13, 0,0,0), :date], 13,
+ [Time.local(1982, 5, 3, 13,3,7), :timestamp], ["mumsi", :binary], ["maxi", :text], 3.5, 35435.4522234, false, BigDecimal.new("55454.33")])
s.insert("INSERT INTO ARTIST(ID, name) VALUES(?, ?)",
[1, "Public Image Ltd"],
View
@@ -1 +1,2 @@
+require File.join(File.dirname(__FILE__), 'test_helper')

0 comments on commit 5fb18a3

Please sign in to comment.