column types

update to connection_string
remove Driver.quote
This commit is contained in:
Brian J. Cardiff 2016-01-31 17:31:35 -03:00
parent 2e6e6ed7e5
commit dd5c10ba6e
4 changed files with 33 additions and 20 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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