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)
|
|
|
|
check LibSQLite3.prepare_v2(@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-02-04 00:29:19 +00:00
|
|
|
rs = perform_query(args)
|
|
|
|
rs.move_next
|
|
|
|
rs.close
|
2016-01-30 00:58:04 +00:00
|
|
|
|
2016-06-21 21:32:06 +00:00
|
|
|
rows_affected = LibSQLite3.changes(connection).to_i64
|
2016-02-04 00:29:19 +00:00
|
|
|
last_id = LibSQLite3.last_insert_rowid(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
|
|
|
|
|
2016-02-04 00:29:19 +00:00
|
|
|
protected def on_close
|
|
|
|
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
|
|
|
|
|
|
|
|
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-01-31 00:14:46 +00:00
|
|
|
raise Exception.new(@connection) unless code == 0
|
2016-01-29 19:15:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_unsafe
|
|
|
|
@stmt
|
|
|
|
end
|
|
|
|
end
|