Skip to content

Commit

Permalink
DbAdmin module
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushKun committed May 30, 2024
1 parent de3825c commit d8a8e5c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
7 changes: 6 additions & 1 deletion next_app/src/components/ao/modules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import { runLua, parseOutupt } from "@/lib/ao-vars";
import { ReloadIcon } from "@radix-ui/react-icons"

import { source as graphSource } from "@/modules/ao/graph";
import { source as dbadminSource } from "@/modules/ao/db-admin";

const modules = [
"graph.lua"
"graph.lua",
"dbAdmin.lua"
]

export default function Modules() {
Expand Down Expand Up @@ -58,6 +60,9 @@ export default function Modules() {
case "graph.lua":
setCode(graphSource);
break;
case "dbAdmin.lua":
setCode(dbadminSource);
break;
default:
setCode("");
}
Expand Down
1 change: 1 addition & 0 deletions next_app/src/lib/ao-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export async function getResults(process: string, cursor = "") {
}

export function parseOutupt(out: any) {
if (!out.Output) return out;
const data = out.Output.data;
const { json, output } = data
if (json != "undefined") {
Expand Down
52 changes: 52 additions & 0 deletions next_app/src/modules/ao/db-admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export const source = `
local func, err = load([[
local function _load()
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
end
_G.package.loaded["DbAdmin"] = _load()
]])
if not func then
print(err)
error("Error compiling load function: ")
end
func()
print("Loaded DbAdmin module")
`

0 comments on commit d8a8e5c

Please sign in to comment.