From dd5c10ba6e395e517e1e3ef8ee47810a921baa3e Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Sun, 31 Jan 2016 17:31:35 -0300 Subject: [PATCH] column types update to connection_string remove Driver.quote --- spec/driver_spec.cr | 30 +++++++++++++++++++++--------- src/sqlite3/connection.cr | 5 ++--- src/sqlite3/driver.cr | 8 +------- src/sqlite3/result_set2.cr | 10 +++++++++- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/spec/driver_spec.cr b/spec/driver_spec.cr index 1e11476..276bcc5 100644 --- a/spec/driver_spec.cr +++ b/spec/driver_spec.cr @@ -1,13 +1,13 @@ require "./spec_helper" def with_db(&block : DB::Database ->) - DB.open "sqlite3", {"database": DB_FILENAME}, &block + DB.open "sqlite3", DB_FILENAME, &block ensure File.delete(DB_FILENAME) end def with_mem_db(&block : DB::Database ->) - DB.open "sqlite3", {"database": ":memory:"}, &block + DB.open "sqlite3", ":memory:", &block end def sql(s : String) @@ -156,7 +156,23 @@ describe Driver do end end - # TODO gets column types + it "gets column types" do + with_mem_db do |db| + db.exec "create table table1 (aText text, anInteger integer, aReal real, aBlob blob)" + db.exec "insert into table1 (aText, anInteger, aReal, aBlob) values ('a', 1, 1.5, X'53')" + + # sqlite is unable to get column_type information + # from the query itself without executing and getting + # actual data. + db.query "select * from table1" do |rs| + rs.move_next + rs.column_type(0).should eq(String) + rs.column_type(1).should eq(Int64) + rs.column_type(2).should eq(Float64) + rs.column_type(3).should eq(Slice(UInt8)) + end + end + end it "gets last insert row id" do with_mem_db do |db| @@ -210,19 +226,15 @@ describe Driver do end end - it "quotes" do - Driver.quote("'hello'").should eq("''hello''") - end - it "ensures statements are closed" do begin - DB.open "sqlite3", {"database": DB_FILENAME} do |db| + DB.open "sqlite3", DB_FILENAME do |db| db.exec %(create table if not exists a (i int not null, str text not null);) db.exec %(insert into a (i, str) values (23, "bai bai");) end 2.times do |i| - DB.open "sqlite3", {"database": DB_FILENAME} do |db| + DB.open "sqlite3", DB_FILENAME do |db| begin db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs| rs.move_next diff --git a/src/sqlite3/connection.cr b/src/sqlite3/connection.cr index 3d64d6c..805179b 100644 --- a/src/sqlite3/connection.cr +++ b/src/sqlite3/connection.cr @@ -1,8 +1,7 @@ class SQLite3::Connection < DB::Connection - def initialize(options) + def initialize(connection_string) super - filename = options["database"] - check LibSQLite3.open_v2(filename, out @db, (LibSQLite3::Flag::READWRITE | LibSQLite3::Flag::CREATE), nil) + check LibSQLite3.open_v2(connection_string, out @db, (LibSQLite3::Flag::READWRITE | LibSQLite3::Flag::CREATE), nil) end def prepare(query) diff --git a/src/sqlite3/driver.cr b/src/sqlite3/driver.cr index 1b17fd9..52f5d6a 100644 --- a/src/sqlite3/driver.cr +++ b/src/sqlite3/driver.cr @@ -1,12 +1,6 @@ class SQLite3::Driver < DB::Driver def build_connection - SQLite3::Connection.new(options) - end - - # Quotes the given string, making it safe to use in an SQL statement. - # It replaces all instances of the single-quote character with two single-quote characters. - def self.quote(string) - string.gsub('\'', "''") + SQLite3::Connection.new(connection_string) end end diff --git a/src/sqlite3/result_set2.cr b/src/sqlite3/result_set2.cr index a411d8e..21f930d 100644 --- a/src/sqlite3/result_set2.cr +++ b/src/sqlite3/result_set2.cr @@ -66,7 +66,15 @@ class SQLite3::ResultSet2 < DB::ResultSet end def column_type(index : Int32) - raise "not implemented" + case LibSQLite3.column_type(self, index) + when Type::INTEGER; Int64 + when Type::FLOAT ; Float64 + when Type::BLOB ; Slice(UInt8) + when Type::TEXT ; String + when Type::NULL ; Nil + else + raise "not implemented" + end end def to_unsafe