package sqlite3import ()// Stmt is a prepared statement object.//// https://sqlite.org/c3ref/stmt.htmltypeStmtstruct { c *Conn err error sql string handle ptr_t}// Close destroys the prepared statement object.//// It is safe to close a nil, zero or closed Stmt.//// https://sqlite.org/c3ref/finalize.htmlfunc ( *Stmt) () error {if == nil || .handle == 0 {returnnil } := res_t(.c.call("sqlite3_finalize", stk_t(.handle))) := .c.stmtsfor := range {if == [] { := len() - 1 [] = [] [] = nil .c.stmts = [:]break } } .handle = 0return .c.error()}// Conn returns the database connection to which the prepared statement belongs.//// https://sqlite.org/c3ref/db_handle.htmlfunc ( *Stmt) () *Conn {return .c}// SQL returns the SQL text used to create the prepared statement.//// https://sqlite.org/c3ref/expanded_sql.htmlfunc ( *Stmt) () string {return .sql}// ExpandedSQL returns the SQL text of the prepared statement// with bound parameters expanded.//// https://sqlite.org/c3ref/expanded_sql.htmlfunc ( *Stmt) () string { := ptr_t(.c.call("sqlite3_expanded_sql", stk_t(.handle))) := util.ReadString(.c.mod, , _MAX_SQL_LENGTH) .c.free()return}// ReadOnly returns true if and only if the statement// makes no direct changes to the content of the database file.//// https://sqlite.org/c3ref/stmt_readonly.htmlfunc ( *Stmt) () bool { := int32(.c.call("sqlite3_stmt_readonly", stk_t(.handle)))return != 0}// Reset resets the prepared statement object.//// https://sqlite.org/c3ref/reset.htmlfunc ( *Stmt) () error { := res_t(.c.call("sqlite3_reset", stk_t(.handle))) .err = nilreturn .c.error()}// Busy determines if a prepared statement has been reset.//// https://sqlite.org/c3ref/stmt_busy.htmlfunc ( *Stmt) () bool { := res_t(.c.call("sqlite3_stmt_busy", stk_t(.handle)))return != 0}// Step evaluates the SQL statement.// If the SQL statement being executed returns any data,// then true is returned each time a new row of data is ready for processing by the caller.// The values may be accessed using the Column access functions.// Step is called again to retrieve the next row of data.// If an error has occurred, Step returns false;// call [Stmt.Err] or [Stmt.Reset] to get the error.//// https://sqlite.org/c3ref/step.htmlfunc ( *Stmt) () bool {if .c.interrupt.Err() != nil { .err = INTERRUPTreturnfalse } := res_t(.c.call("sqlite3_step", stk_t(.handle)))switch {case_ROW: .err = nilreturntruecase_DONE: .err = nildefault: .err = .c.error() }returnfalse}// Err gets the last error occurred during [Stmt.Step].// Err returns nil after [Stmt.Reset] is called.//// https://sqlite.org/c3ref/step.htmlfunc ( *Stmt) () error {return .err}// Exec is a convenience function that repeatedly calls [Stmt.Step] until it returns false,// then calls [Stmt.Reset] to reset the statement and get any error that occurred.func ( *Stmt) () error {if .c.interrupt.Err() != nil {returnINTERRUPT } := res_t(.c.call("sqlite3_exec_go", stk_t(.handle))) .err = nilreturn .c.error()}// Status monitors the performance characteristics of prepared statements.//// https://sqlite.org/c3ref/stmt_status.htmlfunc ( *Stmt) ( StmtStatus, bool) int {if > STMTSTATUS_FILTER_HIT && != STMTSTATUS_MEMUSED {return0 }varint32if { = 1 } := int32(.c.call("sqlite3_stmt_status", stk_t(.handle),stk_t(), stk_t()))returnint()}// ClearBindings resets all bindings on the prepared statement.//// https://sqlite.org/c3ref/clear_bindings.htmlfunc ( *Stmt) () error { := res_t(.c.call("sqlite3_clear_bindings", stk_t(.handle)))return .c.error()}// BindCount returns the number of SQL parameters in the prepared statement.//// https://sqlite.org/c3ref/bind_parameter_count.htmlfunc ( *Stmt) () int { := int32(.c.call("sqlite3_bind_parameter_count",stk_t(.handle)))returnint()}// BindIndex returns the index of a parameter in the prepared statement// given its name.//// https://sqlite.org/c3ref/bind_parameter_index.htmlfunc ( *Stmt) ( string) int {defer .c.arena.mark()() := .c.arena.string() := int32(.c.call("sqlite3_bind_parameter_index",stk_t(.handle), stk_t()))returnint()}// BindName returns the name of a parameter in the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_parameter_name.htmlfunc ( *Stmt) ( int) string { := ptr_t(.c.call("sqlite3_bind_parameter_name",stk_t(.handle), stk_t()))if == 0 {return"" }returnutil.ReadString(.c.mod, , _MAX_NAME)}// BindBool binds a bool to the prepared statement.// The leftmost SQL parameter has an index of 1.// SQLite does not have a separate boolean storage class.// Instead, boolean values are stored as integers 0 (false) and 1 (true).//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, bool) error {varint64if { = 1 }return .BindInt64(, )}// BindInt binds an int to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, int) error {return .BindInt64(, int64())}// BindInt64 binds an int64 to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, int64) error { := res_t(.c.call("sqlite3_bind_int64",stk_t(.handle), stk_t(), stk_t()))return .c.error()}// BindFloat binds a float64 to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, float64) error { := res_t(.c.call("sqlite3_bind_double",stk_t(.handle), stk_t(),stk_t(math.Float64bits())))return .c.error()}// BindText binds a string to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, string) error {iflen() > _MAX_LENGTH {returnTOOBIG } := .c.newString() := res_t(.c.call("sqlite3_bind_text_go",stk_t(.handle), stk_t(),stk_t(), stk_t(len())))return .c.error()}// BindRawText binds a []byte to the prepared statement as text.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, []byte) error {iflen() > _MAX_LENGTH {returnTOOBIG }iflen() == 0 {return .BindText(, "") } := .c.newBytes() := res_t(.c.call("sqlite3_bind_text_go",stk_t(.handle), stk_t(),stk_t(), stk_t(len())))return .c.error()}// BindBlob binds a []byte to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, []byte) error {iflen() > _MAX_LENGTH {returnTOOBIG }iflen() == 0 {return .BindZeroBlob(, 0) } := .c.newBytes() := res_t(.c.call("sqlite3_bind_blob_go",stk_t(.handle), stk_t(),stk_t(), stk_t(len())))return .c.error()}// BindZeroBlob binds a zero-filled, length n BLOB to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, int64) error { := res_t(.c.call("sqlite3_bind_zeroblob64",stk_t(.handle), stk_t(), stk_t()))return .c.error()}// BindNull binds a NULL to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int) error { := res_t(.c.call("sqlite3_bind_null",stk_t(.handle), stk_t()))return .c.error()}// BindTime binds a [time.Time] to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, time.Time, TimeFormat) error {switch {caseTimeFormatDefault, TimeFormatAuto, time.RFC3339Nano:return .bindRFC3339Nano(, ) }switch v := .Encode().(type) {casestring:return .BindText(, )caseint64:return .BindInt64(, )casefloat64:return .BindFloat(, )default:panic(util.AssertErr()) }}func ( *Stmt) ( int, time.Time) error {const = int64(len(time.RFC3339Nano)) + 5 := .c.new() := util.View(.c.mod, , ) = .AppendFormat([:0], time.RFC3339Nano) := res_t(.c.call("sqlite3_bind_text_go",stk_t(.handle), stk_t(),stk_t(), stk_t(len())))return .c.error()}// BindPointer binds a NULL to the prepared statement, just like [Stmt.BindNull],// but it also associates ptr with that NULL value such that it can be retrieved// within an application-defined SQL function using [Value.Pointer].// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, any) error { := util.AddHandle(.c.ctx, ) := res_t(.c.call("sqlite3_bind_pointer_go",stk_t(.handle), stk_t(), stk_t()))return .c.error()}// BindValue binds a copy of value to the prepared statement.// The leftmost SQL parameter has an index of 1.//// https://sqlite.org/c3ref/bind_blob.htmlfunc ( *Stmt) ( int, Value) error {if .c != .c {returnMISUSE } := res_t(.c.call("sqlite3_bind_value",stk_t(.handle), stk_t(), stk_t(.handle)))return .c.error()}// DataCount resets the number of columns in a result set.//// https://sqlite.org/c3ref/data_count.htmlfunc ( *Stmt) () int { := int32(.c.call("sqlite3_data_count",stk_t(.handle)))returnint()}// ColumnCount returns the number of columns in a result set.//// https://sqlite.org/c3ref/column_count.htmlfunc ( *Stmt) () int { := int32(.c.call("sqlite3_column_count",stk_t(.handle)))returnint()}// ColumnName returns the name of the result column.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_name.htmlfunc ( *Stmt) ( int) string { := ptr_t(.c.call("sqlite3_column_name",stk_t(.handle), stk_t()))if == 0 {panic(util.OOMErr) }returnutil.ReadString(.c.mod, , _MAX_NAME)}// ColumnType returns the initial [Datatype] of the result column.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) Datatype {returnDatatype(.c.call("sqlite3_column_type",stk_t(.handle), stk_t()))}// ColumnDeclType returns the declared datatype of the result column.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_decltype.htmlfunc ( *Stmt) ( int) string { := ptr_t(.c.call("sqlite3_column_decltype",stk_t(.handle), stk_t()))if == 0 {return"" }returnutil.ReadString(.c.mod, , _MAX_NAME)}// ColumnDatabaseName returns the name of the database// that is the origin of a particular result column.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_database_name.htmlfunc ( *Stmt) ( int) string { := ptr_t(.c.call("sqlite3_column_database_name",stk_t(.handle), stk_t()))if == 0 {return"" }returnutil.ReadString(.c.mod, , _MAX_NAME)}// ColumnTableName returns the name of the table// that is the origin of a particular result column.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_database_name.htmlfunc ( *Stmt) ( int) string { := ptr_t(.c.call("sqlite3_column_table_name",stk_t(.handle), stk_t()))if == 0 {return"" }returnutil.ReadString(.c.mod, , _MAX_NAME)}// ColumnOriginName returns the name of the table column// that is the origin of a particular result column.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_database_name.htmlfunc ( *Stmt) ( int) string { := ptr_t(.c.call("sqlite3_column_origin_name",stk_t(.handle), stk_t()))if == 0 {return"" }returnutil.ReadString(.c.mod, , _MAX_NAME)}// ColumnBool returns the value of the result column as a bool.// The leftmost column of the result set has the index 0.// SQLite does not have a separate boolean storage class.// Instead, boolean values are retrieved as numbers,// with 0 converted to false and any other value to true.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) bool {return .ColumnFloat() != 0}// ColumnInt returns the value of the result column as an int.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) int {returnint(.ColumnInt64())}// ColumnInt64 returns the value of the result column as an int64.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) int64 {returnint64(.c.call("sqlite3_column_int64",stk_t(.handle), stk_t()))}// ColumnFloat returns the value of the result column as a float64.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) float64 { := uint64(.c.call("sqlite3_column_double",stk_t(.handle), stk_t()))returnmath.Float64frombits()}// ColumnTime returns the value of the result column as a [time.Time].// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int, TimeFormat) time.Time {varanyswitch .ColumnType() {caseINTEGER: = .ColumnInt64()caseFLOAT: = .ColumnFloat()caseTEXT, BLOB: = .ColumnText()caseNULL:returntime.Time{}default:panic(util.AssertErr()) } , := .Decode()if != nil { .err = }return}// ColumnText returns the value of the result column as a string.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) string {returnstring(.ColumnRawText())}// ColumnBlob appends to buf and returns// the value of the result column as a []byte.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int, []byte) []byte {returnappend(, .ColumnRawBlob()...)}// ColumnRawText returns the value of the result column as a []byte.// The []byte is owned by SQLite and may be invalidated by// subsequent calls to [Stmt] methods.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) []byte { := ptr_t(.c.call("sqlite3_column_text",stk_t(.handle), stk_t()))return .columnRawBytes(, , 1)}// ColumnRawBlob returns the value of the result column as a []byte.// The []byte is owned by SQLite and may be invalidated by// subsequent calls to [Stmt] methods.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) []byte { := ptr_t(.c.call("sqlite3_column_blob",stk_t(.handle), stk_t()))return .columnRawBytes(, , 0)}func ( *Stmt) ( int, ptr_t, int32) []byte {if == 0 { := res_t(.c.call("sqlite3_errcode", stk_t(.c.handle)))if != _ROW && != _DONE { .err = .c.error() }returnnil } := int32(.c.call("sqlite3_column_bytes",stk_t(.handle), stk_t()))returnutil.View(.c.mod, , int64(+))[:]}// ColumnValue returns the unprotected value of the result column.// The leftmost column of the result set has the index 0.//// https://sqlite.org/c3ref/column_blob.htmlfunc ( *Stmt) ( int) Value { := ptr_t(.c.call("sqlite3_column_value",stk_t(.handle), stk_t()))returnValue{c: .c,handle: , }}// Columns populates result columns into the provided slice.// The slice must have [Stmt.ColumnCount] length.//// [INTEGER] columns will be retrieved as int64 values,// [FLOAT] as float64, [NULL] as nil,// [TEXT] as string, and [BLOB] as []byte.func ( *Stmt) ( ...any) error {defer .c.arena.mark()() , , := .columns(int64(len()))if != nil {return }// Avoid bounds checks on types below.iflen() != len() {panic(util.AssertErr()) }for := range {switch [] {casebyte(INTEGER): [] = util.Read64[int64](.c.mod, )casebyte(FLOAT): [] = util.ReadFloat64(.c.mod, )casebyte(NULL): [] = nilcasebyte(TEXT): := util.Read32[int32](.c.mod, +4)if != 0 { := util.Read32[ptr_t](.c.mod, ) := util.View(.c.mod, , int64()) [] = string() } else { [] = "" }casebyte(BLOB): := util.Read32[int32](.c.mod, +4)if != 0 { := util.Read32[ptr_t](.c.mod, ) := util.View(.c.mod, , int64()) , := [].([]byte) [] = append([:0], ...) } else { [], _ = [].([]byte) } } += 8 }returnnil}// ColumnsRaw populates result columns into the provided slice.// The slice must have [Stmt.ColumnCount] length.//// [INTEGER] columns will be retrieved as int64 values,// [FLOAT] as float64, [NULL] as nil,// [TEXT] and [BLOB] as []byte.// Any []byte are owned by SQLite and may be invalidated by// subsequent calls to [Stmt] methods.func ( *Stmt) ( ...any) error {defer .c.arena.mark()() , , := .columns(int64(len()))if != nil {return }// Avoid bounds checks on types below.iflen() != len() {panic(util.AssertErr()) }for := range {switch [] {casebyte(INTEGER): [] = util.Read64[int64](.c.mod, )casebyte(FLOAT): [] = util.ReadFloat64(.c.mod, )casebyte(NULL): [] = nildefault: := util.Read32[int32](.c.mod, +4)if == 0 && [] == byte(BLOB) { [] = []byte{} } else { := if [] == byte(TEXT) { ++ } := util.Read32[ptr_t](.c.mod, ) := util.View(.c.mod, , int64())[:] [] = } } += 8 }returnnil}func ( *Stmt) ( int64) ([]byte, ptr_t, error) { := .c.arena.new() := .c.arena.new( * 8) := res_t(.c.call("sqlite3_columns_go",stk_t(.handle), stk_t(), stk_t(), stk_t()))if == res_t(MISUSE) {returnnil, 0, MISUSE }if := .c.error(); != nil {returnnil, 0, }returnutil.View(.c.mod, , ), , nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.