API Reference
OhMyCH.HttpConfig — TypeHttpConfigDefines the configuration for the HTTP connection to the database.
Fields
url::String: Database server URL.database::String: Name of the database.user::String: Username for authentication.password::String: Password for authentication.verify_ssl::Bool: Verify SSL certificates for HTTPS.
OhMyCH.HttpClient — TypeHttpClientDefines an HTTP client to connect to the database.
Fields
config::HttpConfig: Connection configuration, including URL, database name, and credentials.curl_client::CurlClient: Handles HTTP communication with the database server.
OhMyCH.ohmych_connect — Functionohmych_connect(url::String, database::String, user::String, password::String) -> HttpClient
ohmych_connect(config::HttpConfig) -> HttpClient
ohmych_connect(f::Function, args...) -> HttpClientCreates an HttpClient instance to connect to a database. Once the connection is no longer needed, it must be closed using the close method. The method that takes a function f as the first argument will close the connection automatically.
Keyword arguments
verify_ssl::Bool: Whether to verify the SSL certificate (default:true).
Base.isopen — Functionisopen(client::HttpClient) -> BoolChecks that the client is connected to the database.
Base.close — Functionclose(client::HttpClient)Closes the client connection to the database.
Database requests
OhMyCH.execute — Functionexecute(client::HttpClient, query::String [, parameters::NamedTuple]; options...) -> NothingSends a query to the database using the HttpClient client. Values that need to be substituted into the query can be specified as parameters (see more in Queries with parameters).
Keyword arguments
use_compression::Bool: Flag for enabling data compression (default:true). See more inContent encodingsection.max_execution_time::Int: Maximum allowed execution time in seconds (default:60seconds).options...: Additional options passed to the query execution function.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> employees_query = """
CREATE TABLE IF NOT EXISTS employees
(
name String
,age Int32
,position String
,salary Float64
)
ENGINE = MergeTree()
ORDER BY name
""";
julia> execute(client, employees_query)OhMyCH.insert — Functioninsert(client::HttpClient, query::String, values::Vector; options...) -> NothingSends a query to insert values into the database using the HttpClient client
Keyword arguments
chunk_size::Int: Specifies the approximate size (in bytes) of one chunk of data being sent (default:256 * 1024bytes).use_compression::Bool: Flag for enabling data compression (default:true). See more inContent encodingsection.max_execution_time::Int: Maximum allowed execution time in seconds (default:60seconds).options...: Additional options passed to the query execution function.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> insert(client, "INSERT INTO employees (name, age, position, salary)", [
(name = "Alice", age = Int32(29), position = "Developer", salary = 75000.5),
(name = "Bob", age = Int32(35), position = "Manager", salary = 92000.75),
(name = "Clara", age = Int32(28), position = "Designer", salary = 68000.0),
(name = "David", age = Int32(40), position = "Developer", salary = 81000.3),
])OhMyCH.query — Functionquery(client::HttpClient, query::String [, parameters::NamedTuple]; options...) -> RowBinaryWithNamesAndTypesSends a query to the database using the HttpClient client. Values that need to be substituted into the query can be specified as parameters (see more in Queries with parameters).
This type of query involves returning the result from the database. The returned data is represented as a RowBinaryWithNamesAndTypes object.
Keyword arguments
use_compression::Bool: Flag for enabling data compression (default:true). See more inContent encodingsection.max_execution_time::Int: Maximum allowed execution time in seconds (default:60seconds).options...: Additional options passed to the query execution function.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> query(client, "SELECT * FROM employees")
RowBinaryWithNamesAndTypes(161-bytes):
name::String
age::Int32
position::String
salary::Float64OhMyCH.insert_binary — Functioninsert_binary(client::HttpClient, query::String, values::RowBinary; options...) -> RowBinaryWorks similarly to the insert method, except that the sending data must be a RowBinary object.
This method is effective when sending small portions of data frequently. Intended use in conjunction with query_binary for fast data transfer between databases.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> binary_data = query_binary(client, "SELECT * FROM employees")
RowBinary(107-bytes)
julia> insert_binary(client, "INSERT INTO another_employees", binary_data)OhMyCH.query_binary — Functionquery_binary(client::HttpClient, query::String [, parameters::NamedTuple]; options...) -> RowBinaryWorks similarly to the query method, except that the retrieved data is represented as a RowBinary object.
This method is effective when requesting small portions of data frequently, since the response does not include information about names and data types. However, in this case, the user will be required to describe the column types and names themselves using his own type T.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> query_binary(client, "SELECT * FROM employees")
RowBinary(107-bytes)Row iteration
Base.eachrow — Functioneachrow(binary::RowBinaryWithNamesAndTypes) -> BinaryToRowIterCreates a new iterator BinaryToRowIter that determines column types and their names from binary object. The elements of such an iterator are NamedTuple objects.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> employees = query(client, "SELECT * FROM employees");
julia> for user in eachrow(employees)
println(user)
end
(name = "Alice", age = 29, position = "Developer", salary = 75000.5)
(name = "Bob", age = 35, position = "Manager", salary = 92000.75)
(name = "Clara", age = 28, position = "Designer", salary = 68000.0)
(name = "David", age = 40, position = "Developer", salary = 81000.3)eachrow(::Type{T}, binary::RowBinaryResult) -> BinaryToRowIterCreates a new iterator BinaryToRowIter that uses fields of type T to determine column types. The elements of such an iterator are objects of type T.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> employees = query_binary(client, "SELECT * FROM employees");
julia> struct Employee
name::String
age::Int32
position::String
salary::Float64
end
julia> for employee in eachrow(Employee, employees)
println(employee)
end
Employee("Alice", 29, "Developer", 75000.5)
Employee("Bob", 35, "Manager", 92000.75)
Employee("Clara", 28, "Designer", 68000.0)
Employee("David", 40, "Developer", 81000.3)Base.collect — Functioncollect(binary::RowBinaryWithNamesAndTypes) -> Vector{NamedTuple}
collect(::Type{T}, binary::RowBinaryResult) -> Vector{T}Works similarly to the eachrow method, but instead of creating an iterator, it returns all values from the binary representation of the data.
If you don't need all the values at once, it's preferable to iterate over the rows using the eachrow method.
Examples
julia> client = ohmych_connect("http://127.0.0.1:8123", "database", "username", "password");
julia> employees = query(client, "SELECT * FROM employees");
julia> collect(employees)
4-element Vector{NamedTuple{(:name, :age, :position, :salary), Tuple{String,Int32,String,Float64}}}:
(name = "Alice", age = 29, position = "Developer", salary = 75000.5)
(name = "Bob", age = 35, position = "Manager", salary = 92000.75)
(name = "Clara", age = 28, position = "Designer", salary = 68000.0)
(name = "David", age = 40, position = "Developer", salary = 81000.3)Column types
Most of the ClickHouse column types are the same as the Julia base types.
| ClickHouse Type | Julia Type |
|---|---|
| Bool | Bool |
| Int8-128, UInt8-128 | Int8-128, UInt8-128 |
| Float32, Float64 | Float32, Float64 |
| Decimal(P,S) | Decimal{P,S} |
| String | String |
| FixedString(N) | FixedString{N} |
| Date | Date |
| DateTime | DateTime |
| DateTime64 | NanoDate |
| Enum8, Enum16 | UInt8, UInt16 |
| UUID | UUID |
| IPv4, IPv6 | IPv4, IPv6 |
| Array(T) | AbstractVector{T} |
| Tuple(T1, T2, ...) | Tuple |
| Map(K, V) | AbstractDict{K,V} |
| LowCardinality(T) | T |
| Nullable(T) | Union{Nothing,T} |
However, some types had to be implemented independently.
Decimal
OhMyCH.Decimal — TypeDecimal{P,S} <: AbstractFloatA type that represents a fixed-precision decimal number with precision P and scale S. Precision P is a total number of digits in the number (including digits before and after the decimal point). Scale S is a number of digits after the decimal point.
OhMyCH.Decimal — MethodDecimal{P,S}(value::Union{Real,AbstractString})Constructs a Decimal with precision P and scale S from a value.
Examples
julia> Decimal{5,2}(123.45)
Decimal{5,2}(123.45)
julia> Decimal{5,2}("123.45")
Decimal{5,2}(123.45)
julia> Decimal{5,2}(123456.0)
ERROR: ArgumentError: Decimal value 123456.0 exceeds limits for Decimal{P=5,S=2}. Max allowed: ±999.99.OhMyCH.Decimal — MethodDecimal{P,S}([sign::Integer, ] significand::Integer, exponent::Integer)Constructs a Decimal with precision P and scale S from its components:
sign: The sign of the decimal (+1or-1).significand: The integer part of the number (absolute value).exponent: The base-10 exponent.
Examples
julia> Decimal{5,2}(12345, -2)
Decimal{5,2}(123.45)
julia> Decimal{5,2}(-1, 12345, -3)
Decimal{5,2}(-12.345)
julia> Decimal{5,2}(123456, -2)
ERROR: ArgumentError: Decimal value 1234.56 exceeds limits for Decimal{P=5,S=2}. Max allowed: ±999.99.FixedString
OhMyCH.FixedString — TypeFixedString{N} <: AbstractStringA string type with a fixed maximum size N.
Fields
data::String: The string data that is constrained by the fixed sizeN.
OhMyCH.FixedString — MethodFixedString{N}(x::String)Constructs a FixedString object with a fixed maximum size N from the given string x.
Examples
julia> FixedString{10}("Hello")
FixedString{10}("Hello")
julia> FixedString{3}("Hello")
ERROR: ArgumentError: Input string is longer than 3 bytes (5)Exceptions
OhMyCH.OhMyCHException — TypeOhMyCHException <: ExceptionAn abstract base exception for database-related errors.
OhMyCH.CHServerException — TypeCHServerException <: OhMyCHExceptionRaised when the ClickHouse server returns an error response while processing a query.
Fields
code::ErrorCodes: An error code indicating the type of database error.message::String: A descriptive message providing details about the error.
See also ErrorCodes.
OhMyCH.CHClientException — TypeCHClientException <: OhMyCHExceptionRepresents an error that occurs during HTTP communication with the ClickHouse database.
Fields
message::String: A descriptive message providing details about the error.