package sqlite3
import "github.com/mutecomm/go-sqlcipher/v4"
Index ¶
- Constants
- Variables
- func IsEncrypted(filename string) (bool, error)
- func Version() (libVersion string, libVersionNumber int, sourceID string)
- type ErrNo
- type ErrNoExtended
- type Error
- type SQLiteConn
- func (c *SQLiteConn) AutoCommit() bool
- func (c *SQLiteConn) Begin() (driver.Tx, error)
- func (c *SQLiteConn) Close() error
- func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error)
- func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error)
- func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error)
- type SQLiteDriver
- type SQLiteResult
- func (r *SQLiteResult) LastInsertId() (int64, error)
- func (r *SQLiteResult) RowsAffected() (int64, error)
- type SQLiteRows
- func (rc *SQLiteRows) Close() error
- func (rc *SQLiteRows) Columns() []string
- func (rc *SQLiteRows) DeclTypes() []string
- func (rc *SQLiteRows) Next(dest []driver.Value) error
- type SQLiteStmt
- func (s *SQLiteStmt) Close() error
- func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error)
- func (s *SQLiteStmt) NumInput() int
- func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error)
- type SQLiteTx
Examples ¶
Constants ¶
const ErrNoMask C.int = 0xff
ErrNoMask is mask code.
Variables ¶
var ( ErrError = ErrNo(1) /* SQL error or missing database */ ErrInternal = ErrNo(2) /* Internal logic error in SQLite */ ErrPerm = ErrNo(3) /* Access permission denied */ ErrAbort = ErrNo(4) /* Callback routine requested an abort */ ErrBusy = ErrNo(5) /* The database file is locked */ ErrLocked = ErrNo(6) /* A table in the database is locked */ ErrNomem = ErrNo(7) /* A malloc() failed */ ErrReadonly = ErrNo(8) /* Attempt to write a readonly database */ ErrInterrupt = ErrNo(9) /* Operation terminated by sqlite3_interrupt() */ ErrIoErr = ErrNo(10) /* Some kind of disk I/O error occurred */ ErrCorrupt = ErrNo(11) /* The database disk image is malformed */ ErrNotFound = ErrNo(12) /* Unknown opcode in sqlite3_file_control() */ ErrFull = ErrNo(13) /* Insertion failed because database is full */ ErrCantOpen = ErrNo(14) /* Unable to open the database file */ ErrProtocol = ErrNo(15) /* Database lock protocol error */ ErrEmpty = ErrNo(16) /* Database is empty */ ErrSchema = ErrNo(17) /* The database schema changed */ ErrTooBig = ErrNo(18) /* String or BLOB exceeds size limit */ ErrConstraint = ErrNo(19) /* Abort due to constraint violation */ ErrMismatch = ErrNo(20) /* Data type mismatch */ ErrMisuse = ErrNo(21) /* Library used incorrectly */ ErrNoLFS = ErrNo(22) /* Uses OS features not supported on host */ ErrAuth = ErrNo(23) /* Authorization denied */ ErrFormat = ErrNo(24) /* Auxiliary database format error */ ErrRange = ErrNo(25) /* 2nd parameter to sqlite3_bind out of range */ ErrNotADB = ErrNo(26) /* File opened that is not a database file */ ErrNotice = ErrNo(27) /* Notifications from sqlite3_log() */ ErrWarning = ErrNo(28) /* Warnings from sqlite3_log() */ )
result codes from http://www.sqlite.org/c3ref/c_abort.html
var ( ErrIoErrRead = ErrIoErr.Extend(1) ErrIoErrShortRead = ErrIoErr.Extend(2) ErrIoErrWrite = ErrIoErr.Extend(3) ErrIoErrFsync = ErrIoErr.Extend(4) ErrIoErrDirFsync = ErrIoErr.Extend(5) ErrIoErrTruncate = ErrIoErr.Extend(6) ErrIoErrFstat = ErrIoErr.Extend(7) ErrIoErrUnlock = ErrIoErr.Extend(8) ErrIoErrRDlock = ErrIoErr.Extend(9) ErrIoErrDelete = ErrIoErr.Extend(10) ErrIoErrBlocked = ErrIoErr.Extend(11) ErrIoErrNoMem = ErrIoErr.Extend(12) ErrIoErrAccess = ErrIoErr.Extend(13) ErrIoErrCheckReservedLock = ErrIoErr.Extend(14) ErrIoErrLock = ErrIoErr.Extend(15) ErrIoErrClose = ErrIoErr.Extend(16) ErrIoErrDirClose = ErrIoErr.Extend(17) ErrIoErrSHMOpen = ErrIoErr.Extend(18) ErrIoErrSHMSize = ErrIoErr.Extend(19) ErrIoErrSHMLock = ErrIoErr.Extend(20) ErrIoErrSHMMap = ErrIoErr.Extend(21) ErrIoErrSeek = ErrIoErr.Extend(22) ErrIoErrDeleteNoent = ErrIoErr.Extend(23) ErrIoErrMMap = ErrIoErr.Extend(24) ErrIoErrGetTempPath = ErrIoErr.Extend(25) ErrIoErrConvPath = ErrIoErr.Extend(26) = ErrLocked.Extend(1) ErrBusyRecovery = ErrBusy.Extend(1) ErrBusySnapshot = ErrBusy.Extend(2) ErrCantOpenNoTempDir = ErrCantOpen.Extend(1) ErrCantOpenIsDir = ErrCantOpen.Extend(2) ErrCantOpenFullPath = ErrCantOpen.Extend(3) ErrCantOpenConvPath = ErrCantOpen.Extend(4) ErrCorruptVTab = ErrCorrupt.Extend(1) ErrReadonlyRecovery = ErrReadonly.Extend(1) ErrReadonlyCantLock = ErrReadonly.Extend(2) ErrReadonlyRollback = ErrReadonly.Extend(3) ErrReadonlyDbMoved = ErrReadonly.Extend(4) ErrAbortRollback = ErrAbort.Extend(2) ErrConstraintCheck = ErrConstraint.Extend(1) ErrConstraintCommitHook = ErrConstraint.Extend(2) ErrConstraintForeignKey = ErrConstraint.Extend(3) ErrConstraintFunction = ErrConstraint.Extend(4) ErrConstraintNotNull = ErrConstraint.Extend(5) ErrConstraintPrimaryKey = ErrConstraint.Extend(6) ErrConstraintTrigger = ErrConstraint.Extend(7) ErrConstraintUnique = ErrConstraint.Extend(8) ErrConstraintVTab = ErrConstraint.Extend(9) ErrConstraintRowID = ErrConstraint.Extend(10) ErrNoticeRecoverWAL = ErrNotice.Extend(1) ErrNoticeRecoverRollback = ErrNotice.Extend(2) ErrWarningAutoIndex = ErrWarning.Extend(1) )
result codes from http://www.sqlite.org/c3ref/c_abort_rollback.html
var SQLiteTimestampFormats = []string{ "2006-01-02 15:04:05.999999999-07:00", "2006-01-02T15:04:05.999999999-07:00", "2006-01-02 15:04:05.999999999", "2006-01-02T15:04:05.999999999", "2006-01-02 15:04:05", "2006-01-02T15:04:05", "2006-01-02 15:04", "2006-01-02T15:04", "2006-01-02", }
SQLiteTimestampFormats is timestamp formats understood by both this module and SQLite. The first format in the slice will be used when saving time values into the database. When parsing a string from a timestamp or datetime column, the formats are tried in order.
Functions ¶
func IsEncrypted ¶
IsEncrypted returns true, if the database with the given filename is
encrypted, and false otherwise.
If the database header cannot be read properly an error is returned.
Code:play
Output:Example¶
package main
import (
"crypto/rand"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
sqlite3 "github.com/mutecomm/go-sqlcipher/v4"
)
var (
db *sql.DB
testDir = "go-sqlcipher_test"
tables = `
CREATE TABLE KeyValueStore (
KeyEntry TEXT NOT NULL UNIQUE,
ValueEntry TEXT NOT NULL
);`
)
func init() {
key := "passphrase"
tmpdir, err := ioutil.TempDir("", testDir)
if err != nil {
panic(err)
}
dbname := filepath.Join(tmpdir, "sqlcipher_test")
dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=%s&_pragma_cipher_page_size=4096", key)
db, err = sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
panic(err)
}
_, err = db.Exec(tables)
if err != nil {
panic(err)
}
db.Close()
encrypted, err := sqlite3.IsEncrypted(dbname)
if err != nil {
panic(err)
}
if !encrypted {
panic(errors.New("go-sqlcipher: DB not encrypted"))
}
db, err = sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
panic(err)
}
_, err = db.Exec("SELECT count(*) FROM sqlite_master;")
if err != nil {
panic(err)
}
}
func main() {
// create random key
var key [32]byte
_, err := io.ReadFull(rand.Reader, key[:])
if err != nil {
log.Fatal(err)
}
// set DB name
dbname := "go-sqlcipher.sqlite"
dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=x'%s'",
hex.EncodeToString(key[:]))
// create encrypted DB file
db, err := sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
log.Fatal(err)
}
defer os.Remove(dbname)
defer db.Close()
// create table
_, err = db.Exec("CREATE TABLE t(x INTEGER);")
if err != nil {
log.Fatal(err)
}
// make sure database is encrypted
encrypted, err := sqlite3.IsEncrypted(dbname)
if err != nil {
log.Fatal(err)
}
if encrypted {
fmt.Println("DB is encrypted")
} else {
fmt.Println("DB is unencrypted")
}
}
DB is encrypted
func Version ¶
Version returns SQLite library version information.
Types ¶
type ErrNo ¶
type ErrNo int
ErrNo inherit errno.
func (ErrNo) Error ¶
Error return error message from errno.
func (ErrNo) Extend ¶
func (err ErrNo) Extend(by int) ErrNoExtended
Extend return extended errno.
type ErrNoExtended ¶
type ErrNoExtended int
ErrNoExtended is extended errno.
func (ErrNoExtended) Error ¶
func (err ErrNoExtended) Error() string
Error return error message that is extended code.
type Error ¶
type Error struct { Code ErrNo /* The error code returned by SQLite */ ExtendedCode ErrNoExtended /* The extended error code returned by SQLite */ // contains filtered or unexported fields }
Error implement sqlite error code.
func (Error) Error ¶
type SQLiteConn ¶
type SQLiteConn struct {
// contains filtered or unexported fields
}
SQLiteConn implement sql.Conn.
func (*SQLiteConn) AutoCommit ¶
func (c *SQLiteConn) AutoCommit() bool
AutoCommit return which currently auto commit or not.
func (*SQLiteConn) Begin ¶
func (c *SQLiteConn) Begin() (driver.Tx, error)
Begin transaction.
func (*SQLiteConn) Close ¶
func (c *SQLiteConn) Close() error
Close the connection.
func (*SQLiteConn) Exec ¶
Exec implements Execer.
func (*SQLiteConn) Prepare ¶
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error)
Prepare the query string. Return a new statement.
func (*SQLiteConn) Query ¶
Query implements Queryer.
type SQLiteDriver ¶
type SQLiteDriver struct { ConnectHook func(*SQLiteConn) error }
SQLiteDriver implement sql.Driver.
func (*SQLiteDriver) Open ¶
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error)
Open database and return a new connection. You can specify a DSN string using a URI as the filename.
test.db file:test.db?cache=shared&mode=memory :memory: file::memory:
go-sqlite3 adds the following query parameters to those used by SQLite:
_loc=XXX Specify location of time format. It's possible to specify "auto". _busy_timeout=XXX Specify value for sqlite3_busy_timeout. _txlock=XXX Specify locking behavior for transactions. XXX can be "immediate", "deferred", "exclusive". _foreign_keys=X Enable or disable enforcement of foreign keys. X can be 1 or 0.
go-sqlcipher adds the following query parameters to those used by SQLite:
_pragma_key=XXX Specify PRAGMA key. _pragma_cipher_page_size=XXX Set the PRAGMA cipher_page_size to adjust the page size.
type SQLiteResult ¶
type SQLiteResult struct {
// contains filtered or unexported fields
}
SQLiteResult implement sql.Result.
func (*SQLiteResult) LastInsertId ¶
func (r *SQLiteResult) LastInsertId() (int64, error)
LastInsertId teturn last inserted ID.
func (*SQLiteResult) RowsAffected ¶
func (r *SQLiteResult) RowsAffected() (int64, error)
RowsAffected return how many rows affected.
type SQLiteRows ¶
type SQLiteRows struct {
// contains filtered or unexported fields
}
SQLiteRows implement sql.Rows.
func (*SQLiteRows) Close ¶
func (rc *SQLiteRows) Close() error
Close the rows.
func (*SQLiteRows) Columns ¶
func (rc *SQLiteRows) Columns() []string
Columns return column names.
func (*SQLiteRows) DeclTypes ¶
func (rc *SQLiteRows) DeclTypes() []string
DeclTypes return column types.
func (*SQLiteRows) Next ¶
func (rc *SQLiteRows) Next(dest []driver.Value) error
Next move cursor to next.
type SQLiteStmt ¶
type SQLiteStmt struct {
// contains filtered or unexported fields
}
SQLiteStmt implement sql.Stmt.
func (*SQLiteStmt) Close ¶
func (s *SQLiteStmt) Close() error
Close the statement.
func (*SQLiteStmt) Exec ¶
Exec execute the statement with arguments. Return result object.
func (*SQLiteStmt) NumInput ¶
func (s *SQLiteStmt) NumInput() int
NumInput return a number of parameters.
func (*SQLiteStmt) Query ¶
Query the statement with arguments. Return records.
type SQLiteTx ¶
type SQLiteTx struct {
// contains filtered or unexported fields
}
SQLiteTx implemen sql.Tx.
func (*SQLiteTx) Commit ¶
Commit transaction.
func (*SQLiteTx) Rollback ¶
Rollback transaction.
Source Files ¶
error.go flags.go sqlcipher.go sqlite3.go sqlite3_other.go
Directories ¶
Path | Synopsis |
---|---|
_example | |
_example/simple | |
sqlite3_test |
- Version
- v4.4.0
- Published
- Jul 23, 2020
- Platform
- darwin/amd64
- Imports
- 15 packages
- Last checked
- now –
Tools for package owners.