github.com/tailscale/sqlite@v0.0.0-20240515181108-c667cbe57c66/sqliteh/sqliteh.go (about)

     1  // Package sqliteh contains SQLite constants for Gophers.
     2  package sqliteh
     3  
     4  // Given everything in here has an sqliteh. prefix,
     5  // why not strip the SQLITE_ prefix from constants?
     6  // Because this way standard names show up in search.
     7  
     8  import (
     9  	"context"
    10  	"sync"
    11  	"time"
    12  )
    13  
    14  // OpenFunc is sqlite3_open_v2.
    15  //
    16  // Surprisingly: an error opening the DB can return a non-nil handle.
    17  // Call Close on it.
    18  //
    19  // https://sqlite.org/c3ref/open.html
    20  type OpenFunc func(filename string, flags OpenFlags, vfs string) (DB, error)
    21  
    22  // DB is an sqlite3* database connection object.
    23  // https://sqlite.org/c3ref/sqlite3.html
    24  type DB interface {
    25  	// Close is sqlite3_close.
    26  	// https://sqlite.org/c3ref/close.html
    27  	Close() error
    28  	// Interrupt is sqlite3_interrupt.
    29  	// https://www.sqlite.org/c3ref/interrupt.html
    30  	Interrupt()
    31  	// ErrMsg is sqlite3_errmsg.
    32  	// https://sqlite.org/c3ref/errcode.html
    33  	ErrMsg() string
    34  	// Changes is sqlite3_changes.
    35  	// https://sqlite.org/c3ref/changes.html
    36  	Changes() int
    37  	// TotalChanges is sqlite3_total_changes.
    38  	// https://sqlite.org/c3ref/total_changes.html
    39  	TotalChanges() int
    40  	// ExtendedErrCode is sqlite3_extended_errcode.
    41  	// https://sqlite.org/c3ref/errcode.html
    42  	ExtendedErrCode() Code
    43  	// LastInsertRowid is sqlite3_last_insert_rowid.
    44  	// https://sqlite.org/c3ref/last_insert_rowid.html
    45  	LastInsertRowid() int64
    46  	// Prepare is sqlite3_prepare_v3.
    47  	// https://www.sqlite.org/c3ref/prepare.html
    48  	Prepare(query string, prepFlags PrepareFlags) (stmt Stmt, remainingQuery string, err error)
    49  	// BusyTimeout is sqlite3_busy_timeout.
    50  	// https://www.sqlite.org/c3ref/busy_timeout.html
    51  	BusyTimeout(time.Duration)
    52  	// Checkpoint is sqlite3_wal_checkpoint_v2.
    53  	Checkpoint(db string, mode Checkpoint) (numFrames, numFramesCheckpointed int, err error)
    54  	// AutoCheckpoint is sqlite3_wal_autocheckpoint.
    55  	// https://sqlite.org/c3ref/wal_autocheckpoint.html
    56  	AutoCheckpoint(n int) error
    57  	// TxnState is sqlite3_txn_state.
    58  	TxnState(schema string) TxnState
    59  	// SetWALHook is sqlite3_wal_hook.
    60  	//
    61  	// If hook is nil, the hook is removed.
    62  	SetWALHook(hook func(dbName string, pages int))
    63  }
    64  
    65  // Stmt is an sqlite3_stmt* database connection object.
    66  // https://sqlite.org/c3ref/stmt.html
    67  type Stmt interface {
    68  	// DBHandle is sqlite3_db_handle.
    69  	// https://www.sqlite.org/c3ref/db_handle.html.
    70  	DBHandle() DB
    71  	// SQL is sqlite3_sql.
    72  	// https://www.sqlite.org/c3ref/expanded_sql.html
    73  	SQL() string
    74  	// ExpandedSQL is sqlite3_expanded_sql.
    75  	// https://www.sqlite.org/c3ref/expanded_sql.html
    76  	ExpandedSQL() string
    77  	// StartTimer starts recording elapsed duration.
    78  	StartTimer()
    79  	// Reset is sqlite3_reset.
    80  	// https://www.sqlite.org/c3ref/reset.html
    81  	Reset() error
    82  	// ResetAndClear is sqlite3_reset + sqlite3_clear_bindings.
    83  	// It reports the duration elapsed since the call to StartTimer.
    84  	ResetAndClear() (time.Duration, error)
    85  	// Finalize is sqlite3_finalize.
    86  	// https://sqlite.org/c3ref/finalize.html
    87  	Finalize() error
    88  	// ClearBindings sqlite3_clear_bindings.
    89  	//
    90  	// https://www.sqlite.org/c3ref/clear_bindings.html
    91  	ClearBindings() error
    92  	// Step is sqlite3_step.
    93  	//
    94  	// For SQLITE_ROW, Step returns (true, nil).
    95  	// For SQLITE_DONE, Step returns (false, nil).
    96  	// For any error, Step returns (false, err).
    97  	//
    98  	// As an optional optimization, if colType is provided, up to len(colType)
    99  	// bytes are populated with the corresponding ColumnType values. If nil or
   100  	// zero length, or the result is other than (true, nil), it's not used.
   101  	// If the slice is too short (len is less than the number of columns), then
   102  	// the results are truncated.
   103  	//
   104  	// https://www.sqlite.org/c3ref/step.html
   105  	Step(colType []ColumnType) (row bool, err error)
   106  	// StepResult executes a one-row query and resets the statment:
   107  	//	sqlite3_step
   108  	//	sqlite3_last_insert_rowid + sqlite3_changes
   109  	//	sqlite3_reset + sqlite3_clear_bindings
   110  	// Results:
   111  	// 	For SQLITE_ROW, Step returns row=true err=nil
   112  	// 	For SQLITE_DONE, Step returns row=false err=nil
   113  	// 	For any error, Step returns row=false err.
   114  	// https://www.sqlite.org/c3ref/step.html
   115  	StepResult() (row bool, lastInsertRowID, changes int64, d time.Duration, err error)
   116  	// BindDouble is sqlite3_bind_double.
   117  	// https://sqlite.org/c3ref/bind_blob.html
   118  	BindDouble(col int, val float64) error
   119  	// BindInt64 is sqlite3_bind_int64.
   120  	// https://sqlite.org/c3ref/bind_blob.html
   121  	BindInt64(col int, val int64) error
   122  	// BindNull is sqlite3_bind_null.
   123  	// https://sqlite.org/c3ref/bind_blob.html
   124  	BindNull(col int) error
   125  	// BindText64 is sqlite3_bind_text64.
   126  	// https://sqlite.org/c3ref/bind_blob.html
   127  	BindText64(col int, val string) error
   128  	// BindZeroBlob64 is sqlite3_bind_zeroblob64.
   129  	// https://sqlite.org/c3ref/bind_blob.html
   130  	BindZeroBlob64(col int, n uint64) error
   131  	// BindBlob64 is sqlite3_bind_blob64.
   132  	// https://sqlite.org/c3ref/bind_blob.html
   133  	BindBlob64(col int, val []byte) error
   134  	// BindParameterCount is sqlite3_bind_parameter_count.
   135  	// https://sqlite.org/c3ref/bind_parameter_count.html
   136  	BindParameterCount() int
   137  	// BindParameterName is sqlite3_bind_parameter_name.
   138  	// https://sqlite.org/c3ref/bind_parameter_count.html
   139  	BindParameterName(col int) string
   140  	// BindParameterIndex is sqlite3_bind_parameter_index.
   141  	// Returns zero if no matching parameter is found.
   142  	// https://sqlite.org/c3ref/bind_parameter_index.html
   143  	BindParameterIndex(name string) int
   144  	// BindParameterIndexSearch calls sqlite3_bind_parameter_index,
   145  	// prepending ':', '@', and '?' until it finds a matching paramter.
   146  	BindParameterIndexSearch(name string) int
   147  	// ColumnCount is sqlite3_column_count.
   148  	// https://sqlite.org/c3ref/column_count.html
   149  	ColumnCount() int
   150  	// ColumnName is sqlite3_column_name.
   151  	// https://sqlite.org/c3ref/column_name.html
   152  	ColumnName(col int) string
   153  	// ColumnText is sqlite3_column_text.
   154  	// https://sqlite.org/c3ref/column_blob.html
   155  	ColumnText(col int) string
   156  	// ColumnBlob is sqlite3_column_blob.
   157  	//
   158  	// WARNING: The returned memory is managed by C and is only valid until
   159  	//          another call is made on this Stmt.
   160  	//
   161  	// https://sqlite.org/c3ref/column_blob.html
   162  	ColumnBlob(col int) []byte
   163  	// ColumnDouble is sqlite3_column_double.
   164  	// https://sqlite.org/c3ref/column_blob.html
   165  	ColumnDouble(col int) float64
   166  	// ColumnInt64 is sqlite3_column_int64.
   167  	// https://sqlite.org/c3ref/column_blob.html
   168  	ColumnInt64(col int) int64
   169  	// ColumnType is sqlite3_column_type.
   170  	// https://www.sqlite.org/c3ref/column_blob.html
   171  	ColumnType(col int) ColumnType
   172  	// ColumnDeclType is sqlite3_column_decltype.
   173  	// https://sqlite.org/c3ref/column_decltype.html
   174  	ColumnDeclType(col int) string
   175  	// ColumnDatabaseName is sqlite3_column_database_name.
   176  	// https://sqlite.org/c3ref/column_database_name.html
   177  	ColumnDatabaseName(col int) string
   178  	// ColumnTableName is sqlite3_column_table_name.
   179  	// https://sqlite.org/c3ref/column_database_name.html
   180  	ColumnTableName(col int) string
   181  }
   182  
   183  // ColumnType are constants for each of the SQLite datatypes.
   184  // https://www.sqlite.org/c3ref/c_blob.html
   185  type ColumnType byte
   186  
   187  const (
   188  	SQLITE_INTEGER ColumnType = 1
   189  	SQLITE_FLOAT   ColumnType = 2
   190  	SQLITE_TEXT    ColumnType = 3
   191  	SQLITE_BLOB    ColumnType = 4
   192  	SQLITE_NULL    ColumnType = 5
   193  )
   194  
   195  func (t ColumnType) String() string {
   196  	switch t {
   197  	case SQLITE_INTEGER:
   198  		return "SQLITE_INTEGER"
   199  	case SQLITE_FLOAT:
   200  		return "SQLITE_FLOAT"
   201  	case SQLITE_TEXT:
   202  		return "SQLITE_TEXT"
   203  	case SQLITE_BLOB:
   204  		return "SQLITE_BLOB"
   205  	case SQLITE_NULL:
   206  		return "SQLITE_NULL"
   207  	default:
   208  		return "UNKNOWN_SQLITE_DATATYPE"
   209  	}
   210  }
   211  
   212  // https://www.sqlite.org/c3ref/c_prepare_normalize.html
   213  type PrepareFlags int
   214  
   215  const (
   216  	SQLITE_PREPARE_PERSISTENT PrepareFlags = 0x01
   217  	SQLITE_PREPARE_NORMALIZE  PrepareFlags = 0x02
   218  	SQLITE_PREPARE_NO_VTAB    PrepareFlags = 0x04
   219  )
   220  
   221  // https://sqlite.org/c3ref/c_dbconfig_defensive.html
   222  type DBConfig int
   223  
   224  const (
   225  	SQLITE_DBCONFIG_MAINDBNAME            DBConfig = 1000
   226  	SQLITE_DBCONFIG_LOOKASIDE             DBConfig = 1001
   227  	SQLITE_DBCONFIG_ENABLE_FKEY           DBConfig = 1002
   228  	SQLITE_DBCONFIG_ENABLE_TRIGGER        DBConfig = 1003
   229  	SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER DBConfig = 1004
   230  	SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION DBConfig = 1005
   231  	SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      DBConfig = 1006
   232  	SQLITE_DBCONFIG_ENABLE_QPSG           DBConfig = 1007
   233  	SQLITE_DBCONFIG_TRIGGER_EQP           DBConfig = 1008
   234  	SQLITE_DBCONFIG_RESET_DATABASE        DBConfig = 1009
   235  	SQLITE_DBCONFIG_DEFENSIVE             DBConfig = 1010
   236  	SQLITE_DBCONFIG_WRITABLE_SCHEMA       DBConfig = 1011
   237  	SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    DBConfig = 1012
   238  	SQLITE_DBCONFIG_DQS_DML               DBConfig = 1013
   239  	SQLITE_DBCONFIG_DQS_DDL               DBConfig = 1014
   240  	SQLITE_DBCONFIG_ENABLE_VIEW           DBConfig = 1015
   241  	SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    DBConfig = 1016
   242  	SQLITE_DBCONFIG_TRUSTED_SCHEMA        DBConfig = 1017
   243  )
   244  
   245  // OpenFlags are flags used when opening a DB.
   246  //
   247  // https://www.sqlite.org/c3ref/c_open_autoproxy.html
   248  type OpenFlags int
   249  
   250  const (
   251  	SQLITE_OPEN_READONLY       OpenFlags = 0x00000001
   252  	SQLITE_OPEN_READWRITE      OpenFlags = 0x00000002
   253  	SQLITE_OPEN_CREATE         OpenFlags = 0x00000004
   254  	SQLITE_OPEN_DELETEONCLOSE  OpenFlags = 0x00000008
   255  	SQLITE_OPEN_EXCLUSIVE      OpenFlags = 0x00000010
   256  	SQLITE_OPEN_AUTOPROXY      OpenFlags = 0x00000020
   257  	SQLITE_OPEN_URI            OpenFlags = 0x00000040
   258  	SQLITE_OPEN_MEMORY         OpenFlags = 0x00000080
   259  	SQLITE_OPEN_MAIN_DB        OpenFlags = 0x00000100
   260  	SQLITE_OPEN_TEMP_DB        OpenFlags = 0x00000200
   261  	SQLITE_OPEN_TRANSIENT_DB   OpenFlags = 0x00000400
   262  	SQLITE_OPEN_MAIN_JOURNAL   OpenFlags = 0x00000800
   263  	SQLITE_OPEN_TEMP_JOURNAL   OpenFlags = 0x00001000
   264  	SQLITE_OPEN_SUBJOURNAL     OpenFlags = 0x00002000
   265  	SQLITE_OPEN_MASTER_JOURNAL OpenFlags = 0x00004000
   266  	SQLITE_OPEN_NOMUTEX        OpenFlags = 0x00008000
   267  	SQLITE_OPEN_FULLMUTEX      OpenFlags = 0x00010000
   268  	SQLITE_OPEN_SHAREDCACHE    OpenFlags = 0x00020000
   269  	SQLITE_OPEN_PRIVATECACHE   OpenFlags = 0x00040000
   270  	SQLITE_OPEN_WAL            OpenFlags = 0x00080000
   271  	SQLITE_OPEN_NOFOLLOW       OpenFlags = 0x00100000
   272  
   273  	OpenFlagsDefault = SQLITE_OPEN_READWRITE |
   274  		SQLITE_OPEN_CREATE |
   275  		SQLITE_OPEN_WAL |
   276  		SQLITE_OPEN_URI |
   277  		SQLITE_OPEN_NOMUTEX
   278  )
   279  
   280  var allOpenFlags = []OpenFlags{
   281  	SQLITE_OPEN_READONLY,
   282  	SQLITE_OPEN_READWRITE,
   283  	SQLITE_OPEN_CREATE,
   284  	SQLITE_OPEN_DELETEONCLOSE,
   285  	SQLITE_OPEN_EXCLUSIVE,
   286  	SQLITE_OPEN_AUTOPROXY,
   287  	SQLITE_OPEN_URI,
   288  	SQLITE_OPEN_MEMORY,
   289  	SQLITE_OPEN_MAIN_DB,
   290  	SQLITE_OPEN_TEMP_DB,
   291  	SQLITE_OPEN_TRANSIENT_DB,
   292  	SQLITE_OPEN_MAIN_JOURNAL,
   293  	SQLITE_OPEN_TEMP_JOURNAL,
   294  	SQLITE_OPEN_SUBJOURNAL,
   295  	SQLITE_OPEN_MASTER_JOURNAL,
   296  	SQLITE_OPEN_NOMUTEX,
   297  	SQLITE_OPEN_FULLMUTEX,
   298  	SQLITE_OPEN_SHAREDCACHE,
   299  	SQLITE_OPEN_PRIVATECACHE,
   300  	SQLITE_OPEN_WAL,
   301  	SQLITE_OPEN_NOFOLLOW,
   302  }
   303  
   304  var openFlagsStrings = map[OpenFlags]string{
   305  	SQLITE_OPEN_READONLY:       "SQLITE_OPEN_READONLY",
   306  	SQLITE_OPEN_READWRITE:      "SQLITE_OPEN_READWRITE",
   307  	SQLITE_OPEN_CREATE:         "SQLITE_OPEN_CREATE",
   308  	SQLITE_OPEN_DELETEONCLOSE:  "SQLITE_OPEN_DELETEONCLOSE",
   309  	SQLITE_OPEN_EXCLUSIVE:      "SQLITE_OPEN_EXCLUSIVE",
   310  	SQLITE_OPEN_AUTOPROXY:      "SQLITE_OPEN_AUTOPROXY",
   311  	SQLITE_OPEN_URI:            "SQLITE_OPEN_URI",
   312  	SQLITE_OPEN_MEMORY:         "SQLITE_OPEN_MEMORY",
   313  	SQLITE_OPEN_MAIN_DB:        "SQLITE_OPEN_MAIN_DB",
   314  	SQLITE_OPEN_TEMP_DB:        "SQLITE_OPEN_TEMP_DB",
   315  	SQLITE_OPEN_TRANSIENT_DB:   "SQLITE_OPEN_TRANSIENT_DB",
   316  	SQLITE_OPEN_MAIN_JOURNAL:   "SQLITE_OPEN_MAIN_JOURNAL",
   317  	SQLITE_OPEN_TEMP_JOURNAL:   "SQLITE_OPEN_TEMP_JOURNAL",
   318  	SQLITE_OPEN_SUBJOURNAL:     "SQLITE_OPEN_SUBJOURNAL",
   319  	SQLITE_OPEN_MASTER_JOURNAL: "SQLITE_OPEN_MASTER_JOURNAL",
   320  	SQLITE_OPEN_NOMUTEX:        "SQLITE_OPEN_NOMUTEX",
   321  	SQLITE_OPEN_FULLMUTEX:      "SQLITE_OPEN_FULLMUTEX",
   322  	SQLITE_OPEN_SHAREDCACHE:    "SQLITE_OPEN_SHAREDCACHE",
   323  	SQLITE_OPEN_PRIVATECACHE:   "SQLITE_OPEN_PRIVATECACHE",
   324  	SQLITE_OPEN_WAL:            "SQLITE_OPEN_WAL",
   325  	SQLITE_OPEN_NOFOLLOW:       "SQLITE_OPEN_NOFOLLOW",
   326  }
   327  
   328  func (o OpenFlags) String() string {
   329  	var flags []byte
   330  	for i, flag := range allOpenFlags {
   331  		if o&flag == 0 {
   332  			continue
   333  		}
   334  		if i > 0 {
   335  			flags = append(flags, '|')
   336  		}
   337  		flagStr, ok := openFlagsStrings[flag]
   338  		if ok {
   339  			flags = append(flags, flagStr...)
   340  		} else {
   341  			flags = append(flags, "UNKNOWN_FLAG:"...)
   342  			var buf [20]byte
   343  			flags = append(flags, itoa(buf[:], int64(flag))...)
   344  		}
   345  	}
   346  	return string(flags)
   347  }
   348  
   349  // Checkpoint is a WAL checkpoint mode.
   350  // It is used by sqlite3_wal_checkpoint_v2.
   351  //
   352  // https://sqlite.org/c3ref/wal_checkpoint_v2.html
   353  type Checkpoint int
   354  
   355  const (
   356  	SQLITE_CHECKPOINT_PASSIVE  Checkpoint = 0
   357  	SQLITE_CHECKPOINT_FULL     Checkpoint = 1
   358  	SQLITE_CHECKPOINT_RESTART  Checkpoint = 2
   359  	SQLITE_CHECKPOINT_TRUNCATE Checkpoint = 3
   360  )
   361  
   362  func (mode Checkpoint) String() string {
   363  	switch mode {
   364  	default:
   365  		var buf [20]byte
   366  		return "SQLITE_CHECKPOINT_UNKNOWN(" + string(itoa(buf[:], int64(mode))) + ")"
   367  	case SQLITE_CHECKPOINT_PASSIVE:
   368  		return "SQLITE_CHECKPOINT_PASSIVE"
   369  	case SQLITE_CHECKPOINT_FULL:
   370  		return "SQLITE_CHECKPOINT_FULL"
   371  	case SQLITE_CHECKPOINT_RESTART:
   372  		return "SQLITE_CHECKPOINT_RESTART"
   373  	case SQLITE_CHECKPOINT_TRUNCATE:
   374  		return "SQLITE_CHECKPOINT_TRUNCATE"
   375  	}
   376  }
   377  
   378  // TxnState is a transaction state.
   379  // It is used by sqlite3_txn_state.
   380  //
   381  // https://sqlite.org/c3ref/txn_state.html
   382  type TxnState int
   383  
   384  const (
   385  	SQLITE_TXN_NONE  TxnState = 0
   386  	SQLITE_TXN_READ  TxnState = 1
   387  	SQLITE_TXN_WRITE TxnState = 2
   388  )
   389  
   390  func (state TxnState) String() string {
   391  	switch state {
   392  	default:
   393  		var buf [20]byte
   394  		return "SQLITE_TXN_UNKNOWN(" + string(itoa(buf[:], int64(state))) + ")"
   395  	case SQLITE_TXN_NONE:
   396  		return "SQLITE_TXN_NONE"
   397  	case SQLITE_TXN_READ:
   398  		return "SQLITE_TXN_READ"
   399  	case SQLITE_TXN_WRITE:
   400  		return "SQLITE_TXN_WRITE"
   401  	}
   402  }
   403  
   404  // ErrCode is an SQLite error code as a Go error.
   405  // It must not be one of the status codes SQLITE_OK, SQLITE_ROW, or SQLITE_DONE.
   406  type ErrCode Code
   407  
   408  func (e ErrCode) Error() string {
   409  	return Code(e).String()
   410  }
   411  
   412  // Code is an SQLite extended error code.
   413  //
   414  // The three SQLite result codes (SQLITE_OK, SQLITE_ROW, and SQLITE_DONE),
   415  // are not errors so they should not be used in an Error.
   416  type Code int
   417  
   418  func (code Code) String() string {
   419  	switch code {
   420  	default:
   421  		var buf [20]byte
   422  		return "SQLITE_UNKNOWN_ERR(" + string(itoa(buf[:], int64(code))) + ")"
   423  	case SQLITE_OK:
   424  		return "SQLITE_OK(not an error)"
   425  	case SQLITE_ROW:
   426  		return "SQLITE_ROW(not an error)"
   427  	case SQLITE_DONE:
   428  		return "SQLITE_DONE(not an error)"
   429  	case SQLITE_ERROR:
   430  		return "SQLITE_ERROR"
   431  	case SQLITE_INTERNAL:
   432  		return "SQLITE_INTERNAL"
   433  	case SQLITE_PERM:
   434  		return "SQLITE_PERM"
   435  	case SQLITE_ABORT:
   436  		return "SQLITE_ABORT"
   437  	case SQLITE_BUSY:
   438  		return "SQLITE_BUSY"
   439  	case SQLITE_LOCKED:
   440  		return "SQLITE_LOCKED"
   441  	case SQLITE_NOMEM:
   442  		return "SQLITE_NOMEM"
   443  	case SQLITE_READONLY:
   444  		return "SQLITE_READONLY"
   445  	case SQLITE_INTERRUPT:
   446  		return "SQLITE_INTERRUPT"
   447  	case SQLITE_IOERR:
   448  		return "SQLITE_IOERR"
   449  	case SQLITE_CORRUPT:
   450  		return "SQLITE_CORRUPT"
   451  	case SQLITE_NOTFOUND:
   452  		return "SQLITE_NOTFOUND"
   453  	case SQLITE_FULL:
   454  		return "SQLITE_FULL"
   455  	case SQLITE_CANTOPEN:
   456  		return "SQLITE_CANTOPEN"
   457  	case SQLITE_PROTOCOL:
   458  		return "SQLITE_PROTOCOL"
   459  	case SQLITE_EMPTY:
   460  		return "SQLITE_EMPTY"
   461  	case SQLITE_SCHEMA:
   462  		return "SQLITE_SCHEMA"
   463  	case SQLITE_TOOBIG:
   464  		return "SQLITE_TOOBIG"
   465  	case SQLITE_CONSTRAINT:
   466  		return "SQLITE_CONSTRAINT"
   467  	case SQLITE_MISMATCH:
   468  		return "SQLITE_MISMATCH"
   469  	case SQLITE_MISUSE:
   470  		return "SQLITE_MISUSE"
   471  	case SQLITE_NOLFS:
   472  		return "SQLITE_NOLFS"
   473  	case SQLITE_AUTH:
   474  		return "SQLITE_AUTH"
   475  	case SQLITE_FORMAT:
   476  		return "SQLITE_FORMAT"
   477  	case SQLITE_RANGE:
   478  		return "SQLITE_RANGE"
   479  	case SQLITE_NOTADB:
   480  		return "SQLITE_NOTADB"
   481  	case SQLITE_NOTICE:
   482  		return "SQLITE_NOTICE"
   483  	case SQLITE_WARNING:
   484  		return "SQLITE_WARNING"
   485  
   486  	case SQLITE_ERROR_MISSING_COLLSEQ:
   487  		return "SQLITE_ERROR_MISSING_COLLSEQ"
   488  	case SQLITE_ERROR_RETRY:
   489  		return "SQLITE_ERROR_RETRY"
   490  	case SQLITE_ERROR_SNAPSHOT:
   491  		return "SQLITE_ERROR_SNAPSHOT"
   492  	case SQLITE_IOERR_READ:
   493  		return "SQLITE_IOERR_READ"
   494  	case SQLITE_IOERR_SHORT_READ:
   495  		return "SQLITE_IOERR_SHORT_READ"
   496  	case SQLITE_IOERR_WRITE:
   497  		return "SQLITE_IOERR_WRITE"
   498  	case SQLITE_IOERR_FSYNC:
   499  		return "SQLITE_IOERR_FSYNC"
   500  	case SQLITE_IOERR_DIR_FSYNC:
   501  		return "SQLITE_IOERR_DIR_FSYNC"
   502  	case SQLITE_IOERR_TRUNCATE:
   503  		return "SQLITE_IOERR_TRUNCATE"
   504  	case SQLITE_IOERR_FSTAT:
   505  		return "SQLITE_IOERR_FSTAT"
   506  	case SQLITE_IOERR_UNLOCK:
   507  		return "SQLITE_IOERR_UNLOCK"
   508  	case SQLITE_IOERR_RDLOCK:
   509  		return "SQLITE_IOERR_RDLOCK"
   510  	case SQLITE_IOERR_DELETE:
   511  		return "SQLITE_IOERR_DELETE"
   512  	case SQLITE_IOERR_BLOCKED:
   513  		return "SQLITE_IOERR_BLOCKED"
   514  	case SQLITE_IOERR_NOMEM:
   515  		return "SQLITE_IOERR_NOMEM"
   516  	case SQLITE_IOERR_ACCESS:
   517  		return "SQLITE_IOERR_ACCESS"
   518  	case SQLITE_IOERR_CHECKRESERVEDLOCK:
   519  		return "SQLITE_IOERR_CHECKRESERVEDLOCK"
   520  	case SQLITE_IOERR_LOCK:
   521  		return "SQLITE_IOERR_LOCK"
   522  	case SQLITE_IOERR_CLOSE:
   523  		return "SQLITE_IOERR_CLOSE"
   524  	case SQLITE_IOERR_DIR_CLOSE:
   525  		return "SQLITE_IOERR_DIR_CLOSE"
   526  	case SQLITE_IOERR_SHMOPEN:
   527  		return "SQLITE_IOERR_SHMOPEN"
   528  	case SQLITE_IOERR_SHMSIZE:
   529  		return "SQLITE_IOERR_SHMSIZE"
   530  	case SQLITE_IOERR_SHMLOCK:
   531  		return "SQLITE_IOERR_SHMLOCK"
   532  	case SQLITE_IOERR_SHMMAP:
   533  		return "SQLITE_IOERR_SHMMAP"
   534  	case SQLITE_IOERR_SEEK:
   535  		return "SQLITE_IOERR_SEEK"
   536  	case SQLITE_IOERR_DELETE_NOENT:
   537  		return "SQLITE_IOERR_DELETE_NOENT"
   538  	case SQLITE_IOERR_MMAP:
   539  		return "SQLITE_IOERR_MMAP"
   540  	case SQLITE_IOERR_GETTEMPPATH:
   541  		return "SQLITE_IOERR_GETTEMPPATH"
   542  	case SQLITE_IOERR_CONVPATH:
   543  		return "SQLITE_IOERR_CONVPATH"
   544  	case SQLITE_IOERR_VNODE:
   545  		return "SQLITE_IOERR_VNODE"
   546  	case SQLITE_IOERR_AUTH:
   547  		return "SQLITE_IOERR_AUTH"
   548  	case SQLITE_IOERR_BEGIN_ATOMIC:
   549  		return "SQLITE_IOERR_BEGIN_ATOMIC"
   550  	case SQLITE_IOERR_COMMIT_ATOMIC:
   551  		return "SQLITE_IOERR_COMMIT_ATOMIC"
   552  	case SQLITE_IOERR_ROLLBACK_ATOMIC:
   553  		return "SQLITE_IOERR_ROLLBACK_ATOMIC"
   554  	case SQLITE_LOCKED_SHAREDCACHE:
   555  		return "SQLITE_LOCKED_SHAREDCACHE"
   556  	case SQLITE_BUSY_RECOVERY:
   557  		return "SQLITE_BUSY_RECOVERY"
   558  	case SQLITE_BUSY_SNAPSHOT:
   559  		return "SQLITE_BUSY_SNAPSHOT"
   560  	case SQLITE_CANTOPEN_NOTEMPDIR:
   561  		return "SQLITE_CANTOPEN_NOTEMPDIR"
   562  	case SQLITE_CANTOPEN_ISDIR:
   563  		return "SQLITE_CANTOPEN_ISDIR"
   564  	case SQLITE_CANTOPEN_FULLPATH:
   565  		return "SQLITE_CANTOPEN_FULLPATH"
   566  	case SQLITE_CANTOPEN_CONVPATH:
   567  		return "SQLITE_CANTOPEN_CONVPATH"
   568  	case SQLITE_CORRUPT_VTAB:
   569  		return "SQLITE_CORRUPT_VTAB"
   570  	case SQLITE_READONLY_RECOVERY:
   571  		return "SQLITE_READONLY_RECOVERY"
   572  	case SQLITE_READONLY_CANTLOCK:
   573  		return "SQLITE_READONLY_CANTLOCK"
   574  	case SQLITE_READONLY_ROLLBACK:
   575  		return "SQLITE_READONLY_ROLLBACK"
   576  	case SQLITE_READONLY_DBMOVED:
   577  		return "SQLITE_READONLY_DBMOVED"
   578  	case SQLITE_READONLY_CANTINIT:
   579  		return "SQLITE_READONLY_CANTINIT"
   580  	case SQLITE_READONLY_DIRECTORY:
   581  		return "SQLITE_READONLY_DIRECTORY"
   582  	case SQLITE_ABORT_ROLLBACK:
   583  		return "SQLITE_ABORT_ROLLBACK"
   584  	case SQLITE_CONSTRAINT_CHECK:
   585  		return "SQLITE_CONSTRAINT_CHECK"
   586  	case SQLITE_CONSTRAINT_COMMITHOOK:
   587  		return "SQLITE_CONSTRAINT_COMMITHOOK"
   588  	case SQLITE_CONSTRAINT_FOREIGNKEY:
   589  		return "SQLITE_CONSTRAINT_FOREIGNKEY"
   590  	case SQLITE_CONSTRAINT_FUNCTION:
   591  		return "SQLITE_CONSTRAINT_FUNCTION"
   592  	case SQLITE_CONSTRAINT_NOTNULL:
   593  		return "SQLITE_CONSTRAINT_NOTNULL"
   594  	case SQLITE_CONSTRAINT_PRIMARYKEY:
   595  		return "SQLITE_CONSTRAINT_PRIMARYKEY"
   596  	case SQLITE_CONSTRAINT_TRIGGER:
   597  		return "SQLITE_CONSTRAINT_TRIGGER"
   598  	case SQLITE_CONSTRAINT_UNIQUE:
   599  		return "SQLITE_CONSTRAINT_UNIQUE"
   600  	case SQLITE_CONSTRAINT_VTAB:
   601  		return "SQLITE_CONSTRAINT_VTAB"
   602  	case SQLITE_CONSTRAINT_ROWID:
   603  		return "SQLITE_CONSTRAINT_ROWID"
   604  	case SQLITE_NOTICE_RECOVER_WAL:
   605  		return "SQLITE_NOTICE_RECOVER_WAL"
   606  	case SQLITE_NOTICE_RECOVER_ROLLBACK:
   607  		return "SQLITE_NOTICE_RECOVER_ROLLBACK"
   608  	case SQLITE_WARNING_AUTOINDEX:
   609  		return "SQLITE_WARNING_AUTOINDEX"
   610  	case SQLITE_AUTH_USER:
   611  		return "SQLITE_AUTH_USER"
   612  	}
   613  }
   614  
   615  const (
   616  	SQLITE_OK         = Code(0) // do not use in Error
   617  	SQLITE_ERROR      = Code(1)
   618  	SQLITE_INTERNAL   = Code(2)
   619  	SQLITE_PERM       = Code(3)
   620  	SQLITE_ABORT      = Code(4)
   621  	SQLITE_BUSY       = Code(5)
   622  	SQLITE_LOCKED     = Code(6)
   623  	SQLITE_NOMEM      = Code(7)
   624  	SQLITE_READONLY   = Code(8)
   625  	SQLITE_INTERRUPT  = Code(9)
   626  	SQLITE_IOERR      = Code(10)
   627  	SQLITE_CORRUPT    = Code(11)
   628  	SQLITE_NOTFOUND   = Code(12)
   629  	SQLITE_FULL       = Code(13)
   630  	SQLITE_CANTOPEN   = Code(14)
   631  	SQLITE_PROTOCOL   = Code(15)
   632  	SQLITE_EMPTY      = Code(16)
   633  	SQLITE_SCHEMA     = Code(17)
   634  	SQLITE_TOOBIG     = Code(18)
   635  	SQLITE_CONSTRAINT = Code(19)
   636  	SQLITE_MISMATCH   = Code(20)
   637  	SQLITE_MISUSE     = Code(21)
   638  	SQLITE_NOLFS      = Code(22)
   639  	SQLITE_AUTH       = Code(23)
   640  	SQLITE_FORMAT     = Code(24)
   641  	SQLITE_RANGE      = Code(25)
   642  	SQLITE_NOTADB     = Code(26)
   643  	SQLITE_NOTICE     = Code(27)
   644  	SQLITE_WARNING    = Code(28)
   645  	SQLITE_ROW        = Code(100) // do not use in Error
   646  	SQLITE_DONE       = Code(101) // do not use in Error
   647  
   648  	// Extended error codes
   649  
   650  	SQLITE_ERROR_MISSING_COLLSEQ   = Code(SQLITE_ERROR | (1 << 8))
   651  	SQLITE_ERROR_RETRY             = Code(SQLITE_ERROR | (2 << 8))
   652  	SQLITE_ERROR_SNAPSHOT          = Code(SQLITE_ERROR | (3 << 8))
   653  	SQLITE_IOERR_READ              = Code(SQLITE_IOERR | (1 << 8))
   654  	SQLITE_IOERR_SHORT_READ        = Code(SQLITE_IOERR | (2 << 8))
   655  	SQLITE_IOERR_WRITE             = Code(SQLITE_IOERR | (3 << 8))
   656  	SQLITE_IOERR_FSYNC             = Code(SQLITE_IOERR | (4 << 8))
   657  	SQLITE_IOERR_DIR_FSYNC         = Code(SQLITE_IOERR | (5 << 8))
   658  	SQLITE_IOERR_TRUNCATE          = Code(SQLITE_IOERR | (6 << 8))
   659  	SQLITE_IOERR_FSTAT             = Code(SQLITE_IOERR | (7 << 8))
   660  	SQLITE_IOERR_UNLOCK            = Code(SQLITE_IOERR | (8 << 8))
   661  	SQLITE_IOERR_RDLOCK            = Code(SQLITE_IOERR | (9 << 8))
   662  	SQLITE_IOERR_DELETE            = Code(SQLITE_IOERR | (10 << 8))
   663  	SQLITE_IOERR_BLOCKED           = Code(SQLITE_IOERR | (11 << 8))
   664  	SQLITE_IOERR_NOMEM             = Code(SQLITE_IOERR | (12 << 8))
   665  	SQLITE_IOERR_ACCESS            = Code(SQLITE_IOERR | (13 << 8))
   666  	SQLITE_IOERR_CHECKRESERVEDLOCK = Code(SQLITE_IOERR | (14 << 8))
   667  	SQLITE_IOERR_LOCK              = Code(SQLITE_IOERR | (15 << 8))
   668  	SQLITE_IOERR_CLOSE             = Code(SQLITE_IOERR | (16 << 8))
   669  	SQLITE_IOERR_DIR_CLOSE         = Code(SQLITE_IOERR | (17 << 8))
   670  	SQLITE_IOERR_SHMOPEN           = Code(SQLITE_IOERR | (18 << 8))
   671  	SQLITE_IOERR_SHMSIZE           = Code(SQLITE_IOERR | (19 << 8))
   672  	SQLITE_IOERR_SHMLOCK           = Code(SQLITE_IOERR | (20 << 8))
   673  	SQLITE_IOERR_SHMMAP            = Code(SQLITE_IOERR | (21 << 8))
   674  	SQLITE_IOERR_SEEK              = Code(SQLITE_IOERR | (22 << 8))
   675  	SQLITE_IOERR_DELETE_NOENT      = Code(SQLITE_IOERR | (23 << 8))
   676  	SQLITE_IOERR_MMAP              = Code(SQLITE_IOERR | (24 << 8))
   677  	SQLITE_IOERR_GETTEMPPATH       = Code(SQLITE_IOERR | (25 << 8))
   678  	SQLITE_IOERR_CONVPATH          = Code(SQLITE_IOERR | (26 << 8))
   679  	SQLITE_IOERR_VNODE             = Code(SQLITE_IOERR | (27 << 8))
   680  	SQLITE_IOERR_AUTH              = Code(SQLITE_IOERR | (28 << 8))
   681  	SQLITE_IOERR_BEGIN_ATOMIC      = Code(SQLITE_IOERR | (29 << 8))
   682  	SQLITE_IOERR_COMMIT_ATOMIC     = Code(SQLITE_IOERR | (30 << 8))
   683  	SQLITE_IOERR_ROLLBACK_ATOMIC   = Code(SQLITE_IOERR | (31 << 8))
   684  	SQLITE_IOERR_DATA              = Code(SQLITE_IOERR | (32 << 8))
   685  	SQLITE_IOERR_CORRUPTFS         = Code(SQLITE_IOERR | (33 << 8))
   686  	SQLITE_LOCKED_SHAREDCACHE      = Code(SQLITE_LOCKED | (1 << 8))
   687  	SQLITE_LOCKED_VTAB             = Code(SQLITE_LOCKED | (2 << 8))
   688  	SQLITE_BUSY_RECOVERY           = Code(SQLITE_BUSY | (1 << 8))
   689  	SQLITE_BUSY_SNAPSHOT           = Code(SQLITE_BUSY | (2 << 8))
   690  	SQLITE_BUSY_TIMEOUT            = Code(SQLITE_BUSY | (3 << 8))
   691  	SQLITE_CANTOPEN_NOTEMPDIR      = Code(SQLITE_CANTOPEN | (1 << 8))
   692  	SQLITE_CANTOPEN_ISDIR          = Code(SQLITE_CANTOPEN | (2 << 8))
   693  	SQLITE_CANTOPEN_FULLPATH       = Code(SQLITE_CANTOPEN | (3 << 8))
   694  	SQLITE_CANTOPEN_CONVPATH       = Code(SQLITE_CANTOPEN | (4 << 8))
   695  	SQLITE_CANTOPEN_DIRTYWAL       = Code(SQLITE_CANTOPEN | (5 << 8)) /* Not Used */
   696  	SQLITE_CANTOPEN_SYMLINK        = Code(SQLITE_CANTOPEN | (6 << 8))
   697  	SQLITE_CORRUPT_VTAB            = Code(SQLITE_CORRUPT | (1 << 8))
   698  	SQLITE_CORRUPT_SEQUENCE        = Code(SQLITE_CORRUPT | (2 << 8))
   699  	SQLITE_CORRUPT_INDEX           = Code(SQLITE_CORRUPT | (3 << 8))
   700  	SQLITE_READONLY_RECOVERY       = Code(SQLITE_READONLY | (1 << 8))
   701  	SQLITE_READONLY_CANTLOCK       = Code(SQLITE_READONLY | (2 << 8))
   702  	SQLITE_READONLY_ROLLBACK       = Code(SQLITE_READONLY | (3 << 8))
   703  	SQLITE_READONLY_DBMOVED        = Code(SQLITE_READONLY | (4 << 8))
   704  	SQLITE_READONLY_CANTINIT       = Code(SQLITE_READONLY | (5 << 8))
   705  	SQLITE_READONLY_DIRECTORY      = Code(SQLITE_READONLY | (6 << 8))
   706  	SQLITE_ABORT_ROLLBACK          = Code(SQLITE_ABORT | (2 << 8))
   707  	SQLITE_CONSTRAINT_CHECK        = Code(SQLITE_CONSTRAINT | (1 << 8))
   708  	SQLITE_CONSTRAINT_COMMITHOOK   = Code(SQLITE_CONSTRAINT | (2 << 8))
   709  	SQLITE_CONSTRAINT_FOREIGNKEY   = Code(SQLITE_CONSTRAINT | (3 << 8))
   710  	SQLITE_CONSTRAINT_FUNCTION     = Code(SQLITE_CONSTRAINT | (4 << 8))
   711  	SQLITE_CONSTRAINT_NOTNULL      = Code(SQLITE_CONSTRAINT | (5 << 8))
   712  	SQLITE_CONSTRAINT_PRIMARYKEY   = Code(SQLITE_CONSTRAINT | (6 << 8))
   713  	SQLITE_CONSTRAINT_TRIGGER      = Code(SQLITE_CONSTRAINT | (7 << 8))
   714  	SQLITE_CONSTRAINT_UNIQUE       = Code(SQLITE_CONSTRAINT | (8 << 8))
   715  	SQLITE_CONSTRAINT_VTAB         = Code(SQLITE_CONSTRAINT | (9 << 8))
   716  	SQLITE_CONSTRAINT_ROWID        = Code(SQLITE_CONSTRAINT | (10 << 8))
   717  	SQLITE_CONSTRAINT_PINNED       = Code(SQLITE_CONSTRAINT | (11 << 8))
   718  	SQLITE_NOTICE_RECOVER_WAL      = Code(SQLITE_NOTICE | (1 << 8))
   719  	SQLITE_NOTICE_RECOVER_ROLLBACK = Code(SQLITE_NOTICE | (2 << 8))
   720  	SQLITE_WARNING_AUTOINDEX       = Code(SQLITE_WARNING | (1 << 8))
   721  	SQLITE_AUTH_USER               = Code(SQLITE_AUTH | (1 << 8))
   722  	SQLITE_OK_LOAD_PERMANENTLY     = Code(SQLITE_OK | (1 << 8))
   723  	SQLITE_OK_SYMLINK              = Code(SQLITE_OK | (2 << 8))
   724  )
   725  
   726  // CodeAsError is used to intern Codes into ErrCodes.
   727  // SQLite non-error status codes return nil.
   728  func CodeAsError(code Code) error {
   729  	if code == SQLITE_OK || code == SQLITE_ROW || code == SQLITE_DONE {
   730  		return nil
   731  	}
   732  	codeAsErrorInitOnce.Do(codeAsErrorInit)
   733  	err := codeAsError[code]
   734  	if err == nil {
   735  		return ErrCode(code)
   736  	}
   737  	return err
   738  }
   739  
   740  var codeAsError map[Code]error
   741  
   742  var codeAsErrorInitOnce sync.Once
   743  
   744  func codeAsErrorInit() {
   745  	codeAsError = map[Code]error{
   746  		SQLITE_ERROR:                   ErrCode(SQLITE_ERROR),
   747  		SQLITE_INTERNAL:                ErrCode(SQLITE_INTERNAL),
   748  		SQLITE_PERM:                    ErrCode(SQLITE_PERM),
   749  		SQLITE_ABORT:                   ErrCode(SQLITE_ABORT),
   750  		SQLITE_BUSY:                    ErrCode(SQLITE_BUSY),
   751  		SQLITE_LOCKED:                  ErrCode(SQLITE_LOCKED),
   752  		SQLITE_NOMEM:                   ErrCode(SQLITE_NOMEM),
   753  		SQLITE_READONLY:                ErrCode(SQLITE_READONLY),
   754  		SQLITE_INTERRUPT:               ErrCode(SQLITE_INTERRUPT),
   755  		SQLITE_IOERR:                   ErrCode(SQLITE_IOERR),
   756  		SQLITE_CORRUPT:                 ErrCode(SQLITE_CORRUPT),
   757  		SQLITE_NOTFOUND:                ErrCode(SQLITE_NOTFOUND),
   758  		SQLITE_FULL:                    ErrCode(SQLITE_FULL),
   759  		SQLITE_CANTOPEN:                ErrCode(SQLITE_CANTOPEN),
   760  		SQLITE_PROTOCOL:                ErrCode(SQLITE_PROTOCOL),
   761  		SQLITE_EMPTY:                   ErrCode(SQLITE_EMPTY),
   762  		SQLITE_SCHEMA:                  ErrCode(SQLITE_SCHEMA),
   763  		SQLITE_TOOBIG:                  ErrCode(SQLITE_TOOBIG),
   764  		SQLITE_CONSTRAINT:              ErrCode(SQLITE_CONSTRAINT),
   765  		SQLITE_MISMATCH:                ErrCode(SQLITE_MISMATCH),
   766  		SQLITE_MISUSE:                  ErrCode(SQLITE_MISUSE),
   767  		SQLITE_NOLFS:                   ErrCode(SQLITE_NOLFS),
   768  		SQLITE_AUTH:                    ErrCode(SQLITE_AUTH),
   769  		SQLITE_FORMAT:                  ErrCode(SQLITE_FORMAT),
   770  		SQLITE_RANGE:                   ErrCode(SQLITE_RANGE),
   771  		SQLITE_NOTADB:                  ErrCode(SQLITE_NOTADB),
   772  		SQLITE_NOTICE:                  ErrCode(SQLITE_NOTICE),
   773  		SQLITE_WARNING:                 ErrCode(SQLITE_WARNING),
   774  		SQLITE_ERROR_MISSING_COLLSEQ:   ErrCode(SQLITE_ERROR_MISSING_COLLSEQ),
   775  		SQLITE_ERROR_RETRY:             ErrCode(SQLITE_ERROR_RETRY),
   776  		SQLITE_ERROR_SNAPSHOT:          ErrCode(SQLITE_ERROR_SNAPSHOT),
   777  		SQLITE_IOERR_READ:              ErrCode(SQLITE_IOERR_READ),
   778  		SQLITE_IOERR_SHORT_READ:        ErrCode(SQLITE_IOERR_SHORT_READ),
   779  		SQLITE_IOERR_WRITE:             ErrCode(SQLITE_IOERR_WRITE),
   780  		SQLITE_IOERR_FSYNC:             ErrCode(SQLITE_IOERR_FSYNC),
   781  		SQLITE_IOERR_DIR_FSYNC:         ErrCode(SQLITE_IOERR_DIR_FSYNC),
   782  		SQLITE_IOERR_TRUNCATE:          ErrCode(SQLITE_IOERR_TRUNCATE),
   783  		SQLITE_IOERR_FSTAT:             ErrCode(SQLITE_IOERR_FSTAT),
   784  		SQLITE_IOERR_UNLOCK:            ErrCode(SQLITE_IOERR_UNLOCK),
   785  		SQLITE_IOERR_RDLOCK:            ErrCode(SQLITE_IOERR_RDLOCK),
   786  		SQLITE_IOERR_DELETE:            ErrCode(SQLITE_IOERR_DELETE),
   787  		SQLITE_IOERR_BLOCKED:           ErrCode(SQLITE_IOERR_BLOCKED),
   788  		SQLITE_IOERR_NOMEM:             ErrCode(SQLITE_IOERR_NOMEM),
   789  		SQLITE_IOERR_ACCESS:            ErrCode(SQLITE_IOERR_ACCESS),
   790  		SQLITE_IOERR_CHECKRESERVEDLOCK: ErrCode(SQLITE_IOERR_CHECKRESERVEDLOCK),
   791  		SQLITE_IOERR_LOCK:              ErrCode(SQLITE_IOERR_LOCK),
   792  		SQLITE_IOERR_CLOSE:             ErrCode(SQLITE_IOERR_CLOSE),
   793  		SQLITE_IOERR_DIR_CLOSE:         ErrCode(SQLITE_IOERR_DIR_CLOSE),
   794  		SQLITE_IOERR_SHMOPEN:           ErrCode(SQLITE_IOERR_SHMOPEN),
   795  		SQLITE_IOERR_SHMSIZE:           ErrCode(SQLITE_IOERR_SHMSIZE),
   796  		SQLITE_IOERR_SHMLOCK:           ErrCode(SQLITE_IOERR_SHMLOCK),
   797  		SQLITE_IOERR_SHMMAP:            ErrCode(SQLITE_IOERR_SHMMAP),
   798  		SQLITE_IOERR_SEEK:              ErrCode(SQLITE_IOERR_SEEK),
   799  		SQLITE_IOERR_DELETE_NOENT:      ErrCode(SQLITE_IOERR_DELETE_NOENT),
   800  		SQLITE_IOERR_MMAP:              ErrCode(SQLITE_IOERR_MMAP),
   801  		SQLITE_IOERR_GETTEMPPATH:       ErrCode(SQLITE_IOERR_GETTEMPPATH),
   802  		SQLITE_IOERR_CONVPATH:          ErrCode(SQLITE_IOERR_CONVPATH),
   803  		SQLITE_IOERR_VNODE:             ErrCode(SQLITE_IOERR_VNODE),
   804  		SQLITE_IOERR_AUTH:              ErrCode(SQLITE_IOERR_AUTH),
   805  		SQLITE_IOERR_BEGIN_ATOMIC:      ErrCode(SQLITE_IOERR_BEGIN_ATOMIC),
   806  		SQLITE_IOERR_COMMIT_ATOMIC:     ErrCode(SQLITE_IOERR_COMMIT_ATOMIC),
   807  		SQLITE_IOERR_ROLLBACK_ATOMIC:   ErrCode(SQLITE_IOERR_ROLLBACK_ATOMIC),
   808  		SQLITE_IOERR_DATA:              ErrCode(SQLITE_IOERR_DATA),
   809  		SQLITE_IOERR_CORRUPTFS:         ErrCode(SQLITE_IOERR_CORRUPTFS),
   810  		SQLITE_LOCKED_SHAREDCACHE:      ErrCode(SQLITE_LOCKED_SHAREDCACHE),
   811  		SQLITE_LOCKED_VTAB:             ErrCode(SQLITE_LOCKED_VTAB),
   812  		SQLITE_BUSY_RECOVERY:           ErrCode(SQLITE_BUSY_RECOVERY),
   813  		SQLITE_BUSY_SNAPSHOT:           ErrCode(SQLITE_BUSY_SNAPSHOT),
   814  		SQLITE_BUSY_TIMEOUT:            ErrCode(SQLITE_BUSY_TIMEOUT),
   815  		SQLITE_CANTOPEN_NOTEMPDIR:      ErrCode(SQLITE_CANTOPEN_NOTEMPDIR),
   816  		SQLITE_CANTOPEN_ISDIR:          ErrCode(SQLITE_CANTOPEN_ISDIR),
   817  		SQLITE_CANTOPEN_FULLPATH:       ErrCode(SQLITE_CANTOPEN_FULLPATH),
   818  		SQLITE_CANTOPEN_CONVPATH:       ErrCode(SQLITE_CANTOPEN_CONVPATH),
   819  		SQLITE_CANTOPEN_DIRTYWAL:       ErrCode(SQLITE_CANTOPEN_DIRTYWAL),
   820  		SQLITE_CANTOPEN_SYMLINK:        ErrCode(SQLITE_CANTOPEN_SYMLINK),
   821  		SQLITE_CORRUPT_VTAB:            ErrCode(SQLITE_CORRUPT_VTAB),
   822  		SQLITE_CORRUPT_SEQUENCE:        ErrCode(SQLITE_CORRUPT_SEQUENCE),
   823  		SQLITE_CORRUPT_INDEX:           ErrCode(SQLITE_CORRUPT_INDEX),
   824  		SQLITE_READONLY_RECOVERY:       ErrCode(SQLITE_READONLY_RECOVERY),
   825  		SQLITE_READONLY_CANTLOCK:       ErrCode(SQLITE_READONLY_CANTLOCK),
   826  		SQLITE_READONLY_ROLLBACK:       ErrCode(SQLITE_READONLY_ROLLBACK),
   827  		SQLITE_READONLY_DBMOVED:        ErrCode(SQLITE_READONLY_DBMOVED),
   828  		SQLITE_READONLY_CANTINIT:       ErrCode(SQLITE_READONLY_CANTINIT),
   829  		SQLITE_READONLY_DIRECTORY:      ErrCode(SQLITE_READONLY_DIRECTORY),
   830  		SQLITE_ABORT_ROLLBACK:          ErrCode(SQLITE_ABORT_ROLLBACK),
   831  		SQLITE_CONSTRAINT_CHECK:        ErrCode(SQLITE_CONSTRAINT_CHECK),
   832  		SQLITE_CONSTRAINT_COMMITHOOK:   ErrCode(SQLITE_CONSTRAINT_COMMITHOOK),
   833  		SQLITE_CONSTRAINT_FOREIGNKEY:   ErrCode(SQLITE_CONSTRAINT_FOREIGNKEY),
   834  		SQLITE_CONSTRAINT_FUNCTION:     ErrCode(SQLITE_CONSTRAINT_FUNCTION),
   835  		SQLITE_CONSTRAINT_NOTNULL:      ErrCode(SQLITE_CONSTRAINT_NOTNULL),
   836  		SQLITE_CONSTRAINT_PRIMARYKEY:   ErrCode(SQLITE_CONSTRAINT_PRIMARYKEY),
   837  		SQLITE_CONSTRAINT_TRIGGER:      ErrCode(SQLITE_CONSTRAINT_TRIGGER),
   838  		SQLITE_CONSTRAINT_UNIQUE:       ErrCode(SQLITE_CONSTRAINT_UNIQUE),
   839  		SQLITE_CONSTRAINT_VTAB:         ErrCode(SQLITE_CONSTRAINT_VTAB),
   840  		SQLITE_CONSTRAINT_ROWID:        ErrCode(SQLITE_CONSTRAINT_ROWID),
   841  		SQLITE_CONSTRAINT_PINNED:       ErrCode(SQLITE_CONSTRAINT_PINNED),
   842  		SQLITE_NOTICE_RECOVER_WAL:      ErrCode(SQLITE_NOTICE_RECOVER_WAL),
   843  		SQLITE_NOTICE_RECOVER_ROLLBACK: ErrCode(SQLITE_NOTICE_RECOVER_ROLLBACK),
   844  		SQLITE_WARNING_AUTOINDEX:       ErrCode(SQLITE_WARNING_AUTOINDEX),
   845  		SQLITE_AUTH_USER:               ErrCode(SQLITE_AUTH_USER),
   846  		SQLITE_OK_LOAD_PERMANENTLY:     ErrCode(SQLITE_OK_LOAD_PERMANENTLY),
   847  		SQLITE_OK_SYMLINK:              ErrCode(SQLITE_OK_SYMLINK),
   848  	}
   849  }
   850  
   851  func itoa(buf []byte, val int64) []byte {
   852  	i := len(buf) - 1
   853  	neg := false
   854  	if val < 0 {
   855  		neg = true
   856  		val = 0 - val
   857  	}
   858  	for val >= 10 {
   859  		buf[i] = byte(val%10 + '0')
   860  		i--
   861  		val /= 10
   862  	}
   863  	buf[i] = byte(val + '0')
   864  	if neg {
   865  		i--
   866  		buf[i] = '-'
   867  	}
   868  	return buf[i:]
   869  }
   870  
   871  // TraceConnID uniquely identifies an SQLite connection in this process.
   872  //
   873  // It is provided to the Tracer to let it associate transaction events.
   874  type TraceConnID int
   875  
   876  // A Tracer traces use of an SQLite database connection.
   877  //
   878  // Each tracer method is provided with a TraceConnID, which is a stable
   879  // identifier of the underlying sql connection that the event happened
   880  // to, which can be used to collate events.
   881  //
   882  // Some tracer methods take a ctx which is the context object
   883  // provided by the user to that method. This can be used by the tracer
   884  // to plumb through context values.
   885  //
   886  // Any error that occurred executing the event is reported to the
   887  // tracer in the err parameter. If err is not nil, the event failed.
   888  type Tracer interface {
   889  	// Query is called by the driver to report a completed query.
   890  	//
   891  	// The query string is the string the user provided to Prepare.
   892  	// No parameters are filled in.
   893  	//
   894  	// The duration covers the complete execution time of the query,
   895  	// including both time spent inside SQLite, and time spent in user
   896  	// code between calls to rows.Next.
   897  	Query(prepCtx context.Context, id TraceConnID, query string, duration time.Duration, err error)
   898  
   899  	// BeginTx is called by the driver to report the beginning of Tx.
   900  	BeginTx(beginCtx context.Context, id TraceConnID, why string, readOnly bool, err error)
   901  
   902  	// Commit is called by the driver to report the end of a Tx.
   903  	Commit(id TraceConnID, err error)
   904  
   905  	// Rollback is called by the driver to report the end of a Tx.
   906  	Rollback(id TraceConnID, err error)
   907  }