ql: github.com/cznic/ql/driver Index | Examples | Files

package driver

import "github.com/cznic/ql/driver"

Package driver registers QL sql/drivers named "ql", "ql2" and a memory driver named "ql-mem".

See also [0], [1] and [3].

Usage

A skeleton program using ql/driver.

package main

import (
	"database/sql"

	_ "github.com/cznic/ql/driver"
)

func main() {
	...
	// Disk file DB
	db, err := sql.Open("ql", "ql.db")  // [2]
	// alternatively
	db, err := sql.Open("ql", "file://ql.db")

	// and/or

	// Disk file DB using V2 format
	db, err := sql.Open("ql2", "ql.db")
	// alternatively
	db, err := sql.Open("ql2", "file://ql.db")

	// and/or

	// RAM DB
	mdb, err := sql.Open("ql-mem", "mem.db")
	// alternatively
	mdb, err := sql.Open("ql", "memory://mem.db")
	if err != nil {
		log.Fatal(err)
	}

	// Use db/mdb here
	...
}

This package exports nothing.

Referenced from above:

[0]: http://godoc.org/github.com/cznic/ql
[1]: http://golang.org/pkg/database/sql/
[2]: http://golang.org/pkg/database/sql/#Open
[3]: http://golang.org/pkg/database/sql/driver

Code:

dir, err := ioutil.TempDir("", "ql-driver-test")
if err != nil {
    return
}

defer func() {
    os.RemoveAll(dir)
}()

db, err := sql.Open("ql", filepath.Join(dir, "ql.db"))
if err != nil {
    return
}

defer func() {
    if err := db.Close(); err != nil {
        return
    }

    fmt.Println("OK")
}()

tx, err := db.Begin()
if err != nil {
    return
}

if _, err := tx.Exec("CREATE TABLE t (Qty int, Name string);"); err != nil {
    return
}

result, err := tx.Exec(`
	INSERT INTO t VALUES
		($1, $2),
		($3, $4),
	;
	`,
    42, "foo",
    314, "bar",
)
if err != nil {
    return
}

if err = tx.Commit(); err != nil {
    return
}

id, err := result.LastInsertId()
if err != nil {
    return
}

aff, err := result.RowsAffected()
if err != nil {
    return
}

fmt.Printf("LastInsertId %d, RowsAffected %d\n", id, aff)

rows, err := db.Query("SELECT * FROM t;")
if err != nil {
    return
}

cols, err := rows.Columns()
if err != nil {
    return
}

fmt.Printf("Columns: %v\n", cols)

var data struct {
    Qty  int
    Name string
}

for rows.Next() {
    if err = rows.Scan(&data.Qty, &data.Name); err != nil {
        rows.Close()
        break
    }

    fmt.Printf("%+v\n", data)
}

if err = rows.Err(); err != nil {
    return
}

Output:

LastInsertId 2, RowsAffected 2
Columns: [Qty Name]
{Qty:314 Name:bar}
{Qty:42 Name:foo}
OK

Code:

dir, err := ioutil.TempDir("", "ql-driver-test")
if err != nil {
    return
}

defer func() {
    os.RemoveAll(dir)
}()

db, err := sql.Open("ql2", filepath.Join(dir, "ql2.db"))
if err != nil {
    return
}

defer func() {
    if err := db.Close(); err != nil {
        return
    }

    fmt.Println("OK")
}()

tx, err := db.Begin()
if err != nil {
    return
}

if _, err := tx.Exec("CREATE TABLE t (Qty int, Name string);"); err != nil {
    return
}

result, err := tx.Exec(`
	INSERT INTO t VALUES
		($1, $2),
		($3, $4),
	;
	`,
    42, "foo",
    314, "bar",
)
if err != nil {
    return
}

if err = tx.Commit(); err != nil {
    return
}

id, err := result.LastInsertId()
if err != nil {
    return
}

aff, err := result.RowsAffected()
if err != nil {
    return
}

fmt.Printf("LastInsertId %d, RowsAffected %d\n", id, aff)

rows, err := db.Query("SELECT * FROM t;")
if err != nil {
    return
}

cols, err := rows.Columns()
if err != nil {
    return
}

fmt.Printf("Columns: %v\n", cols)

var data struct {
    Qty  int
    Name string
}

for rows.Next() {
    if err = rows.Scan(&data.Qty, &data.Name); err != nil {
        rows.Close()
        break
    }

    fmt.Printf("%+v\n", data)
}

if err = rows.Err(); err != nil {
    return
}

Output:

LastInsertId 2, RowsAffected 2
Columns: [Qty Name]
{Qty:314 Name:bar}
{Qty:42 Name:foo}
OK

Code:

db, err := sql.Open("ql-mem", "mem.db")
if err != nil {
    return
}

defer func() {
    if err := db.Close(); err != nil {
        return
    }

    fmt.Println("OK")
}()

tx, err := db.Begin()
if err != nil {
    return
}

if _, err := tx.Exec("CREATE TABLE t (Qty int, Name string);"); err != nil {
    return
}

result, err := tx.Exec(`
	INSERT INTO t VALUES
		($1, $2),
		($3, $4),
	;
	`,
    1042, "foo-mem",
    1314, "bar-mem",
)
if err != nil {
    return
}

if err = tx.Commit(); err != nil {
    return
}

id, err := result.LastInsertId()
if err != nil {
    return
}

aff, err := result.RowsAffected()
if err != nil {
    return
}

fmt.Printf("LastInsertId %d, RowsAffected %d\n", id, aff)

rows, err := db.Query("SELECT * FROM t;")
if err != nil {
    return
}

cols, err := rows.Columns()
if err != nil {
    return
}

fmt.Printf("Columns: %v\n", cols)

var data struct {
    Qty  int
    Name string
}

for rows.Next() {
    if err = rows.Scan(&data.Qty, &data.Name); err != nil {
        rows.Close()
        break
    }

    fmt.Printf("%+v\n", data)
}

if err = rows.Err(); err != nil {
    return
}

Output:

LastInsertId 2, RowsAffected 2
Columns: [Qty Name]
{Qty:1314 Name:bar-mem}
{Qty:1042 Name:foo-mem}
OK

Index

Examples

Package Files

driver.go

Package driver imports 1 packages (graph) and is imported by 68 packages. Updated 2018-11-04. Refresh now. Tools for package owners.