XU XUv0.16.0

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

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

counts: 3 functions, 8 methods, 2 types, 0 constants, 0 method sets

Neo4j Graph Database Native Client for Xu Communicates directly with Neo4j via HTTP Cypher Transactional API.

github.com/xulang-org/packages · 1.17.2 · Install and use

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

41_standalone_neo4j/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@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 declarations

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
}

Purpose

Neo4j Client session maintaining connection and operations.

Public fields

host string
host
port int
port
secure bool
field (bool)
user string
field (string)
password string
field (string)
database string
field (string)
auth_header string
field (string)

new_client

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

Purpose

Construct client.

Parameters

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

Returns

Neo4jClient — a value of this type

(Neo4jClient) execute

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

Purpose

Operation on Neo4jClient: apply data context to template and execute rendering. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Neo4jClient
The Neo4jClient instance used by this call (value receiver).
stmt string
stmt (string)

Returns

string — result string

(Neo4jClient) execute_on_fd

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

Purpose

Operation on Neo4jClient: execute on fd. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Neo4jClient
The Neo4jClient instance used by this call (value receiver).
fd int
file descriptor (int)
stmt string
stmt (string)
keep_alive bool
keep_alive (bool)

Returns

string — result string

struct Pool

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

Purpose

Neo4j Connection pool manager scheduling reusable socket connections.

Public fields

client Neo4jClient
field (Neo4jClient)
state chan pool.State
field (channel; type chan pool.State)
max_idle int
field (int)

new_pool

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

Purpose

Construct pool.

Parameters

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

Returns

Pool — a value of this type

(&Pool) dup

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

Purpose

Copies configuration while sharing the connection and close lifecycle.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

Pool — a value of this type

(&Pool) get_conn

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

Purpose

Operation on &Pool: read conn.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

int — a value of this type

(&Pool) put_conn

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

Purpose

Operation on &Pool: put connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) discard_conn

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

Purpose

Operation on &Pool: discard connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

open_pool

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

Purpose

Open pool.

Parameters

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

Returns

Pool — a value of this type

See usage example

(&Pool) execute

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

Purpose

Operation on &Pool: apply data context to template and execute rendering. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
stmt string
stmt (string)

Returns

string — result string

(&Pool) close

XuAPI
pub fn (p &Pool) close()

Purpose

Operation on &Pool: close the resource.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

This call does not return a value.