Commit 478b66c6 authored by gwenn's avatar gwenn

Docs

parent ff7149ac
...@@ -33,7 +33,7 @@ type BlobReadWriter struct { ...@@ -33,7 +33,7 @@ type BlobReadWriter struct {
// Zeroblobs are used to reserve space for a BLOB that is later written. // Zeroblobs are used to reserve space for a BLOB that is later written.
type ZeroBlobLength int type ZeroBlobLength int
// NewBlobReader opens a BLOB for incremental I/O. // NewBlobReader opens a BLOB for incremental I/O in read-only mode.
// //
// (See http://sqlite.org/c3ref/blob_open.html) // (See http://sqlite.org/c3ref/blob_open.html)
func (c *Conn) NewBlobReader(db, table, column string, row int64) (*BlobReader, error) { func (c *Conn) NewBlobReader(db, table, column string, row int64) (*BlobReader, error) {
...@@ -44,7 +44,7 @@ func (c *Conn) NewBlobReader(db, table, column string, row int64) (*BlobReader, ...@@ -44,7 +44,7 @@ func (c *Conn) NewBlobReader(db, table, column string, row int64) (*BlobReader,
return &BlobReader{c, bl, -1, 0}, nil return &BlobReader{c, bl, -1, 0}, nil
} }
// NewBlobReadWriter open a BLOB for incremental I/O. // NewBlobReadWriter opens a BLOB for incremental I/O.
// (See http://sqlite.org/c3ref/blob_open.html) // (See http://sqlite.org/c3ref/blob_open.html)
func (c *Conn) NewBlobReadWriter(db, table, column string, row int64) (*BlobReadWriter, error) { func (c *Conn) NewBlobReadWriter(db, table, column string, row int64) (*BlobReadWriter, error) {
bl, err := c.blob_open(db, table, column, row, true) bl, err := c.blob_open(db, table, column, row, true)
......
...@@ -87,7 +87,7 @@ func (c *cache) flush() { ...@@ -87,7 +87,7 @@ func (c *cache) flush() {
} }
// CacheSize returns (current, max) sizes. // CacheSize returns (current, max) sizes.
// Cache is turned off when max size is 0 // Prepared statements cache is turned off when max size is 0
func (c *Conn) CacheSize() (int, int) { func (c *Conn) CacheSize() (int, int) {
if c.stmtCache.maxSize <= 0 { if c.stmtCache.maxSize <= 0 {
return 0, 0 return 0, 0
......
...@@ -39,7 +39,7 @@ type rowsImpl struct { ...@@ -39,7 +39,7 @@ type rowsImpl struct {
} }
// Open opens a new database connection. // Open opens a new database connection.
// ":memory:" for memory db // ":memory:" for memory db,
// "" for temp file db // "" for temp file db
// TODO How to specify open flags? // TODO How to specify open flags?
func (d *Driver) Open(name string) (driver.Conn, error) { func (d *Driver) Open(name string) (driver.Conn, error) {
......
...@@ -128,7 +128,7 @@ func (c *Conn) Columns(dbName, table string) ([]Column, error) { ...@@ -128,7 +128,7 @@ func (c *Conn) Columns(dbName, table string) ([]Column, error) {
return columns, nil return columns, nil
} }
// Column extracts metadata about a column of a table // Column extracts metadata about a column of a table.
// Column.Cid and Column.DfltValue are left unspecified. // Column.Cid and Column.DfltValue are left unspecified.
// (See http://sqlite.org/c3ref/table_column_metadata.html) // (See http://sqlite.org/c3ref/table_column_metadata.html)
func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) { func (c *Conn) Column(dbName, tableName, columnName string) (*Column, error) {
......
...@@ -8,20 +8,20 @@ import ( ...@@ -8,20 +8,20 @@ import (
"fmt" "fmt"
) )
// Check database integrity // IntegrityCheck checks database integrity.
// Database name is optional // Database name is optional (default is 'main').
// (See http://www.sqlite.org/pragma.html#pragma_integrity_check // (See http://www.sqlite.org/pragma.html#pragma_integrity_check
// and http://www.sqlite.org/pragma.html#pragma_quick_check) // and http://www.sqlite.org/pragma.html#pragma_quick_check)
// TODO Make possible to specify the database-name (PRAGMA %Q.integrity_check(.))
func (c *Conn) IntegrityCheck(dbName string, max int, quick bool) error { func (c *Conn) IntegrityCheck(dbName string, max int, quick bool) error {
var pragma string var prefix string
if quick { if quick {
pragma = "quick" prefix = "quick"
} else { } else {
pragma = "integrity" prefix = "integrity"
} }
pragmaName = fmt.Sprintf("%s_check(%d)", prefix, max)
var msg string var msg string
err := c.OneValue(fmt.Sprintf("PRAGMA %s_check(%d)", pragma, max), &msg) err := c.OneValue(pragma(dbName, pragmaName), &msg)
if err != nil { if err != nil {
return err return err
} }
...@@ -31,38 +31,40 @@ func (c *Conn) IntegrityCheck(dbName string, max int, quick bool) error { ...@@ -31,38 +31,40 @@ func (c *Conn) IntegrityCheck(dbName string, max int, quick bool) error {
return nil return nil
} }
// Database name is optional // Encoding returns the text encoding used by the specified database.
// Returns the text encoding used by the main database // Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_encoding) // (See http://sqlite.org/pragma.html#pragma_encoding)
func (c *Conn) Encoding(dbName string) (string, error) { func (c *Conn) Encoding(dbName string) (string, error) {
var encoding string var encoding string
err := c.OneValue(pragma(dbName, "PRAGMA encoding", "PRAGMA %Q.encoding"), &encoding) err := c.OneValue(pragma(dbName, "encoding"), &encoding)
if err != nil { if err != nil {
return "", err return "", err
} }
return encoding, nil return encoding, nil
} }
// Database name is optional // SchemaVersion gets the value of the schema-version.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_schema_version) // (See http://sqlite.org/pragma.html#pragma_schema_version)
func (c *Conn) SchemaVersion(dbName string) (int, error) { func (c *Conn) SchemaVersion(dbName string) (int, error) {
var version int var version int
err := c.OneValue(pragma(dbName, "PRAGMA schema_version", "PRAGMA %Q.schema_version"), &version) err := c.OneValue(pragma(dbName, "schema_version"), &version)
if err != nil { if err != nil {
return -1, err return -1, err
} }
return version, nil return version, nil
} }
// SetRecursiveTriggers sets or clears the recursive trigger capability.
// Database name is optional (default is 'main').
// (See http://sqlite.org/pragma.html#pragma_recursive_triggers) // (See http://sqlite.org/pragma.html#pragma_recursive_triggers)
// TODO Make possible to specify the database-name (PRAGMA %Q.recursive_triggers=%) func (c *Conn) SetRecursiveTriggers(dbName string, on bool) error {
func (c *Conn) SetRecursiveTriggers(on bool) error { return c.exec(pragma(dbName, fmt.Sprintf("recursive_triggers=%t"), on))
return c.exec(fmt.Sprintf("PRAGMA recursive_triggers=%t", on))
} }
func pragma(dbName, unqualified, qualified string) string { func pragma(dbName, pragmaName string) string {
if len(dbName) == 0 { if len(dbName) == 0 {
return unqualified return "PRAGMA " + pragmaName
} }
return Mprintf(qualified, dbName) return Mprintf("PRAGMA %Q."+pragmaName, dbName)
} }
...@@ -41,7 +41,7 @@ func (e *ConnError) ExtendedCode() int { ...@@ -41,7 +41,7 @@ func (e *ConnError) ExtendedCode() int {
return int(C.sqlite3_extended_errcode(e.c.db)) return int(C.sqlite3_extended_errcode(e.c.db))
} }
// Return Database file name from which the error comes from. // Filename returns database file name from which the error comes from.
func (e *ConnError) Filename() string { func (e *ConnError) Filename() string {
return e.c.Filename("main") return e.c.Filename("main")
} }
...@@ -148,6 +148,8 @@ func (c *Conn) specificError(msg string, a ...interface{}) error { ...@@ -148,6 +148,8 @@ func (c *Conn) specificError(msg string, a ...interface{}) error {
return &ConnError{c: c, code: ErrSpecific, msg: fmt.Sprintf(msg, a...)} return &ConnError{c: c, code: ErrSpecific, msg: fmt.Sprintf(msg, a...)}
} }
// LastError returns the error for the most recent failed sqlite3_* API call associated with a database connection.
// (See http://sqlite.org/c3ref/errcode.html)
func (c *Conn) LastError() error { func (c *Conn) LastError() error {
if c == nil { if c == nil {
return errors.New("nil sqlite database") return errors.New("nil sqlite database")
...@@ -192,7 +194,7 @@ const ( ...@@ -192,7 +194,7 @@ const (
) )
// Open opens a new database connection. // Open opens a new database connection.
// ":memory:" for memory db // ":memory:" for memory db,
// "" for temp file db // "" for temp file db
// //
// (See sqlite3_open_v2: http://sqlite.org/c3ref/open.html) // (See sqlite3_open_v2: http://sqlite.org/c3ref/open.html)
...@@ -241,8 +243,8 @@ func (c *Conn) BusyTimeout(ms int) error { // TODO time.Duration ? ...@@ -241,8 +243,8 @@ func (c *Conn) BusyTimeout(ms int) error { // TODO time.Duration ?
return c.error(C.sqlite3_busy_timeout(c.db, C.int(ms))) return c.error(C.sqlite3_busy_timeout(c.db, C.int(ms)))
} }
// Enable or disable the enforcement of foreign key constraints // EnableFKey enables or disables the enforcement of foreign key constraints.
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, b) // Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, b).
// Another way is PRAGMA foreign_keys = boolean; // Another way is PRAGMA foreign_keys = boolean;
// //
// (See http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html) // (See http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html)
...@@ -250,7 +252,8 @@ func (c *Conn) EnableFKey(b bool) (bool, error) { ...@@ -250,7 +252,8 @@ func (c *Conn) EnableFKey(b bool) (bool, error) {
return c.queryOrSetEnableDbConfig(C.SQLITE_DBCONFIG_ENABLE_FKEY, btocint(b)) return c.queryOrSetEnableDbConfig(C.SQLITE_DBCONFIG_ENABLE_FKEY, btocint(b))
} }
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, -1) // IsFKeyEnabled reports if the enforcement of foreign key constraints is enabled or not.
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, -1).
// Another way is PRAGMA foreign_keys; // Another way is PRAGMA foreign_keys;
// //
// (See http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html) // (See http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html)
...@@ -259,7 +262,7 @@ func (c *Conn) IsFKeyEnabled() (bool, error) { ...@@ -259,7 +262,7 @@ func (c *Conn) IsFKeyEnabled() (bool, error) {
} }
// EnableTriggers enables or disables triggers. // EnableTriggers enables or disables triggers.
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_TRIGGER, b) // Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_TRIGGER, b).
// //
// (See http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html) // (See http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html)
func (c *Conn) EnableTriggers(b bool) (bool, error) { func (c *Conn) EnableTriggers(b bool) (bool, error) {
...@@ -283,12 +286,13 @@ func (c *Conn) queryOrSetEnableDbConfig(key, i C.int) (bool, error) { ...@@ -283,12 +286,13 @@ func (c *Conn) queryOrSetEnableDbConfig(key, i C.int) (bool, error) {
return false, c.error(rv) return false, c.error(rv)
} }
// Enable or disable the extended result codes feature of SQLite. // EnableExtendedResultCodes enables or disables the extended result codes feature of SQLite.
// (See http://sqlite.org/c3ref/extended_result_codes.html) // (See http://sqlite.org/c3ref/extended_result_codes.html)
func (c *Conn) EnableExtendedResultCodes(b bool) error { func (c *Conn) EnableExtendedResultCodes(b bool) error {
return c.error(C.sqlite3_extended_result_codes(c.db, btocint(b))) return c.error(C.sqlite3_extended_result_codes(c.db, btocint(b)))
} }
// Readonly determines if a database is read-only.
// (See http://sqlite.org/c3ref/db_readonly.html) // (See http://sqlite.org/c3ref/db_readonly.html)
func (c *Conn) Readonly(dbName string) (bool, error) { func (c *Conn) Readonly(dbName string) (bool, error) {
cname := C.CString(dbName) cname := C.CString(dbName)
...@@ -300,6 +304,7 @@ func (c *Conn) Readonly(dbName string) (bool, error) { ...@@ -300,6 +304,7 @@ func (c *Conn) Readonly(dbName string) (bool, error) {
return rv == 1, nil return rv == 1, nil
} }
// Filename returns the filename for a database connection.
// (See http://sqlite.org/c3ref/db_filename.html) // (See http://sqlite.org/c3ref/db_filename.html)
func (c *Conn) Filename(dbName string) string { func (c *Conn) Filename(dbName string) string {
cname := C.CString(dbName) cname := C.CString(dbName)
...@@ -307,7 +312,7 @@ func (c *Conn) Filename(dbName string) string { ...@@ -307,7 +312,7 @@ func (c *Conn) Filename(dbName string) string {
return C.GoString(C.sqlite3_db_filename(c.db, cname)) return C.GoString(C.sqlite3_db_filename(c.db, cname))
} }
// Prepare and execute one parameterized statement or many statements (separated by semi-colon). // Exec prepares and executes one parameterized statement or many statements (separated by semi-colon).
// Don't use it with SELECT or anything that returns data. // Don't use it with SELECT or anything that returns data.
func (c *Conn) Exec(cmd string, args ...interface{}) error { func (c *Conn) Exec(cmd string, args ...interface{}) error {
for len(cmd) > 0 { for len(cmd) > 0 {
...@@ -338,7 +343,7 @@ func (c *Conn) Exec(cmd string, args ...interface{}) error { ...@@ -338,7 +343,7 @@ func (c *Conn) Exec(cmd string, args ...interface{}) error {
return nil return nil
} }
// Return true if the specified query returns at least one row. // Exists returns true if the specified query returns at least one row.
func (c *Conn) Exists(query string, args ...interface{}) (bool, error) { func (c *Conn) Exists(query string, args ...interface{}) (bool, error) {
s, err := c.Prepare(query, args...) s, err := c.Prepare(query, args...)
if err != nil { if err != nil {
...@@ -351,7 +356,7 @@ func (c *Conn) Exists(query string, args ...interface{}) (bool, error) { ...@@ -351,7 +356,7 @@ func (c *Conn) Exists(query string, args ...interface{}) (bool, error) {
return s.Next() return s.Next()
} }
// Use it with SELECT that returns only one row with only one column. // OneValue is used with SELECT that returns only one row with only one column.
// Returns io.EOF when there is no row. // Returns io.EOF when there is no row.
func (c *Conn) OneValue(query string, value interface{}, args ...interface{}) error { func (c *Conn) OneValue(query string, value interface{}, args ...interface{}) error {
s, err := c.Prepare(query, args...) s, err := c.Prepare(query, args...)
...@@ -371,33 +376,33 @@ func (c *Conn) OneValue(query string, value interface{}, args ...interface{}) er ...@@ -371,33 +376,33 @@ func (c *Conn) OneValue(query string, value interface{}, args ...interface{}) er
return s.Scan(value) return s.Scan(value)
} }
// Changes counts the number of rows modified. // Changes returns the number of database rows that were changed or inserted or deleted by the most recently completed SQL statement on the database connection.
// If a separate thread makes changes on the same database connection while Changes() is running then the value returned is unpredictable and not meaningful. // If a separate thread makes changes on the same database connection while Changes() is running then the value returned is unpredictable and not meaningful.
// (See http://sqlite.org/c3ref/changes.html) // (See http://sqlite.org/c3ref/changes.html)
func (c *Conn) Changes() int { func (c *Conn) Changes() int {
return int(C.sqlite3_changes(c.db)) return int(C.sqlite3_changes(c.db))
} }
// Total number of rows Modified // TotalChanges returns the number of row changes caused by INSERT, UPDATE or DELETE statements since the database connection was opened.
// (See http://sqlite.org/c3ref/total_changes.html) // (See http://sqlite.org/c3ref/total_changes.html)
func (c *Conn) TotalChanges() int { func (c *Conn) TotalChanges() int {
return int(C.sqlite3_total_changes(c.db)) return int(C.sqlite3_total_changes(c.db))
} }
// Return the rowid of the most recent successful INSERT into the database. // LastInsertRowid returns the rowid of the most recent successful INSERT into the database.
// If a separate thread performs a new INSERT on the same database connection while the LastInsertRowid() function is running and thus changes the last insert rowid, then the value returned by LastInsertRowid() is unpredictable and might not equal either the old or the new last insert rowid. // If a separate thread performs a new INSERT on the same database connection while the LastInsertRowid() function is running and thus changes the last insert rowid, then the value returned by LastInsertRowid() is unpredictable and might not equal either the old or the new last insert rowid.
// (See http://sqlite.org/c3ref/last_insert_rowid.html) // (See http://sqlite.org/c3ref/last_insert_rowid.html)
func (c *Conn) LastInsertRowid() int64 { func (c *Conn) LastInsertRowid() int64 {
return int64(C.sqlite3_last_insert_rowid(c.db)) return int64(C.sqlite3_last_insert_rowid(c.db))
} }
// Interrupt a long-running query // Interrupt interrupts a long-running query.
// (See http://sqlite.org/c3ref/interrupt.html) // (See http://sqlite.org/c3ref/interrupt.html)
func (c *Conn) Interrupt() { func (c *Conn) Interrupt() {
C.sqlite3_interrupt(c.db) C.sqlite3_interrupt(c.db)
} }
// Test for auto-commit mode // GetAutocommit tests for auto-commit mode.
// (See http://sqlite.org/c3ref/get_autocommit.html) // (See http://sqlite.org/c3ref/get_autocommit.html)
func (c *Conn) GetAutocommit() bool { func (c *Conn) GetAutocommit() bool {
return C.sqlite3_get_autocommit(c.db) != 0 return C.sqlite3_get_autocommit(c.db) != 0
...@@ -444,16 +449,19 @@ func (c *Conn) Rollback() error { ...@@ -444,16 +449,19 @@ func (c *Conn) Rollback() error {
return c.exec("ROLLBACK") return c.exec("ROLLBACK")
} }
// Savepoint starts a new transaction with a name.
// (See http://sqlite.org/lang_savepoint.html) // (See http://sqlite.org/lang_savepoint.html)
func (c *Conn) Savepoint(name string) error { func (c *Conn) Savepoint(name string) error {
return c.exec(Mprintf("SAVEPOINT %Q", name)) return c.exec(Mprintf("SAVEPOINT %Q", name))
} }
// ReleaseSavepoint causes all savepoints back to and including the most recent savepoint with a matching name to be removed from the transaction stack.
// (See http://sqlite.org/lang_savepoint.html) // (See http://sqlite.org/lang_savepoint.html)
func (c *Conn) ReleaseSavepoint(name string) error { func (c *Conn) ReleaseSavepoint(name string) error {
return c.exec(Mprintf("RELEASE %Q", name)) return c.exec(Mprintf("RELEASE %Q", name))
} }
// RollbackSavepoint reverts the state of the database back to what it was just before the corresponding SAVEPOINT.
// (See http://sqlite.org/lang_savepoint.html) // (See http://sqlite.org/lang_savepoint.html)
func (c *Conn) RollbackSavepoint(name string) error { func (c *Conn) RollbackSavepoint(name string) error {
return c.exec(Mprintf("ROLLBACK TO SAVEPOINT %Q", name)) return c.exec(Mprintf("ROLLBACK TO SAVEPOINT %Q", name))
...@@ -505,13 +513,13 @@ func (c *Conn) Close() error { ...@@ -505,13 +513,13 @@ func (c *Conn) Close() error {
return nil return nil
} }
// Enable or disable extension loading // EnableLoadExtension enables or disables extension loading.
// (See http://sqlite.org/c3ref/enable_load_extension.html) // (See http://sqlite.org/c3ref/enable_load_extension.html)
func (c *Conn) EnableLoadExtension(b bool) { func (c *Conn) EnableLoadExtension(b bool) {
C.sqlite3_enable_load_extension(c.db, btocint(b)) C.sqlite3_enable_load_extension(c.db, btocint(b))
} }
// Load an extension // LoadExtension loads an extension
// (See http://sqlite.org/c3ref/load_extension.html) // (See http://sqlite.org/c3ref/load_extension.html)
func (c *Conn) LoadExtension(file string, proc ...string) error { func (c *Conn) LoadExtension(file string, proc ...string) error {
cfile := C.CString(file) cfile := C.CString(file)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment