2016-01-29 19:15:28 +00:00
|
|
|
class SQLite3::Statement2 < 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-01-31 00:14:46 +00:00
|
|
|
protected def begin_parameters
|
2016-01-29 19:15:28 +00:00
|
|
|
LibSQLite3.reset(self)
|
2016-01-29 20:13:22 +00:00
|
|
|
end
|
|
|
|
|
2016-01-30 00:58:04 +00:00
|
|
|
protected def on_close
|
|
|
|
check LibSQLite3.finalize(self)
|
|
|
|
end
|
|
|
|
|
2016-01-29 20:13:22 +00:00
|
|
|
protected def add_parameter(index : Int32, value)
|
|
|
|
bind_arg(index, 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
|
|
|
|
|
2016-01-31 00:14:46 +00:00
|
|
|
protected def perform
|
2016-01-29 19:15:28 +00:00
|
|
|
ResultSet2.new(self)
|
|
|
|
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-01-29 22:22:30 +00:00
|
|
|
private def bind_arg(index, value : Slice(UInt8))
|
|
|
|
check LibSQLite3.bind_blob(self, index, value, value.size, nil)
|
|
|
|
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
|