2016-01-31 00:14:46 +00:00
|
|
|
class SQLite3::Connection < DB::Connection
|
2016-02-04 00:29:19 +00:00
|
|
|
def initialize(database)
|
2016-01-31 00:14:46 +00:00
|
|
|
super
|
2016-02-04 00:29:19 +00:00
|
|
|
filename = self.class.filename(database.uri)
|
|
|
|
check LibSQLite3.open_v2(filename, out @db, (LibSQLite3::Flag::READWRITE | LibSQLite3::Flag::CREATE), nil)
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
def self.filename(uri : URI)
|
|
|
|
URI.unescape (if path = uri.path
|
|
|
|
(uri.host || "") + path
|
|
|
|
else
|
|
|
|
uri.opaque.not_nil!
|
|
|
|
end)
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
def build_statement(query)
|
|
|
|
Statement2.new(self, query)
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
def do_close
|
|
|
|
@statements_cache.values.each &.close
|
|
|
|
super
|
|
|
|
LibSQLite3.close_v2(self)
|
2016-01-31 17:03:05 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 00:14:46 +00:00
|
|
|
def to_unsafe
|
|
|
|
@db
|
|
|
|
end
|
|
|
|
|
|
|
|
private def check(code)
|
|
|
|
raise Exception.new(self) unless code == 0
|
|
|
|
end
|
|
|
|
end
|