#github.com/xulang-org/packages/clickhouse
Xu 0.16.0 API
import: clickhouse #github.com/xulang-org/packages/clickhouse
counts: 5 functions, 15 methods, 3 types, 0 constants, 0 method sets
ClickHouse High-Performance Analytical Database (OLAP) Driver for Xu Features Native HTTP/1.1 REST communication via Xu standard library. SQL query execution with high-throughput batch insert. Connection pooling via reusable TCP socket channels. Full compatibility with ClickHouse 22.x/23.x/24.x+.
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.
29_standalone_clickhouse/main.xu
Required SDK: Xu 0.8.3 · Complete project · Download source
@main
clickhouse #github.com/xulang-org/packages/clickhouse
#errors
fn verify_clickhouse_basic() {
println("--- 1. Testing ClickHouse Client & Basic SQL ---")
$ch := clickhouse.open("http://127.0.0.1:8123", "default", "secret", "default")
// Ping check
pong := ch.ping()
println(" ClickHouse ping: " + str(pong))
// DDL: Create table
println(" Creating table 'xu_metrics'...")
ch.exec("CREATE TABLE IF NOT EXISTS xu_metrics (id UInt64, service String, latency Float64) ENGINE = Memory") >>> (err) {
println(" [FAIL] Create table error: " + err.msg())
<<
}
println(" [PASS] Table created.")
// DML: Insert records
println(" Inserting records...")
ch.insert("xu_metrics", "(1, 'order_service', 24.5), (2, 'user_service', 12.0), (3, 'order_service', 35.5)") >>> (err) {
println(" [FAIL] Insert error: " + err.msg())
<<
}
println(" [PASS] 3 rows inserted.")
// Query: Aggregation
println(" Running aggregation query...")
res := ch.query("SELECT service, count(), avg(latency) FROM xu_metrics GROUP BY service ORDER BY service") >>> (err) {
println(" [FAIL] Query error: " + err.msg())
<<
}
println(" Query Results (TSV):\n" + res)
// Cleanup: Drop table
ch.exec("DROP TABLE IF EXISTS xu_metrics") >>> (err) {
println(" [FAIL] Drop table error: " + err.msg())
<<
}
println(" [PASS] Table dropped successfully.")
}
fn verify_clickhouse_pool() {
println("\n--- 2. Testing ClickHouse Connection Pool ---")
$p := clickhouse.open_pool("http://127.0.0.1:8123", "default", "secret", "default", 5)
// 1. Connection Reuse Test
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)
// 3. Delegated Queries & Execution via Pool
println(" Testing delegated operations via pool...")
p.exec("CREATE TABLE IF NOT EXISTS xu_pool_events (id UInt64, name String) ENGINE = Memory") >>> (err) {
println(" [FAIL] Pool create table error: " + err.msg())
<<
}
p.exec("INSERT INTO xu_pool_events VALUES (101, 'login'), (102, 'checkout')") >>> (err) {
println(" [FAIL] Pool insert error: " + err.msg())
<<
}
rows := p.query_tsv("SELECT id, name FROM xu_pool_events ORDER BY id") >>> (err) {
println(" [FAIL] Pool query_tsv error: " + err.msg())
<<
}
println(" Retrieved rows count via pool: " + str(rows.len))
if rows.len == 2 {
println(" [PASS] Query TSV row count verified: 2 rows")
println(" Row 0: id=" + rows[0][0] + ", name=" + rows[0][1])
println(" Row 1: id=" + rows[1][0] + ", name=" + rows[1][1])
} else {
println(" [FAIL] Row count mismatch!")
}
p.exec("DROP TABLE IF EXISTS xu_pool_events") >>> (err) {
println(" [FAIL] Pool drop table error: " + err.msg())
<<
}
println(" [PASS] Pool drop table completed.")
p.close()
println(" [PASS] ClickHouse Pool closed and verified.")
}
fn main() {
println("=== Xu ClickHouse Native Driver Verification ===\n")
verify_clickhouse_basic()
verify_clickhouse_pool()
println("\n[CLICKHOUSE VERIFICATION 100% COMPLETED]")
}
API declarations
open
pub fn open(endpoint string, user string, password string, database string) Client
Purpose
Open resource or establish connection.
Parameters
endpoint string- endpoint (string)
user string- user (string)
password string- password (string)
database string- database (string)
Returns
Client — a value of this type
See usage example
open_pool
pub fn open_pool(endpoint string, user string, password string, database string, max_idle int) Pool
Purpose
Open pool.
Parameters
endpoint string- endpoint (string)
user string- user (string)
password string- password (string)
database string- database (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
See usage example
struct Response
pub struct Response {
pub status_code int
pub body string
}
Purpose
Clickhouse Server response payload containing status, headers, and body.
Public fields
status_code int- field (int)
body string- body
struct Client
pub struct Client {
pub host string
pub port int
pub secure bool
pub user string
pub password string
pub database string
}
Purpose
Clickhouse 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)
new_client
pub fn new_client(endpoint string, user string, password string, database string) Client
Purpose
Construct client.
Parameters
endpoint string- endpoint (string)
user string- user (string)
password string- password (string)
database string- database (string)
Returns
Client — a value of this type
parse_tsv
pub fn parse_tsv(raw string) [][]string
Purpose
Parse as tsv.
Parameters
raw string- raw (string)
Returns
[][]string — slice result
($&Client) request
pub fn (c $&Client) request(sql_query string) Response
Purpose
Execute request on $&Client. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
sql_query string- sql_query (string)
Returns
Response — a value of this type
struct Pool
pub struct Pool {
pub host string
pub port int
pub secure bool
pub user string
pub password string
pub database string
pub state chan pool.State
pub max_idle int
}
Purpose
Connection Pool for ClickHouse. Manages reusable TCP socket connections.
Public fields
host string- host
port int- port
secure bool- field (bool)
user string- field (string)
password string- field (string)
database string- field (string)
state chan pool.State- field (channel; type chan pool.State)
max_idle int- field (int)
new_pool
pub fn new_pool(endpoint string, user string, password string, database string, max_idle int) Pool
Purpose
Construct pool.
Parameters
endpoint string- endpoint (string)
user string- user (string)
password string- password (string)
database string- database (string)
max_idle int- max_idle (int)
Returns
Pool — a value of this type
(&Pool) get
pub fn (p &Pool) get() Client
Purpose
Execute get on &Pool.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
Client — a value of this type
(&Pool) dup
pub fn (p &Pool) dup() Pool
Purpose
Create and return a duplicate object holding independent state.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
Returns
Pool — a value of this type
(&Pool) get_conn
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
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
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.
(&Pool) exec
pub fn (p &Pool) exec(sql_query string) int
Purpose
Execute SQL statement or service command and return result. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
sql_query string- sql_query (string)
Returns
int — a value of this type
(&Pool) query
pub fn (p &Pool) query(sql_query string) string
Purpose
Execute query on &Pool. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
sql_query string- sql_query (string)
Returns
string — result string
(&Pool) query_tsv
pub fn (p &Pool) query_tsv(sql_query string) [][]string
Purpose
Operation on &Pool: query tsv. Throws on failure; handle the error or propagate it to the caller.
Parameters
p &Pool- The
Pool instance used by this call (borrow).
sql_query string- sql_query (string)
Returns
[][]string — slice result
(&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.
($&Client) exec
pub fn (c $&Client) exec(sql_query string) int
Purpose
Execute SQL statement or service command and return result. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
sql_query string- sql_query (string)
Returns
int — a value of this type
($&Client) query
pub fn (c $&Client) query(sql_query string) string
Purpose
Execute query on $&Client. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
sql_query string- sql_query (string)
Returns
string — result string
($&Client) query_tsv
pub fn (c $&Client) query_tsv(sql_query string) [][]string
Purpose
Operation on $&Client: query tsv. Throws on failure; handle the error or propagate it to the caller.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
sql_query string- sql_query (string)
Returns
[][]string — slice result
($&Client) ping
pub fn (c $&Client) ping() bool
Purpose
Send a ping heartbeat request to verify connection liveness.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
Returns
bool — true or false
($&Client) insert
pub fn (c $&Client) insert(table string, values_sql string) int
Purpose
Execute insert on $&Client.
Parameters
c $&Client- The
Client instance used by this call (exclusive borrow).
table string- table (string)
values_sql string- values_sql (string)
Returns
int — a value of this type