參考文件
libSQL Flutter / Dart 參考文件
Flutter / Dart 的 libSQL 套件包含使用 Turso / libSQL 所需的一切,且能完美支援所有功能,因為它是建構於 libSQL Rust crate 與 flutter_rust_bridge 套件之上,讓 Rust 與 Dart 之間能夠順暢通訊。
將套件加入你的專案
flutter pub add libsql_dart
或者,手動將它加入專案的 pubspec.yaml
libsql_dart:
初始化
呼叫 LibsqlClient 建構函式來建立資料庫用戶端。支援不同的設定方式,可連線至記憶體內資料庫、本機 SQLite 檔案、遠端 Turso / libSQL 資料庫,或嵌入式副本。
記憶體內資料庫
libSQL 支援連線至記憶體內資料庫,適用於你不需要持久化的情況:
final client = LibsqlClient(":memory:");
本機開發
你可以在本機使用 SQLite 檔案,並將路徑傳給 LibsqlClient:
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';
final client = LibsqlClient(path);
遠端
你可以傳入 Turso 資料庫 URL 來使用遠端資料庫:
final client = LibsqlClient('<TURSO_OR_LIBSQL_URL>')
..authToken = '<TOKEN>';
嵌入式副本
你可以將 Turso 資料庫 URL 傳給 syncUrl 來使用嵌入式副本:
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';
final client = LibsqlClient(path)
..authToken = '<TOKEN>'
..syncUrl = '<TURSO_OR_LIBSQL_URL>'
..readYourWrites = true;
嵌入式副本僅能在你具有檔案系統存取權限的環境中運作。
連線
await client.connect();
手動同步
sync() 函式可讓你手動將本機資料庫與遠端資料庫同步:
await client.sync();
定期同步
你可以在建立用戶端時設定 syncIntervalSeconds 屬性,以固定間隔自動同步:
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';
final client = LibsqlClient(path)
..authToken = '<TOKEN>'
..syncUrl = '<TURSO_OR_LIBSQL_URL>'
..syncIntervalSeconds = 5
..readYourWrites = true;
靜態加密
若要為 SQLite 檔案啟用加密,請傳入 encryptionKey:
final dir = await getApplicationCacheDirectory();
final path = '${dir.path}/local.db';
final client = LibsqlClient(path)..encryptionKey = '<KEY>';
加密後的資料庫會以原始資料的形式呈現,無法以標準 SQLite 資料庫的方式讀取。你必須使用 libSQL 用戶端進行任何操作 — 深入了解。
執行
回傳受影響的資料列數量:
await client.execute("create table if not exists customers (id integer primary key, name text);");
查詢
以 List<Map<String, dynamic>> 回傳資料列。若執行的不是 select 查詢,則會回傳空清單:
await client.query("insert into customers(name) values ('John Doe')");
print(await client.query("select * from customers"));
預留位置
libSQL 支援在 SQL 陳述式中使用位置型與具名預留位置:
final statement = await client
.prepare("select * from customers where id = ?");
await statement.query(positional: [1])
final statement = await client
.prepare("select * from customers where id = :id");
await statement.query(named: {"id": 1})
libSQL 支援與 SQLite 相同的具名預留位置字元 — :、@ 與 $。
批次交易
批次由多個 SQL 陳述式組成,會在隱含的交易中依序執行。後端會負責處理交易:成功時會提交所有變更,而任何失敗都會導致完整回滾,不會留下任何修改。
await client.batch("""insert into customers (name) values ('Jane Doe'); insert into customers (name) values ('Jake Doe');""");
交易模式
| 模式 | SQLite 命令 | 說明 |
|---|---|---|
LibsqlTransactionBehavior.immediate |
BEGIN IMMEDIATE |
此交易可執行讀取與寫入資料的陳述式。在複本上執行的寫入交易會被轉送至主要執行個體,且無法平行執行。 |
LibsqlTransactionBehavior.readOnly |
BEGIN TRANSACTION READONLY |
此交易僅能執行讀取資料(select)的陳述式。讀取交易可在複本上執行,並可與其他讀取交易平行執行。 |
LibsqlTransactionBehavior.deferred_ |
BEGIN DEFERRED |
交易會以讀取模式開始,一旦執行寫入陳述式就會切換為寫入模式。若主要執行個體目前正在執行寫入交易,此模式切換可能會失敗。 |
互動式交易
SQLite 中的互動式交易可確保交易範圍內一連串讀取與寫入操作的一致性。這些交易讓你控制何時提交或回滾變更,並將其與其他用戶端活動隔離。
| 方法 | 說明 |
|---|---|
execute() |
與 execute() 類似,差別在於是在交易的情境中執行 |
query() |
與 query() 類似,差別在於是在交易的情境中執行 |
commit() |
提交交易中的所有寫入陳述式 |
rollback() |
回滾整個交易 |
final tx = await client.transaction();
await tx
.execute("update customers set name = 'John Noe' where id = 1");
await tx
.execute("update customers set name = 'Jane Noe' where id = 2");
print(await tx
.query("select * from customers where id = ?", positional: [1]));
await tx.commit();
libSQL 中的互動式交易會鎖定資料庫的寫入權限,直到提交或回滾為止,並有 5 秒逾時。在高延遲或忙碌的資料庫上,可能會影響效能。
ATTACH
你可以使用 ATTACH 附加功能,將多個資料庫附加到目前的連線:
final tx = await client.transaction(behavior: LibsqlTransactionBehavior.readOnly);
await tx.execute("ATTACH "<database-id>" AS attached");
print(await tx.execute("SELECT * FROM attached.customers"));
await tx.commit();