-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.typ
More file actions
73 lines (66 loc) · 1.84 KB
/
Copy pathsqlite.typ
File metadata and controls
73 lines (66 loc) · 1.84 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
// SQLite Plugin for Typst
// Allows querying SQLite databases at compile time
#let _plugin = plugin("zig-out/bin/typst_sqlite_zig.wasm")
/// Open a SQLite database from bytes
/// Returns a database object with query methods
///
/// Example:
/// ```typst
/// #let db = sqlite(read("data.sqlite", encoding: none))
/// #let cities = (db.query)("SELECT * FROM cities")
/// ```
#let sqlite(db_bytes) = {
(
/// Execute a SQL query and return results
/// Returns a dictionary with `columns` and `rows` keys
query: (sql) => {
let result_bytes = _plugin.query(db_bytes, bytes(sql))
json(result_bytes)
},
/// List all table names in the database
tables: () => {
let result_bytes = _plugin.tables(db_bytes)
json(result_bytes)
},
/// Get schema information for a table
schema: (table_name) => {
let result_bytes = _plugin.schema(db_bytes, bytes(table_name))
json(result_bytes)
},
/// Raw database bytes (for advanced use)
_bytes: db_bytes,
)
}
/// Execute a query and format as a Typst table
///
/// Example:
/// ```typst
/// #let db = sqlite(read("data.sqlite", encoding: none))
/// #sqlite-table((db.query)("SELECT name, population FROM cities"))
/// ```
#let sqlite-table(result, ..args) = {
table(
columns: result.columns.len(),
..args,
..result.columns.map(c => [*#c*]),
..result.rows.flatten().map(v => [#v])
)
}
/// Query a database and return results as table-ready data
/// Convenience function combining query and table formatting
///
/// Example:
/// ```typst
/// #let db = sqlite(read("data.sqlite", encoding: none))
/// #table(
/// columns: 2,
/// ..query-table(db, "SELECT name, pop FROM cities")
/// )
/// ```
#let query-table(db, sql) = {
let result = (db.query)(sql)
(
..result.columns.map(c => [*#c*]),
..result.rows.flatten().map(v => [#v])
)
}