XU XUv0.16.0

#database/sql/driver

导入方式: #database/sql/driver

公开 API: 8 函数, 0 方法, 7 类型, 0 常量, 5 方法集

驱动快照 RowsResult 与 Conn/Stmt/Tx/Driver。

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

34_standalone_postgres/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@main

postgres #github.com/xulang-org/packages/postgres
#database/sql/driver
#context

fn verify_postgres_live() {
    println("--- Testing Native Postgres Driver Wire Protocol ---")
    dsn := "postgres://zicq:zicq@127.0.0.1:5432/xu_demo?sslmode=disable"
    $conn := postgres.open(dsn)

    // 1. Ping
    err := conn.ping()
    if err.len > 0 {
        println("   [FAIL] Ping failed: " + err)
        conn.close()
        <<
    }
    println("   [PASS] Postgres Ping succeeded.")

    // 2. Exec DDL
    r1 := conn.exec("DROP TABLE IF EXISTS xu_heroes;", []driver.NamedValue{})
    if r1.err.len > 0 {
        println("   [FAIL] Drop table error: " + r1.err)
        conn.close()
        <<
    }
    r2 := conn.exec("CREATE TABLE xu_heroes (id INTEGER PRIMARY KEY, name VARCHAR(50), power INTEGER);", []driver.NamedValue{})
    if r2.err.len > 0 {
        println("   [FAIL] Create table error: " + r2.err)
        conn.close()
        <<
    }
    println("   [PASS] Table 'xu_heroes' created.")

    // 3. Exec DML Insert
    $args1 := []driver.NamedValue{}
    args1.push(driver.NamedValue{ordinal: 1, name: "", value: 1})
    args1.push(driver.NamedValue{ordinal: 2, name: "", value: "Ironman"})
    args1.push(driver.NamedValue{ordinal: 3, name: "", value: 100})
    r3 := conn.exec("INSERT INTO xu_heroes (id, name, power) VALUES ($$1, $$2, $$3);", args1)
    if r3.err.len > 0 {
        println("   [FAIL] Insert error: " + r3.err)
        conn.close()
        <<
    }
    println("   [PASS] Hero inserted (rows=" + str(r3.rows_affected) + ").")

    // 4. Query
    $q_args := []driver.NamedValue{}
    q_args.push(driver.NamedValue{ordinal: 1, name: "", value: 1})
    rows := conn.query("SELECT id, name, power FROM xu_heroes WHERE id = $$1;", q_args)
    if rows.err.len > 0 {
        println("   [FAIL] Query error: " + rows.err)
        conn.close()
        <<
    }
    println("   [PASS] Query executed, columns count: " + str(rows.columns.len))
    $i := 0
    while i < rows.columns.len {
        println("      Col[" + str(i) + "]: " + rows.columns[i])
        i = i + 1
    }
    println("   Total cells retrieved: " + str(rows.cells.len))
    $j := 0
    while j < rows.cells.len {
        println("      Cell[" + str(j) + "]: " + rows.cells[j])
        j = j + 1
    }

    // 5. Transaction
    println("   Testing Transaction Commit...")
    $tx := conn.begin()
    $args2 := []driver.NamedValue{}
    args2.push(driver.NamedValue{ordinal: 1, name: "", value: 2})
    args2.push(driver.NamedValue{ordinal: 2, name: "", value: "Thor"})
    args2.push(driver.NamedValue{ordinal: 3, name: "", value: 200})
    tx_res := conn.exec("INSERT INTO xu_heroes (id, name, power) VALUES ($$1, $$2, $$3);", args2)
    if tx_res.err.len > 0 {
        println("   [FAIL] Tx insert error: " + tx_res.err)
    }
    tx_err := tx.commit()
    if tx_err.len > 0 {
        println("   [FAIL] Tx commit error: " + tx_err)
    } else {
        println("   [PASS] Transaction committed.")
    }

    // 6. Reset session
    ctx := context.background()
    reset_err := conn.reset_session(&ctx)
    if reset_err.len > 0 {
        println("   [FAIL] Reset session error: " + reset_err)
    } else {
        println("   [PASS] Reset session completed.")
    }

    // 7. Close
    close_err := conn.close()
    if close_err.len > 0 {
        println("   [FAIL] Close error: " + close_err)
    } else {
        println("   [PASS] Connection closed cleanly.")
    }
}

fn verify_postgres_pool() {
    println("\n--- Testing Native Postgres Connection Pool ---")
    dsn := "postgres://zicq:zicq@127.0.0.1:5432/xu_demo?sslmode=disable"
    $pool := postgres.open_pool(dsn + "", 5, 2)

    pool.ping() >>> (err) {
        println("   [FAIL] Pool ping error: " + err.msg())
        <<
    }
    println("   [PASS] Postgres Pool ping succeeded.")

    // 1. DDL
    pool.exec("DROP TABLE IF EXISTS xu_pool_users;") >>> (err) {
        println("   [FAIL] Drop table error: " + err.msg())
        <<
    }
    pool.exec("CREATE TABLE xu_pool_users (id INTEGER PRIMARY KEY, name VARCHAR(50), balance INTEGER);") >>> (err) {
        println("   [FAIL] Create table error: " + err.msg())
        <<
    }
    println("   [PASS] Pool created table xu_pool_users.")

    // 2. Concurrency / multiple inserts
    $i := 1
    while i <= 5 {
        pool.exec("INSERT INTO xu_pool_users (id, name, balance) VALUES ($$1, $$2, $$3);", i, "User_" + str(i), i * 100) >>> (err) {
            println("   [FAIL] Insert row error: " + err.msg())
            <<
        }
        i = i + 1
    }
    println("   [PASS] Concurrently inserted 5 rows across pooled connections.")

    // 3. Query
    $rows := pool.query("SELECT id, name, balance FROM xu_pool_users ORDER BY id;") >>> (err) {
        println("   [FAIL] Pool query error: " + err.msg())
        <<
    }
    $count := 0
    while rows.next() {
        vals := rows.take() >>> (err) {
            println("   [FAIL] rows.take error: " + err.msg())
            <<
        }
        uid := vals[0] as int
        uname := vals[1] as string
        ubal := vals[2] as int
        println("      Row[" + str(count) + "]: id=" + str(uid) + ", name=" + uname + ", bal=" + str(ubal))
        count = count + 1
    }
    rows.close()
    if count == 5 {
        println("   [PASS] Pool query verified 5 rows.")
    } else {
        println("   [FAIL] Row count mismatch: " + str(count))
    }

    // 4. Transaction via Pool
    $tx := pool.begin() >>> (err) {
        println("   [FAIL] Pool begin tx error: " + err.msg())
        <<
    }
    tx.exec("INSERT INTO xu_pool_users (id, name, balance) VALUES ($$1, $$2, $$3);", 6, "User_6", 600) >>> (err) {
        println("   [FAIL] Tx insert error: " + err.msg())
        <<
    }
    tx.commit() >>> (err) {
        println("   [FAIL] Tx commit error: " + err.msg())
        <<
    }
    println("   [PASS] Transaction committed via pool.")

    // 5. Cleanup & Close
    pool.exec("DROP TABLE IF EXISTS xu_pool_users;") >>> (err) {
        println("   [FAIL] Cleanup table error: " + err.msg())
        <<
    }
    pool.close() >>> (err) {
        println("   [FAIL] Pool close error: " + err.msg())
        <<
    }
    println("   [PASS] Postgres Pool closed cleanly.")
}

fn main() {
    println("=== Xu PostgreSQL Native Driver Verification ===\n")
    verify_postgres_live()
    verify_postgres_pool()
    println("\n[POSTGRES VERIFICATION 100% COMPLETED]")
}
35_standalone_mysql/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@main

mysql #github.com/xulang-org/packages/mysql
#database/sql/driver
#context

fn verify_mysql_live() {
    println("--- Testing Native MySQL/MariaDB Driver Wire Protocol ---")
    dsn := "root:root@tcp(127.0.0.1:3306)/xu_demo?tls=false"
    $conn := mysql.open(dsn)

    // 1. Ping
    err := conn.ping()
    if err.len > 0 {
        println("   [FAIL] Ping failed: " + err)
        conn.close()
        <<
    }
    println("   [PASS] MySQL/MariaDB Ping succeeded.")

    // 2. Exec DDL
    r1 := conn.exec("DROP TABLE IF EXISTS xu_developers;", []driver.NamedValue{})
    if r1.err.len > 0 {
        println("   [FAIL] Drop table error: " + r1.err)
        conn.close()
        <<
    }
    r2 := conn.exec("CREATE TABLE xu_developers (id INT PRIMARY KEY, name VARCHAR(50), commits INT);", []driver.NamedValue{})
    if r2.err.len > 0 {
        println("   [FAIL] Create table error: " + r2.err)
        conn.close()
        <<
    }
    println("   [PASS] Table 'xu_developers' created.")

    // 3. Exec DML Insert
    $args1 := []driver.NamedValue{}
    args1.push(driver.NamedValue{ordinal: 1, name: "", value: 1})
    args1.push(driver.NamedValue{ordinal: 2, name: "", value: "Antigravity"})
    args1.push(driver.NamedValue{ordinal: 3, name: "", value: 500})
    r3 := conn.exec("INSERT INTO xu_developers (id, name, commits) VALUES (?, ?, ?);", args1)
    if r3.err.len > 0 {
        println("   [FAIL] Insert error: " + r3.err)
        conn.close()
        <<
    }
    println("   [PASS] Developer inserted (rows=" + str(r3.rows_affected) + ").")

    // 4. Query
    $q_args := []driver.NamedValue{}
    q_args.push(driver.NamedValue{ordinal: 1, name: "", value: 1})
    rows := conn.query("SELECT id, name, commits FROM xu_developers WHERE id = ?;", q_args)
    if rows.err.len > 0 {
        println("   [FAIL] Query error: " + rows.err)
        conn.close()
        <<
    }
    println("   [PASS] Query executed, columns count: " + str(rows.columns.len))
    $i := 0
    while i < rows.columns.len {
        println("      Col[" + str(i) + "]: " + rows.columns[i])
        i = i + 1
    }
    println("   Total cells retrieved: " + str(rows.cells.len))
    $j := 0
    while j < rows.cells.len {
        println("      Cell[" + str(j) + "]: " + rows.cells[j])
        j = j + 1
    }

    // 5. Transaction
    println("   Testing Transaction Commit...")
    $tx := conn.begin()
    $args2 := []driver.NamedValue{}
    args2.push(driver.NamedValue{ordinal: 1, name: "", value: 2})
    args2.push(driver.NamedValue{ordinal: 2, name: "", value: "XuArchitect"})
    args2.push(driver.NamedValue{ordinal: 3, name: "", value: 1000})
    tx_res := conn.exec("INSERT INTO xu_developers (id, name, commits) VALUES (?, ?, ?);", args2)
    if tx_res.err.len > 0 {
        println("   [FAIL] Tx insert error: " + tx_res.err)
    }
    tx_err := tx.commit()
    if tx_err.len > 0 {
        println("   [FAIL] Tx commit error: " + tx_err)
    } else {
        println("   [PASS] Transaction committed.")
    }

    // 6. Reset session
    ctx := context.background()
    reset_err := conn.reset_session(&ctx)
    if reset_err.len > 0 {
        println("   [FAIL] Reset session error: " + reset_err)
    } else {
        println("   [PASS] Reset session completed.")
    }

    // 7. Close
    close_err := conn.close()
    if close_err.len > 0 {
        println("   [FAIL] Close error: " + close_err)
    } else {
        println("   [PASS] Connection closed cleanly.")
    }
}

fn verify_mysql_pool() {
    println("\n--- Testing Native MySQL/MariaDB Connection Pool ---")
    dsn := "root:root@tcp(127.0.0.1:3306)/xu_demo?tls=false"
    $pool := mysql.open_pool(dsn + "", 5, 2)

    pool.ping() >>> (err) {
        println("   [FAIL] Pool ping error: " + err.msg())
        <<
    }
    println("   [PASS] MySQL Pool ping succeeded.")

    // 1. DDL
    pool.exec("DROP TABLE IF EXISTS xu_pool_orders;") >>> (err) {
        println("   [FAIL] Drop table error: " + err.msg())
        <<
    }
    pool.exec("CREATE TABLE xu_pool_orders (id INT PRIMARY KEY, title VARCHAR(50), amount INT);") >>> (err) {
        println("   [FAIL] Create table error: " + err.msg())
        <<
    }
    println("   [PASS] Pool created table xu_pool_orders.")

    // 2. Concurrency / multiple inserts
    $i := 1
    while i <= 5 {
        pool.exec("INSERT INTO xu_pool_orders (id, title, amount) VALUES (?, ?, ?);", i, "Order_" + str(i), i * 150) >>> (err) {
            println("   [FAIL] Insert row error: " + err.msg())
            <<
        }
        i = i + 1
    }
    println("   [PASS] Concurrently inserted 5 rows across pooled connections.")

    // 3. Query
    $rows := pool.query("SELECT id, title, amount FROM xu_pool_orders ORDER BY id;") >>> (err) {
        println("   [FAIL] Pool query error: " + err.msg())
        <<
    }
    $count := 0
    while rows.next() {
        vals := rows.take() >>> (err) {
            println("   [FAIL] rows.take error: " + err.msg())
            <<
        }
        oid := vals[0] as int
        otitle := vals[1] as string
        oamt := vals[2] as int
        println("      Row[" + str(count) + "]: id=" + str(oid) + ", title=" + otitle + ", amount=" + str(oamt))
        count = count + 1
    }
    rows.close()
    if count == 5 {
        println("   [PASS] Pool query verified 5 rows.")
    } else {
        println("   [FAIL] Row count mismatch: " + str(count))
    }

    // 4. Transaction via Pool
    $tx := pool.begin() >>> (err) {
        println("   [FAIL] Pool begin tx error: " + err.msg())
        <<
    }
    tx.exec("INSERT INTO xu_pool_orders (id, title, amount) VALUES (?, ?, ?);", 6, "Order_6", 900) >>> (err) {
        println("   [FAIL] Tx insert error: " + err.msg())
        <<
    }
    tx.commit() >>> (err) {
        println("   [FAIL] Tx commit error: " + err.msg())
        <<
    }
    println("   [PASS] Transaction committed via pool.")

    // 5. Cleanup & Close
    pool.exec("DROP TABLE IF EXISTS xu_pool_orders;") >>> (err) {
        println("   [FAIL] Cleanup table error: " + err.msg())
        <<
    }
    pool.close() >>> (err) {
        println("   [FAIL] Pool close error: " + err.msg())
        <<
    }
    println("   [PASS] MySQL Pool closed cleanly.")
}

fn main() {
    println("=== Xu MySQL / MariaDB Native Driver Verification ===\n")
    verify_mysql_live()
    verify_mysql_pool()
    println("\n[MYSQL VERIFICATION 100% COMPLETED]")
}
22_standalone_sqlite/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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
}

API 声明

struct NamedValue

XuAPI
pub struct NamedValue {
    pub name string
    pub ordinal int
    pub value temp
}

用途

承载 name、ordinal、value 等属性的结构体。

公开字段

name string
名字
ordinal int
字段(int)
value temp
字段(temp)

查看用法示例

struct ExecResult

XuAPI
pub struct ExecResult {
    pub last_insert_id int
    pub rows_affected int
    pub err string
}

用途

承载 last_insert_id、rows_affected、err 等属性的结构体。

公开字段

last_insert_id int
字段(int)
rows_affected int
字段(int)
err string
错误

struct RowsResult

XuAPI
pub struct RowsResult {
    pub columns []string
    pub cells []string
    pub ncols int
    pub err string
}

用途

承载 columns、cells、ncols、err 等属性的结构体。

公开字段

columns []string
字段([]string)
cells []string
字段([]string)
ncols int
字段(int)
err string
错误

struct Number

XuAPI
pub struct Number {
    pub n int
    pub ok bool
}

用途

承载 n、ok 等属性的结构体。

公开字段

n int
数量、长度或上限
ok bool
字段(bool)

struct NullInfo

XuAPI
pub struct NullInfo {
    pub nullable bool
    pub ok bool
}

用途

承载 nullable、ok 等属性的结构体。

公开字段

nullable bool
字段(bool)
ok bool
字段(bool)

struct DecimalSize

XuAPI
pub struct DecimalSize {
    pub precision int
    pub scale int
    pub ok bool
}

用途

承载 precision、scale、ok 等属性的结构体。

公开字段

precision int
字段(int)
scale int
字段(int)
ok bool
字段(bool)

struct Null

XuAPI
pub struct Null {

}

用途

Null 上下文或状态结构体。

公开字段

此类型没有公开字段。

struct@ Stmt

XuAPI
struct@ Stmt {
    fn num_input() int
    fn exec([]NamedValue) ExecResult
    fn query([]NamedValue) RowsResult
    fn close() string
}

用途

方法集契约约束 Stmt,实现类型须提供其定义的方法。

要求的方法

num_input fn num_input() int
Stmt 上的操作:执行 num input 操作。
exec fn exec([]NamedValue) ExecResult
执行 SQL 语句或服务命令,返回受影响行数或执行结果。
query fn query([]NamedValue) RowsResult
Stmt 上的操作:查询串。
close fn close() string
Stmt 上的操作:关闭资源。

(Stmt) num_input

XuAPI
fn num_input() int

用途

Stmt 上的操作:执行 num input 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

int — 该类型的值

(Stmt) exec

XuAPI
fn exec([]NamedValue) ExecResult

用途

执行 SQL 语句或服务命令,返回受影响行数或执行结果。

参数

self Self
本次调用使用的 Self 实例;按值接收。
namedValue []NamedValue
namedValue([]NamedValue)

返回值

ExecResult — 该类型的值

(Stmt) query

XuAPI
fn query([]NamedValue) RowsResult

用途

Stmt 上的操作:查询串。

参数

self Self
本次调用使用的 Self 实例;按值接收。
namedValue []NamedValue
namedValue([]NamedValue)

返回值

RowsResult — 该类型的值

(Stmt) close

XuAPI
fn close() string

用途

Stmt 上的操作:关闭资源。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

string — 结果字符串

struct@ Tx

XuAPI
struct@ Tx {
    fn commit() string
    fn rollback() string
}

用途

方法集契约约束 Tx,实现类型须提供其定义的方法。

要求的方法

commit fn commit() string
在 Tx 上执行 commit 操作。
rollback fn rollback() string
在 Tx 上执行 rollback 操作。

(Tx) commit

XuAPI
fn commit() string

用途

在 Tx 上执行 commit 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

string — 结果字符串

(Tx) rollback

XuAPI
fn rollback() string

用途

在 Tx 上执行 rollback 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

string — 结果字符串

struct@ Conn

XuAPI
struct@ Conn {
    fn ping() string
    fn exec(string, []NamedValue) ExecResult
    fn query(string, []NamedValue) RowsResult
    fn close() string
    fn dup() Conn
    fn prepare(string) Stmt
    fn prepare_context(&context.Context, string) Stmt
    fn begin() Tx
    fn begin_tx(int, bool) Tx
    fn reset_session(&context.Context) string
    fn is_valid() bool
}

用途

方法集契约约束 Conn,实现类型须提供其定义的方法。

要求的方法

ping fn ping() string
发送心跳保活探测请求,检测连接活性与服务可用性。
exec fn exec(string, []NamedValue) ExecResult
执行 SQL 语句或服务命令,返回受影响行数或执行结果。
query fn query(string, []NamedValue) RowsResult
Conn 上的操作:查询串。
close fn close() string
Conn 上的操作:关闭资源。
dup fn dup() Conn
创建并返回持有独立状态或底层的副本对象。
prepare fn prepare(string) Stmt
在 Conn 上执行 prepare 操作。
prepare_context fn prepare_context(&context.Context, string) Stmt
Conn 上的操作:执行 prepare context 操作。
begin fn begin() Tx
在 Conn 上执行 begin 操作。
begin_tx fn begin_tx(int, bool) Tx
Conn 上的操作:执行 begin tx 操作。
reset_session fn reset_session(&context.Context) string
Conn 上的操作:执行 reset session 操作。
is_valid fn is_valid() bool
Conn 上的操作:判断是否 valid。

(Conn) ping

XuAPI
fn ping() string

用途

发送心跳保活探测请求,检测连接活性与服务可用性。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

string — 结果字符串

(Conn) exec

XuAPI
fn exec(string, []NamedValue) ExecResult

用途

执行 SQL 语句或服务命令,返回受影响行数或执行结果。

参数

self Self
本次调用使用的 Self 实例;按值接收。
string string
string(string)
namedValue []NamedValue
namedValue([]NamedValue)

返回值

ExecResult — 该类型的值

(Conn) query

XuAPI
fn query(string, []NamedValue) RowsResult

用途

Conn 上的操作:查询串。

参数

self Self
本次调用使用的 Self 实例;按值接收。
string string
string(string)
namedValue []NamedValue
namedValue([]NamedValue)

返回值

RowsResult — 该类型的值

(Conn) close

XuAPI
fn close() string

用途

Conn 上的操作:关闭资源。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

string — 结果字符串

(Conn) dup

XuAPI
fn dup() Conn

用途

创建并返回持有独立状态或底层的副本对象。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

Conn — 该类型的值

(Conn) prepare

XuAPI
fn prepare(string) Stmt

用途

在 Conn 上执行 prepare 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。
string string
string(string)

返回值

Stmt — 该类型的值

(Conn) prepare_context

XuAPI
fn prepare_context(&context.Context, string) Stmt

用途

Conn 上的操作:执行 prepare context 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。
context &context.Context
context(只读借用,调用方仍持有原值;类型 &context.Context)
string string
string(string)

返回值

Stmt — 该类型的值

(Conn) begin

XuAPI
fn begin() Tx

用途

在 Conn 上执行 begin 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

Tx — 该类型的值

(Conn) begin_tx

XuAPI
fn begin_tx(int, bool) Tx

用途

Conn 上的操作:执行 begin tx 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。
int int
int(int)
bool bool
bool(bool)

返回值

Tx — 该类型的值

(Conn) reset_session

XuAPI
fn reset_session(&context.Context) string

用途

Conn 上的操作:执行 reset session 操作。

参数

self Self
本次调用使用的 Self 实例;按值接收。
context &context.Context
context(只读借用,调用方仍持有原值;类型 &context.Context)

返回值

string — 结果字符串

(Conn) is_valid

XuAPI
fn is_valid() bool

用途

Conn 上的操作:判断是否 valid。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

bool — 是否成立

struct@ Driver

XuAPI
struct@ Driver {
    fn open(string) Conn
    fn dup() Driver
}

用途

方法集契约约束 Driver,实现类型须提供其定义的方法。

要求的方法

open fn open(string) Conn
Driver 上的操作:打开资源或建立网络连接。
dup fn dup() Driver
创建并返回持有独立状态或底层的副本对象。

(Driver) open

XuAPI
fn open(string) Conn

用途

Driver 上的操作:打开资源或建立网络连接。

参数

self Self
本次调用使用的 Self 实例;按值接收。
string string
string(string)

返回值

Conn — 该类型的值

(Driver) dup

XuAPI
fn dup() Driver

用途

创建并返回持有独立状态或底层的副本对象。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

Driver — 该类型的值

struct@ Connector

XuAPI
struct@ Connector {
    fn connect(&context.Context) Conn
    fn driver() Driver
}

用途

方法集契约约束 Connector,实现类型须提供其定义的方法。

要求的方法

connect fn connect(&context.Context) Conn
建立与目标服务端点的网络通信连接。
driver fn driver() Driver
Connector 上的操作:返回该模块提供的数据库驱动实例(实现 database/sql/driver.Driver 接口)。

(Connector) connect

XuAPI
fn connect(&context.Context) Conn

用途

建立与目标服务端点的网络通信连接。

参数

self Self
本次调用使用的 Self 实例;按值接收。
context &context.Context
context(只读借用,调用方仍持有原值;类型 &context.Context)

返回值

Conn — 该类型的值

(Connector) driver

XuAPI
fn driver() Driver

用途

Connector 上的操作:返回该模块提供的数据库驱动实例(实现 database/sql/driver.Driver 接口)。

参数

self Self
本次调用使用的 Self 实例;按值接收。

返回值

Driver — 该类型的值

encode_value

XuAPI
pub fn encode_value(v temp) string

用途

编码 value 数据。

参数

v temp
值(temp)

返回值

string — 结果字符串

decode_value_checked

XuAPI
pub fn decode_value_checked(s string) temp

用途

解码 value checked 数据。失败时抛出错误,调用方需处理或继续传播。

参数

s string
源字符串(string)

返回值

temp — 该类型的值

decode_value

XuAPI
pub fn decode_value(s string) temp

用途

解码 value 数据。

参数

s string
源字符串(string)

返回值

temp — 该类型的值

exec_ok

XuAPI
pub fn exec_ok(id int, n int) ExecResult

用途

构造表示 SQL 语句执行成功的 ExecResult(包含最后插入 ID 与受影响行数)。

参数

id int
id(int)
n int
数量、长度或上限(int)

返回值

ExecResult — 该类型的值

exec_err

XuAPI
pub fn exec_err(msg string) ExecResult

用途

构造表示 SQL 语句执行失败并附带错误信息的 ExecResult。

参数

msg string
消息(string)

返回值

ExecResult — 该类型的值

rows_err

XuAPI
pub fn rows_err(msg string) RowsResult

用途

构造表示查询结果集出错的 RowsResult。

参数

msg string
消息(string)

返回值

RowsResult — 该类型的值

err_bad_conn

XuAPI
pub fn err_bad_conn() string

用途

执行 err bad conn 操作。

参数

无需传入参数。

返回值

string — 结果字符串

is_bad_conn

XuAPI
pub fn is_bad_conn(msg string) bool

用途

判断是否 bad conn。

参数

msg string
消息(string)

返回值

bool — 是否成立