SQLite
Pam\Desktop\Database\SQLite gives your application local databases. SQLite is compiled into the host, so nothing has to be installed. Each database is a file under pam-databases/ in the data directory, opened on first use, and every call runs on one dedicated host thread, so results arrive in call order and a slow query never blocks the interface or PHP. Like all modules, calls are asynchronous and answer through callbacks.
Running statements
Section titled “Running statements”use Pam\Desktop\Database\SQLite;use Pam\Desktop\Modules\NativeModuleException;
SQLite::execute('app.db', 'INSERT INTO notes (body) VALUES (?)', [$this->draft]);
SQLite::query( 'app.db', 'SELECT id, body FROM notes ORDER BY id DESC LIMIT ?', [50], function (array $rows): void { $this->notes = $rows; }, function (NativeModuleException $error): void { $this->error = $error->getMessage(); },);| Call | Meaning |
|---|---|
execute($database, $sql, $arguments, $callback, $onError) |
One statement. |
query($database, $sql, $arguments, $callback, $onError) |
Rows as a list of associative arrays. |
executeMany($database, $sql, $argumentSets, $callback, $onError) |
One prepared statement for every argument set, in one transaction. |
transaction($database, $statements, $callback, $onError) |
Several statements atomically. |
transactionIf($database, $guardSql, $guardArguments, $statements, $callback, $onError) |
Several statements atomically, only when a guard query says so. |
Arguments are scalars or null, bound with their types. Always pass values as arguments, never by concatenating them into the SQL.
Failures go to $onError as a NativeModuleException whose message is the SQLite error; without $onError the exception is thrown. Invalid arguments throw InvalidArgumentException before the call.
Limits
Section titled “Limits”- A database name matches
[A-Za-z0-9_.-]{1,128}. - A query returns at most 1000 rows, 256 columns and about one mebibyte. Beyond that the call fails with a message telling you to paginate: use
LIMITandOFFSET, which pairs naturally with a virtualized list. - A batch holds 1 to 10,000 statements or argument sets.
Migrations
Section titled “Migrations”Migrations::run() brings a schema up to date. Each migration has a version from 1 and one statement or a list of them. The version lives in the database (PRAGMA user_version), so a fresh install runs every migration and an update runs only the new ones:
<?php
declare(strict_types=1);
namespace App\Services;
use Closure;use Pam\Desktop\Database\Migrations;
final class Database{ public const NAME = 'board.db';
public static function migrate(Closure $ready): void { Migrations::run(self::NAME, [ 1 => 'CREATE TABLE boards (id TEXT PRIMARY KEY, title TEXT NOT NULL)', 2 => [ 'CREATE TABLE cards (id TEXT PRIMARY KEY, board_id TEXT NOT NULL REFERENCES boards(id), title TEXT)', 'CREATE INDEX cards_board ON cards (board_id)', ], 3 => 'ALTER TABLE cards ADD COLUMN due_at INTEGER', ], function (int $version, array $applied) use ($ready): void { $ready($version, $applied); }); }}public function mount(): void{ Database::migrate(fn () => $this->load());}- Every pending migration and the new version are written in one transaction. When one fails, the error callback receives it and the database stays at its previous version.
- The version is checked again inside the transaction, so two windows starting together never run a migration twice.
- Keep old migrations as they are and only add new versions. A database already past the last version (opened by a newer build) is left alone.
Batches
Section titled “Batches”Insert many rows with one prepared statement and one transaction:
SQLite::executeMany( 'shop.db', 'INSERT INTO products (sku, name, price) VALUES (?, ?, ?)', array_map(static fn (array $p): array => [$p['sku'], $p['name'], $p['price']], $catalog), fn () => $this->imported = true, fn (NativeModuleException $error) => $this->error = $error->getMessage(),);Transactions
Section titled “Transactions”transaction() takes a list of statements with their arguments and applies them all or none:
SQLite::transaction('shop.db', [ ['sql' => 'UPDATE accounts SET balance = balance - ? WHERE id = ?', 'arguments' => [$amount, $from]], ['sql' => 'UPDATE accounts SET balance = balance + ? WHERE id = ?', 'arguments' => [$amount, $to]],], fn () => $this->status = 'Transferred.');Guarded transactions
Section titled “Guarded transactions”transactionIf() opens a BEGIN IMMEDIATE transaction, runs a read-only guard that must return one row with one integer column of 0 or 1, and runs the statements only when it is 1. Because the guard and the writes share the transaction, nothing can change the guarded rows in between:
SQLite::transactionIf( 'shop.db', 'SELECT COUNT(*) = 0 FROM payments WHERE order_id = ?', [$orderId], [ ['sql' => 'INSERT INTO payments (order_id, amount) VALUES (?, ?)', 'arguments' => [$orderId, $amount]], ['sql' => 'UPDATE orders SET paid = 1 WHERE id = ?', 'arguments' => [$orderId]], ], function (bool $applied): void { $this->message = $applied ? 'Paid.' : 'This order was already paid.'; },);Syncing with a server
Section titled “Syncing with a server”For data that must reach a server reliably, the bundled pushinbr/pam-desktop-sync package keeps a durable SQLite outbox with leases and bounded retries. See Bundled Packages.
Next steps
Section titled “Next steps”- Lists to show query results.
- Storage & Files for preferences and documents.