1
- module Sqlite
1
+ module SQLite
2
2
3
3
using DataFrames
4
4
5
- export sqlitedb, readdlmsql
5
+ export sqlitedb, readdlmsql, query, connect
6
6
7
- include (" Sqlite_consts .jl" )
8
- include (" Sqlite_api .jl" )
7
+ include (" SQLite_consts .jl" )
8
+ include (" SQLite_api .jl" )
9
9
10
- type SqliteDB
10
+ type SQLiteDB
11
11
file:: String
12
12
handle:: Ptr{Void}
13
13
resultset:: Any
14
14
end
15
- function show (io:: IO ,db:: SqliteDB )
16
- if db == null_SqliteDB
15
+ function show (io:: IO ,db:: SQLiteDB )
16
+ if db == null_SQLiteDB
17
17
print (io," Null sqlite connection" )
18
18
else
19
19
println (io," sqlite connection" )
31
31
typealias TableInput Union (DataFrame,String)
32
32
33
33
const null_resultset = DataFrame (0 )
34
- const null_SqliteDB = SqliteDB (" " ,C_NULL ,null_resultset)
35
- sqlitedb = null_SqliteDB # Create default connection = null
34
+ const null_SQLiteDB = SQLiteDB (" " ,C_NULL ,null_resultset)
35
+ sqlitedb = null_SQLiteDB # Create default connection = null
36
36
const INTrx = r" ^\d +$"
37
37
const STRINGrx = r" [^eE0-9\.\-\+ ]" i
38
38
const FLOATrx = r" ^[+-]?([0-9]+(\. [0-9]*)?|\. [0-9]+)([eE][+-]?[0-9]+)?$"
@@ -44,10 +44,10 @@ function connect(file::String)
44
44
if @FAILED sqlite3_open (file,handle)
45
45
error (" [sqlite]: Error opening $file ; $(bytestring (sqlite3_errmsg (conn. handle))) " )
46
46
else
47
- return (sqlitedb = SqliteDB (file,handle[1 ],null_resultset))
47
+ return (sqlitedb = SQLiteDB (file,handle[1 ],null_resultset))
48
48
end
49
49
end
50
- function internal_query (conn:: SqliteDB ,q:: String ,finalize:: Bool = true ,stepped:: Bool = true )
50
+ function internal_query (conn:: SQLiteDB ,q:: String ,finalize:: Bool = true ,stepped:: Bool = true )
51
51
stmt = Array (Ptr{Void},1 )
52
52
if @FAILED sqlite3_prepare_v2 (conn. handle,utf8 (q),stmt,[C_NULL ])
53
53
ret = bytestring (sqlite3_errmsg (conn. handle))
@@ -67,25 +67,25 @@ function internal_query(conn::SqliteDB,q::String,finalize::Bool=true,stepped::Bo
67
67
return stmt, r
68
68
end
69
69
end
70
- function query (q:: String ,conn:: SqliteDB = sqlitedb)
71
- conn == null_SqliteDB && error (" [sqlite]: A valid SqliteDB was not specified (and no valid default SqliteDB exists)" )
72
- stmt, r = Sqlite . internal_query (conn,q,false )
70
+ function query (q:: String ,conn:: SQLiteDB = sqlitedb)
71
+ conn == null_SQLiteDB && error (" [sqlite]: A valid SQLiteDB was not specified (and no valid default SQLiteDB exists)" )
72
+ stmt, r = SQLite . internal_query (conn,q,false )
73
73
r == SQLITE_DONE && return DataFrame (" No Rows Returned" )
74
74
# get resultset metadata: column count, column types, and column names
75
- ncols = Sqlite . sqlite3_column_count (stmt)
75
+ ncols = SQLite . sqlite3_column_count (stmt)
76
76
colnames = Array (ASCIIString,ncols)
77
77
resultset = Array (Any,ncols)
78
78
check = 0
79
79
for i = 1 : ncols
80
- colnames[i] = bytestring (Sqlite . sqlite3_column_name (stmt,i- 1 ))
81
- t = Sqlite . sqlite3_column_type (stmt,i- 1 )
82
- if t == Sqlite . SQLITE3_TEXT
80
+ colnames[i] = bytestring (SQLite . sqlite3_column_name (stmt,i- 1 ))
81
+ t = SQLite . sqlite3_column_type (stmt,i- 1 )
82
+ if t == SQLite . SQLITE3_TEXT
83
83
resultset[i] = DataArray (String,0 )
84
84
check += 1
85
- elseif t == Sqlite . SQLITE_FLOAT
85
+ elseif t == SQLite . SQLITE_FLOAT
86
86
resultset[i] = DataArray (Float64,0 )
87
87
check += 1
88
- elseif t == Sqlite . SQLITE_INTEGER
88
+ elseif t == SQLite . SQLITE_INTEGER
89
89
resultset[i] = DataArray (WORD_SIZE == 64 ? Int64 : Int32,0 )
90
90
check += 1
91
91
else
@@ -95,7 +95,7 @@ function query(q::String,conn::SqliteDB=sqlitedb)
95
95
# retrieve resultset
96
96
while true
97
97
for i = 1 : ncols
98
- t = Sqlite . sqlite3_column_type (stmt,i- 1 )
98
+ t = SQLite . sqlite3_column_type (stmt,i- 1 )
99
99
if t == SQLITE3_TEXT
100
100
r = bytestring ( sqlite3_column_text (stmt,i- 1 ) )
101
101
elseif t == SQLITE_FLOAT
@@ -132,8 +132,8 @@ function query(q::String,conn::SqliteDB=sqlitedb)
132
132
sqlite3_finalize (stmt)
133
133
return (conn. resultset = DataFrame (resultset,Index (colnames)))
134
134
end
135
- function createtable (input:: TableInput ,conn:: SqliteDB = sqlitedb;name:: String = " " ,delim:: Char = ' \0 ' ,header:: Bool = true ,types:: Array{DataType,1} = DataType[],infer:: Bool = true )
136
- conn == null_SqliteDB && error (" [sqlite]: A valid SqliteDB was not specified (and no valid default SqliteDB exists)" )
135
+ function createtable (input:: TableInput ,conn:: SQLiteDB = sqlitedb;name:: String = " " ,delim:: Char = ' \0 ' ,header:: Bool = true ,types:: Array{DataType,1} = DataType[],infer:: Bool = true )
136
+ conn == null_SQLiteDB && error (" [sqlite]: A valid SQLiteDB was not specified (and no valid default SQLiteDB exists)" )
137
137
# these 2 calls are for performance
138
138
internal_query (conn," PRAGMA synchronous = OFF" )
139
139
@@ -145,7 +145,7 @@ function createtable(input::TableInput,conn::SqliteDB=sqlitedb;name::String="",d
145
145
internal_query (conn," PRAGMA synchronous = ON" )
146
146
return r
147
147
end
148
- function df2table (df:: DataFrame ,conn:: SqliteDB ,name:: String )
148
+ function df2table (df:: DataFrame ,conn:: SQLiteDB ,name:: String )
149
149
# get column names and types
150
150
ncols = length (df)
151
151
colnames = join (df. colindex. names,' ,' )
@@ -170,7 +170,7 @@ function df2table(df::DataFrame,conn::SqliteDB,name::String)
170
170
d = df[row,col]
171
171
t = typeof (d)
172
172
if t <: FloatingPoint
173
- Sqlite . sqlite3_bind_double (stmt,col,d)
173
+ SQLite . sqlite3_bind_double (stmt,col,d)
174
174
elseif t <: Integer
175
175
WORD_SIZE == 64 ? sqlite3_bind_int64 (stmt,col,d) : sqlite3_bind_int (stmt,col,d)
176
176
elseif <: NAtype
@@ -186,14 +186,14 @@ function df2table(df::DataFrame,conn::SqliteDB,name::String)
186
186
internal_query (conn," COMMIT" )
187
187
return
188
188
end
189
- function droptable (table:: String ,conn:: SqliteDB = sqlitedb)
190
- conn == null_SqliteDB && error (" [sqlite]: A valid SqliteDB was not specified (and no valid default SqliteDB exists)" )
189
+ function droptable (table:: String ,conn:: SQLiteDB = sqlitedb)
190
+ conn == null_SQLiteDB && error (" [sqlite]: A valid SQLiteDB was not specified (and no valid default SQLiteDB exists)" )
191
191
internal_query (conn," DROP TABLE $table " )
192
192
internal_query (conn," VACUUM" )
193
193
return
194
194
end
195
195
# read raw file direct to sqlite table
196
- function dlm2table (file:: String ,conn:: SqliteDB ,name:: String ,delim:: Char ,header:: Bool ,types:: Array{DataType,1} ,infer:: Bool )
196
+ function dlm2table (file:: String ,conn:: SQLiteDB ,name:: String ,delim:: Char ,header:: Bool ,types:: Array{DataType,1} ,infer:: Bool )
197
197
# determine tablename and delimiter
198
198
tablename = name
199
199
if tablename == " "
@@ -263,26 +263,26 @@ function dlm2table(file::String,conn::SqliteDB,name::String,delim::Char,header::
263
263
stmt, r = internal_query (conn," insert into $tablename values ($params )" ,false ,false )
264
264
# bind, step, reset loop for inserting values
265
265
for r in eachline (f)
266
- row = Sqlite . split_quoted (chomp (r),delimiter)
266
+ row = SQLite . split_quoted (chomp (r),delimiter)
267
267
for col = 1 : ncols
268
268
d = row[col]
269
- Sqlite . sqlite3_bind_text (stmt,col,d,length (d),C_NULL )
269
+ SQLite . sqlite3_bind_text (stmt,col,d,length (d),C_NULL )
270
270
end
271
- Sqlite . sqlite3_step (stmt)
272
- Sqlite . sqlite3_reset (stmt)
271
+ SQLite . sqlite3_step (stmt)
272
+ SQLite . sqlite3_reset (stmt)
273
273
end
274
274
sqlite3_finalize (stmt)
275
275
internal_query (conn," COMMIT" )
276
276
close (f)
277
277
return
278
278
end
279
279
# read raw file to sqlite table (call dlm2table), then run sql statement on table to return df (call to query)
280
- function readdlmsql (input:: String ,conn:: SqliteDB = sqlitedb;sql:: String = " select * from file" ,name:: String = " file" ,delim:: Char = ' \0 ' ,header:: Bool = true ,types:: Array{DataType,1} = DataType[],infer:: Bool = true )
281
- if conn == null_SqliteDB
280
+ function readdlmsql (input:: String ,conn:: SQLiteDB = sqlitedb;sql:: String = " select * from file" ,name:: String = " file" ,delim:: Char = ' \0 ' ,header:: Bool = true ,types:: Array{DataType,1} = DataType[],infer:: Bool = true )
281
+ if conn == null_SQLiteDB
282
282
handle = Array (Ptr{Void},1 )
283
283
file = tempname ()
284
- Sqlite . sqlite3_open (file,handle)
285
- conn = Sqlite . SqliteDB (file,handle[1 ],Sqlite . null_resultset)
284
+ SQLite . sqlite3_open (file,handle)
285
+ conn = SQLite . SQLiteDB (file,handle[1 ],SQLite . null_resultset)
286
286
end
287
287
createtable (input,conn;name= name,delim= delim,header= header,types= types,infer= infer)
288
288
return query (sql,conn)
@@ -343,8 +343,8 @@ end #sqlite module
343
343
function sqldf (q:: String )
344
344
handle = Array (Ptr{Void},1 )
345
345
file = tempname ()
346
- Sqlite . sqlite3_open (file,handle)
347
- conn = Sqlite . SqliteDB (file,handle[1 ],Sqlite . null_resultset)
346
+ SQLite . sqlite3_open (file,handle)
347
+ conn = SQLite . SQLiteDB (file,handle[1 ],SQLite . null_resultset)
348
348
# todo: do we need to change column names at all?
349
349
tables = ref (String)
350
350
# find tablenames
@@ -365,12 +365,12 @@ function sqldf(q::String)
365
365
check != length (tables) && error (" [sqlite]: DataFrames specified in query were not found" )
366
366
for df in tables
367
367
d = eval (symbol (df))
368
- Sqlite . createtable (d,conn;name= df)
368
+ SQLite . createtable (d,conn;name= df)
369
369
end
370
- result = Sqlite . query (q,conn)
370
+ result = SQLite . query (q,conn)
371
371
for df in tables
372
- Sqlite . droptable (df,conn)
372
+ SQLite . droptable (df,conn)
373
373
end
374
- Sqlite . sqlite3_close_v2 (conn. handle)
374
+ SQLite . sqlite3_close_v2 (conn. handle)
375
375
return result
376
376
end
0 commit comments