Commit 4a0bef16 authored by gwenn's avatar gwenn

Rename CacheOrPrepare to Prepare.

parent cc56fa03
......@@ -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.
......
......@@ -31,7 +31,7 @@ func newCacheSize(maxSize int) *cache {
return &cache{l: list.New(), maxSize: maxSize}
}
// To be called in Conn#CacheOrPrepare
// To be called in Conn#Prepare
func (c *cache) find(sql string) *Stmt {
if c.maxSize <= 0 {
return nil
......
......@@ -18,7 +18,7 @@ func TestDisabledCache(t *testing.T) {
db.SetCacheSize(0)
checkCacheSize(t, db, 0, 0)
s, err := db.CacheOrPrepare("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.CacheOrPrepare("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.CacheOrPrepare("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.CacheOrPrepare("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.CacheOrPrepare("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()
}
......
......@@ -56,7 +56,7 @@ func (c *connImpl) RowsAffected() (int64, error) {
}
func (c *connImpl) Prepare(query string) (driver.Stmt, error) {
s, err := c.c.CacheOrPrepare(query)
s, err := c.c.Prepare(query)
if err != nil {
return nil, err
}
......
......@@ -36,7 +36,7 @@ import "unsafe"
// Executes pragma 'database_list'
func (c *Conn) Databases() (map[string]string, error) {
s, err := c.Prepare("PRAGMA database_list")
s, err := c.prepare("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.Prepare("SELECT name FROM sqlite_master WHERE type IN ('table') AND name NOT LIKE 'sqlite_%'")
s, err := c.prepare("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.Prepare(Mprintf("PRAGMA table_info(%Q)", table))
s, err := c.prepare(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.Prepare(Mprintf("PRAGMA foreign_key_list(%Q)", table))
s, err := c.prepare(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.Prepare(Mprintf("PRAGMA index_list(%Q)", table))
s, err := c.prepare(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.Prepare(Mprintf("PRAGMA index_info(%Q)", index))
s, err := c.prepare(Mprintf("PRAGMA index_info(%Q)", index))
if err != nil {
return nil, err
}
......
......@@ -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.Prepare(cmd)
s, err := c.prepare(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.CacheOrPrepare(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.CacheOrPrepare(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.Prepare(cmd)
s, err := c.prepare(cmd)
if err != nil {
return err
}
......
......@@ -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.Prepare(cmd, args...)
s, err := c.prepare(cmd, args...)
if s != nil {
s.Cacheable = true
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment