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
4a0bef16
Commit
4a0bef16
authored
Mar 04, 2012
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename CacheOrPrepare to Prepare.
parent
cc56fa03
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
31 additions
and
31 deletions
+31
-31
README
README
+1
-1
cache.go
cache.go
+1
-1
cache_test.go
cache_test.go
+5
-5
driver.go
driver.go
+1
-1
meta.go
meta.go
+6
-6
sqlite.go
sqlite.go
+4
-4
stmt.go
stmt.go
+13
-13
No files found.
README
View file @
4a0bef16
...
...
@@ -6,7 +6,7 @@ This binding implements the "database/sql/driver" interface.
Open supports flags.
Conn#Exec handles multiple statements (separated by semicolons) properly.
Conn#Prepare can optionnaly #Bind as well.
Conn#
CacheOrPrepare can reused
already prepared Stmt.
Conn#
Prepare can reuse
already prepared Stmt.
Conn#Close ensures that all dangling statements are finalized.
Stmt#Exec is renamed in Stmt#Bind and a new Stmt#Exec method is introduced to #Bind and #Step.
Stmt#Bind uses native sqlite3_bind_x methods and failed if unsupported type.
...
...
cache.go
View file @
4a0bef16
...
...
@@ -31,7 +31,7 @@ func newCacheSize(maxSize int) *cache {
return
&
cache
{
l
:
list
.
New
(),
maxSize
:
maxSize
}
}
// To be called in Conn#
CacheOr
Prepare
// To be called in Conn#Prepare
func
(
c
*
cache
)
find
(
sql
string
)
*
Stmt
{
if
c
.
maxSize
<=
0
{
return
nil
...
...
cache_test.go
View file @
4a0bef16
...
...
@@ -18,7 +18,7 @@ func TestDisabledCache(t *testing.T) {
db
.
SetCacheSize
(
0
)
checkCacheSize
(
t
,
db
,
0
,
0
)
s
,
err
:=
db
.
CacheOr
Prepare
(
"SELECT 1"
)
s
,
err
:=
db
.
Prepare
(
"SELECT 1"
)
checkNoError
(
t
,
err
,
"couldn't prepare stmt: %#v"
)
if
!
s
.
Cacheable
{
t
.
Error
(
"expected cacheable stmt"
)
...
...
@@ -36,7 +36,7 @@ func TestEnabledCache(t *testing.T) {
db
.
SetCacheSize
(
10
)
checkCacheSize
(
t
,
db
,
0
,
10
)
s
,
err
:=
db
.
CacheOr
Prepare
(
"SELECT 1"
)
s
,
err
:=
db
.
Prepare
(
"SELECT 1"
)
checkNoError
(
t
,
err
,
"couldn't prepare stmt: %#v"
)
if
!
s
.
Cacheable
{
t
.
Error
(
"expected cacheable stmt"
)
...
...
@@ -46,7 +46,7 @@ func TestEnabledCache(t *testing.T) {
checkNoError
(
t
,
err
,
"couldn't finalize stmt: %#v"
)
checkCacheSize
(
t
,
db
,
1
,
10
)
ns
,
err
:=
db
.
CacheOr
Prepare
(
"SELECT 1"
)
ns
,
err
:=
db
.
Prepare
(
"SELECT 1"
)
checkNoError
(
t
,
err
,
"couldn't prepare stmt: %#v"
)
checkCacheSize
(
t
,
db
,
0
,
10
)
...
...
@@ -65,7 +65,7 @@ func BenchmarkDisabledCache(b *testing.B) {
b
.
StartTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
s
,
_
:=
db
.
CacheOr
Prepare
(
"SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71"
)
s
,
_
:=
db
.
Prepare
(
"SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71"
)
s
.
Finalize
()
}
...
...
@@ -81,7 +81,7 @@ func BenchmarkEnabledCache(b *testing.B) {
b
.
StartTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
s
,
_
:=
db
.
CacheOr
Prepare
(
"SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71"
)
s
,
_
:=
db
.
Prepare
(
"SELECT 1, 'test', 3.14 UNION SELECT 2, 'exp', 2.71"
)
s
.
Finalize
()
}
...
...
driver.go
View file @
4a0bef16
...
...
@@ -56,7 +56,7 @@ func (c *connImpl) RowsAffected() (int64, error) {
}
func
(
c
*
connImpl
)
Prepare
(
query
string
)
(
driver
.
Stmt
,
error
)
{
s
,
err
:=
c
.
c
.
CacheOr
Prepare
(
query
)
s
,
err
:=
c
.
c
.
Prepare
(
query
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
meta.go
View file @
4a0bef16
...
...
@@ -36,7 +36,7 @@ import "unsafe"
// Executes pragma 'database_list'
func
(
c
*
Conn
)
Databases
()
(
map
[
string
]
string
,
error
)
{
s
,
err
:=
c
.
P
repare
(
"PRAGMA database_list"
)
s
,
err
:=
c
.
p
repare
(
"PRAGMA database_list"
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -59,7 +59,7 @@ func (c *Conn) Databases() (map[string]string, error) {
// Selects tables (no view) from 'sqlite_master' and filters system tables out.
// TODO Make possible to specified the database name (main.sqlite_master)
func
(
c
*
Conn
)
Tables
()
([]
string
,
error
)
{
s
,
err
:=
c
.
P
repare
(
"SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'"
)
s
,
err
:=
c
.
p
repare
(
"SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'"
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -91,7 +91,7 @@ type Column struct {
// Executes pragma 'table_info'
// TODO Make possible to specify the database-name (PRAGMA %Q.table_info(%Q))
func
(
c
*
Conn
)
Columns
(
table
string
)
([]
Column
,
error
)
{
s
,
err
:=
c
.
P
repare
(
Mprintf
(
"PRAGMA table_info(%Q)"
,
table
))
s
,
err
:=
c
.
p
repare
(
Mprintf
(
"PRAGMA table_info(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -144,7 +144,7 @@ type ForeignKey struct {
// Executes pragma 'foreign_key_list'
// TODO Make possible to specify the database-name (PRAGMA %Q.foreign_key_list(%Q))
func
(
c
*
Conn
)
ForeignKeys
(
table
string
)
(
map
[
int
]
*
ForeignKey
,
error
)
{
s
,
err
:=
c
.
P
repare
(
Mprintf
(
"PRAGMA foreign_key_list(%Q)"
,
table
))
s
,
err
:=
c
.
p
repare
(
Mprintf
(
"PRAGMA foreign_key_list(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -181,7 +181,7 @@ type Index struct {
// Executes pragma 'index_list'
// TODO Make possible to specify the database-name (PRAGMA %Q.index_list(%Q))
func
(
c
*
Conn
)
Indexes
(
table
string
)
([]
Index
,
error
)
{
s
,
err
:=
c
.
P
repare
(
Mprintf
(
"PRAGMA index_list(%Q)"
,
table
))
s
,
err
:=
c
.
p
repare
(
Mprintf
(
"PRAGMA index_list(%Q)"
,
table
))
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -204,7 +204,7 @@ func (c *Conn) Indexes(table string) ([]Index, error) {
// Executes pragma 'index_info'
// Only Column.Cid and Column.Name are specified. All other fields are unspecifed.
func
(
c
*
Conn
)
IndexColumns
(
index
string
)
([]
Column
,
error
)
{
s
,
err
:=
c
.
P
repare
(
Mprintf
(
"PRAGMA index_info(%Q)"
,
index
))
s
,
err
:=
c
.
p
repare
(
Mprintf
(
"PRAGMA index_info(%Q)"
,
index
))
if
err
!=
nil
{
return
nil
,
err
}
...
...
sqlite.go
View file @
4a0bef16
...
...
@@ -313,7 +313,7 @@ func (c *Conn) EnableExtendedResultCodes(b bool) error {
//
func
(
c
*
Conn
)
Exec
(
cmd
string
,
args
...
interface
{})
error
{
for
len
(
cmd
)
>
0
{
s
,
err
:=
c
.
P
repare
(
cmd
)
s
,
err
:=
c
.
p
repare
(
cmd
)
if
err
!=
nil
{
return
err
}
else
if
s
.
stmt
==
nil
{
...
...
@@ -345,7 +345,7 @@ func (c *Conn) Exec(cmd string, args ...interface{}) error {
// Return true if the specified query returns at least one row.
func
(
c
*
Conn
)
Exists
(
query
string
,
args
...
interface
{})
(
bool
,
error
)
{
s
,
err
:=
c
.
CacheOr
Prepare
(
query
,
args
...
)
s
,
err
:=
c
.
Prepare
(
query
,
args
...
)
if
err
!=
nil
{
return
false
,
err
}
...
...
@@ -359,7 +359,7 @@ func (c *Conn) Exists(query string, args ...interface{}) (bool, error) {
// Use it with SELECT that returns only one row with only one column.
// Returns io.EOF when there is no row.
func
(
c
*
Conn
)
OneValue
(
query
string
,
value
interface
{},
args
...
interface
{})
error
{
s
,
err
:=
c
.
CacheOr
Prepare
(
query
,
args
...
)
s
,
err
:=
c
.
Prepare
(
query
,
args
...
)
if
err
!=
nil
{
return
err
}
...
...
@@ -447,7 +447,7 @@ func (c *Conn) Rollback() error {
}
func
(
c
*
Conn
)
exec
(
cmd
string
)
error
{
s
,
err
:=
c
.
P
repare
(
cmd
)
s
,
err
:=
c
.
p
repare
(
cmd
)
if
err
!=
nil
{
return
err
}
...
...
stmt.go
View file @
4a0bef16
...
...
@@ -82,16 +82,7 @@ type Stmt struct {
Cacheable
bool
}
// Compile an SQL statement and optionally bind values.
// Example:
// stmt, err := db.Prepare("SELECT 1 where 1 = ?", 1)
// if err != nil {
// ...
// }
// defer stmt.Finalize()
//
// (See sqlite3_prepare_v2: http://sqlite.org/c3ref/prepare.html)
func
(
c
*
Conn
)
Prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
error
)
{
func
(
c
*
Conn
)
prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
error
)
{
if
c
==
nil
{
return
nil
,
errors
.
New
(
"nil sqlite database"
)
}
...
...
@@ -118,8 +109,17 @@ func (c *Conn) Prepare(cmd string, args ...interface{}) (*Stmt, error) {
return
s
,
nil
}
// Like Prepare but first look in the Stmt cache.
func
(
c
*
Conn
)
CacheOrPrepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
error
)
{
// First look in the statement cache or compile the SQL statement.
// And optionally bind values.
// Example:
// stmt, err := db.Prepare("SELECT 1 where 1 = ?", 1)
// if err != nil {
// ...
// }
// defer stmt.Finalize()
//
// (See sqlite3_prepare_v2: http://sqlite.org/c3ref/prepare.html)
func
(
c
*
Conn
)
Prepare
(
cmd
string
,
args
...
interface
{})
(
*
Stmt
,
error
)
{
s
:=
c
.
stmtCache
.
find
(
cmd
)
if
s
!=
nil
{
if
len
(
args
)
>
0
{
...
...
@@ -131,7 +131,7 @@ func (c *Conn) CacheOrPrepare(cmd string, args ...interface{}) (*Stmt, error) {
}
return
s
,
nil
}
s
,
err
:=
c
.
P
repare
(
cmd
,
args
...
)
s
,
err
:=
c
.
p
repare
(
cmd
,
args
...
)
if
s
!=
nil
{
s
.
Cacheable
=
true
}
...
...
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