1
0
Fork 0

Start with new DB layer

master
Philip O'Toole 9 years ago
parent c07c11f134
commit f4c9ff0c3b

@ -19,29 +19,14 @@ func init() {
DBVersion, _, _ = sqlite3.Version()
}
// Config represents the configuration of the SQLite database.
type Config struct {
DSN string // Datasource name
Memory bool // In-memory database enabled?
}
// NewConfig returns an instance of Config for the database at path.
func NewConfig() *Config {
return &Config{}
}
// FQDSN returns the fully-qualified datasource name.
func (c *Config) FQDSN(path string) string {
if c.DSN != "" {
return fmt.Sprintf("file:%s?%s", path, c.DSN)
}
return path
}
// DB is the SQL database.
type DB struct {
sqlite3conn *sqlite3.SQLiteConn // Driver connection to database.
path string // Path to database file.
dsn string // DSN, if any.
memory bool // In-memory only.
}
// Result represents the outcome of an operation that changes rows.
@ -61,8 +46,32 @@ type Rows struct {
Time float64 `json:"time,omitempty"`
}
// Open an existing database, creating it if it does not exist.
// Open opens a file-based database, creating it if it does not exist.
func Open(dbPath string) (*DB, error) {
return open(fqdsn(dbPath, ""))
}
// OpenwithDSN opens a file-based database, creating it if it does not exist.
func OpenWithDSN(dbPath, dsn string) (*DB, error) {
return open(fqdsn(dbPath, dsn))
}
// OpenInMemory opens an in-memory database.
func OpenInMemory() (*DB, error) {
return open(fqdsn(":memory:", ""))
}
// OpenInMemoryWithDSN opens an in-memory database with a specific DSN.
func OpenInMemoryWithDSN(dsn string) (*DB, error) {
return open(fqdsn(":memory:", dsn))
}
// Close closes the underlying database connection.
func (db *DB) Close() error {
return db.sqlite3conn.Close()
}
func open(dbPath string) (*DB, error) {
d := sqlite3.SQLiteDriver{}
dbc, err := d.Open(dbPath)
if err != nil {
@ -75,16 +84,6 @@ func Open(dbPath string) (*DB, error) {
}, nil
}
// OpenWithConfiguration an existing database, creating it if it does not exist.
func OpenWithConfiguration(dbPath string, conf *Config) (*DB, error) {
return Open(conf.FQDSN(dbPath))
}
// Close closes the underlying database connection.
func (db *DB) Close() error {
return db.sqlite3conn.Close()
}
// Execute executes queries that modify the database.
func (db *DB) Execute(queries []string, tx, xTime bool) ([]*Result, error) {
type Execer interface {
@ -298,3 +297,11 @@ func (db *DB) Backup(path string) error {
return nil
}
// fqdsn returns the fully-qualified datasource name.
func fqdsn(path, dsn string) string {
if dsn != "" {
return fmt.Sprintf("file:%s?%s", path, dsn)
}
return path
}

@ -12,14 +12,6 @@ import (
* Lowest-layer database tests
*/
func Test_Config(t *testing.T) {
c := NewConfig()
c.DSN = "cache=shared&mode=memory"
if c.FQDSN("/foo/bar/db.sqlite") != "file:/foo/bar/db.sqlite?cache=shared&mode=memory" {
t.Fatalf("Fully qualified DSN not correct, got: %s", c.FQDSN("/foo/bar/db.sqlite"))
}
}
func Test_DbFileCreation(t *testing.T) {
dir, err := ioutil.TempDir("", "rqlite-test-")
defer os.RemoveAll(dir)

Loading…
Cancel
Save