Skip to content
Gert-Jan Timmer edited this page Jul 20, 2018 · 1 revision

Go: 1.10+ add additional functionality for configuring drivers.

This can be achieved through by either creating a Config and using it as a driver.DriverContext or convert the Config into a DSN.

Config

type Config struct {
	// Database
	Database string

	// Mode of the SQLite database
	Mode Mode

	// CacheMode of the SQLite Connection
	Cache CacheMode

	// Mutex flag SQLITE_OPEN_MUTEX_NO, SQLITE_OPEN_MUTEX_FULL
	// Defaults to SQLITE_OPEN_MUTEX_FULL
	Mutex Mutex

	// The immutable parameter is a boolean query parameter that indicates
    // that the database file is stored on read-only media. 
    // When immutable is set, SQLite assumes that the database file 
    // cannot be changed, even by a process with higher privilege,
    // and so the database is opened read-only and all locking and 
    // change detection is disabled.
    //
    // Caution: Setting the immutable property on a database file 
    // that does in fact change can result in incorrect query results
    // and/or SQLITE_CORRUPT errors.
	Immutable bool

	// TimeZone location
	TimeZone *time.Location

	// TransactionLock behaviour
	TransactionLock TxLock

	// LockingMode behaviour
	LockingMode LockingMode

	// Authentication holds the UserAuth configuration
	Authentication *Auth

	// AutoVacuum sets the auto vacuum status of the database
	// Defaults to NONE
	AutoVacuum AutoVacuum

	// BusyTimeout defines the time a connection will wait when the
	// connection is BUSY and locked by an other connection.
	// BusyTimeout is defined in milliseconds
	BusyTimeout time.Duration

	// CaseSensitiveLike controls the behaviour of the LIKE operator.
	// Default or disabled the LIKE operation is case-insensitive.
	// When enabling this options behaviour of LIKE will become case-sensitive.
	CaseSensitiveLike bool

	// DeferForeignKeys when enabled will cause the enforcement
	// of all foreign key constraints is delayed until
	// the outermost transaction is committed.
	// The defer_foreign_keys pragma defaults to false
	// so that foreign key constraints are only deferred
	// if they are created as "DEFERRABLE INITIALLY DEFERRED".
    // The defer_foreign_keys pragma is automatically switched off 
    // at each COMMIT or ROLLBACK.
    // Hence, the defer_foreign_keys pragma must be separately 
    // enabled for each transaction.
    // This pragma is only meaningful if foreign key 
    // constraints are enabled, of course.
	DeferForeignKeys bool

    // ForeignKeyConstraints enable or disable the 
    // enforcement of foreign key constraints.
	ForeignKeyConstraints bool

    // IgnoreCheckConstraints enables or disables the 
    // enforcement of CHECK constraints.
    // The default setting is off, meaning that CHECK constraints 
    // are enforced by default.
	IgnoreCheckConstraints bool

    // JournalMode sets the journal mode for databases associated 
    // with the current database connection.
	JournalMode JournalMode

	// QueryOnly prevents all changes to the database when set to true.
	QueryOnly bool

	// RecursiveTriggers enable or disable the recursive trigger capability.
	RecursiveTriggers bool

    // SecureDelete enables or disables or sets the secure deletion 
    // within the database.
	SecureDelete SecureDelete

	// Synchronous Mode of the database
	Synchronous Synchronous

    // WriteableSchema enables of disables the ability to using 
    // UPDATE, INSERT, DELETE
    // 
	// Warning: misuse of this pragma can easily result in a corrupt database file.
	WriteableSchema bool

	// Extensions
	Extensions []string

	// ConnectHook
	ConnectHook func(*SQLiteConn) error
}

// Auth holds the authentication configuration for the SQLite UserAuth module.
type Auth struct {
	// Username for authentication
	Username string

	// Password for authentication
	Password string

	// Salt for encryption
	Salt string

	// CryptEncoder used for the password encryption
	Encoder CryptEncoder
}

Create Default Config

To create a new Config with default values please use the NewConfig() function.

import (
    _ "github.com/mattn/go-sqlite3/driver"
)

cfg := sqlite3.NewConfig()

Open Connector

import (
    _ "github.com/mattn/go-sqlite3/driver"
)

cfg := sqlite3.NewConfig()
cfg.Database = tempFilename

// OpenDB
db := sql.OpenDB(cfg)

Convert Into DSN

import (
    _ "github.com/mattn/go-sqlite3/driver"
)

cfg := sqlite3.NewConfig()
dsn := cfg.FormatDSN()
Clone this wiki locally