sqlite3

package module
v0.0.0-...-0ef1ba5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2012 License: BSD-2-Clause Imports: 8 Imported by: 3

README

== About ==

GoSQLite3 provides both low-level and high-level wrappers for accessing the SQLite3 database from Go.
It started as simple wrapper around the sqlite C API which has since been extended with a number of 
high-level conveniences.


== Installation ==

We support installation via the go tool which can be invoked using the following command-line:

	go get github.com/kuroneko/gosqlite3


== Usage ==

Please read the tests for examples of how to perform queries and handle results.
Documentation will follow in due course.


== License ==

TBD


== Contributors ==

Chris Collins
Eleanor McHugh

Documentation

Overview

Package sqlite3 implements a Go interface to the SQLite database.

Index

Constants

View Source
const (
	OK = Errno(iota)
	ERROR
	INTERNAL
	PERM
	ABORT
	BUSY
	LOCKED
	NOMEM
	READONLY
	INTERRUPT
	IOERR
	CORRUPT
	NOTFOUND
	FULL
	CANTOPEN
	PROTOCOL
	EMPTY
	SCHEMA
	TOOBIG
	CONSTRAINT
	MISMATCH
	MISUSE
	NOLFS
	AUTH
	FORMAT
	RANGE
	NOTDB
	ROW       = Errno(100)
	DONE      = Errno(101)
	ENCODER   = Errno(1000)
	SAVEPOINT = Errno(1001)
)
View Source
const (
	INTEGER = 1
	FLOAT   = 2
	TEXT    = 3
	BLOB    = 4
	NULL    = 5
)

Variables

This section is empty.

Functions

func Initialize

func Initialize()

Initialize starts the SQLite3 engine.

func LibVersion

func LibVersion() string

LibVersion returns the version of the SQLite3 engine.

func SQLiteError

func SQLiteError(code C.int) (e error)

func Session

func Session(filename string, f func(db *Database))

Session initializes a database and calls `f` to access it.

func Shutdown

func Shutdown()

Shutdown stops the SQLite3 engine.

func TransientSession

func TransientSession(f func(db *Database))

TransientDatabase initializes a in-memory database and calls `f` to access it.

Types

type Backup

type Backup struct {
	// contains filtered or unexported fields
}

Backup implements the SQLite Online Backup API.

The backup API copies the content of one database to another. It is useful either for creating backups of databases or for copying in-memory databases to or from persistent files.

func NewBackup

func NewBackup(d *Database, ddb string, s *Database, sdb string) (b *Backup, e error)

NewBackup initializes and returns the handle to a backup.

func (*Backup) Finish

func (b *Backup) Finish() error

Finish should be called when the backup is done, an error occured or when the application wants to abandon the backup operation.

func (*Backup) Full

func (b *Backup) Full() error

Full creates a full backup of the database.

func (*Backup) PageCount

func (b *Backup) PageCount() int

PageCount returns the total number of pages in the source database.

func (*Backup) Remaining

func (b *Backup) Remaining() int

Remaining returns the number of pages still to be backed up.

func (*Backup) Step

func (b *Backup) Step(pages int) error

Step will copy up to `pages` between the source and destination database. If `pages` is negative, all remaining source pages are copied.

type BackupParameters

type BackupParameters struct {
	Target       string
	PagesPerStep int
	QueueLength  int
	Interval     time.Duration
	Verbose      bool
}

type Blob

type Blob struct {
	// contains filtered or unexported fields
}

Blob represents the SQLite3 blob type.

type DBFlag

type DBFlag int
const (
	O_READONLY       DBFlag = 0x00000001
	O_READWRITE      DBFlag = 0x00000002
	O_CREATE         DBFlag = 0x00000004
	O_DELETEONCLOSE  DBFlag = 0x00000008
	O_EXCLUSIVE      DBFlag = 0x00000010
	O_AUTOPROXY      DBFlag = 0x00000020
	O_URI            DBFlag = 0x00000040
	O_MAIN_DB        DBFlag = 0x00000100
	O_TEMP_DB        DBFlag = 0x00000200
	O_TRANSIENT_DB   DBFlag = 0x00000400
	O_MAIN_JOURNAL   DBFlag = 0x00000800
	O_TEMP_JOURNAL   DBFlag = 0x00001000
	O_SUBJOURNAL     DBFlag = 0x00002000
	O_MASTER_JOURNAL DBFlag = 0x00004000
	O_NOMUTEX        DBFlag = 0x00008000
	O_FULLMUTEX      DBFlag = 0x00010000
	O_SHAREDCACHE    DBFlag = 0x00020000
	O_PRIVATECACHE   DBFlag = 0x00040000
	O_WAL            DBFlag = 0x00080000
)

func (DBFlag) String

func (d DBFlag) String() string

type Database

type Database struct {
	Filename string
	DBFlag
	Savepoints []interface{}
	// contains filtered or unexported fields
}

Database implements high level view of the underlying database.

func Open

func Open(filename string, flags ...DBFlag) (db *Database, e error)

Open returns a handle to the sqlite3 database specified by filename.

func TransientDatabase

func TransientDatabase() (db *Database)

TransientDatabase returns a handle to an in-memory database.

func (*Database) Backup

func (db *Database) Backup(p BackupParameters) (r Reporter, e error)

Backup creates a copy (backup) of the current database to the target file specified in BackupParameters.

func (*Database) Begin

func (db *Database) Begin() (e error)

Begin initializes a SQL Transaction block.

func (*Database) Changes

func (db *Database) Changes() int

Changes returns the number of database rows that were changed or inserted or deleted by the most recently completed SQL statement.

func (*Database) Close

func (db *Database) Close()

Close is used to close the database.

func (*Database) Commit

func (db *Database) Commit() (e error)

Commit ends the current transaction and makes all changes performed in the transaction permanent.

func (*Database) Error

func (db *Database) Error() error

Error returns the numeric result code for the most recently failed database call.

func (*Database) Execute

func (db *Database) Execute(sql string, f ...func(*Statement, ...interface{})) (c int, e error)

Execute runs the SQL statement.

func (*Database) LastInsertRowID

func (db *Database) LastInsertRowID() int64

LastInsertRowID returns the id of the most recently successful INSERT.

Each entry in an SQLite table has a unique 64-bit signed integer key called the "rowid". The rowid is always available as an undeclared column named ROWID, OID, or _ROWID_ as long as those names are not also used by explicitly declared columns. If the table has a column of type INTEGER PRIMARY KEY then that column is another alias for the rowid.

This routine returns the rowid of the most recently successful INSERT into the database from the database connection in the first argument. As of SQLite version 3.7.7, this routines records the last insert rowid of both ordinary tables and virtual tables. If no successful INSERTs have ever occurred on that database connection, zero is returned.

func (*Database) Load

func (db *Database) Load(source *Database, dbname string) (e error)

Load creates a backup of the source database and loads that.

func (*Database) Mark

func (db *Database) Mark(id interface{}) (e error)

Mark creates a SAVEPOINT.

A SAVEPOINT is a method of creating transactions, similar to BEGIN and COMMIT, except that Mark and MergeSteps are named and may be nested.

func (*Database) MergeSteps

func (db *Database) MergeSteps(id interface{}) (e error)

MergeSteps can be seen as the equivalent of COMMIT for a Mark command.

func (*Database) Open

func (db *Database) Open(flags ...DBFlag) (e error)

Open initializes and opens the database.

func (*Database) Prepare

func (db *Database) Prepare(sql string, values ...interface{}) (s *Statement, e error)

Prepare compiles the SQL query into a byte-code program and binds the supplied values.

func (*Database) Release

func (db *Database) Release(id interface{}) (e error)

Release rolls back all transactions to the specified SAVEPOINT (Mark).

More specificly ...
- Some people view RELEASE as the equivalent of COMMIT for a SAVEPOINT.
  This is an acceptable point of view as long as one remembers that the
  changes committed by an inner transaction might later be undone by a
  rollback in an outer transaction.
- Another view of RELEASE is that it merges a named transaction into its
  parent transaction, so that the named transaction and its parent
  become the same transaction. After RELEASE, the named transaction and
  its parent will commit or rollback together, whatever their fate may
  be.
- One can also think of savepoints as "marks" in the transaction
  timeline. In this view, the SAVEPOINT command creates a new mark, the
  ROLLBACK TO command rewinds the timeline back to a point just after
  the named mark, and the RELEASE command erases marks from the timeline
  without actually making any changes to the database.

func (*Database) Rollback

func (db *Database) Rollback() (e error)

Rollback reverts the changes since the most recent Begin() call.

func (*Database) Save

func (db *Database) Save(target *Database, dbname string) (e error)

Save stores the content of the database in the target database.

func (*Database) SavePoints

func (db *Database) SavePoints() (s []interface{})

SavePoints returns the currently active SAVEPOINTs created using Mark.

func (*Database) TotalChanges

func (db *Database) TotalChanges() int

TotalChanges retruns the number of row changes.

This function returns the number of row changes caused by INSERT, UPDATE or DELETE statements since the database connection was opened. The count returned by TotalChanges includes all changes from all trigger contexts and changes made by foreign key actions. However, the count does not include changes used to implement REPLACE constraints, do rollbacks or ABORT processing, or DROP TABLE processing. The count does not include rows of views that fire an INSTEAD OF trigger, though if the INSTEAD OF trigger makes changes of its own, those changes are counted. The TotalChanges function counts the changes as soon as the statement that makes them is completed.

type Errno

type Errno int

func (Errno) Error

func (e Errno) Error() (err string)

type ProgressReport

type ProgressReport struct {
	Error     error
	Total     int
	Remaining int
	Source    string
	Target    string
	Verbose   bool
}

type QueryParameter

type QueryParameter int

func (QueryParameter) Bind

func (p QueryParameter) Bind(s *Statement, value interface{}) (e error)

Bind replaces the literals placed in the SQL statement with the actual values supplied to the function.

The following templates may be replaced by the values:

  • ?
  • ?NNN
  • :VVV
  • @VVV
  • $VVV

In the templates above, NNN represents an integer literal, VVV represents an alphanumeric identifier.

type Reporter

type Reporter chan *ProgressReport

type ResultColumn

type ResultColumn int

ResultColumn implements the high level view of the SQLite result column.

func (ResultColumn) ByteCount

func (c ResultColumn) ByteCount(s *Statement) int

ByteCount returns the number of bytes of the BLOB or string without the zero terminators at the end of the string.

func (ResultColumn) Name

func (c ResultColumn) Name(s *Statement) string

Name returns the name assigned to a particular column in the result set of a SELECT statement.

func (ResultColumn) Type

func (c ResultColumn) Type(s *Statement) int

Type returns the datatype code for the initial data type of the column.

func (ResultColumn) Value

func (c ResultColumn) Value(s *Statement) (value interface{})

Value returns the value of the ResultColumn converted to a Go type.

type Statement

type Statement struct {
	// contains filtered or unexported fields
}

Statement represents a "SQL prepared Statement" also known as "compiled SQL statement".

func (*Statement) All

func (s *Statement) All(f ...func(*Statement, ...interface{})) (c int, e error)

All can be used to return all rows of a prepared statement after the statement has been prepared.

func (*Statement) Bind

func (s *Statement) Bind(start_column int, values ...interface{}) (e error, index int)

Bind replaces the SQL parameters with actual values.

func (*Statement) BindAll

func (s *Statement) BindAll(values ...interface{}) (e error, index int)

BindAll replaces all SQL parameters with their actual values.

func (*Statement) ClearBindings

func (s *Statement) ClearBindings() error

ClearBindings is used to reset all parameters to NULL.

func (*Statement) Column

func (s *Statement) Column(column int) (value interface{})

Column returns the value of the column.

func (*Statement) ColumnName

func (s *Statement) ColumnName(column int) string

ColumnName returns the name of the column.

func (*Statement) ColumnType

func (s *Statement) ColumnType(column int) int

ColumnType returns the type of the column.

func (*Statement) Columns

func (s *Statement) Columns() int

Columns returns the number of columns in the result set of the prepared statement.

func (*Statement) Finalize

func (s *Statement) Finalize() (e error)

Finalize is used to delete a prepared statement in the SQLite engine.

func (*Statement) Parameters

func (s *Statement) Parameters() int

Parameters returns the number of SQL parameters.

This routine actually returns the index of the largest (rightmost) parameter. For all forms except ?NNN, this will correspond to the number of unique parameters. If parameters of the ?NNN form are used, there may be gaps in the list.

func (*Statement) Reset

func (s *Statement) Reset() error

Reset may be used to reset the statement to its initial state, ready to be re-executed.

Any SQL statement variables that had values bound to them retain their values. Use `ClearBindings` to reset the bindings.

func (*Statement) Row

func (s *Statement) Row() (values []interface{})

Row returns all values of the row.

func (*Statement) SQLSource

func (s *Statement) SQLSource() (sql string)

SQLSource can be used to retrieve a saved copy of the original SQL text used to create the prepared statement -- if that statement was compiled using `Prepare`.

func (*Statement) Step

func (s *Statement) Step(f ...func(*Statement, ...interface{})) (e error)

Step must be called one or more times to evaluate the statement after the prepared statement has been prepared.

type Table

type Table struct {
	Name       string
	ColumnSpec string
}

Table implements a high level view of a SQL table.

func (*Table) Create

func (t *Table) Create(db *Database) (e error)

Create is used to create a SQL table.

func (*Table) Drop

func (t *Table) Drop(db *Database) (e error)

Drop is used to delete a SQL table.

func (*Table) Rows

func (t *Table) Rows(db *Database) (c int, e error)

Rows returns the number of rows in the table.

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value represents any SQLite3 value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL