Skip to content

Commit 2e4957e

Browse files
committed
Some renaming from Sqlite to SQLite
1 parent d8935d8 commit 2e4957e

File tree

2 files changed

+58
-61
lines changed

2 files changed

+58
-61
lines changed

src/Sqlite.jl

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
module Sqlite
1+
module SQLite
22

33
using DataFrames
44

5-
export sqlitedb, readdlmsql
5+
export sqlitedb, readdlmsql, query, connect
66

7-
include("Sqlite_consts.jl")
8-
include("Sqlite_api.jl")
7+
include("SQLite_consts.jl")
8+
include("SQLite_api.jl")
99

10-
type SqliteDB
10+
type SQLiteDB
1111
file::String
1212
handle::Ptr{Void}
1313
resultset::Any
1414
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
1717
print(io,"Null sqlite connection")
1818
else
1919
println(io,"sqlite connection")
@@ -31,8 +31,8 @@ end
3131
typealias TableInput Union(DataFrame,String)
3232

3333
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
3636
const INTrx = r"^\d+$"
3737
const STRINGrx = r"[^eE0-9\.\-\+]"i
3838
const FLOATrx = r"^[+-]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][+-]?[0-9]+)?$"
@@ -44,10 +44,10 @@ function connect(file::String)
4444
if @FAILED sqlite3_open(file,handle)
4545
error("[sqlite]: Error opening $file; $(bytestring(sqlite3_errmsg(conn.handle)))")
4646
else
47-
return (sqlitedb = SqliteDB(file,handle[1],null_resultset))
47+
return (sqlitedb = SQLiteDB(file,handle[1],null_resultset))
4848
end
4949
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)
5151
stmt = Array(Ptr{Void},1)
5252
if @FAILED sqlite3_prepare_v2(conn.handle,utf8(q),stmt,[C_NULL])
5353
ret = bytestring(sqlite3_errmsg(conn.handle))
@@ -67,25 +67,25 @@ function internal_query(conn::SqliteDB,q::String,finalize::Bool=true,stepped::Bo
6767
return stmt, r
6868
end
6969
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)
7373
r == SQLITE_DONE && return DataFrame("No Rows Returned")
7474
#get resultset metadata: column count, column types, and column names
75-
ncols = Sqlite.sqlite3_column_count(stmt)
75+
ncols = SQLite.sqlite3_column_count(stmt)
7676
colnames = Array(ASCIIString,ncols)
7777
resultset = Array(Any,ncols)
7878
check = 0
7979
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
8383
resultset[i] = DataArray(String,0)
8484
check += 1
85-
elseif t == Sqlite.SQLITE_FLOAT
85+
elseif t == SQLite.SQLITE_FLOAT
8686
resultset[i] = DataArray(Float64,0)
8787
check += 1
88-
elseif t == Sqlite.SQLITE_INTEGER
88+
elseif t == SQLite.SQLITE_INTEGER
8989
resultset[i] = DataArray(WORD_SIZE == 64 ? Int64 : Int32,0)
9090
check += 1
9191
else
@@ -95,7 +95,7 @@ function query(q::String,conn::SqliteDB=sqlitedb)
9595
#retrieve resultset
9696
while true
9797
for i = 1:ncols
98-
t = Sqlite.sqlite3_column_type(stmt,i-1)
98+
t = SQLite.sqlite3_column_type(stmt,i-1)
9999
if t == SQLITE3_TEXT
100100
r = bytestring( sqlite3_column_text(stmt,i-1) )
101101
elseif t == SQLITE_FLOAT
@@ -132,8 +132,8 @@ function query(q::String,conn::SqliteDB=sqlitedb)
132132
sqlite3_finalize(stmt)
133133
return (conn.resultset = DataFrame(resultset,Index(colnames)))
134134
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)")
137137
#these 2 calls are for performance
138138
internal_query(conn,"PRAGMA synchronous = OFF")
139139

@@ -145,7 +145,7 @@ function createtable(input::TableInput,conn::SqliteDB=sqlitedb;name::String="",d
145145
internal_query(conn,"PRAGMA synchronous = ON")
146146
return r
147147
end
148-
function df2table(df::DataFrame,conn::SqliteDB,name::String)
148+
function df2table(df::DataFrame,conn::SQLiteDB,name::String)
149149
#get column names and types
150150
ncols = length(df)
151151
colnames = join(df.colindex.names,',')
@@ -170,7 +170,7 @@ function df2table(df::DataFrame,conn::SqliteDB,name::String)
170170
d = df[row,col]
171171
t = typeof(d)
172172
if t <: FloatingPoint
173-
Sqlite.sqlite3_bind_double(stmt,col,d)
173+
SQLite.sqlite3_bind_double(stmt,col,d)
174174
elseif t <: Integer
175175
WORD_SIZE == 64 ? sqlite3_bind_int64(stmt,col,d) : sqlite3_bind_int(stmt,col,d)
176176
elseif <: NAtype
@@ -186,14 +186,14 @@ function df2table(df::DataFrame,conn::SqliteDB,name::String)
186186
internal_query(conn,"COMMIT")
187187
return
188188
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)")
191191
internal_query(conn,"DROP TABLE $table")
192192
internal_query(conn,"VACUUM")
193193
return
194194
end
195195
#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)
197197
#determine tablename and delimiter
198198
tablename = name
199199
if tablename == ""
@@ -263,26 +263,26 @@ function dlm2table(file::String,conn::SqliteDB,name::String,delim::Char,header::
263263
stmt, r = internal_query(conn,"insert into $tablename values ($params)",false,false)
264264
#bind, step, reset loop for inserting values
265265
for r in eachline(f)
266-
row = Sqlite.split_quoted(chomp(r),delimiter)
266+
row = SQLite.split_quoted(chomp(r),delimiter)
267267
for col = 1:ncols
268268
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)
270270
end
271-
Sqlite.sqlite3_step(stmt)
272-
Sqlite.sqlite3_reset(stmt)
271+
SQLite.sqlite3_step(stmt)
272+
SQLite.sqlite3_reset(stmt)
273273
end
274274
sqlite3_finalize(stmt)
275275
internal_query(conn,"COMMIT")
276276
close(f)
277277
return
278278
end
279279
#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
282282
handle = Array(Ptr{Void},1)
283283
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)
286286
end
287287
createtable(input,conn;name=name,delim=delim,header=header,types=types,infer=infer)
288288
return query(sql,conn)
@@ -343,8 +343,8 @@ end #sqlite module
343343
function sqldf(q::String)
344344
handle = Array(Ptr{Void},1)
345345
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)
348348
#todo: do we need to change column names at all?
349349
tables = ref(String)
350350
#find tablenames
@@ -365,12 +365,12 @@ function sqldf(q::String)
365365
check != length(tables) && error("[sqlite]: DataFrames specified in query were not found")
366366
for df in tables
367367
d = eval(symbol(df))
368-
Sqlite.createtable(d,conn;name=df)
368+
SQLite.createtable(d,conn;name=df)
369369
end
370-
result = Sqlite.query(q,conn)
370+
result = SQLite.query(q,conn)
371371
for df in tables
372-
Sqlite.droptable(df,conn)
372+
SQLite.droptable(df,conn)
373373
end
374-
Sqlite.sqlite3_close_v2(conn.handle)
374+
SQLite.sqlite3_close_v2(conn.handle)
375375
return result
376376
end

test/test.jl

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
# include("C:/Users/karbarcca/Google Drive/Dropbox/Dropbox/GitHub/Sqlite.jl/src/Sqlite.jl")
2-
# include("/home/quinnj/Dropbox/GitHub/Sqlite.jl/src/Sqlite.jl")
3-
using Sqlite
4-
co = Sqlite.connect(Pkg.dir() * "/Sqlite/test/Chinook_Sqlite.sqlite")
51

6-
df = Sqlite.query("SELECT * FROM Employee;")
7-
Sqlite.createtable(df; name="test")
8-
Sqlite.query("select * from test")
9-
Sqlite.query("drop table test")
2+
using SQLite
3+
co = SQLite.connect(Pkg.dir() * "/SQLite/test/Chinook_SQLite.sqlite")
104

11-
Sqlite.query("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name;")
12-
Sqlite.query("SELECT * FROM Album;")
13-
Sqlite.query("SELECT *
5+
df = SQLite.query("SELECT * FROM Employee;")
6+
SQLite.createtable(df; name="test")
7+
SQLite.query("select * from test")
8+
SQLite.query("drop table test")
9+
10+
SQLite.query("SELECT * FROM sqlite_master WHERE type='table' ORDER BY name;")
11+
SQLite.query("SELECT * FROM Album;")
12+
SQLite.query("SELECT *
1413
FROM Artist a
1514
LEFT OUTER JOIN Album b ON b.ArtistId = a.ArtistId
1615
ORDER BY name;")
1716

1817
using DataFrames
1918
df2 = DataFrame(ones(1000000,5))
20-
@time Sqlite.createtable(df2;name="test2")
21-
@time Sqlite.query("SELECT * FROM test2;")
22-
Sqlite.droptable("test2")
19+
@time SQLite.createtable(df2;name="test2")
20+
@time SQLite.query("SELECT * FROM test2;")
21+
SQLite.droptable("test2")
2322

2423
using DataFrames
2524
df3 = DataFrame(ones(1000,5))
2625
sqldf("select * from df3")
2726

28-
# Sqlite.droptable("sales")
29-
@time Sqlite.readdlmsql(Pkg.dir() * "/Sqlite/test/sales.csv";sql="select * from sales",name="sales")
27+
# SQLite.droptable("sales")
28+
@time SQLite.readdlmsql(Pkg.dir() * "/SQLite/test/sales.csv";sql="select * from sales",name="sales")
3029

31-
@time Sqlite.query("select typeof(f_year), typeof(base_lines), typeof(dollars) from sales")
32-
#TODO
33-
#adjust df2table to determine types and then while looping, just check if NA or not
30+
@time SQLite.query("select typeof(f_year), typeof(base_lines), typeof(dollars) from sales")

0 commit comments

Comments
 (0)