2016-02-04 00:52:45 +00:00
|
|
|
class SQLite3::Statement < DB::Statement
|
2016-01-31 00:14:46 +00:00
|
|
|
def initialize(connection, sql)
|
|
|
|
super(connection)
|
2016-07-14 18:21:59 +00:00
|
|
|
check LibSQLite3.prepare_v2(sqlite3_connection, sql, sql.bytesize + 1, out @stmt, nil)
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
|
2016-06-21 15:47:12 +00:00
|
|
|
protected def perform_query(args : Enumerable) : DB::ResultSet
|
2016-01-29 19:15:28 +00:00
|
|
|
LibSQLite3.reset(self)
|
2016-02-04 00:29:19 +00:00
|
|
|
args.each_with_index(1) do |arg, index|
|
|
|
|
bind_arg(index, arg)
|
|
|
|
end
|
2016-02-04 00:52:45 +00:00
|
|
|
ResultSet.new(self)
|
2016-01-29 20:13:22 +00:00
|
|
|
end
|
|
|
|
|
2016-06-21 15:47:12 +00:00
|
|
|
protected def perform_exec(args : Enumerable) : DB::ExecResult
|
2016-08-29 19:41:29 +00:00
|
|
|
LibSQLite3.reset(self.to_unsafe)
|
|
|
|
args.each_with_index(1) do |arg, index|
|
|
|
|
bind_arg(index, arg)
|
|
|
|
end
|
|
|
|
|
|
|
|
# exec
|
|
|
|
step = LibSQLite3::Code.new LibSQLite3.step(self)
|
|
|
|
raise Exception.new(sqlite3_connection) unless step == LibSQLite3::Code::DONE
|
2016-01-30 00:58:04 +00:00
|
|
|
|
2016-07-14 18:21:59 +00:00
|
|
|
rows_affected = LibSQLite3.changes(sqlite3_connection).to_i64
|
|
|
|
last_id = LibSQLite3.last_insert_rowid(sqlite3_connection)
|
2016-01-29 20:13:22 +00:00
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
DB::ExecResult.new rows_affected, last_id
|
2016-01-29 20:13:22 +00:00
|
|
|
end
|
|
|
|
|
2017-05-29 18:20:32 +00:00
|
|
|
protected def do_close
|
2016-02-04 00:29:19 +00:00
|
|
|
super
|
|
|
|
check LibSQLite3.finalize(self)
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
|
2016-01-29 20:13:22 +00:00
|
|
|
private def bind_arg(index, value : Nil)
|
|
|
|
check LibSQLite3.bind_null(self, index)
|
|
|
|
end
|
|
|
|
|
2016-12-14 16:52:12 +00:00
|
|
|
private def bind_arg(index, value : Bool)
|
|
|
|
check LibSQLite3.bind_int(self, index, value ? 1 : 0)
|
|
|
|
end
|
|
|
|
|
2016-01-29 20:13:22 +00:00
|
|
|
private def bind_arg(index, value : Int32)
|
|
|
|
check LibSQLite3.bind_int(self, index, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def bind_arg(index, value : Int64)
|
|
|
|
check LibSQLite3.bind_int64(self, index, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def bind_arg(index, value : Float32)
|
|
|
|
check LibSQLite3.bind_double(self, index, value.to_f64)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def bind_arg(index, value : Float64)
|
|
|
|
check LibSQLite3.bind_double(self, index, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
private def bind_arg(index, value : String)
|
|
|
|
check LibSQLite3.bind_text(self, index, value, value.bytesize, nil)
|
|
|
|
end
|
|
|
|
|
2016-06-22 17:13:44 +00:00
|
|
|
private def bind_arg(index, value : Bytes)
|
2016-01-29 22:22:30 +00:00
|
|
|
check LibSQLite3.bind_blob(self, index, value, value.size, nil)
|
|
|
|
end
|
|
|
|
|
2016-06-22 19:17:47 +00:00
|
|
|
private def bind_arg(index, value : Time)
|
|
|
|
bind_arg(index, value.to_s(SQLite3::DATE_FORMAT))
|
|
|
|
end
|
|
|
|
|
2016-06-22 03:45:00 +00:00
|
|
|
private def bind_arg(index, value)
|
|
|
|
raise "#{self.class} does not support #{value.class} params"
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:15:28 +00:00
|
|
|
private def check(code)
|
2016-07-14 18:21:59 +00:00
|
|
|
raise Exception.new(sqlite3_connection) unless code == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
protected def sqlite3_connection
|
|
|
|
@connection.as(Connection)
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_unsafe
|
|
|
|
@stmt
|
|
|
|
end
|
|
|
|
end
|