@@ -57,11 +57,12 @@ Base.show(io::IO, db::SQLite.DB) = print(io, string("SQLite.DB(", db.file == ":m
57
57
mutable struct Stmt
58
58
db:: DB
59
59
handle:: Ptr{Cvoid}
60
+ params:: Dict{Int, Any}
60
61
61
62
function Stmt (db:: DB ,sql:: AbstractString )
62
63
handle = Ref {Ptr{Cvoid}} ()
63
64
sqliteprepare (db, sql, handle, Ref {Ptr{Cvoid}} ())
64
- stmt = new (db, handle[])
65
+ stmt = new (db, handle[], Dict {Int, Any} () )
65
66
finalizer (_close, stmt)
66
67
return stmt
67
68
end
@@ -85,6 +86,7 @@ clears any bound values to a prepared SQL statement.
85
86
"""
86
87
function clear! (stmt:: Stmt )
87
88
sqlite3_clear_bindings (stmt. handle)
89
+ empty! (stmt. params)
88
90
return
89
91
end
90
92
@@ -136,14 +138,14 @@ function bind!(stmt::Stmt,name::AbstractString, val)
136
138
end
137
139
return bind! (stmt, i, val)
138
140
end
139
- bind! (stmt:: Stmt , i:: Int , val:: AbstractFloat ) = (sqlite3_bind_double (stmt. handle, i ,Float64 (val)); return nothing )
140
- bind! (stmt:: Stmt , i:: Int , val:: Int32 ) = (sqlite3_bind_int (stmt. handle, i ,val); return nothing )
141
- bind! (stmt:: Stmt , i:: Int , val:: Int64 ) = (sqlite3_bind_int64 (stmt. handle, i ,val); return nothing )
142
- bind! (stmt:: Stmt , i:: Int , val:: Missing ) = (sqlite3_bind_null (stmt. handle, i ); return nothing )
143
- bind! (stmt:: Stmt , i:: Int , val:: AbstractString ) = (sqlite3_bind_text (stmt. handle, i ,val); return nothing )
144
- bind! (stmt:: Stmt , i:: Int , val:: WeakRefString{UInt8} ) = (sqlite3_bind_text (stmt. handle, i, val. ptr, val. len); return nothing )
145
- bind! (stmt:: Stmt , i:: Int , val:: WeakRefString{UInt16} ) = (sqlite3_bind_text16 (stmt. handle, i, val. ptr, val. len* 2 ); return nothing )
146
- bind! (stmt:: Stmt , i:: Int , val:: Vector{UInt8} ) = (sqlite3_bind_blob (stmt. handle, i, val); return nothing )
141
+ bind! (stmt:: Stmt , i:: Int , val:: AbstractFloat ) = (stmt . params[i] = val; sqlite3_bind_double (stmt. handle, i ,Float64 (val)); return nothing )
142
+ bind! (stmt:: Stmt , i:: Int , val:: Int32 ) = (stmt . params[i] = val; sqlite3_bind_int (stmt. handle, i ,val); return nothing )
143
+ bind! (stmt:: Stmt , i:: Int , val:: Int64 ) = (stmt . params[i] = val; sqlite3_bind_int64 (stmt. handle, i ,val); return nothing )
144
+ bind! (stmt:: Stmt , i:: Int , val:: Missing ) = (stmt . params[i] = val; sqlite3_bind_null (stmt. handle, i ); return nothing )
145
+ bind! (stmt:: Stmt , i:: Int , val:: AbstractString ) = (stmt . params[i] = val; sqlite3_bind_text (stmt. handle, i ,val); return nothing )
146
+ bind! (stmt:: Stmt , i:: Int , val:: WeakRefString{UInt8} ) = (stmt . params[i] = val; sqlite3_bind_text (stmt. handle, i, val. ptr, val. len); return nothing )
147
+ bind! (stmt:: Stmt , i:: Int , val:: WeakRefString{UInt16} ) = (stmt . params[i] = val; sqlite3_bind_text16 (stmt. handle, i, val. ptr, val. len* 2 ); return nothing )
148
+ bind! (stmt:: Stmt , i:: Int , val:: Vector{UInt8} ) = (stmt . params[i] = val; sqlite3_bind_blob (stmt. handle, i, val); return nothing )
147
149
# Fallback is BLOB and defaults to serializing the julia value
148
150
149
151
# internal wrapper mutable struct to, in-effect, mark something which has been serialized
@@ -169,11 +171,12 @@ struct SerializeError <: Exception
169
171
end
170
172
171
173
# magic bytes that indicate that a value is in fact a serialized julia value, instead of just a byte vector
172
- const SERIALIZATION = UInt8[0x37 ,0x4a ,0x4c ,0x07 ,0x04 ,0x00 ,0x00 ,0x00 ,0x34 ,0x10 ,0x01 ,0x0a ,0x53 ,0x65 ,0x72 ,0x69 ,0x61 ,0x6c ]
174
+ # these bytes depend on the julia version and other things, so they are determined using an actual serialization
175
+ const SERIALIZATION = sqlserialize (0 )[1 : 18 ]
173
176
174
177
function sqldeserialize (r)
175
178
ret = ccall (:memcmp , Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt),
176
- SERIALIZATION, r, min (18 , length (r)))
179
+ SERIALIZATION, r, min (sizeof (SERIALIZATION), sizeof (r)))
177
180
if ret == 0
178
181
try
179
182
v = Serialization. deserialize (IOBuffer (r))
252
255
253
256
function execute! (db:: DB , sql:: AbstractString )
254
257
stmt = Stmt (db, sql)
255
- return execute! (stmt)
258
+ r = execute! (stmt)
259
+ finalize (stmt)
260
+ return r
256
261
end
257
262
258
263
"""
0 commit comments