-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdatabase.lua
More file actions
executable file
·372 lines (298 loc) · 8.88 KB
/
Copy pathdatabase.lua
File metadata and controls
executable file
·372 lines (298 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
--====================================================================--
-- Module: database
--
-- Copyright (C) 2013-2017 Anedix Technologies, Inc. All Rights Reserved.
--
-- License:
--
--
-- Overview:
--
-- This module allows you to save to a database.
--
--
-- Usage:
--
-- local panel = require("database")
--
-- Examples:
--
-- myDatabase.open()
-- myDatabase.insert("players",{name="tommy",scoreint=25,mdate="now()",rowstate=1})
-- myDatabase.update("players",{name="tommy lee",scoreint=50,mdate="now()",rowstate=1},"name='tommy'")
-- myDatabase.query("SELECT * FROM players WHERE rowstate=1 LIMIT 1")
-- myDatabase.close()
--
-- Notice:
--
--
--====================================================================--
--
local M = {}
M.myDB = nil
M.pagination = {}
M.databaseName = nil
M.version = 1.0
--function to remove table
function M.dropTable( tablename )
if M.open() == false or tablename == nil then
return false
end
local sql = string.format([[DROP TABLE IF EXISTS %s]], tablename)
M.myDB:exec(sql)
return true
end
-- method: exists
-- expects: tab as string
-- returns: true or false if table exists
function M.tableExist(tab)
local ret = false
local sql = string.format([[SELECT count(name) as count FROM sqlite_master WHERE type='table' AND name='%s']],tab)
if M.open() == false then return false end
for total in M.myDB:urows(sql) do
if total > 0 then ret = true end
end
return ret
end
-- method: exec
-- expects: query
-- returns: nothing
function M.exec(query)
if query == nil then return end
if M.open() == false then return false end
M.myDB:exec( query )
end
-- method: query
-- expects: string
-- returns: rows
function M.query( query )
if M.open() == false then return {} end
local result = {}
-- if nothing to do
if query == nil or (query ~=nil and query == "") then
return result
end
local n = 1
for row in M.myDB:nrows(query) do
result[n] = {}
result[n] = row
n = n + 1
end
return result
end
function M.startPagination(name)
if name == nil then return end
M.pagination[name] = { total=0, from=0, per_page = 5, page_count=0, page=0 }
end
function M.removePagination(name)
if name == nil then return end
M.pagination[name] = nil
end
function M.getPagination(name)
if name == nil then return end
return M.pagination[name]
end
-- method: paginate
-- expects: table {}
-- returns: rows
function M.paginate( attr )
local name = ""
if M.open() == false or attr == nil then return {} end
if attr.name == nil then
print("paginate: Missing pagination name. Specify by { name = <string> } in options")
return {}
end
name = attr.name
if M.pagination[name] == nil then M.startPagination( name ) end
local result = {}
local query = attr.query
local query_count = attr.query_count
local direction = attr.direction or "next"
local per_page = attr.per_page or 5
local from = attr.from or 0
local debug = attr.debug or false
-- if nothing to do
if (query == nil or (query ~=nil and query == "")) or (query_count == nil or (query_count ~=nil and query_count == "")) then
return result
end
-- handle per_page
M.pagination[name].per_page = per_page
-- handle from
if attr.from == nil then
if direction == "next" then
if M.pagination[name].page < M.pagination[name].page_count-1 then
M.pagination[name].from = (M.pagination[name].page + 1) * M.pagination[name].per_page
end
elseif direction == "prev" then
if M.pagination[name].page > 0 then
M.pagination[name].from = (M.pagination[name].page - 1) * M.pagination[name].per_page
else
M.pagination[name].from = 0
end
end
from = M.pagination[name].from
end
-- query_count
local total = 0
for x in M.myDB:urows(query_count) do
total = x
end
M.pagination[name].total = total
-- query
local limit = from .. ", " .. M.pagination[name].per_page
local n = 1
if debug == true then print("paginate() query: ".. query .. " LIMIT " .. limit) end
for row in M.myDB:nrows(query .. " LIMIT " .. limit) do
result[n] = {}
result[n] = row
n = n + 1
end
from = math.min(from, total)
M.pagination[name].from = from
M.pagination[name].page_count = math.floor((total + M.pagination[name].per_page - 1) / M.pagination[name].per_page)
M.pagination[name].page = math.floor(from / M.pagination[name].per_page)
return result
end
-- method: clearResult
-- expects: table {}
-- returns: nothing
function M.clearResult( tab )
if tab == nil or (tab ~= nil and (type(tab) ~= "table")) then return end
for k,v in pairs(tab) do
if (type(v) == "table") then
for l, _ in pairs(v) do
v[l] = nil
end
end
tab[k]=nil
end
end
-- method: insert
-- expects: table name, sql w/ 1 insert or string with multiple inserts but requires 'rawmode' set to true
-- returns: array of {number of changes for insert, last row id inserted}
function M.insert(tab, data, rawmode)
if M.open() == false or tab == nil or data == nil then return {total=0, newid=0} end
-- do some database calls...
local count = 0
local id = 0
if rawmode == nil or rawmode == false then
local sql = ""
local fields = ""
local values = ""
for k,val in pairs(data) do
if fields ~= "" then
fields = fields .. ", "
end
fields = fields .. k
if values ~= "" then
values = values .. ", "
end
if type(val) == "number" then
values = values .. "" .. val .. ""
else
-- check for current date using "now()" check
if string.lower(val) == "now()" then
values = values .. "date('now')"
else
values = values .. "'" .. val .. "'"
end
end
end
sql = "INSERT INTO " .. tab .. " (id, " .. fields .. ") VALUES (NULL," .. values .. ");"
M.myDB:exec(sql)
--print("M.myDB:changes()=",M.myDB:changes())
--print("INSERT WITH DATA AND PARMS:",sql)
else
if type(data) == "string" then
db:exec(data)
end
end
count = M.myDB:changes()
id = M.myDB:last_insert_rowid()
return {total=count, newid=id}
end
-- method: update
-- expects: sql w/ 1 update
-- returns: db:changes() or number of changes for update
function M.update(tab, data, where, rawmode)
if M.open() == false or tab == nil or data == nil then return 0 end
-- do some database calls...
local count = 0
if rawmode == nil or rawmode == false then
local sql = ""
for k,val in pairs(data) do
if sql ~= "" then
sql = sql .. ", "
end
if type(val) == "number" then
sql = sql .. k .. "=" .. val .. ""
else
-- check for current date using "now()" check
if string.lower(val) == "now()" then
sql = sql .. k .. "=date('now')"
else
sql = sql .. k .. "='" .. val .. "'"
end
end
end
if where ~= nil and type(where) == "string" then
sql = sql .. " WHERE " .. where
end
sql = "UPDATE " .. tab .. " SET " .. sql .. ";"
M.myDB:exec(sql)
else
if type(data) == "string" then
M.myDB:exec(data)
end
end
count = M.myDB:changes()
return count
end
-- method: delete
-- expects: sql w/ 1 delete
-- returns: db:changes() or number of changes for delete
function M.delete(tab, where)
if tab == nil or where == nil then
return 0
end
if tab == "" or where == "" then
return 0
end
if M.open() == false then return 0 end
local count = 0
M.myDB:exec("DELETE FROM " .. tab .. " WHERE " .. where .. ";")
count = M.myDB:changes()
return count
end
-- method: open
-- expects: database name
-- returns: true or false if successful
function M.open(db)
local ret = false
if M.myDB ~= nil and M.myDB:isopen() == true then
return true
end
if db == nil then
db = M.databaseName
end
if db ~= nil then
local path = system.pathForFile( db .. ".db", system.DocumentsDirectory )
M.myDB = sqlite3.open(path)
if M.myDB == nil then
ret = false
else
M.databaseName = db
ret = true
end
end
return ret
end
-- method: close
-- expects: nothing
-- returns: nothing
function M.close()
if M.myDB ~= nil and M.myDB:isopen() == true then
M.myDB:close() -- close
end
end
return M