index.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <!doctype html>
  2. <html lang="en-nz">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Invercargill-Sql — Astrologue Developer</title>
  7. <meta name="description" content="A SQL abstraction built on Invercargill data structures: provider-neutral connections, lazy Enumerable rows, fluent parameter binding, transactions, migrations and an ORM with entity mappers and projections. Free software, LGPL-3.0-or-later.">
  8. <meta name="color-scheme" content="light dark">
  9. <meta name="theme-color" content="#f2ebda" media="(prefers-color-scheme: light)">
  10. <meta name="theme-color" content="#0a0a12" media="(prefers-color-scheme: dark)">
  11. <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Ccircle cx='32' cy='32' r='27' fill='none' stroke='%23a8801f' stroke-width='4'/%3E%3Cpath d='M32 8l6 18 18 6-18 6-6 18-6-18-18-6 18-6z' fill='%23a8801f'/%3E%3Ccircle cx='32' cy='32' r='4' fill='%23003078'/%3E%3C/svg%3E">
  12. <meta property="og:title" content="Invercargill-Sql — Astrologue Developer">
  13. <meta property="og:description" content="A generic SQL abstraction on Invercargill data structures: lazy Enumerable rows over a provider-neutral API, with migrations and an ORM.">
  14. <meta property="og:type" content="website">
  15. <link rel="stylesheet" href="../../assets/css/style.css">
  16. </head>
  17. <body>
  18. <a class="skip-link" href="#main">Skip to content</a>
  19. <header class="masthead">
  20. <div class="container masthead__inner">
  21. <a class="brand" href="../../">
  22. <img class="brand__logo brand__logo--light" src="../../assets/img/astrologue-light.png" width="392" height="89" alt="Astrologue">
  23. <img class="brand__logo brand__logo--dark" src="../../assets/img/astrologue-dark.png" width="392" height="89" alt="Astrologue">
  24. </a>
  25. <nav class="site-nav" aria-label="Primary">
  26. <ul class="site-nav__list">
  27. <li><a href="../../">Home</a></li>
  28. <li><a href="../../#news">News</a></li>
  29. <li><a href="../../#download">Download</a></li>
  30. <li><a href="../" aria-current="page">Developer</a></li>
  31. <li><a class="nav-cta" href="../../#download">Get Astrologue</a></li>
  32. </ul>
  33. </nav>
  34. </div>
  35. </header>
  36. <main id="main">
  37. <nav class="breadcrumb" aria-label="Breadcrumb">
  38. <div class="container">
  39. <ol>
  40. <li><a href="../../">Astrologue</a></li>
  41. <li><a href="../">Developer</a></li>
  42. <li aria-current="page">Invercargill-Sql</li>
  43. </ol>
  44. </div>
  45. </nav>
  46. <section class="docs-header">
  47. <div class="container">
  48. <p class="kicker"><span>Data · Rows, schemas and migrations</span></p>
  49. <h1>Invercargill-Sql</h1>
  50. <p class="lede">A generic SQL abstraction built on Invercargill data structures. Connections are provider-neutral, query results return as lazy Enumerable rows, and above that sits a migration system and an ORM with entity mappers and projections. SQLite is the engine beneath today.</p>
  51. <dl class="docs-facts">
  52. <div><dt>Version</dt> <dd><code>0.1</code></dd></div>
  53. <div><dt>Licence</dt> <dd>LGPL-3.0-or-later</dd></div>
  54. <div><dt>Package</dt> <dd><code>usm&nbsp;install&nbsp;invercargill-sql</code></dd></div>
  55. <div><dt>Language</dt> <dd>Vala</dd></div>
  56. </dl>
  57. </div>
  58. </section>
  59. <section class="band band--plain" id="docs">
  60. <div class="container docs-layout">
  61. <nav class="docs-toc" aria-label="On this page">
  62. <h2>On this page</h2>
  63. <ul>
  64. <li><a href="#overview">Overview</a></li>
  65. <li><a href="#features">Features</a></li>
  66. <li><a href="#installation">Installation</a></li>
  67. <li><a href="#usage">Usage &amp; documentation</a></li>
  68. <li><a href="#package">Package &amp; source</a></li>
  69. </ul>
  70. </nav>
  71. <div class="docs-prose">
  72. <header class="section-head" id="overview">
  73. <p class="kicker"><span>Overview</span></p>
  74. <h2>Why it exists</h2>
  75. </header>
  76. <p>Databases in Vala usually mean provider-specific APIs and eager row fetching. Invercargill-Sql takes the other road: a small public surface — <code>ConnectionFactory</code>, <code>Connection</code>, <code>Command</code>, <code>Transaction</code> — under which the concrete engine is an internal detail. Query results come back as <code>Enumerable&lt;Properties&gt;</code>, so a result set is not a materialised list you then re-shape but a lazy sequence you query with the same operations as everything else in the stack.</p>
  77. <p>On that ground stands the rest of the data story: a fluent migration system that builds and versions schemas, an ORM that maps GObject classes onto tables with schema introspection, and projections that shape joins and aggregates into purpose-built read models. Spry's user and permission tables are defined here, as migrations.</p>
  78. <header class="section-head" id="features">
  79. <p class="kicker"><span>Features</span></p>
  80. <h2>What it hands you</h2>
  81. </header>
  82. <ul class="docs-features">
  83. <li><strong>Generic engine interface.</strong> Abstract away specific SQL server implementations; SQLite ships as the first-class engine.</li>
  84. <li><strong>Lazy Enumerable rows.</strong> Query results return as <code>Enumerable&lt;Properties&gt;</code> — filter, project and cache them with Invercargill itself.</li>
  85. <li><strong>Connection strings.</strong> URI-style strings such as <code>sqlite::memory:</code> or <code>sqlite:///db.sqlite?mode=ro</code>, parsed into scheme, database and options.</li>
  86. <li><strong>Parameterized queries.</strong> A fluent API for type-safe parameter binding.</li>
  87. <li><strong>Transactions.</strong> Full commit/rollback management.</li>
  88. <li><strong>Async throughout.</strong> Every operation has an async variant for non-blocking I/O.</li>
  89. <li><strong>Migrations.</strong> A fluent builder for tables, columns, indexes and foreign keys, with dependency-aware ordering and <code>up</code>/<code>down</code> reversibility.</li>
  90. <li><strong>ORM.</strong> Entity mappers, sessions, typed queries and projections — including join and aggregate read models.</li>
  91. </ul>
  92. <header class="section-head" id="installation">
  93. <p class="kicker"><span>Installation</span></p>
  94. <h2>Getting the library</h2>
  95. </header>
  96. <figure class="code-plate">
  97. <pre><code>usm install invercargill
  98. usm install invercargill-sql</code></pre>
  99. <figcaption>USM resolves SQLite and the other build dependencies itself.</figcaption>
  100. </figure>
  101. <p>From source:</p>
  102. <figure class="code-plate">
  103. <pre><code>git clone &lt;the Invercargill-Sql repository&gt;
  104. cd Invercargill-Sql
  105. meson setup build
  106. ninja -C build
  107. ninja -C build install</code></pre>
  108. <figcaption>The test suite runs with <code>meson test -C build</code>.</figcaption>
  109. </figure>
  110. <figure class="code-plate">
  111. <pre><code>valac --pkg invercargill-sql my-program.vala</code></pre>
  112. <figcaption>The binding pulls in its <code>.deps</code> chain automatically.</figcaption>
  113. </figure>
  114. <header class="section-head" id="usage">
  115. <p class="kicker"><span>Usage &amp; documentation</span></p>
  116. <h2>Connecting and querying</h2>
  117. </header>
  118. <p>Use <code>ConnectionFactory</code> to create connections — no database-specific types in sight:</p>
  119. <figure class="code-plate">
  120. <pre><code>using InvercargillSql;
  121. void main() throws SqlError {
  122. // Create and open a connection using a connection string
  123. var conn = ConnectionFactory.create_and_open("sqlite:///mydb.db");
  124. // Or create and open separately
  125. var conn2 = ConnectionFactory.create("sqlite:///another.db");
  126. conn2.open();
  127. // Execute commands...
  128. conn.close();
  129. conn2.close();
  130. }</code></pre>
  131. <figcaption>From the README. Connection strings also cover <code>sqlite://./relative/path.db</code>, <code>sqlite::memory:</code> and read-only or read-write modes via <code>?mode=</code>.</figcaption>
  132. </figure>
  133. <p>DDL and inserts go through fluent commands with bound parameters:</p>
  134. <figure class="code-plate">
  135. <pre><code>// Create a table
  136. conn.create_command("""
  137. CREATE TABLE IF NOT EXISTS users (
  138. id INTEGER PRIMARY KEY,
  139. name TEXT NOT NULL,
  140. email TEXT
  141. )
  142. """).execute_non_query();
  143. // Insert data with parameters
  144. conn.create_command("INSERT INTO users (name, email) VALUES (:name, :email)")
  145. .with_parameter("name", "Alice")
  146. .with_parameter("email", "alice@example.com")
  147. .execute_non_query();</code></pre>
  148. <figcaption>From the README.</figcaption>
  149. </figure>
  150. <p>Query results are lazy — enumerate them, read columns by name, or keep querying:</p>
  151. <figure class="code-plate">
  152. <pre><code>var results = conn.create_command("SELECT * FROM users")
  153. .execute_query();
  154. foreach (var row in results) {
  155. string? name = row.get("name")?.as_string_or_null();
  156. string? email = row.get("email")?.as_string_or_null();
  157. int64? id = row.get("id")?.as&lt;int64?&gt;();
  158. print(@"User: $name ($email)\n");
  159. }
  160. // Results are Enumerable&lt;Properties&gt; — the whole toolkit applies
  161. var activeUsers = results.where(row =&gt; row.get("active")?.as&lt;bool&gt;() ?? false);
  162. var names = results.select(row =&gt; row.get("name")?.as_string_or_null() ?? "unknown");
  163. var cached = results.cache();</code></pre>
  164. <figcaption>From the README — the second half shows Invercargill's modifiers applied directly to a result set.</figcaption>
  165. </figure>
  166. <p>Transactions commit or roll back as a unit, and every operation has an async twin:</p>
  167. <figure class="code-plate">
  168. <pre><code>var txn = conn.begin_transaction();
  169. try {
  170. conn.create_command("INSERT INTO users (name) VALUES ('Charlie')")
  171. .execute_non_query();
  172. conn.create_command("INSERT INTO users (name) VALUES ('Diana')")
  173. .execute_non_query();
  174. txn.commit();
  175. } catch (SqlError e) {
  176. txn.rollback();
  177. warning("Transaction failed: %s", e.message);
  178. }</code></pre>
  179. <figcaption>From the README. Async: <code>conn.open_async()</code>, <code>conn.execute_query_async(...)</code>, <code>conn.close_async()</code>.</figcaption>
  180. </figure>
  181. <h3>Migrations — schemas as code</h3>
  182. <p>Subclass <code>Migration</code>, declare a namespace, serial and name, and build your schema with the fluent builder:</p>
  183. <figure class="code-plate">
  184. <pre><code>using InvercargillSql;
  185. using InvercargillSql.Migrations;
  186. public class V001_CreateUsers : Migration {
  187. public override string migration_namespace { get { return "app"; } }
  188. public override uint64 serial { get { return 1; } }
  189. public override string name { get { return "CreateUsers"; } }
  190. public override void up(MigrationBuilder b) throws SqlError {
  191. b.create_table("users", t =&gt; {
  192. t.column&lt;int64?&gt;("id")
  193. .primary_key()
  194. .auto_increment();
  195. t.column&lt;string&gt;("name")
  196. .not_null();
  197. t.column&lt;string&gt;("email")
  198. .unique();
  199. t.column&lt;int64?&gt;("age");
  200. t.column&lt;bool?&gt;("is_active");
  201. // Create index on email for fast lookups
  202. t.index("idx_users_email").on_column("email");
  203. });
  204. }
  205. public override void down(MigrationBuilder b) throws SqlError {
  206. b.drop_table("users");
  207. }
  208. }</code></pre>
  209. <figcaption>Verbatim from <code>examples/migrations/v001-create-users.vala</code>.</figcaption>
  210. </figure>
  211. <figure class="code-plate">
  212. <pre><code>var runner = new MigrationRunner(conn, dialect);
  213. runner.register_migration(new V001_CreateUsers());
  214. runner.register_migration(new V002_CreateProducts());
  215. runner.register_migration(new V003_CreateOrders());
  216. runner.migrate_to_latest();
  217. var applied = runner.get_applied_migrations();</code></pre>
  218. <figcaption>From <code>examples/demo.vala</code> — the runner orders migrations and remembers what is applied.</figcaption>
  219. </figure>
  220. <h3>The ORM — entities, sessions, projections</h3>
  221. <p>Map a GObject class onto a table once; sessions do the rest:</p>
  222. <figure class="code-plate">
  223. <pre><code>public class User : Object {
  224. public int64 id { get; set; }
  225. public string name { get; set; }
  226. public string email { get; set; }
  227. public int64 age { get; set; }
  228. public bool is_active { get; set; }
  229. public static void configure_mapper(EntityMapperBuilder&lt;User&gt; b) {
  230. b.table("users")
  231. .column&lt;int64?&gt;("id", u =&gt; u.id, (u, v) =&gt; u.id = v)
  232. .column&lt;string&gt;("name", u =&gt; u.name, (u, v) =&gt; u.name = v)
  233. .column&lt;string&gt;("email", u =&gt; u.email, (u, v) =&gt; u.email = v)
  234. .column&lt;int64?&gt;("age", u =&gt; u.age, (u, v) =&gt; u.age = v)
  235. .column&lt;bool?&gt;("is_active", u =&gt; u.is_active, (u, v) =&gt; u.is_active = v);
  236. }
  237. }</code></pre>
  238. <figcaption>From <code>examples/entities/user.vala</code> — the mapper introspects the live schema at registration.</figcaption>
  239. </figure>
  240. <figure class="code-plate">
  241. <pre><code>var registry = new TypeRegistry();
  242. registry.register_with_schema&lt;User&gt;("users", conn, dialect, User.configure_mapper);
  243. var session = new OrmSession(conn, registry, dialect);
  244. // Insert
  245. var alice = new User();
  246. alice.name = "Alice";
  247. alice.email = "alice@example.com";
  248. session.insert(alice);
  249. // Query with typed predicates and ordering
  250. var users_over_25 = session.query&lt;User&gt;()
  251. .where(expr("age &gt; $0", new NativeElement&lt;int?&gt;(25)))
  252. .materialise();
  253. var active_users = session.query&lt;User&gt;()
  254. .where(expr("is_active == $0", new NativeElement&lt;bool?&gt;(true)))
  255. .order_by(expr("age"))
  256. .materialise();
  257. // Update and delete
  258. alice.age = 31;
  259. session.update(alice);
  260. session.delete(alice);</code></pre>
  261. <figcaption>From <code>examples/demo.vala</code> — <code>expr()</code> builds parameterised predicates; pagination is <code>.limit(2).offset(1)</code>.</figcaption>
  262. </figure>
  263. <p>Projections complete the ORM: purpose-built read models — simple, joined and aggregate — registered per type and queried through the same sessions, as <code>UserSummary</code>, <code>OrderDetail</code> and <code>SalesReport</code> in the demo show.</p>
  264. <p>The repository's <code>examples/demo.vala</code> runs the whole story — migrations, registration, inserts, queries, updates, deletes, projections — against an in-memory database; its <code>docs/ARCHITECTURE.md</code> and <code>plans/</code> directory record how the layered design was reached.</p>
  265. <header class="section-head" id="package">
  266. <p class="kicker"><span>Package &amp; source</span></p>
  267. <h2>The ledger</h2>
  268. </header>
  269. <dl class="docs-package">
  270. <div><dt>Version</dt><dd><code>0.1</code> (from <code>MANIFEST.usm</code>)</dd></div>
  271. <div><dt>Licence</dt><dd>LGPL-3.0-or-later — free to run, study, share and improve; the LGPL keeps it free to link from any program.</dd></div>
  272. <div><dt>Package</dt><dd><code>usm install invercargill-sql</code></dd></div>
  273. <div><dt>Provides</dt><dd><code>libinvercargill-sql.so</code>, <code>invercargill-sql.h</code>, <code>invercargill-sql.vapi</code> + <code>.deps</code>, <code>invercargill_sql-1.0.gir</code>, <code>invercargill_sql-1.0.typelib</code>, <code>invercargill-sql.pc</code></dd></div>
  274. <div><dt>Needs at runtime</dt><dd>GLib, GObject, Gio, <code>libinvercargill-1.so</code> and <code>libsqlite3.so.0</code>.</dd></div>
  275. <div><dt>Source</dt><dd>The repository <code>Tilo15/Invercargill-Sql</code> on the Astrologue forge (git over ssh, port 222). Free software: the licence guarantees your right to the source.</dd></div>
  276. </dl>
  277. <nav class="docs-siblings" aria-label="Neighbours in the catalogue">
  278. <p>Its neighbours:</p>
  279. <ul>
  280. <li><a href="../inversion/">Inversion</a></li>
  281. <li><a href="../invercargill-sql-inversion/">Invercargill-Sql-Inversion</a></li>
  282. <li><a href="../spry/">Spry</a></li>
  283. </ul>
  284. </nav>
  285. <nav class="docs-pager" aria-label="Project pages">
  286. <a class="docs-pager__link docs-pager__link--prev" href="../inversion/">
  287. <span>Previous</span>
  288. <strong>Inversion</strong>
  289. </a>
  290. <p class="docs-pager__up"><a href="../">All eleven</a></p>
  291. <a class="docs-pager__link docs-pager__link--next" href="../invercargill-sql-inversion/">
  292. <span>Next in the catalogue</span>
  293. <strong>Invercargill-Sql-Inversion</strong>
  294. </a>
  295. </nav>
  296. </div>
  297. </div>
  298. </section>
  299. </main>
  300. <footer class="site-footer">
  301. <div class="container">
  302. <div class="footer-grid">
  303. <div class="foot-brand">
  304. <img class="brand__logo brand__logo--dark" src="../../assets/img/astrologue-dark.png" width="392" height="89" alt="Astrologue">
  305. <p>An operating system for change — kept for self sufficiency, resilience, and software freedom, so regular people can hold the power of general purpose computers in their own hands.</p>
  306. </div>
  307. <nav aria-label="The four freedoms">
  308. <p class="foot-title">The Four Freedoms</p>
  309. <ol class="freedoms">
  310. <li><strong>Run</strong> the software as you wish, for any purpose.</li>
  311. <li><strong>Study</strong> the source and change it to do your computing as you wish.</li>
  312. <li><strong>Share</strong> copies so you can help others.</li>
  313. <li><strong>Modify</strong> and publish your versions so everyone benefits.</li>
  314. </ol>
  315. </nav>
  316. <nav aria-label="Site">
  317. <p class="foot-title">Site</p>
  318. <ul class="foot-list">
  319. <li><a href="../../">Home</a></li>
  320. <li><a href="../../#news">News</a></li>
  321. <li><a href="../../#download">Download</a></li>
  322. <li><a href="../">Developer</a></li>
  323. </ul>
  324. </nav>
  325. </div>
  326. <p class="colophon">Set in Cinzel and EB Garamond (SIL Open Font License). Astrologue is free software, licensed GPL-3.0-or-later: take it, tend it, pass it on. The libraries are LGPL-3.0-or-later, so they remain free to link.</p>
  327. </div>
  328. </footer>
  329. </body>
  330. </html>