lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb in knjrbfw-0.0.7 vs lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3.rb in knjrbfw-0.0.8
- old
+ new
@@ -10,15 +10,27 @@
@esc_col = "`"
@knjdb = knjdb_ob
@path = @knjdb.opts[:path] if @knjdb.opts[:path]
@path = @knjdb.opts["path"] if @knjdb.opts["path"]
- @symbolize = true if !@knjdb.opts.has_key?(:return_keys) or @knjdb.opts[:return_keys] == "symbols"
+ @symbolize = true if !@knjdb.opts.key?(:return_keys) or @knjdb.opts[:return_keys] == "symbols"
+ @knjdb.opts[:subtype] = "java" if !@knjdb.opts.key?(:subtype) and RUBY_ENGINE == "jruby"
raise "No path was given." if !@path
- if @knjdb.opts[:subtype] == "rhodes"
+ if @knjdb.opts[:subtype] == "java"
+ if @knjdb.opts[:sqlite_driver]
+ require @knjdb.opts[:sqlite_driver]
+ else
+ require "#{File.dirname(__FILE__)}/../../sqlitejdbc-v056.jar"
+ end
+
+ require "java"
+ import "org.sqlite.JDBC"
+ @conn = java.sql.DriverManager::getConnection("jdbc:sqlite:#{@knjdb.opts[:path]}")
+ @stat = @conn.createStatement
+ elsif @knjdb.opts[:subtype] == "rhodes"
@conn = SQLite3::Database.new(@path, @path)
else
@conn = SQLite3::Database.open(@path)
@conn.results_as_hash = true
@conn.type_translation = false
@@ -26,20 +38,28 @@
end
def query(string)
begin
if @knjdb.opts[:subtype] == "rhodes"
- res = @conn.execute(string, string)
+ return KnjDB_sqlite3_result.new(self, @conn.execute(string, string))
+ elsif @knjdb.opts[:subtype] == "java"
+ begin
+ return KnjDB_sqlite3_result_java.new(self, @stat.executeQuery(string))
+ rescue java.sql.SQLException => e
+ if e.message == "java.sql.SQLException: query does not return ResultSet"
+ return KnjDB_sqlite3_result_java.new(self, nil)
+ else
+ raise e
+ end
+ end
else
- res = @conn.execute(string)
+ return KnjDB_sqlite3_result.new(self, @conn.execute(string))
end
- rescue Exception => e
- print "SQL: #{string}\n"
- raise e
+ rescue => e
+ #Add SQL to the error message.
+ raise e.class, "#{e.message}\n\nSQL: #{string}"
end
-
- return KnjDB_sqlite3_result.new(self, res)
end
def escape(string)
#This code is taken directly from the documentation so we dont have to rely on the SQLite3::Database class. This way it can also be used with JRuby and IronRuby...
#http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html
@@ -63,26 +83,63 @@
def close
@conn.close
end
end
+class KnjDB_sqlite3_result_java
+ def initialize(driver, rs)
+ @index = 0
+ retkeys = driver.knjdb.opts[:return_keys]
+
+ if rs
+ metadata = rs.getMetaData
+ columns_count = metadata.getColumnCount
+
+ @rows = []
+ while rs.next
+ row_data = {}
+ for i in (1..columns_count)
+ col_name = metadata.getColumnName(i)
+ col_name = col_name.to_s.to_sym if retkeys == "symbols"
+ row_data[col_name] = rs.getString(i)
+ end
+
+ @rows << row_data
+ end
+ end
+ end
+
+ def fetch
+ return false if !@rows
+ ret = @rows[@index]
+ return false if !ret
+ @index += 1
+ return ret
+ end
+end
+
class KnjDB_sqlite3_result
def initialize(driver, result_array)
@result_array = result_array
@index = 0
- @retkeys = driver.knjdb.opts[:return_keys]
+
+ if driver.knjdb.opts[:return_keys] == "symbols"
+ @symbols = true
+ else
+ @symbols = false
+ end
end
def fetch
- tha_return = @result_array[@index]
- return false if !tha_return
+ result_hash = @result_array[@index]
+ return false if !result_hash
@index += 1
ret = {}
- tha_return.each do |key, val|
+ result_hash.each do |key, val|
if Knj::Php::is_numeric(key)
#do nothing.
- elsif @retkeys == "symbols" and !key.is_a?(Symbol)
+ elsif @symbols and !key.is_a?(Symbol)
ret[key.to_sym] = val
else
ret[key] = val
end
end
\ No newline at end of file