update to last db design

This commit is contained in:
Brian J. Cardiff 2016-02-03 21:29:19 -03:00
parent 15417b7c38
commit 503868f434
7 changed files with 104 additions and 97 deletions

View file

@ -1,21 +1,28 @@
class SQLite3::Connection < DB::Connection
def initialize(connection_string)
def initialize(database)
super
check LibSQLite3.open_v2(connection_string, out @db, (LibSQLite3::Flag::READWRITE | LibSQLite3::Flag::CREATE), nil)
filename = self.class.filename(database.uri)
check LibSQLite3.open_v2(filename, out @db, (LibSQLite3::Flag::READWRITE | LibSQLite3::Flag::CREATE), nil)
end
def prepare(query)
def self.filename(uri : URI)
URI.unescape (if path = uri.path
(uri.host || "") + path
else
uri.opaque.not_nil!
end)
end
def build_statement(query)
Statement2.new(self, query)
end
def perform_close
def do_close
@statements_cache.values.each &.close
super
LibSQLite3.close_v2(self)
end
def last_insert_id : Int64
LibSQLite3.last_insert_rowid(self)
end
def to_unsafe
@db
end

View file

@ -1,6 +1,6 @@
class SQLite3::Driver < DB::Driver
def build_connection
SQLite3::Connection.new(connection_string)
def build_connection(db)
SQLite3::Connection.new(db)
end
end

View file

@ -6,37 +6,37 @@ lib LibSQLite3
type Statement = Void*
enum Flag
READONLY = 0x00000001 # Ok for sqlite3_open_v2()
READWRITE = 0x00000002 # Ok for sqlite3_open_v2()
CREATE = 0x00000004 # Ok for sqlite3_open_v2()
DELETEONCLOSE = 0x00000008 # VFS only
EXCLUSIVE = 0x00000010 # VFS only
AUTOPROXY = 0x00000020 # VFS only
URI = 0x00000040 # Ok for sqlite3_open_v2()
MEMORY = 0x00000080 # Ok for sqlite3_open_v2()
MAIN_DB = 0x00000100 # VFS only
TEMP_DB = 0x00000200 # VFS only
TRANSIENT_DB = 0x00000400 # VFS only
MAIN_JOURNAL = 0x00000800 # VFS only
TEMP_JOURNAL = 0x00001000 # VFS only
SUBJOURNAL = 0x00002000 # VFS only
MASTER_JOURNAL = 0x00004000 # VFS only
NOMUTEX = 0x00008000 # Ok for sqlite3_open_v2()
FULLMUTEX = 0x00010000 # Ok for sqlite3_open_v2()
SHAREDCACHE = 0x00020000 # Ok for sqlite3_open_v2()
PRIVATECACHE = 0x00040000 # Ok for sqlite3_open_v2()
WAL = 0x00080000 # VFS only
READONLY = 0x00000001 # Ok for sqlite3_open_v2()
READWRITE = 0x00000002 # Ok for sqlite3_open_v2()
CREATE = 0x00000004 # Ok for sqlite3_open_v2()
DELETEONCLOSE = 0x00000008 # VFS only
EXCLUSIVE = 0x00000010 # VFS only
AUTOPROXY = 0x00000020 # VFS only
URI = 0x00000040 # Ok for sqlite3_open_v2()
MEMORY = 0x00000080 # Ok for sqlite3_open_v2()
MAIN_DB = 0x00000100 # VFS only
TEMP_DB = 0x00000200 # VFS only
TRANSIENT_DB = 0x00000400 # VFS only
MAIN_JOURNAL = 0x00000800 # VFS only
TEMP_JOURNAL = 0x00001000 # VFS only
SUBJOURNAL = 0x00002000 # VFS only
MASTER_JOURNAL = 0x00004000 # VFS only
NOMUTEX = 0x00008000 # Ok for sqlite3_open_v2()
FULLMUTEX = 0x00010000 # Ok for sqlite3_open_v2()
SHAREDCACHE = 0x00020000 # Ok for sqlite3_open_v2()
PRIVATECACHE = 0x00040000 # Ok for sqlite3_open_v2()
WAL = 0x00080000 # VFS only
end
enum Code
ROW = 100
ROW = 100
DONE = 101
end
alias Callback = (Void*, Int32, UInt8**, UInt8**) -> Int32
fun open = sqlite3_open_v2(filename : UInt8*, db : SQLite3*) : Int32
fun open_v2 = sqlite3_open_v2(filename : UInt8*, db : SQLite3*, flags: Flag, zVfs : UInt8*) : Int32
fun open_v2 = sqlite3_open_v2(filename : UInt8*, db : SQLite3*, flags : Flag, zVfs : UInt8*) : Int32
fun errcode = sqlite3_errcode(SQLite3) : Int32
fun errmsg = sqlite3_errmsg(SQLite3) : UInt8*
@ -62,6 +62,7 @@ lib LibSQLite3
fun reset = sqlite3_reset(stmt : Statement) : Int32
fun column_name = sqlite3_column_name(stmt : Statement, idx : Int32) : UInt8*
fun last_insert_rowid = sqlite3_last_insert_rowid(db : SQLite3) : Int64
fun changes = sqlite3_changes(db : SQLite3) : Int32
fun finalize = sqlite3_finalize(stmt : Statement) : Int32
fun close_v2 = sqlite3_close_v2(SQLite3) : Int32

View file

@ -1,6 +1,11 @@
class SQLite3::ResultSet2 < DB::ResultSet
@column_index = 0
protected def do_close
super
LibSQLite3.reset(self)
end
# Advances to the next row. Returns `true` if there's a next row,
# `false` otherwise. Must be called at least once to advance to the first
# row.

View file

@ -4,29 +4,30 @@ class SQLite3::Statement2 < DB::Statement
check LibSQLite3.prepare_v2(@connection, sql, sql.bytesize + 1, out @stmt, nil)
end
protected def begin_parameters
protected def perform_query(args : Slice(DB::Any))
LibSQLite3.reset(self)
args.each_with_index(1) do |arg, index|
bind_arg(index, arg)
end
ResultSet2.new(self)
end
protected def perform_exec(args : Slice(DB::Any))
rs = perform_query(args)
rs.move_next
rs.close
rows_affected = LibSQLite3.changes(connection)
last_id = LibSQLite3.last_insert_rowid(connection)
DB::ExecResult.new rows_affected, last_id
end
protected def on_close
super
check LibSQLite3.finalize(self)
end
protected def add_parameter(index : Int32, value)
bind_arg(index + 1, value)
end
protected def add_parameter(name : String, value)
converted_name = ":#{name}"
index = LibSQLite3.bind_parameter_index(self, converted_name)
raise "Unknown parameter: #{name}" if index == 0
bind_arg(index, value)
end
protected def perform
ResultSet2.new(self)
end
private def bind_arg(index, value : Nil)
check LibSQLite3.bind_null(self, index)
end