Skip to main content

ONQL Python client

Project description

ONQL Python Driver

Official Python client for the ONQL database server.

Installation

From PyPI

pip install onql-client

From GitHub (latest main)

pip install git+https://github.com/ONQL/onqlclient-python.git

Pinned to a release tag

pip install git+https://github.com/ONQL/onqlclient-python.git@v0.1.5

Quick Start

import asyncio
from onqlclient import ONQLClient

async def main():
    client = await ONQLClient.create("localhost", 5656)

    # Execute a query
    result = await client.send_request("onql", json.dumps({
        "db": "mydb",
        "table": "users",
        "query": 'name = "John"'
    }))
    print(result["payload"])

    # Subscribe to live updates
    rid = await client.subscribe("", 'name = "John"', lambda rid, kw, payload:
        print(f"Update: {payload}")
    )

    # Unsubscribe
    await client.unsubscribe(rid)

    await client.close()

asyncio.run(main())

API Reference

ONQLClient.create(host, port, data_limit, default_timeout)

Creates and returns a connected client instance.

Parameter Type Default Description
host str "localhost" Server hostname
port int 5656 Server port
data_limit int 16777216 Max buffer size in bytes (16 MB)
default_timeout int 10 Default request timeout in seconds

await client.send_request(keyword, payload, timeout=None)

Sends a request and waits for a response. Returns a dict with request_id, source, and payload.

await client.subscribe(onquery, query, callback)

Opens a streaming subscription. Returns the subscription ID. Callback receives (rid, keyword, payload).

await client.unsubscribe(rid)

Stops receiving events for a subscription.

await client.close()

Closes the connection.

Direct ORM-style API

On top of the raw send_request protocol, the client ships convenience methods that build the standard payload envelopes for common operations and unwrap the {error, data} response automatically.

Call client.setup(db) once to bind a default database name; every subsequent insert / update / delete / onql call will use it.

client.setup(db)

Sets the default database. Returns self, so calls can be chained.

client.setup("mydb")

await client.insert(table, data)

Insert one record or a list of records.

Parameter Type Description
table str Target table name
data dict | list[dict] A single record or a list of records

Returns the parsed data field from the server response. Raises Exception when the server returns a non-empty error field.

await client.insert("users", {"name": "John", "age": 30})
await client.insert("users", [{"name": "A"}, {"name": "B"}])

await client.update(table, data, query, protopass="default", ids=None)

Update records matching query.

Parameter Type Default Description
table str Target table
data dict Fields to update
query dict | str Match query
protopass str "default" Proto-pass profile
ids list[str] [] Explicit record IDs
await client.update("users", {"age": 31}, {"name": "John"})
await client.update("users", {"active": False}, {"id": "u1"}, protopass="admin")

await client.delete(table, query, protopass="default", ids=None)

Delete records matching query.

Parameter Type Default Description
table str Target table
query dict | str Match query
protopass str "default" Proto-pass profile
ids list[str] [] Explicit record IDs
await client.delete("users", {"active": False})

await client.onql(query, protopass="default", ctxkey="", ctxvalues=None)

Run a raw ONQL query. The server's {error, data} envelope is unwrapped.

Parameter Type Default Description
query str ONQL query text
protopass str "default" Proto-pass profile
ctxkey str "" Context key
ctxvalues list[str] [] Context values
rows = await client.onql('select * from users where age > 18')

client.build(query, *values)

Replace $1, $2, … placeholders with values. Strings are automatically double-quoted; numbers and booleans are inlined verbatim.

q = client.build('select * from users where name = $1 and age > $2', "John", 18)
# -> 'select * from users where name = "John" and age > 18'
rows = await client.onql(q)

Full example

import asyncio
from onqlclient import ONQLClient

async def main():
    client = await ONQLClient.create("localhost", 5656)
    client.setup("mydb")

    await client.insert("users", {"name": "John", "age": 30})

    rows = await client.onql(
        client.build("select * from users where age >= $1", 18)
    )
    print(rows)

    await client.update("users", {"age": 31}, {"name": "John"})
    await client.delete("users", {"name": "John"})
    await client.close()

asyncio.run(main())

Protocol

The client communicates over TCP using a delimiter-based message format:

<request_id>\x1E<keyword>\x1E<payload>\x04
  • \x1E — field delimiter
  • \x04 — end-of-message marker

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onql_client-0.1.5.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

onql_client-0.1.5-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file onql_client-0.1.5.tar.gz.

File metadata

  • Download URL: onql_client-0.1.5.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for onql_client-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2c6ce1a77d4d3c6fa6db04844e3618b183207139d8c96ce696a459497b3da35f
MD5 2dacce0a04d521266fb5678989bd288f
BLAKE2b-256 ee717e28a035bdbbc738dede0c7c51b6bca53e43b0cfcabc98685d9c25f548c8

See more details on using hashes here.

File details

Details for the file onql_client-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: onql_client-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for onql_client-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8812bd45598de57f2cc00f08a807ccad06571075009509b97ff0c0e1de923945
MD5 0c443c925f021d1025fe20b2139dca20
BLAKE2b-256 1159a63172ed6b2c58ea5a4b98a908d90cd66181c140836b15f1fe0a6b544c8c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page