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.wrp.Xsqlite3_finalize(int32(.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.wrp.Xsqlite3_expanded_sql(int32(.handle))) := .c.wrp.ReadString(, _MAX_SQL_LENGTH) .c.wrp.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.wrp.Xsqlite3_stmt_readonly(int32(.handle)))return != 0}// Reset resets the prepared statement object.//// https://sqlite.org/c3ref/reset.htmlfunc ( *Stmt) () error { := res_t(.c.wrp.Xsqlite3_reset(int32(.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.wrp.Xsqlite3_stmt_busy(int32(.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.wrp.Xsqlite3_step(int32(.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.wrp.Xsqlite3_exec_go(int32(.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.wrp.Xsqlite3_stmt_status(int32(.handle),int32(), ))returnint()}// ClearBindings resets all bindings on the prepared statement.//// https://sqlite.org/c3ref/clear_bindings.htmlfunc ( *Stmt) () error { := res_t(.c.wrp.Xsqlite3_clear_bindings(int32(.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.wrp.Xsqlite3_bind_parameter_count(int32(.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.wrp.Xsqlite3_bind_parameter_index(int32(.handle), int32()))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.wrp.Xsqlite3_bind_parameter_name(int32(.handle), int32()))if == 0 {return"" }return .c.wrp.ReadString(, _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.wrp.Xsqlite3_bind_int64(int32(.handle), int32(), ))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.wrp.Xsqlite3_bind_double(int32(.handle), int32(), ))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.wrp.NewString() := res_t(.c.wrp.Xsqlite3_bind_text_go(int32(.handle), int32(),int32(), int64(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 {return .BindText(, string()) // does not escape}// 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.wrp.NewBytes() := res_t(.c.wrp.Xsqlite3_bind_blob_go(int32(.handle), int32(),int32(), int64(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.wrp.Xsqlite3_bind_zeroblob64(int32(.handle), int32(), ))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.wrp.Xsqlite3_bind_null(int32(.handle), int32()))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(errutil.AssertErr()) }}func ( *Stmt) ( int, time.Time) error {const = 48 := .c.wrp.New() := .c.wrp.Bytes(, ) = .AppendFormat([:0], time.RFC3339Nano) _ = append(, 0) := res_t(.c.wrp.Xsqlite3_bind_text_go(int32(.handle), int32(),int32(), int64(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 { := .c.wrp.AddHandle() := res_t(.c.wrp.Xsqlite3_bind_pointer_go(int32(.handle), int32(), int32()))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.wrp.Xsqlite3_bind_value(int32(.handle), int32(), int32(.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.wrp.Xsqlite3_data_count(int32(.handle)))returnint()}// ColumnCount returns the number of columns in a result set.//// https://sqlite.org/c3ref/column_count.htmlfunc ( *Stmt) () int { := int32(.c.wrp.Xsqlite3_column_count(int32(.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.wrp.Xsqlite3_column_name(int32(.handle), int32()))if == 0 {panic(errutil.OOMErr) }return .c.wrp.ReadString(, _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.wrp.Xsqlite3_column_type(int32(.handle), int32()))}// 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.wrp.Xsqlite3_column_decltype(int32(.handle), int32()))if == 0 {return"" }return .c.wrp.ReadString(, _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.wrp.Xsqlite3_column_database_name(int32(.handle), int32()))if == 0 {return"" }return .c.wrp.ReadString(, _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.wrp.Xsqlite3_column_table_name(int32(.handle), int32()))if == 0 {return"" }return .c.wrp.ReadString(, _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.wrp.Xsqlite3_column_origin_name(int32(.handle), int32()))if == 0 {return"" }return .c.wrp.ReadString(, _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 {return .c.wrp.Xsqlite3_column_int64(int32(.handle), int32())}// 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 {return .c.wrp.Xsqlite3_column_double(int32(.handle), int32())}// 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(errutil.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.wrp.Xsqlite3_column_text(int32(.handle), int32()))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.wrp.Xsqlite3_column_blob(int32(.handle), int32()))return .columnRawBytes(, , 0)}func ( *Stmt) ( int, ptr_t, int32) []byte {if == 0 { := res_t(.c.wrp.Xsqlite3_errcode(int32(.c.handle)))if != _ROW && != _DONE { .err = .c.error() }returnnil } := int32(.c.wrp.Xsqlite3_column_bytes(int32(.handle), int32()))return .c.wrp.Bytes(, 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.wrp.Xsqlite3_column_value(int32(.handle), int32()))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(errutil.AssertErr()) } := .c.wrp.Memoryfor := range {switch [] {casebyte(INTEGER): [] = int64(.Read64())casebyte(FLOAT): [] = .ReadFloat64()casebyte(NULL): [] = nilcasebyte(TEXT): := int32(.Read32( + 4))if != 0 { := ptr_t(.Read32()) := .Bytes(, int64()) [] = string() } else { [] = "" }casebyte(BLOB): := int32(.Read32( + 4))if != 0 { := ptr_t(.Read32()) := .Bytes(, 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(errutil.AssertErr()) } := .c.wrp.Memoryfor := range {switch [] {casebyte(INTEGER): [] = int64(.Read64())casebyte(FLOAT): [] = .ReadFloat64()casebyte(NULL): [] = nildefault: := int32(.Read32( + 4))if == 0 && [] == byte(BLOB) { [] = []byte{} } else { := if [] == byte(TEXT) { ++ } := ptr_t(.Read32()) := .Bytes(, int64())[:] [] = } } += 8 }returnnil}func ( *Stmt) ( int64) ([]byte, ptr_t, error) { := .c.arena.New() := .c.arena.New( * 8) := res_t(.c.wrp.Xsqlite3_columns_go(int32(.handle), int32(), int32(), int32()))if == res_t(MISUSE) {returnnil, 0, MISUSE }if := .c.error(); != nil {returnnil, 0, }return .c.wrp.Bytes(, ), , nil}
The pages are generated with Goldsv0.8.4. (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.