Skip to content

Commit 5112237

Browse files
committed
feat: database pragma queries, fix: #132
1 parent 27e6123 commit 5112237

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lua-astra"
3-
version = "0.38.0"
3+
version = "0.39.0"
44
authors = ["Elham Aryanpur <elhamaryanpur5@gmail.com>"]
55
description = "🔥 Blazingly Fast 🔥 runtime environment for Lua"
66
documentation = "https://astra.arkforge.net/docs/latest"

astra/lua/database.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
---@field execute fun(database: Database, sql: string, parameters: table | nil)
44
---@field query_one fun(database: Database, sql: string, parameters: table | nil): table | nil
55
---@field query_all fun(database: Database, sql: string, parameters: table | nil): table | nil
6+
---@field query_pragma_int fun(database: Database, sql: string): number | nil
7+
---@field query_pragma_text fun(database: Database, sql: string): string | nil
68
---@field close fun(database: Database)
79

810
---Opens a new SQL connection using the provided URL and returns a table representing the connection.

astra/teal/database.tl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ global interface Database is userdata
33
execute: function(database: Database, sql: string, parameters?: table)
44
query_one: function(database: Database, sql: string, parameters?: table): table | nil
55
query_all: function(database: Database, sql: string, parameters?: table): table | nil
6+
query_pragma_int: function(database: Database, sql: string): number | nil
7+
query_pragma_text: function(database: Database, sql: string): string | nil
68
close: function(database: Database)
79
end
810

src/components/database.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use mlua::{LuaSerdeExt, UserData};
1+
use mlua::{ExternalError, LuaSerdeExt, UserData};
22
use sqlx::{Pool, Postgres, Row, Sqlite, migrate::MigrateDatabase};
33
use std::sync::LazyLock;
44
use tokio::sync::Mutex;
@@ -238,6 +238,45 @@ impl UserData for Database {
238238
},
239239
);
240240

241+
macro_rules! query_pragma {
242+
($type:ty, $lua:ident, $sql:ident, $db:ident) => {
243+
match &$db {
244+
DatabaseType::Sqlite(pool) => {
245+
match sqlx::query_scalar::<_, $type>(&$sql)
246+
.fetch_optional(pool)
247+
.await
248+
{
249+
Ok(row) => $lua.to_value(&row),
250+
Err(e) => Err(e.into_lua_err()),
251+
}
252+
}
253+
DatabaseType::Postgres(pool) => {
254+
match sqlx::query_scalar::<_, $type>(&$sql)
255+
.fetch_optional(pool)
256+
.await
257+
{
258+
Ok(row) => $lua.to_value(&row),
259+
Err(e) => Err(e.into_lua_err()),
260+
}
261+
}
262+
}
263+
};
264+
}
265+
266+
methods.add_async_method("query_pragma_int", |lua, this, sql: String| async move {
267+
match &this.db {
268+
Some(db) => query_pragma!(i32, lua, sql, db),
269+
None => Err(mlua::Error::runtime("The connection is closed")),
270+
}
271+
});
272+
273+
methods.add_async_method("query_pragma_text", |lua, this, sql: String| async move {
274+
match &this.db {
275+
Some(db) => query_pragma!(String, lua, sql, db),
276+
None => Err(mlua::Error::runtime("The connection is closed")),
277+
}
278+
});
279+
241280
methods.add_async_method(
242281
"query_one",
243282
|lua, this, (sql, parameters): (String, Option<mlua::Table>)| async move {

0 commit comments

Comments
 (0)