Skip to content

Commit 5641de3

Browse files
author
brenno.rodrigues
committed
feat add cursor explain interface
1 parent 78293ef commit 5641de3

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Main.lean

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ open IO
55
def double (f : Nat -> Nat) n := f (f n)
66
def inc := fun n => n + 1
77

8+
def cursorExplain (c : Sqlite.FFI.Connection) (emode : UInt32) : IO Unit := do
9+
match ← c.prepare "select * from users limit 50;" with
10+
| Except.error e => println s!"error {e}"
11+
| Except.ok q => do
12+
let _ ← q.cursorExplain emode
13+
let c ← q.columnsCount
14+
println s!"columnsCount: {c}"
15+
for i in [0:100] do
16+
match ← q.step with
17+
| false => pure ()
18+
| true => do
19+
println s!"{i} explain: {← q.columnInt 0} {← q.columnText 1}"
20+
21+
822
def printUser (fuel : Nat) (cursor : Sqlite.FFI.Cursor) : IO Unit := do
923
if fuel = 0 then
1024
pure ()
@@ -16,7 +30,16 @@ def printUser (fuel : Nat) (cursor : Sqlite.FFI.Cursor) : IO Unit := do
1630
printUser (fuel - 1) cursor
1731

1832
def main : IO Unit := do
19-
let conn ← Sqlite.FFI.connect "test.sqlite3"
33+
println $ ← Sqlite.FFI.sqliteThreadsafe
34+
println Sqlite.FFI.Constants.SQLITE_CONFIG_SINGLETHREAD
35+
println Sqlite.FFI.Constants.SQLITE_CONFIG_MULTITHREAD
36+
println $ ← Sqlite.FFI.sqliteConfig Sqlite.FFI.Constants.SQLITE_CONFIG_MULTITHREAD
37+
let flags := Sqlite.FFI.Constants.SQLITE_OPEN_READWRITE ||| Sqlite.FFI.Constants.SQLITE_OPEN_CREATE
38+
let conn ← Sqlite.FFI.connect "test.sqlite3" flags
39+
cursorExplain conn 1
40+
match ← conn.prepare "delete from users where id = 154;" with
41+
| Except.ok c => c.step
42+
| _ => pure false
2043
match ← conn.prepare "insert into users values (?, ?);" with
2144
| Except.error e => println s!"error {e}"
2245
| Except.ok c =>
@@ -26,7 +49,7 @@ def main : IO Unit := do
2649
println "----------------------------------------------------------------"
2750
match ← conn.prepare "select count(1) from users;" with
2851
| Except.ok c =>
29-
println s!"{c.columnsCount}"
52+
println s!"{c.columnsCount}"
3053
match ← c.step with
3154
| false => println "error"
3255
| true => println "step"

Sqlite/FFI.lean

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ structure Cursor where
7171
columnsCount : IO UInt32
7272
columnText : UInt32 → IO String
7373
columnInt : UInt32 → IO Int
74+
cursorExplain : UInt32 → IO Int
7475

7576
structure Connection where
7677
path : String
@@ -112,7 +113,10 @@ private opaque cursorColumnsCount : @&RawCursor → IO UInt32
112113
private opaque cursorColumnText : @&RawCursor → UInt32 → IO String
113114

114115
@[extern "lean_sqlite_cursor_column_int"]
115-
private opaque cursorColumnInt : @&RawCursor → UInt32 → IO Int
116+
private opaque cursorColumnInt : @&RawCursor → UInt32 → IO Int32
117+
118+
@[extern "lean_sqlite_cursor_explain"]
119+
private opaque cursorExplain : @&RawCursor → UInt32 → IO Int
116120

117121
@[extern "lean_sqlite_threadsafe"]
118122
opaque sqliteThreadsafe : IO Int
@@ -129,7 +133,8 @@ private def sqlitePrepareWrap (conn : RawConn) (query : String) : IO (Except Str
129133
reset := cursorReset c,
130134
columnsCount := cursorColumnsCount c,
131135
columnText := cursorColumnText c,
132-
columnInt := cursorColumnInt c }
136+
columnInt := cursorColumnInt c,
137+
cursorExplain := cursorExplain c, }
133138
| Except.error e => Except.error e
134139

135140
def connect (s : String) (flags : UInt32) : IO Connection := do

native/ffi.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,11 @@ lean_obj_res lean_sqlite_config(int32_t config) {
194194

195195
return lean_io_result_mk_ok(lean_box(0));
196196
}
197+
198+
lean_obj_res lean_sqlite_cursor_explain(b_lean_obj_arg cursor_box, int32_t emode) {
199+
sqlite3_stmt* cursor = unbox_cursor(cursor_box);
200+
201+
const int c = sqlite3_stmt_explain(cursor, emode);
202+
203+
return lean_io_result_mk_ok(lean_box(c));
197204
}

0 commit comments

Comments
 (0)