ClickHouse driver for moondb — the HTTP interface, RowBinary results and server-side query parameters, in pure MoonBit.
moon add moonbitstack/moonchdbasync fn run() -> Unit raise {
let db = @moonchdb.ClickHouseConn::connect(
"127.0.0.1", user="default", database="default",
)
db.execute(
"CREATE TABLE hero (id Int32, name String) ENGINE = Memory", [],
)
|> ignore
// `?` becomes ClickHouse's own `{p1:Type}` parameter, chosen from the bound value,
// and the value travels beside the query — never spliced into the SQL.
db.execute("INSERT INTO hero VALUES (?, ?)", [
@moondb.Int(1), @moondb.Text("Nova"),
])
|> ignore
let rows = db.query("SELECT name FROM hero WHERE id = ?", [@moondb.Int(1)])
println(rows[0].text_by("name")) // Nova
db.close()
}POST /?database=…&default_format=RowBinaryWithNamesAndTypes¶m_p1=…
X-ClickHouse-User: …
X-ClickHouse-Key: …
SELECT name FROM hero WHERE id = {p1:Int32}| ClickHouse | Value |
|---|---|
| Int8…Int32, UInt8, UInt16 | Int |
| Int64, UInt32, UInt64 | Int64 |
| Float32, Float64 | Double |
| String, FixedString(N) | Text, or Blob when the bytes are not UTF-8 |
| Bool | Bool |
| Date, Date32, DateTime, DateTime64(P) | Time (YYYY-MM-DD or …THH:MM:SS[.fff]+00) |
| Decimal(P, S), up to P = 18 | Numeric |
| UUID | Uuid (16 bytes) |
| IPv4, IPv6 | Text |
| Enum8, Enum16 | Int (the ordinal; the label mapping stays on the server) |
| Nullable(T) | the inner value, or Null |
| Array(T) | Array |
pub struct ClickHouseConn {
host : String
port : Int
user : String
password : String
database : String
closed : Bool
in_transaction : Bool
}let db = @clickhouse.ClickHouseConn::connect("127.0.0.1", user="default", database="default")
db.execute("CREATE TABLE hero (id Int32, name String) ENGINE = Memory", []) |> ignore
db.execute("INSERT INTO hero VALUES (?, ?)", [Int(1), Text("Nova")]) |> ignore
let rows = db.query("SELECT name FROM hero WHERE id = ?", [Int(1)])impl AsyncDriver for ClickHouseConnasync fn execute(self : ClickHouseConn, sql : String, params : Array[Value]) -> ExecResult raise DbErrorasync fn ClickHouseConn::connect(host : String, port? : Int, user? : String, password? : String, database? : String) -> ClickHouseConn raise DbErrorasync fn ClickHouseConn::execute(self : ClickHouseConn, sql : String, params : Array[Value]) -> ExecResult raise DbErrorasync fn ClickHouseConn::query(self : ClickHouseConn, sql : String, params : Array[Value]) -> Array[Row] raise DbErrorasync fn ClickHouseConn::query_stream(self : ClickHouseConn, sql : String, params : Array[Value]) -> &AsyncCursor raise DbErrorInstall
Download zipClickHouse driver for moondb — the HTTP interface, RowBinary results and server-side query parameters, in pure MoonBit.