driver

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2015 License: BSD-3-Clause, Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package driver registers a QL sql/driver named "ql" 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

	// 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.

Example (TestFile)
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
Example (TestMem)
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

Jump to

Keyboard shortcuts

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