Skip to content

Commit 5349436

Browse files
authored
Merge pull request #440 from t2y/add-sqlite3-limit
Support sqlite3_limit to get/set the value of run-time limits
2 parents 615c193 + 9dddfd1 commit 5349436

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

_example/limit/limit.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
"log"
7+
"os"
8+
"strings"
9+
10+
"github.com/mattn/go-sqlite3"
11+
)
12+
13+
func createBulkInsertQuery(n int, start int) (query string, args []interface{}) {
14+
values := make([]string, n)
15+
args = make([]interface{}, n*2)
16+
pos := 0
17+
for i := 0; i < n; i++ {
18+
values[i] = "(?, ?)"
19+
args[pos] = start + i
20+
args[pos+1] = fmt.Sprintf("こんにちわ世界%03d", i)
21+
pos += 2
22+
}
23+
query = fmt.Sprintf(
24+
"insert into foo(id, name) values %s",
25+
strings.Join(values, ", "),
26+
)
27+
return
28+
}
29+
30+
func bukInsert(db *sql.DB, query string, args []interface{}) (err error) {
31+
stmt, err := db.Prepare(query)
32+
if err != nil {
33+
return
34+
}
35+
36+
_, err = stmt.Exec(args...)
37+
if err != nil {
38+
return
39+
}
40+
41+
return
42+
}
43+
44+
func main() {
45+
var sqlite3conn *sqlite3.SQLiteConn
46+
sql.Register("sqlite3_with_limit", &sqlite3.SQLiteDriver{
47+
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
48+
sqlite3conn = conn
49+
return nil
50+
},
51+
})
52+
53+
os.Remove("./foo.db")
54+
db, err := sql.Open("sqlite3_with_limit", "./foo.db")
55+
if err != nil {
56+
log.Fatal(err)
57+
}
58+
defer db.Close()
59+
60+
sqlStmt := `
61+
create table foo (id integer not null primary key, name text);
62+
delete from foo;
63+
`
64+
_, err = db.Exec(sqlStmt)
65+
if err != nil {
66+
log.Printf("%q: %s\n", err, sqlStmt)
67+
return
68+
}
69+
70+
if sqlite3conn == nil {
71+
log.Fatal("not set sqlite3 connection")
72+
}
73+
74+
limitVariableNumber := sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
75+
log.Printf("default SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)
76+
77+
num := 400
78+
query, args := createBulkInsertQuery(num, 0)
79+
err = bukInsert(db, query, args)
80+
if err != nil {
81+
log.Fatal(err)
82+
}
83+
84+
smallLimitVariableNumber := 100
85+
sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, smallLimitVariableNumber)
86+
87+
limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
88+
log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)
89+
90+
query, args = createBulkInsertQuery(num, num)
91+
err = bukInsert(db, query, args)
92+
if err != nil {
93+
if err != nil {
94+
log.Printf("expect failed since SQLITE_LIMIT_VARIABLE_NUMBER is too small: %v", err)
95+
}
96+
}
97+
98+
bigLimitVariableNumber := 999999
99+
sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, bigLimitVariableNumber)
100+
limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
101+
log.Printf("set SQLITE_LIMIT_VARIABLE_NUMBER: %d", bigLimitVariableNumber)
102+
log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)
103+
104+
query, args = createBulkInsertQuery(500, num+num)
105+
err = bukInsert(db, query, args)
106+
if err != nil {
107+
if err != nil {
108+
log.Fatal(err)
109+
}
110+
}
111+
112+
log.Println("no error if SQLITE_LIMIT_VARIABLE_NUMBER > 999")
113+
}

sqlite3.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ int compareTrampoline(void*, int, char*, int, char*);
105105
int commitHookTrampoline(void*);
106106
void rollbackHookTrampoline(void*);
107107
void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64);
108+
109+
#ifdef SQLITE_LIMIT_WORKER_THREADS
110+
# define _SQLITE_HAS_LIMIT
111+
# define SQLITE_LIMIT_LENGTH 0
112+
# define SQLITE_LIMIT_SQL_LENGTH 1
113+
# define SQLITE_LIMIT_COLUMN 2
114+
# define SQLITE_LIMIT_EXPR_DEPTH 3
115+
# define SQLITE_LIMIT_COMPOUND_SELECT 4
116+
# define SQLITE_LIMIT_VDBE_OP 5
117+
# define SQLITE_LIMIT_FUNCTION_ARG 6
118+
# define SQLITE_LIMIT_ATTACHED 7
119+
# define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
120+
# define SQLITE_LIMIT_VARIABLE_NUMBER 9
121+
# define SQLITE_LIMIT_TRIGGER_DEPTH 10
122+
# define SQLITE_LIMIT_WORKER_THREADS 11
123+
# else
124+
# define SQLITE_LIMIT_WORKER_THREADS 11
125+
#endif
126+
127+
static int _sqlite3_limit(sqlite3* db, int limitId, int newLimit) {
128+
#ifndef _SQLITE_HAS_LIMIT
129+
return -1;
130+
#else
131+
return sqlite3_limit(db, limitId, newLimit);
132+
#endif
133+
}
108134
*/
109135
import "C"
110136
import (
@@ -827,6 +853,36 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
827853
return ss, nil
828854
}
829855

856+
// Run-Time Limit Categories.
857+
// See: http://www.sqlite.org/c3ref/c_limit_attached.html
858+
const (
859+
SQLITE_LIMIT_LENGTH = C.SQLITE_LIMIT_LENGTH
860+
SQLITE_LIMIT_SQL_LENGTH = C.SQLITE_LIMIT_SQL_LENGTH
861+
SQLITE_LIMIT_COLUMN = C.SQLITE_LIMIT_COLUMN
862+
SQLITE_LIMIT_EXPR_DEPTH = C.SQLITE_LIMIT_EXPR_DEPTH
863+
SQLITE_LIMIT_COMPOUND_SELECT = C.SQLITE_LIMIT_COMPOUND_SELECT
864+
SQLITE_LIMIT_VDBE_OP = C.SQLITE_LIMIT_VDBE_OP
865+
SQLITE_LIMIT_FUNCTION_ARG = C.SQLITE_LIMIT_FUNCTION_ARG
866+
SQLITE_LIMIT_ATTACHED = C.SQLITE_LIMIT_ATTACHED
867+
SQLITE_LIMIT_LIKE_PATTERN_LENGTH = C.SQLITE_LIMIT_LIKE_PATTERN_LENGTH
868+
SQLITE_LIMIT_VARIABLE_NUMBER = C.SQLITE_LIMIT_VARIABLE_NUMBER
869+
SQLITE_LIMIT_TRIGGER_DEPTH = C.SQLITE_LIMIT_TRIGGER_DEPTH
870+
SQLITE_LIMIT_WORKER_THREADS = C.SQLITE_LIMIT_WORKER_THREADS
871+
)
872+
873+
// GetLimit returns the current value of a run-time limit.
874+
// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
875+
func (c *SQLiteConn) GetLimit(id int) int {
876+
return int(C._sqlite3_limit(c.db, C.int(id), -1))
877+
}
878+
879+
// SetLimit changes the value of a run-time limits.
880+
// Then this method returns the prior value of the limit.
881+
// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
882+
func (c *SQLiteConn) SetLimit(id int, newVal int) int {
883+
return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
884+
}
885+
830886
// Close the statement.
831887
func (s *SQLiteStmt) Close() error {
832888
s.mu.Lock()

0 commit comments

Comments
 (0)