mirror of
https://gitea.invidious.io/iv-org/shard-crystal-sqlite3.git
synced 2024-08-15 00:53:26 +00:00
column types
update to connection_string remove Driver.quote
This commit is contained in:
parent
2e6e6ed7e5
commit
dd5c10ba6e
4 changed files with 33 additions and 20 deletions
|
@ -1,13 +1,13 @@
|
||||||
require "./spec_helper"
|
require "./spec_helper"
|
||||||
|
|
||||||
def with_db(&block : DB::Database ->)
|
def with_db(&block : DB::Database ->)
|
||||||
DB.open "sqlite3", {"database": DB_FILENAME}, &block
|
DB.open "sqlite3", DB_FILENAME, &block
|
||||||
ensure
|
ensure
|
||||||
File.delete(DB_FILENAME)
|
File.delete(DB_FILENAME)
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_mem_db(&block : DB::Database ->)
|
def with_mem_db(&block : DB::Database ->)
|
||||||
DB.open "sqlite3", {"database": ":memory:"}, &block
|
DB.open "sqlite3", ":memory:", &block
|
||||||
end
|
end
|
||||||
|
|
||||||
def sql(s : String)
|
def sql(s : String)
|
||||||
|
@ -156,7 +156,23 @@ describe Driver do
|
||||||
end
|
end
|
||||||
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
|
it "gets last insert row id" do
|
||||||
with_mem_db do |db|
|
with_mem_db do |db|
|
||||||
|
@ -210,19 +226,15 @@ describe Driver do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "quotes" do
|
|
||||||
Driver.quote("'hello'").should eq("''hello''")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "ensures statements are closed" do
|
it "ensures statements are closed" do
|
||||||
begin
|
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 %(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");)
|
db.exec %(insert into a (i, str) values (23, "bai bai");)
|
||||||
end
|
end
|
||||||
|
|
||||||
2.times do |i|
|
2.times do |i|
|
||||||
DB.open "sqlite3", {"database": DB_FILENAME} do |db|
|
DB.open "sqlite3", DB_FILENAME do |db|
|
||||||
begin
|
begin
|
||||||
db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs|
|
db.query("SELECT i, str FROM a WHERE i = ?", 23) do |rs|
|
||||||
rs.move_next
|
rs.move_next
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
class SQLite3::Connection < DB::Connection
|
class SQLite3::Connection < DB::Connection
|
||||||
def initialize(options)
|
def initialize(connection_string)
|
||||||
super
|
super
|
||||||
filename = options["database"]
|
check LibSQLite3.open_v2(connection_string, out @db, (LibSQLite3::Flag::READWRITE | LibSQLite3::Flag::CREATE), nil)
|
||||||
check LibSQLite3.open_v2(filename, out @db, (LibSQLite3::Flag::READWRITE | LibSQLite3::Flag::CREATE), nil)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def prepare(query)
|
def prepare(query)
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
class SQLite3::Driver < DB::Driver
|
class SQLite3::Driver < DB::Driver
|
||||||
def build_connection
|
def build_connection
|
||||||
SQLite3::Connection.new(options)
|
SQLite3::Connection.new(connection_string)
|
||||||
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('\'', "''")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,15 @@ class SQLite3::ResultSet2 < DB::ResultSet
|
||||||
end
|
end
|
||||||
|
|
||||||
def column_type(index : Int32)
|
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
|
end
|
||||||
|
|
||||||
def to_unsafe
|
def to_unsafe
|
||||||
|
|
Loading…
Reference in a new issue