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)
|
2016-06-23 20:27:58 +00:00
|
|
|
# TODO maybe enable Flag::URI to parse query string in the uri as additional flags
|
|
|
|
check LibSQLite3.open_v2(filename, out @db, (Flag::READWRITE | Flag::CREATE), nil)
|
2016-12-15 17:49:39 +00:00
|
|
|
rescue
|
|
|
|
raise DB::ConnectionRefused.new
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
def self.filename(uri : URI)
|
2020-09-30 14:34:25 +00:00
|
|
|
URI.decode_www_form((uri.host || "") + uri.path)
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
|
|
|
|
2019-07-29 13:25:46 +00:00
|
|
|
def build_prepared_statement(query) : Statement
|
2016-02-04 00:52:45 +00:00
|
|
|
Statement.new(self, query)
|
2016-01-31 00:14:46 +00:00
|
|
|
end
|
|
|
|
|
2019-07-29 13:25:46 +00:00
|
|
|
def build_unprepared_statement(query) : Statement
|
2016-12-03 20:52:24 +00:00
|
|
|
# sqlite3 does not support unprepared statement.
|
|
|
|
# All statements once prepared should be released
|
|
|
|
# when unneeded. Unprepared statement are not aim
|
|
|
|
# to leave state in the connection. Mimicking them
|
|
|
|
# with prepared statement would be wrong with
|
|
|
|
# respect connection resources.
|
|
|
|
raise DB::Error.new("SQLite3 driver does not support unprepared statements")
|
|
|
|
end
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
def do_close
|
|
|
|
super
|
2017-05-29 18:20:32 +00:00
|
|
|
check LibSQLite3.close(self)
|
2016-01-31 17:03:05 +00:00
|
|
|
end
|
|
|
|
|
2016-12-14 15:27:43 +00:00
|
|
|
# :nodoc:
|
|
|
|
def perform_begin_transaction
|
|
|
|
self.prepared.exec "BEGIN"
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def perform_commit_transaction
|
|
|
|
self.prepared.exec "COMMIT"
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def perform_rollback_transaction
|
|
|
|
self.prepared.exec "ROLLBACK"
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def perform_create_savepoint(name)
|
|
|
|
self.prepared.exec "SAVEPOINT #{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def perform_release_savepoint(name)
|
|
|
|
self.prepared.exec "RELEASE SAVEPOINT #{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# :nodoc:
|
|
|
|
def perform_rollback_savepoint(name)
|
|
|
|
self.prepared.exec "ROLLBACK TO #{name}"
|
|
|
|
end
|
|
|
|
|
2016-06-23 20:27:58 +00:00
|
|
|
# Dump the database to another SQLite3 database. This can be used for backing up a SQLite3 Database
|
|
|
|
# to disk or the opposite
|
|
|
|
def dump(to : SQLite3::Connection)
|
|
|
|
backup_item = LibSQLite3.backup_init(to.@db, "main", @db, "main")
|
|
|
|
if backup_item.null?
|
|
|
|
raise Exception.new(to.@db)
|
|
|
|
end
|
|
|
|
code = LibSQLite3.backup_step(backup_item, -1)
|
|
|
|
|
|
|
|
if code != LibSQLite3::Code::DONE
|
|
|
|
raise Exception.new(to.@db)
|
|
|
|
end
|
|
|
|
code = LibSQLite3.backup_finish(backup_item)
|
|
|
|
if code != LibSQLite3::Code::OKAY
|
|
|
|
raise Exception.new(to.@db)
|
|
|
|
end
|
|
|
|
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
|