Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gosqlite
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gosqlite
Commits
fc80494a
Commit
fc80494a
authored
Nov 26, 2011
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add examples.
parent
6bcd838f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
87 deletions
+64
-87
example_test.go
example_test.go
+0
-81
meta.go
meta.go
+3
-0
sqlite.go
sqlite.go
+52
-5
trace.go
trace.go
+9
-1
No files found.
example_test.go
deleted
100644 → 0
View file @
6bcd838f
package
sqlite_test
import
(
"fmt"
"github.com/gwenn/gosqlite"
)
// 0
func
ExampleOpen
()
{
db
,
err
:=
sqlite
.
Open
(
":memory:"
)
if
err
!=
nil
{
panic
(
err
)
}
defer
db
.
Close
()
fmt
.
Printf
(
"%d
\n
"
,
db
.
TotalChanges
())
}
/*
// <nil>
func ExampleExec() {
db, _ := sqlite.Open(":memory:")
defer db.Close()
err := db.Exec("CREATE TABLE test(id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL)")
fmt.Println(err)
}
*/
// true <nil>
func
ExamplePrepare
()
{
db
,
_
:=
sqlite
.
Open
(
":memory:"
)
defer
db
.
Close
()
stmt
,
err
:=
db
.
Prepare
(
"SELECT 1 where 1 = ?"
,
1
)
if
err
!=
nil
{
panic
(
err
)
}
defer
stmt
.
Finalize
()
fmt
.
Println
(
stmt
.
Next
())
}
// OK
func
ExampleNext
()
{
db
,
_
:=
sqlite
.
Open
(
":memory:"
)
defer
db
.
Close
()
stmt
,
err
:=
db
.
Prepare
(
"SELECT 1, 'test'"
)
defer
stmt
.
Finalize
()
var
ok
bool
for
ok
,
err
=
stmt
.
Next
();
ok
;
ok
,
err
=
stmt
.
Next
()
{
fmt
.
Println
(
"OK"
)
}
if
err
!=
nil
{
panic
(
err
)
}
}
// 1 test
func
ExampleScan
()
{
db
,
_
:=
sqlite
.
Open
(
":memory:"
)
defer
db
.
Close
()
stmt
,
_
:=
db
.
Prepare
(
"SELECT 1, 'test'"
)
defer
stmt
.
Finalize
()
var
id
int
var
name
string
for
sqlite
.
Must
(
stmt
.
Next
())
{
stmt
.
Scan
(
&
id
,
&
name
)
fmt
.
Println
(
id
,
name
)
}
}
// 1 test
func
ExampleNamedScan
()
{
db
,
_
:=
sqlite
.
Open
(
":memory:"
)
defer
db
.
Close
()
stmt
,
_
:=
db
.
Prepare
(
"SELECT 1 as id, 'test' as name"
)
defer
stmt
.
Finalize
()
var
id
int
var
name
string
for
sqlite
.
Must
(
stmt
.
Next
())
{
stmt
.
NamedScan
(
"name"
,
&
name
,
"id"
,
&
id
)
fmt
.
Println
(
id
,
name
)
}
}
meta.go
View file @
fc80494a
...
...
@@ -62,6 +62,7 @@ func (c *Conn) Tables() ([]string, error) {
return
tables
,
nil
}
// See Conn.Columns/IndexColumns
type
Column
struct
{
Cid
int
Name
string
...
...
@@ -96,6 +97,7 @@ func (c *Conn) Columns(table string) ([]Column, error) {
return
columns
,
nil
}
// See Conn.ForeignKeys
type
ForeignKey
struct
{
Table
string
From
[]
string
...
...
@@ -134,6 +136,7 @@ func (c *Conn) ForeignKeys(table string) (map[int]*ForeignKey, error) {
return
fks
,
nil
}
// See Conn.Indexes
type
Index
struct
{
Name
string
Unique
bool
...
...
sqlite.go
View file @
fc80494a
...
...
@@ -151,6 +151,7 @@ func Version() string {
return
C
.
GoString
(
p
)
}
// See Open
type
OpenFlag
int
const
(
...
...
@@ -166,6 +167,14 @@ const (
// ":memory:" for memory db
// "" for temp file db
//
// Example:
// db, err := sqlite.Open(":memory:")
// if err != nil {
// ...
// }
// defer db.Close()
//
// Calls sqlite3_open_v2: http://sqlite.org/c3ref/open.html
func
Open
(
filename
string
,
flags
...
OpenFlag
)
(
*
Conn
,
error
)
{
if
C
.
sqlite3_threadsafe
()
==
0
{
...
...
@@ -206,13 +215,15 @@ func (c *Conn) BusyTimeout(ms int) error {
}
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, b)
// Another way is PRAGMA foreign_keys = boolean;
// Another way is PRAGMA foreign_keys = boolean;
//
// http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html
func
(
c
*
Conn
)
EnableFKey
(
b
bool
)
(
bool
,
error
)
{
return
c
.
queryOrSetEnableFKey
(
btocint
(
b
))
}
// Calls sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_FKEY, -1)
// Another way is PRAGMA foreign_keys;
//
// http://sqlite.org/c3ref/c_dbconfig_enable_fkey.html
func
(
c
*
Conn
)
IsFKeyEnabled
()
(
bool
,
error
)
{
return
c
.
queryOrSetEnableFKey
(
-
1
)
...
...
@@ -227,6 +238,10 @@ func (c *Conn) queryOrSetEnableFKey(i C.int) (bool, error) {
}
// Don't use it with SELECT or anything that returns data.
//
// Example:
// err := db.Exec("CREATE TABLE test(id INTEGER PRIMARY KEY NOT NULL, name TEXT NOT NULL)")
//
// Calls sqlite3_prepare_v2, sqlite3_bind_*, sqlite3_step and sqlite3_finalize
// http://sqlite.org/c3ref/prepare.html, http://sqlite.org/c3ref/bind_blob.html,
// http://sqlite.org/c3ref/step.html and http://sqlite.org/c3ref/finalize.html
...
...
@@ -296,6 +311,7 @@ func (c *Conn) GetAutocommit() bool {
return
C
.
sqlite3_get_autocommit
(
c
.
db
)
!=
0
}
// See Conn.BeginTransaction
type
TransactionType
int
const
(
...
...
@@ -351,6 +367,13 @@ type Stmt struct {
CheckTypeMismatch
bool
}
// Example:
// stmt, err := db.Prepare("SELECT 1 where 1 = ?", 1)
// if err != nil {
// ...
// }
// defer stmt.Finalize()
//
// Calls sqlite3_prepare_v2 and sqlite3_bind_*
// http://sqlite.org/c3ref/prepare.html, http://sqlite.org/c3ref/bind_blob.html,
func
(
c
*
Conn
)
Prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
error
)
{
...
...
@@ -479,15 +502,16 @@ func (s *Stmt) Bind(args ...interface{}) error {
// var ok bool
// var err os.Error
// for ok, err = s.Next(); ok; ok, err = s.Next() {
//
c
s.Scan(&fnum, &inum, &sstr)
//
err =
s.Scan(&fnum, &inum, &sstr)
// }
// if err != nil {
// ...
// }
// With panic on error:
// for Must(s.Next()) {
//
c
s.Scan(&fnum, &inum, &sstr)
//
err :=
s.Scan(&fnum, &inum, &sstr)
// }
//
// Calls sqlite3_step
// http://sqlite.org/c3ref/step.html
func
(
s
*
Stmt
)
Next
()
(
bool
,
error
)
{
...
...
@@ -545,6 +569,7 @@ func (s *Stmt) ColumnNames() []string {
return
names
}
// See Stmt.ColumnType
type
Type
int
func
(
t
Type
)
String
()
string
{
...
...
@@ -575,6 +600,17 @@ func (s *Stmt) ColumnType(index int) Type {
return
Type
(
C
.
sqlite3_column_type
(
s
.
stmt
,
C
.
int
(
index
)))
}
// Example:
// stmt, err := db.Prepare("SELECT 1 as id, 'test' as name")
// defer stmt.Finalize()
// var id int
// var name string
// for sqlite.Must(stmt.Next()) {
// stmt.NamedScan("name", &name, "id", &id)
// // TODO error handling
// fmt.Println(id, name)
// }
//
// NULL value is converted to 0 if arg type is *int,*int64,*float,*float64, to "" for *string, to []byte{} for *[]byte and to false for *bool.
// Calls sqlite3_column_count, sqlite3_column_name and sqlite3_column_(blob|double|int|int64|text) depending on args type.
// http://sqlite.org/c3ref/column_blob.html
...
...
@@ -600,6 +636,17 @@ func (s *Stmt) NamedScan(args ...interface{}) error {
return
nil
}
// Example:
// stmt, err := db.Prepare("SELECT 1, 'test'")
// defer stmt.Finalize()
// var id int
// var name string
// for sqlite.Must(stmt.Next()) {
// err = stmt.Scan(&id, &name)
// // TODO error handling
// fmt.Println(id, name)
// }
//
// NULL value is converted to 0 if arg type is *int,*int64,*float,*float64, to "" for *string, to []byte{} for *[]byte and to false for *bool.
// TODO How to avoid NULL conversion?
// Calls sqlite3_column_count and sqlite3_column_(blob|double|int|int64|text) depending on args type.
...
...
@@ -644,7 +691,7 @@ func (s *Stmt) ColumnIndex(name string) (int, error) {
// Set nullable to false to skip NULL type test.
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_count, sqlite3_column_name and sqlite3_column_(blob|double|int|int64|text) depending on arg
s
type.
// Calls sqlite3_column_count, sqlite3_column_name and sqlite3_column_(blob|double|int|int64|text) depending on arg type.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
NamedScanColumn
(
name
string
,
value
interface
{})
(
bool
,
error
)
{
index
,
err
:=
s
.
ColumnIndex
(
name
)
...
...
@@ -656,7 +703,7 @@ func (s *Stmt) NamedScanColumn(name string, value interface{}) (bool, error) {
// The leftmost column/index is number 0.
// Returns true when column is null and Stmt.CheckNull is activated.
// Calls sqlite3_column_(blob|double|int|int64|text) depending on arg
s
type.
// Calls sqlite3_column_(blob|double|int|int64|text) depending on arg type.
// http://sqlite.org/c3ref/column_blob.html
func
(
s
*
Stmt
)
ScanColumn
(
index
int
,
value
interface
{})
(
bool
,
error
)
{
var
isNull
bool
...
...
trace.go
View file @
fc80494a
...
...
@@ -58,6 +58,7 @@ import "C"
import
"unsafe"
// See Conn.Trace
type
Tracer
func
(
udp
interface
{},
sql
string
)
type
sqliteTrace
struct
{
...
...
@@ -83,6 +84,7 @@ func (c *Conn) Trace(f Tracer, udp interface{}) {
C
.
goSqlite3Trace
(
c
.
db
,
unsafe
.
Pointer
(
c
.
trace
))
}
// See Conn.Profile
type
Profiler
func
(
udp
interface
{},
sql
string
,
nanoseconds
uint64
)
type
sqliteProfile
struct
{
...
...
@@ -108,6 +110,7 @@ func (c *Conn) Profile(f Profiler, udp interface{}) {
C
.
goSqlite3Profile
(
c
.
db
,
unsafe
.
Pointer
(
c
.
profile
))
}
// See Authorizer
type
Auth
int
const
(
...
...
@@ -116,6 +119,7 @@ const (
AUTH_IGNORE
Auth
=
C
.
SQLITE_IGNORE
)
// See Authorizer
type
Action
int
const
(
...
...
@@ -154,6 +158,7 @@ const (
COPY
Action
=
C
.
SQLITE_COPY
)
// See Conn.SetAuthorizer
type
Authorizer
func
(
udp
interface
{},
action
Action
,
arg1
,
arg2
,
dbName
,
triggerName
string
)
Auth
type
sqliteAuthorizer
struct
{
...
...
@@ -179,6 +184,7 @@ func (c *Conn) SetAuthorizer(f Authorizer, udp interface{}) error {
return
c
.
error
(
C
.
goSqlite3SetAuthorizer
(
c
.
db
,
unsafe
.
Pointer
(
c
.
authorizer
)))
}
// See Conn.BusyHandler
type
BusyHandler
func
(
udp
interface
{},
count
int
)
int
type
sqliteBusyHandler
struct
{
...
...
@@ -206,6 +212,7 @@ func (c *Conn) BusyHandler(f BusyHandler, udp interface{}) error {
}
// Returns non-zero to interrupt.
// See Conn.ProgressHandler
type
ProgressHandler
func
(
udp
interface
{})
int
type
sqliteProgressHandler
struct
{
...
...
@@ -232,6 +239,7 @@ func (c *Conn) ProgressHandler(f ProgressHandler, numOps int, udp interface{}) {
C
.
goSqlite3ProgressHandler
(
c
.
db
,
C
.
int
(
numOps
),
unsafe
.
Pointer
(
c
.
progressHandler
))
}
// See Stmt.Status
type
StmtStatus
int
const
(
...
...
@@ -277,7 +285,7 @@ func Log(err /*Errno*/ int, msg string) {
C
.
my_log
(
C
.
int
(
err
),
cs
)
}
//
The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a function with a call signature of void(*)(void*,int,const char*), and a pointer to void.
//
See ConfigLog
type
Logger
func
(
udp
interface
{},
err
error
,
msg
string
)
type
sqliteLogger
struct
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment