-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbAdmin.lua
37 lines (32 loc) · 877 Bytes
/
DbAdmin.lua
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
local dbAdmin = {}
dbAdmin.__index = dbAdmin
-- Function to create a new database explorer instance
function dbAdmin.new(db)
local self = setmetatable({}, dbAdmin)
self.db = db
return self
end
-- Function to list all tables in the database
function dbAdmin:tables()
local tables = {}
for row in self.db:nrows("SELECT name FROM sqlite_master WHERE type='table';") do
table.insert(tables, row.name)
end
return tables
end
-- Function to get the record count of a table
function dbAdmin:count(tableName)
local count_query = string.format("SELECT COUNT(*) AS count FROM %s;", tableName)
for row in self.db:nrows(count_query) do
return row.count
end
end
-- Function to execute a given SQL query
function dbAdmin:exec(sql)
local results = {}
for row in self.db:nrows(sql) do
table.insert(results, row)
end
return results
end
return dbAdmin