XU XUv0.16.0

#github.com/xulang-org/packages/neo4j

导入方式: neo4j #github.com/xulang-org/packages/neo4j

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

扩展包 `neo4j #github.com/xulang-org/packages/neo4j`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

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

41_standalone_neo4j/main.xu

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

Xu本地 SDK 示例
@main

neo4j #github.com/xulang-org/packages/neo4j
#errors
#strings

fn verify_neo4j_pool() {
    println("--- 1. Testing Neo4j Connection Pool ---")
    $p := neo4j.open_pool("http://127.0.0.1:7474", "neo4j", "secret123", "neo4j", 5)

    // 1. Connection Reuse
    fd1 := p.get_conn()
    println("   Borrow 1 fd: " + str(fd1))
    p.put_conn(fd1)

    fd2 := p.get_conn()
    println("   Borrow 2 (after return) fd: " + str(fd2))
    if fd1 == fd2 {
        println("   [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
    } else {
        println("   [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
    }

    // 2. Multi-connection concurrent borrow
    fd3 := p.get_conn()
    println("   Borrow 3 (concurrently) fd: " + str(fd3))
    if fd2 != fd3 {
        println("   [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
    } else {
        println("   [FAIL] Expected distinct fds for concurrent connections!")
    }
    p.put_conn(fd2)
    p.put_conn(fd3)

    p.close()
    println("   [PASS] Neo4j Pool closed cleanly.")
}

fn verify_neo4j_cypher_crud() {
    println("\n--- 2. Testing Neo4j Cypher Graph CRUD ---")
    $p := neo4j.open_pool("http://127.0.0.1:7474", "neo4j", "secret123", "neo4j", 5)

    // 1. Clean existing test nodes
    p.execute("MATCH (n:Person) DETACH DELETE n") >>> (err) {
        println("   [NOTE] Clean test nodes: " + err.msg())
        <<
    }
    println("   [PASS] Cleaned pre-existing test data.")

    // 2. Create Node
    c1 := "CREATE (a:Person {name: 'Alice', role: 'Engineer', age: 30}) RETURN a.name, a.role"
    res1 := p.execute(c1) >>> (err) {
        println("   [FAIL] Create Alice node failed: " + err.msg())
        p.close()
        <<
    }
    println("   [PASS] Created Node Alice: " + res1)

    // 3. Create another Node
    c2 := "CREATE (b:Person {name: 'Bob', role: 'Architect', age: 35}) RETURN b.name, b.role"
    res2 := p.execute(c2) >>> (err) {
        println("   [FAIL] Create Bob node failed: " + err.msg())
        p.close()
        <<
    }
    println("   [PASS] Created Node Bob: " + res2)

    // 4. Create Relationship (Graph Edge)
    c3 := "MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'}) CREATE (a)-[r:COLLABORATES_WITH {project: 'Xu'}]->(b) RETURN type(r)"
    res3 := p.execute(c3) >>> (err) {
        println("   [FAIL] Create Relationship failed: " + err.msg())
        p.close()
        <<
    }
    println("   [PASS] Created Relationship COLLABORATES_WITH: " + res3)

    // 5. Query Graph Match
    c4 := "MATCH (a:Person)-[r:COLLABORATES_WITH]->(b:Person) RETURN a.name, type(r), b.name"
    res4 := p.execute(c4) >>> (err) {
        println("   [FAIL] Query Graph failed: " + err.msg())
        p.close()
        <<
    }
    println("   [PASS] Query Graph Traversal Result: " + res4)

    // 6. Detach Delete & Cleanup
    del_res2 := p.execute("MATCH (n:Person) DETACH DELETE n") >>> (err) {
        println("   [FAIL] Cleanup failed: " + err.msg())
        p.close()
        <<
    }
    println("   [PASS] Cleaned up graph test data.")

    p.close()
}

fn main() {
    println("=== Xu Neo4j Graph Database Native Driver Verification ===\n")
    verify_neo4j_pool()
    verify_neo4j_cypher_crud()
    println("\n[NEO4J VERIFICATION 100% COMPLETED]")
}

API 声明

struct Neo4jClient

XuAPI
pub struct Neo4jClient {
    pub host string
    pub port int
    pub secure bool
    pub user string
    pub password string
    pub database string
    pub auth_header string
}

用途

Neo4j 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
user string
字段(string)
password string
字段(string)
database string
字段(string)
auth_header string
字段(string)

new_client

XuAPI
pub fn new_client(endpoint string, user string, pass string, db string) Neo4jClient

用途

构造 client。

参数

endpoint string
endpoint(string)
user string
user(string)
pass string
pass(string)
db string
db(string)

返回值

Neo4jClient — 该类型的值

(Neo4jClient) execute

XuAPI
pub fn (c Neo4jClient) execute(stmt string) string

用途

Neo4jClient 上的操作:将数据上下文应用到模板并执行渲染。失败时抛出错误,调用方需处理或继续传播。

参数

c Neo4jClient
本次调用使用的 Neo4jClient 实例;按值接收。
stmt string
stmt(string)

返回值

string — 结果字符串

(Neo4jClient) execute_on_fd

XuAPI
pub fn (c Neo4jClient) execute_on_fd(fd int, stmt string, keep_alive bool) string

用途

Neo4jClient 上的操作:执行 execute on fd 操作。失败时抛出错误,调用方需处理或继续传播。

参数

c Neo4jClient
本次调用使用的 Neo4jClient 实例;按值接收。
fd int
文件描述符(int)
stmt string
stmt(string)
keep_alive bool
keep_alive(bool)

返回值

string — 结果字符串

struct Pool

XuAPI
pub struct Pool {
    pub client Neo4jClient
    pub state chan pool.State
    pub max_idle int
}

用途

Neo4j 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

client Neo4jClient
字段(Neo4jClient)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(endpoint string, user string, pass string, db string, max_idle int) Pool

用途

构造 pool。

参数

endpoint string
endpoint(string)
user string
user(string)
pass string
pass(string)
db string
db(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

用途

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

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Pool — 该类型的值

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) discard_conn

XuAPI
pub fn (p &Pool) discard_conn(fd int)

用途

&Pool 上的操作:丢弃连接。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

open_pool

XuAPI
pub fn open_pool(endpoint string, user string, pass string, db string, max_idle int) Pool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
user string
user(string)
pass string
pass(string)
db string
db(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

(&Pool) execute

XuAPI
pub fn (p &Pool) execute(stmt string) string

用途

&Pool 上的操作:将数据上下文应用到模板并执行渲染。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
stmt string
stmt(string)

返回值

string — 结果字符串

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。