driver
XuAPI
pub fn driver() driver.Driver用途
返回该模块提供的数据库驱动实例(实现 database/sql/driver.Driver 接口)。
参数
无需传入参数。
返回值
driver.Driver — 该类型的值
#github.com/xulang-org/packages/sqliteSQLite 驱动采用纯 Xu sqlite_native 动态加载系统 libsqlite3(Windows 自动使用内置 winsqlite3.dll,支持 XU_SQLITE_LIBRARY 覆盖),无需手动预编译原生桥接。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
36_test_sqlite_pool/main.xu@main
sqlite #github.com/xulang-org/packages/sqlite
#database/sql
#os
#errors
fn remove_if_exists(p string) {
os.remove(p) >>> (e) {
<<
}
}
fn verify_sqlite_pool_deep() {
println("--- SQLite Connection Pool Deep Verification ---")
db_file := "/tmp/xu_sqlite_pool.db"
remove_if_exists(db_file + "")
$db := sqlite.open_pool(db_file + "", 5, 2)
// 1. Ping
db.ping() >>> (err) {
println(" [FAIL] SQLite pool ping failed: " + err.msg())
<<
}
println(" [PASS] SQLite Pool ping succeeded.")
// 2. Schema
db.exec("CREATE TABLE pool_items (id INTEGER PRIMARY KEY, title TEXT, value INT);") >>> (err) {
println(" [FAIL] Create table failed: " + err.msg())
<<
}
println(" [PASS] Schema created.")
// 3. Sequential & Concurrent Inserts through Pool
$i := 1
while i <= 5 {
db.exec("INSERT INTO pool_items (id, title, value) VALUES (?, ?, ?);", i, "item_" + str(i), i * 10) >>> (err) {
println(" [FAIL] Insert failed at item " + str(i) + ": " + err.msg())
<<
}
i = i + 1
}
println(" [PASS] 5 items inserted through SQLite connection pool.")
// 4. Query through Pool
$rows := db.query("SELECT id, title, value FROM pool_items ORDER BY id ASC;") >>> (err) {
println(" [FAIL] Query failed: " + err.msg())
<<
}
$count := 0
while rows.next() {
vals := rows.take() >>> (err) {
<<
}
count = count + 1
}
rows.close()
println(" [PASS] SQLite Pool query verified: retrieved " + str(count) + " rows.")
// 5. Close Pool
db.close() >>> (err) {
println(" [FAIL] Close failed: " + err.msg())
<<
}
os.remove(db_file + "") >>> (e) {
<<
}
println(" [PASS] SQLite Pool closed cleanly.")
}
fn main() {
println("=== SQLite Connection Pool In-Depth Verification ===\n")
verify_sqlite_pool_deep()
println("\n[SQLITE POOL VERIFICATION 100% COMPLETED]")
}22_standalone_sqlite/main.xu@main
#database/sql/driver
sqlite #github.com/xulang-org/packages/sqlite
#os
#strings
fn remove_if_exists(p string) {
os.remove(p) >>> (err) {
<<
}
}
fn main() int {
println("=== SQLite Standalone Library Integration Test ===")
db_file := "/tmp/xu_sqlite_test.db"
remove_if_exists(strings.clone(&db_file))
// 1. 打开数据库连接
conn := sqlite.open(strings.clone(&db_file))
// 2. 建表
schema := "CREATE TABLE IF NOT EXISTS developers (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INT, salary REAL, active INT);"
res1 := conn.exec(schema, []driver.NamedValue{})
if res1.err.len > 0 {
println("CREATE TABLE error: " + res1.err)
<< 1
}
println("1. Schema created successfully.")
// 3. 参数化插入记录
$args1 := []driver.NamedValue{}
args1.push(driver.NamedValue{name: "name", ordinal: 1, value: "Ada Lovelace"})
args1.push(driver.NamedValue{name: "age", ordinal: 2, value: 36})
args1.push(driver.NamedValue{name: "salary", ordinal: 3, value: 125000.5})
args1.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
res_ins1 := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", args1)
if res_ins1.err.len > 0 {
println("INSERT 1 error: " + res_ins1.err)
<< 1
}
println("2. Inserted developer 1, id=" + str(res_ins1.last_insert_id) + ", rows_affected=" + str(res_ins1.rows_affected))
// 4. 第二条记录插入
$args2 := []driver.NamedValue{}
args2.push(driver.NamedValue{name: "name", ordinal: 1, value: "Alan Turing"})
args2.push(driver.NamedValue{name: "age", ordinal: 2, value: 41})
args2.push(driver.NamedValue{name: "salary", ordinal: 3, value: 140000.0})
args2.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
res_ins2 := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", args2)
if res_ins2.err.len > 0 {
println("INSERT 2 error: " + res_ins2.err)
<< 1
}
println("3. Inserted developer 2, id=" + str(res_ins2.last_insert_id) + ", rows_affected=" + str(res_ins2.rows_affected))
// 5. 查询所有记录
rows := conn.query("SELECT id, name, age, salary, active FROM developers ORDER BY id ASC;", []driver.NamedValue{})
if rows.err.len > 0 {
println("QUERY error: " + rows.err)
<< 1
}
println("4. Query returned columns count: " + str(rows.ncols))
$col_str := " Columns: "
for ci in 0:rows.columns.len {
if ci > 0 {
col_str = col_str + ", "
}
col_str = col_str + rows.columns[ci]
}
println(col_str)
nrows := rows.cells.len / rows.ncols
println(" Total rows: " + str(nrows))
$r := 0
while r < nrows {
base := r * rows.ncols
id_str := rows.cells[base]
name_str := rows.cells[base + 1]
age_str := rows.cells[base + 2]
sal_str := rows.cells[base + 3]
act_str := rows.cells[base + 4]
println(" Row " + str(r + 1) + ": id=" + id_str + ", name=" + name_str + ", age=" + age_str + ", salary=" + sal_str + ", active=" + act_str)
r = r + 1
}
// 6. 带参数的查询
$qargs := []driver.NamedValue{}
qargs.push(driver.NamedValue{name: "name", ordinal: 1, value: "Alan Turing"})
rows_filtered := conn.query("SELECT id, name, age FROM developers WHERE name = ?;", qargs)
if rows_filtered.err.len > 0 {
println("FILTERED QUERY error: " + rows_filtered.err)
<< 1
}
if rows_filtered.cells.len >= 3 {
println("5. Parameterized query successful: found " + rows_filtered.cells[1] + ", age=" + rows_filtered.cells[2])
}
// 7. 更新操作
$uargs := []driver.NamedValue{}
uargs.push(driver.NamedValue{name: "age", ordinal: 1, value: 37})
uargs.push(driver.NamedValue{name: "id", ordinal: 2, value: 1})
res_up := conn.exec("UPDATE developers SET age = ? WHERE id = ?;", uargs)
if res_up.err.len > 0 {
println("UPDATE error: " + res_up.err)
<< 1
}
println("6. Updated developer 1: rows_affected=" + str(res_up.rows_affected))
// 8. 事务支持
tx := conn.begin()
$tx_args := []driver.NamedValue{}
tx_args.push(driver.NamedValue{name: "name", ordinal: 1, value: "Grace Hopper"})
tx_args.push(driver.NamedValue{name: "age", ordinal: 2, value: 85})
tx_args.push(driver.NamedValue{name: "salary", ordinal: 3, value: 160000.0})
tx_args.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
res_tx := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", tx_args)
if res_tx.err.len > 0 {
println("TX INSERT error: " + res_tx.err)
<< 1
}
commit_err := tx.commit()
if commit_err.len > 0 {
println("TX COMMIT error: " + commit_err)
<< 1
}
println("7. Transaction committed successfully.")
// 9. 关闭与清理
conn.close()
remove_if_exists(db_file)
println("=== SQLite Standalone Tests Completed Successfully! ===")
<< 0
}driverpub fn driver() driver.Driver返回该模块提供的数据库驱动实例(实现 database/sql/driver.Driver 接口)。
无需传入参数。
driver.Driver — 该类型的值
openpub fn open(path string) driver.Conn打开资源或建立网络连接。
path stringdriver.Conn — 该类型的值
struct SqliteDriverpub struct SqliteDriver {
pub tag int
pub path string
}承载 tag、path 等属性的结构体。
tag intpath stringstruct SqliteConnpub struct SqliteConn {
pub st chan SqliteState
}承载 st 等属性的结构体。
st chan SqliteStatestruct SqliteStmtpub struct SqliteStmt {
pub st chan SqliteState
pub q string
}承载 st、q 等属性的结构体。
st chan SqliteStateq stringstruct SqliteTxpub struct SqliteTx {
pub st chan SqliteState
pub id int
pub err string
}承载 st、id、err 等属性的结构体。
st chan SqliteStateid interr stringopen_dbpub fn open_db(path string) sql.DB执行 open db 操作。
path stringsql.DB — 该类型的值
open_poolpub fn open_pool(path string, max_open int, max_idle int) sql.DB执行 open pool 操作。
path stringmax_open intmax_idle intsql.DB — 该类型的值