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
c519eeef
Commit
c519eeef
authored
Mar 04, 2012
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stmt cache plugged but current default size is zero.
parent
e0a89fb2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
12 deletions
+61
-12
cache.go
cache.go
+11
-11
cache_test.go
cache_test.go
+29
-0
sqlite.go
sqlite.go
+1
-1
stmt.go
stmt.go
+20
-0
No files found.
cache.go
View file @
c519eeef
...
...
@@ -15,24 +15,24 @@ const (
)
// Like http://www.sqlite.org/tclsqlite.html#cache
type
C
ache
struct
{
type
c
ache
struct
{
m
sync
.
Mutex
l
*
list
.
List
maxSize
int
// Cache turned off when maxSize <= 0
}
func
newCache
()
*
C
ache
{
func
newCache
()
*
c
ache
{
return
newCacheSize
(
defaultCacheSize
)
}
func
newCacheSize
(
maxSize
int
)
*
C
ache
{
func
newCacheSize
(
maxSize
int
)
*
c
ache
{
if
maxSize
<=
0
{
return
&
C
ache
{
maxSize
:
maxSize
}
return
&
c
ache
{
maxSize
:
maxSize
}
}
return
&
C
ache
{
l
:
list
.
New
(),
maxSize
:
maxSize
}
return
&
c
ache
{
l
:
list
.
New
(),
maxSize
:
maxSize
}
}
// T
ODO To be called in Conn#
Prepare
func
(
c
*
C
ache
)
find
(
sql
string
)
*
Stmt
{
// T
o be called in Conn#CacheOr
Prepare
func
(
c
*
c
ache
)
find
(
sql
string
)
*
Stmt
{
if
c
.
maxSize
<=
0
{
return
nil
}
...
...
@@ -50,8 +50,8 @@ func (c *Cache) find(sql string) *Stmt {
}
// To be called in Stmt#Finalize
func
(
c
*
C
ache
)
release
(
s
*
Stmt
)
error
{
if
c
.
maxSize
<=
0
||
len
(
s
.
tail
)
>
0
{
func
(
c
*
c
ache
)
release
(
s
*
Stmt
)
error
{
if
c
.
maxSize
<=
0
||
len
(
s
.
tail
)
>
0
||
s
.
Busy
()
{
return
s
.
finalize
()
}
c
.
m
.
Lock
()
...
...
@@ -67,8 +67,8 @@ func (c *Cache) release(s *Stmt) error {
}
// Finalize and free the cached prepared statements
//
(To be called in Conn#Close)
func
(
c
*
C
ache
)
flush
()
{
//
To be called in Conn#Close
func
(
c
*
c
ache
)
flush
()
{
if
c
.
maxSize
<=
0
{
return
}
...
...
cache_test.go
0 → 100644
View file @
c519eeef
package
sqlite_test
import
(
// . "github.com/gwenn/gosqlite"
"testing"
)
func
TestDisabledCache
(
t
*
testing
.
T
)
{
db
:=
open
(
t
)
defer
db
.
Close
()
db
.
SetCacheSize
(
0
)
if
size
,
maxSize
:=
db
.
CacheSize
();
size
!=
0
||
maxSize
!=
0
{
t
.
Errorf
(
"%d <> %d || %d <> %d"
,
0
,
size
,
0
,
maxSize
)
}
s
,
err
:=
db
.
CacheOrPrepare
(
"SELECT 1"
)
checkNoError
(
t
,
err
,
"couldn't prepare stmt: %#v"
)
if
!
s
.
Cacheable
{
t
.
Error
(
"expected cacheable stmt"
)
}
err
=
s
.
Finalize
()
checkNoError
(
t
,
err
,
"couldn't finalize stmt: %#v"
)
if
size
,
maxSize
:=
db
.
CacheSize
();
size
!=
0
||
maxSize
!=
0
{
t
.
Errorf
(
"%d <> %d || %d <> %d"
,
0
,
size
,
0
,
maxSize
)
}
}
sqlite.go
View file @
c519eeef
...
...
@@ -184,7 +184,7 @@ func (c *Conn) LastError() error {
type
Conn
struct
{
db
*
C
.
sqlite3
Filename
string
stmtCache
*
C
ache
stmtCache
*
c
ache
authorizer
*
sqliteAuthorizer
busyHandler
*
sqliteBusyHandler
profile
*
sqliteProfile
...
...
stmt.go
View file @
c519eeef
...
...
@@ -118,6 +118,26 @@ 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
)
{
s
:=
c
.
stmtCache
.
find
(
cmd
)
if
s
!=
nil
{
if
len
(
args
)
>
0
{
err
:=
s
.
Bind
(
args
...
)
if
err
!=
nil
{
s
.
finalize
()
// don't put it back in the cache
return
nil
,
err
}
}
return
s
,
nil
}
s
,
err
:=
c
.
Prepare
(
cmd
,
args
...
)
if
s
!=
nil
{
s
.
Cacheable
=
true
}
return
s
,
err
}
// One-step statement execution.
// Don't use it with SELECT or anything that returns data.
// The Stmt is reset at each call.
...
...
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