6bfc5b3a3c
CI / test (push) Has been cancelled
CI / verify (push) Has been cancelled
Clients CI / build-server (push) Has been cancelled
Clients CI / test-python (push) Has been cancelled
Clients CI / test-javascript (push) Has been cancelled
Clients CI / test-nim (push) Has been cancelled
Clients CI / test-rust (push) Has been cancelled
- Pure Nim wire protocol client (async + sync, no C FFI) - Query Builder: fluent API, SQL generator, execution, transactions - Schema Builder: create/alter/drop tables, column operations - Connection pool with aging and timeout handling - ORM integration via compile-time DB_BARADB switch - Tests: test_open, test_query - Documentation: README.md, PLAN_BARADB.md
2.3 KiB
2.3 KiB
Example: Prepared Statement for SurrealDB
Warning
SurrealDB の prepared statement は server-side prepare ではなく、client-side template reuse と
/sqlの multi-statement 実行で再現しています。
index
About
Prepared statement の公開 API は他 driver に寄せていますが、SurrealDB では内部的に次の形へ展開します。
LET $a = "user:alice";
SELECT * FROM "user" WHERE "id" = $a;
? は $a, $b, ... に変換され、引数は LET で前置されます。これにより、1 リクエストで安全に再利用できるテンプレートとして扱えます。
Create Connection
import std/asyncdispatch
import allographer/connection
let surreal = dbOpen(SurrealDB, "test", "test", "user", "pass", "http://surreal", 8000, 5, 30, false, false).await
prepare
import std/asyncdispatch
import std/json
import allographer/query_builder
let stmt = surreal.prepare("""SELECT * FROM "user" WHERE "id" = ?""")
let rows = await stmt.get(@["user:alice"])
let row = await stmt.first(@["user:alice"])
await stmt.exec(@["user:alice"])
await stmt.close()
close() は logical close です。SurrealDB 側に物理 prepared handle はないため、キャッシュの整理は flushStmt() / clearStmtCache() で行います。
withConn
await surreal.withConn(
proc(ctx: SurrealPreparedContext): Future[void] {.async.} =
discard await stmt.first(ctx, @["user:alice"])
await stmt.exec(ctx, @["user:alice"])
)
withConn() は同じ connection index を使い回したいときの API です。将来の session 前提最適化や transaction helper とも相性がよい形にしてあります。
Cache Control
await surreal.flushStmt(stmt)
await surreal.clearStmtCache()
flushStmt(stmt) はその prepared statement を閉じて、対応するテンプレートをキャッシュから外します。clearStmtCache() はキャッシュ全体を空にします。