sql
import sql
One way to talk to a relational database, whichever one it is.
sql is the entry point for every database Zuri supports. It defines
what a database adapter has to provide, supplies everything that is the
same whichever engine answers, and picks the adapter from the connection
string. Changing database means changing that string, and whatever SQL
the engines genuinely spell differently.
import sql
var db = sql.open(':memory:')
db.exec('create table posts (id integer primary key, title text)')
var id = db.insert('posts', { title: 'Hello' })
for post in db.query('select * from posts where id = ?', [id]) {
echo post.title
}
db.close()
The only line above that names an engine is the first. Point it at
postgres://localhost/app or mysql://localhost/app and the rest runs
unchanged: the ? becomes $1 on PostgreSQL and stays ? on MySQL,
the insert gets a RETURNING clause where the engine has no last insert
id, and a unique key violation still arrives as UniqueViolation.
What is here
open() | opens a connection from a connection string |
pool() | opens a pool of them |
register() | adds an adapter of your own |
Connection | what a program holds and runs statements on |
Transaction | an open transaction, and the savepoints inside it |
Statement | a statement compiled once and run many times |
Cursor | a result read a row at a time |
ResultSet | the rows a query returned |
Decimal | an exact decimal, for a column a float must not hold |
Time | a signed span of time, as a TIME column holds one |
Schema | what tables exist and what is in them |
SqlError | the root of every error a database raises |
Parameters
Values are bound, never pasted into the statement. Write ? for
positional parameters or :name for named ones and sql translates
them into whatever the adapter wants:
db.query('select * from posts where author = ? and year = ?', [name, 2026])
db.query('select * from posts where author = :who', { who: name })
Errors
Every error is a SqlError. The subclasses are the same on every
engine, so the code that handles a duplicate key does not change when
the engine does:
catch {
db.insert('users', { email: address })
} as error {
if instance_of(error, sql.UniqueViolation) {
echo 'that address is already registered'
} else {
raise error
}
}
Isolates
A connection belongs to the isolate that opened it and cannot be shared with another. An isolate that needs a database opens its own connection or its own pool.
The sql API
Every public name in sql, wherever it is declared. Each links to the
page that documents it.
| Name | Kind | Summary |
|---|---|---|
sql.AuthenticationError | class | The server rejected the credentials. |
sql.CheckViolation | class | A CHECK constraint rejected the row. |
sql.ClosedError | class | The connection, statement, cursor, transaction or pool was already closed. |
sql.Connection | class | An open connection to a database. |
sql.ConnectionError | class | The database could not be reached, or the connection dropped. |
sql.Cursor | class | A result being read a row at a time. |
sql.DeadlockError | class | Two transactions were each waiting on the other, and the engine broke the tie by aborting this one. |
sql.Decimal | class | An exact decimal number. |
sql.Driver | class | Opens connections to one kind of database. |
sql.DriverConnection | class | One open connection to a database, as the layer above it sees one. |
sql.DriverCursor | class | A result being read a batch at a time rather than all at once. |
sql.DriverStatement | class | A statement compiled once and run many times. |
sql.ExecResult | class | What a statement that changed something reports. |
sql.ForeignKeyViolation | class | A foreign key has no matching row, or a referenced row is still in use by another table. |
sql.INDEXED | constant | ?1, ?2 and so on. |
sql.IntegrityError | class | A constraint rejected the data. |
sql.NUMBERED | constant | $1, $2 and so on, numbered from one in binding order. |
sql.NotNullViolation | class | A column declared NOT NULL was given nothing. |
sql.NotSupportedError | class | The engine cannot do what was asked, and no approximation would be honest. |
sql.Pool | class | A pool of connections to one database. |
sql.PoolError | class | Something went wrong with a connection pool rather than with a database. |
sql.PoolExhaustedError | class | Every connection was busy and none came free within the acquire timeout. |
sql.ProtocolError | class | The server sent something the adapter could not make sense of. |
sql.QUESTION | constant | A bare ? for every parameter, in order. |
sql.QueryError | class | The engine refused the statement. |
sql.READ_COMMITTED | constant | Each transaction sees rows committed before its own statement started, and nothing a concurrent transaction… |
sql.READ_UNCOMMITTED | constant | A transaction can see rows another has written and not committed. |
sql.REPEATABLE_READ | constant | A transaction reads the same rows throughout, whatever anyone else commits while it runs. |
sql.Raw | class | Marks a fragment of SQL to be used as written rather than bound. |
sql.ResultSet | class | The rows a query returned, with the shape of the result beside them. |
sql.SERIALIZABLE | constant | Concurrent transactions produce a result some serial order of them could also have produced. |
sql.Schema | class | Introspection for one connection. |
sql.SchemaAdapter | class | The introspection queries for one engine. |
sql.SerializationError | class | Two transactions could not both be treated as though they ran alone. |
sql.SqlError | class | Base class for every error this module and its adapters raise. |
sql.Statement | class | A prepared statement. |
sql.Time | class | A signed span of time, as a SQL TIME column holds one. |
sql.TimeoutError | class | An operation ran out of time. |
sql.Transaction | class | An open transaction. |
sql.TransactionError | class | A transaction could not be carried out as asked. |
sql.UniqueViolation | class | A unique index or primary key already holds this value. |
sql.column_shape | function | The description of one column, as every adapter returns it. |
sql.crud.delete | function | Builds a DELETE. |
sql.crud.insert | function | Builds an INSERT. |
sql.crud.insert_many | function | Builds an INSERT carrying several rows in one statement. |
sql.crud.quote_table | function | Renders a table name, which may carry a schema. |
sql.crud.select | function | Builds a SELECT. |
sql.crud.update | function | Builds an UPDATE. |
sql.crud.where | function | Builds the WHERE clause for a dictionary of conditions. |
sql.decimal | function | Builds a decimal, the same as Decimal() but as a function. |
sql.default_capabilities | function | The capability flags a driver reports, with the value each takes when a driver does not say otherwise. |
sql.driver | function | The adapter registered under name. |
sql.driver_for | function | The adapter a connection string names. |
sql.drivers | function | The names of every registered adapter. |
sql.mysql.CACHING_SHA2 | constant | The default since MySQL 8.0. |
sql.mysql.CAPABILITIES | constant | What the client asks for and what the server offers. |
sql.mysql.CLEAR | constant | Sends the password as it is. |
sql.mysql.COMMANDS | constant | The commands this adapter sends. |
sql.mysql.DEFAULT_PORT | constant | The port a MySQL server listens on unless told otherwise. |
sql.mysql.DRIVER | constant | The driver instance sql registers for MySQL. |
sql.mysql.ED25519 | constant | MariaDB’s signature-based plugin. |
sql.mysql.FLAGS | constant | What the server says about a column beyond its type. |
sql.mysql.MARIADB_DRIVER | constant | The driver instance sql registers for MariaDB. |
sql.mysql.MAX_PARAMETERS | constant | Most parameters one statement can bind. |
sql.mysql.MariaDbDriver | class | Opens MariaDB connections. |
sql.mysql.MySqlConnection | class | A connection to MySQL or MariaDB. |
sql.mysql.MySqlCursor | class | A result being read in batches. |
sql.mysql.MySqlDriver | class | Opens MySQL connections. |
sql.mysql.MySqlStatement | class | A prepared statement. |
sql.mysql.NATIVE | constant | MySQL’s original plugin, and still MariaDB’s default. |
sql.mysql.SHA256 | constant | MySQL 5.7’s stronger option, superseded by the caching one. |
sql.mysql.SSL_MODES | constant | What sslmode can be. |
sql.mysql.TYPES | constant | The type byte the server puts on a column, and that a parameter carries back. |
sql.mysql.auth.NONE | constant | Named in a handshake by a server that wants no password at all. |
sql.mysql.auth.SUPPORTED | constant | The plugins this adapter can answer. |
sql.mysql.auth.caching_sha2_password | function | The fast caching_sha2_password response. |
sql.mysql.auth.clear_password | function | The password as the server reads it when it is sent outright: the bytes and the zero that ends them. |
sql.mysql.auth.ed25519_password | function | The client_ed25519 response, which MariaDB uses. |
sql.mysql.auth.encrypt_password | function | The password encrypted to the server’s public key, for the plugins that need the password itself over a… |
sql.mysql.auth.native_password | function | The mysql_native_password response. |
sql.mysql.auth.obfuscate | function | The password masked with the scramble, which is the form the two RSA plugins encrypt. |
sql.mysql.auth.response_for | function | The first response to send for plugin, before any back and forth. |
sql.mysql.auth.supports | function | Whether plugin is one this adapter knows how to answer. |
sql.mysql.class_for | function | The class that best describes an error the server reported. |
sql.mysql.connect | function | Opens a MySQL connection directly, without going through sql. |
sql.mysql.connection.CACHE_LIMIT | constant | How many compiled statements a connection keeps before releasing the one it has gone longest without using. |
sql.mysql.driver.DEFAULT_TIMEOUT | constant | How long to wait for the socket and for each read, in milliseconds. |
sql.mysql.ed25519.D | constant | The curve constant, -121665/121666. |
sql.mysql.ed25519.D2 | constant | Twice the curve constant, which is what the addition uses. |
sql.mysql.ed25519.L | constant | The order of the base point’s subgroup. |
sql.mysql.ed25519.P | constant | The field prime, 2^255 - 19. |
sql.mysql.ed25519.password_key | function | Expands a password into the 64 bytes MariaDB signs with. |
sql.mysql.ed25519.public_key | function | The public key matching an expanded key. |
sql.mysql.ed25519.sign | function | Signs message with an expanded key. |
sql.mysql.errors.CLASSES | constant | SQLSTATE classes, for numbers the table above does not name. |
sql.mysql.errors.SPECIFIC | constant | Error numbers specific enough to name a class of their own. |
sql.mysql.packets.COMPRESS_THRESHOLD | constant | Below this, a compressed packet is sent uncompressed instead. |
sql.mysql.packets.Cursor | class | Reads values out of one packet payload, keeping its own position. |
sql.mysql.packets.EXACT_INTEGER_LIMIT | constant | Largest integer a Zuri number holds exactly. |
sql.mysql.packets.LENENC_NULL | constant | The first byte of a length-encoded integer that means the value is NULL. |
sql.mysql.packets.MAX_PAYLOAD | constant | The largest payload a single packet can carry. |
sql.mysql.packets.Writer | class | Builds one packet payload. |
sql.mysql.protocol.CURSOR_NONE | constant | No cursor: the whole result comes back at once. |
sql.mysql.protocol.CURSOR_READ_ONLY | constant | Asks the server to keep the result open rather than send it all. |
sql.mysql.protocol.STATUS | constant | What the server says about the session after each command. |
sql.mysql.raise_for | function | Raises the right class for a server error report. |
sql.mysql.results.exec_result | function | What a statement run for its effect reports. |
sql.mysql.results.select_result | function | What a statement run for its rows reports. |
sql.mysql.schema.MySqlSchema | class | Answers introspection questions about a MySQL database. |
sql.mysql.type_name_of | function | The name of a column’s type, as MySQL itself would write it. |
sql.mysql.types.BINARY_CHARSET | constant | The collation id that means a column holds bytes rather than text. |
sql.mysql.types.UNSIGNED_FLAG | constant | The flag byte that goes with a parameter’s type to mark it unsigned. |
sql.mysql.types.decode_binary | function | Reads one value out of a binary protocol row. |
sql.mysql.types.decode_text | function | Reads one value out of a text protocol row. |
sql.mysql.types.describe_columns | function | Reduces the server’s column descriptors to the two fields the shared contract asks for. |
sql.mysql.types.is_binary | function | Whether a column holds bytes rather than text. |
sql.mysql.types.is_unsigned | function | Whether a column’s values are unsigned. |
sql.mysql.types.parameter_type | function | The type byte and flag a value is sent as. |
sql.mysql.types.write_parameter | function | Appends a value in the form its type byte promises. |
sql.open | function | Opens a connection. |
sql.params.STYLES | constant | Every style a driver may declare. |
sql.params.tokenize | function | Splits sql into the pieces that matter, in order. |
sql.parse_time | function | Reads a SQL TIME literal. |
sql.placeholder | function | The placeholder text for the parameter at position, counting from one. |
sql.pool | function | Opens a pool of connections. |
sql.pool.DEFAULT_IDLE_TIMEOUT | constant | How long an idle connection is kept before being closed, in milliseconds. |
sql.pool.DEFAULT_MAX | constant | How many connections a pool opens at most, when it is not told. |
sql.pool.DEFAULT_MAX_LIFETIME | constant | How long any connection is kept before being replaced, in milliseconds. |
sql.postgres.DEFAULT_PORT | constant | The port a PostgreSQL server listens on unless told otherwise. |
sql.postgres.DEFAULT_REGISTRY | constant | The registry every connection uses unless given one of its own. |
sql.postgres.DRIVER | constant | The driver instance sql registers for this engine. |
sql.postgres.Listener | class | Subscribes a connection to channels and collects what arrives. |
sql.postgres.MAX_PARAMETERS | constant | Most parameters one statement can bind. |
sql.postgres.OIDS | constant | PostgreSQL’s built-in type OIDs, by name. |
sql.postgres.PostgresConnection | class | An open PostgreSQL connection. |
sql.postgres.PostgresCursor | class | A portal being read. |
sql.postgres.PostgresDriver | class | Opens PostgreSQL connections. |
sql.postgres.PostgresStatement | class | A compiled PostgreSQL statement. |
sql.postgres.SSL_MODES | constant | What sslmode can be. |
sql.postgres.TypeRegistry | class | Which decoder and encoder each OID uses. |
sql.postgres.affected_rows | function | The number of rows a command tag reports. |
sql.postgres.auth.SCRAM_SHA_256 | constant | The mechanism this adapter implements. |
sql.postgres.auth.client_first | function | The client’s opening SCRAM message. |
sql.postgres.auth.client_proof | function | Works out the client’s proof from the server’s challenge. |
sql.postgres.auth.md5_response | function | Builds the response to an MD5 password request. |
sql.postgres.auth.nonce | function | A fresh SCRAM nonce. |
sql.postgres.auth.offers_scram | function | Whether the server offered SCRAM-SHA-256 among its mechanisms. |
sql.postgres.auth.parse_scram | function | Splits a SCRAM message into its key=value parts. |
sql.postgres.auth.verify_server | function | Checks the server’s closing message really is from a server that knows the password. |
sql.postgres.class_for | function | The class that best describes sqlstate. |
sql.postgres.connect | function | Opens a PostgreSQL connection directly, without going through sql. |
sql.postgres.connection.CACHE_LIMIT | constant | How many compiled statements a connection keeps. |
sql.postgres.describe_columns | function | Reduces the server’s column descriptors to the two fields the shared contract asks for. |
sql.postgres.driver.DEFAULT_TIMEOUT | constant | How long to wait for the socket and for each read, in milliseconds. |
sql.postgres.errors.CLASSES | constant | SQLSTATE classes, for codes the table above does not name. |
sql.postgres.errors.SPECIFIC | constant | Codes specific enough to name a class of their own. |
sql.postgres.messages.AUTHENTICATION | constant | Messages the server sends, by tag. |
sql.postgres.messages.AUTH_CLEARTEXT | constant | |
sql.postgres.messages.AUTH_MD5 | constant | |
sql.postgres.messages.AUTH_OK | constant | Authentication requests, by the number that follows the tag. |
sql.postgres.messages.AUTH_SASL | constant | |
sql.postgres.messages.AUTH_SASL_CONTINUE | constant | |
sql.postgres.messages.AUTH_SASL_FINAL | constant | |
sql.postgres.messages.BACKEND_KEY_DATA | constant | |
sql.postgres.messages.BIND_COMPLETE | constant | |
sql.postgres.messages.CLOSE_COMPLETE | constant | |
sql.postgres.messages.COMMAND_COMPLETE | constant | |
sql.postgres.messages.COPY_DATA | constant | |
sql.postgres.messages.COPY_DONE | constant | |
sql.postgres.messages.COPY_IN_RESPONSE | constant | |
sql.postgres.messages.COPY_OUT_RESPONSE | constant | |
sql.postgres.messages.DATA_ROW | constant | |
sql.postgres.messages.EMPTY_QUERY_RESPONSE | constant | |
sql.postgres.messages.ERROR_RESPONSE | constant | |
sql.postgres.messages.NOTICE_RESPONSE | constant | |
sql.postgres.messages.NOTIFICATION_RESPONSE | constant | |
sql.postgres.messages.NO_DATA | constant | |
sql.postgres.messages.PARAMETER_DESCRIPTION | constant | |
sql.postgres.messages.PARAMETER_STATUS | constant | |
sql.postgres.messages.PARSE_COMPLETE | constant | |
sql.postgres.messages.PORTAL_SUSPENDED | constant | |
sql.postgres.messages.PROTOCOL_VERSION | constant | The protocol version this adapter speaks: 3.0, as a single number with the major version in the high half. |
sql.postgres.messages.READY_FOR_QUERY | constant | |
sql.postgres.messages.ROW_DESCRIPTION | constant | |
sql.postgres.messages.Reader | class | Reads framed messages from a connected stream. |
sql.postgres.messages.SSL_REQUEST | constant | The number the server recognises as a request to start TLS. |
sql.postgres.messages.Writer | class | Builds one outgoing message. |
sql.postgres.messages.read_cstring | function | Splits a run of zero-terminated strings. |
sql.postgres.raise_for | function | Raises the right class for a server error report. |
sql.postgres.schema.PostgresSchema | class | Answers introspection questions about a PostgreSQL database. |
sql.postgres.type_name_of | function | The name of the type an OID stands for, or the OID as text for one this adapter has no name for, which is… |
sql.postgres.types.ARRAY_ELEMENTS | constant | The element type each array OID holds. |
sql.postgres.types.EPOCH_OFFSET | constant | Seconds between the Unix epoch and PostgreSQL’s, which is 2000-01-01 rather than 1970-01-01. |
sql.postgres.types.date_from_micros | function | Turns a count of microseconds since the PostgreSQL epoch into a date.Date in UTC. |
sql.postgres.types.decode_array | function | Reads an array back, however many dimensions it has. |
sql.postgres.types.decode_bool | function | A decoder reads one column value from its binary form. |
sql.postgres.types.decode_box | function | |
sql.postgres.types.decode_bytea | function | |
sql.postgres.types.decode_circle | function | |
sql.postgres.types.decode_date | function | date is a count of days from 2000-01-01, with no time part. |
sql.postgres.types.decode_float4 | function | |
sql.postgres.types.decode_float8 | function | |
sql.postgres.types.decode_inet | function | inet and cidr share a form: address family, prefix bits, a flag saying which of the two it is, then the… |
sql.postgres.types.decode_int2 | function | |
sql.postgres.types.decode_int4 | function | |
sql.postgres.types.decode_int8 | function | |
sql.postgres.types.decode_interval | function | interval is a span rather than a point, so it comes back as its parts. |
sql.postgres.types.decode_json | function | |
sql.postgres.types.decode_jsonb | function | jsonb carries a version byte in front of the text, which every server so far sets to 1. |
sql.postgres.types.decode_lseg | function | |
sql.postgres.types.decode_macaddr | function | |
sql.postgres.types.decode_numeric | function | Decodes PostgreSQL’s numeric into an exact Decimal. |
sql.postgres.types.decode_numeric_value | function | |
sql.postgres.types.decode_oid | function | |
sql.postgres.types.decode_point | function | |
sql.postgres.types.decode_text | function | |
sql.postgres.types.decode_time | function | time is microseconds since midnight, with no date part. |
sql.postgres.types.decode_timestamp | function | Both timestamp and timestamptz are microseconds from the PostgreSQL epoch. |
sql.postgres.types.decode_timetz | function | timetz is a time followed by its offset in seconds west of UTC. |
sql.postgres.types.decode_uuid | function | |
sql.postgres.types.encode_array | function | Builds the binary form of an array. |
sql.postgres.types.encode_bool | function | An encoder turns a Zuri value into the bytes of its binary form. |
sql.postgres.types.encode_bytea | function | |
sql.postgres.types.encode_date | function | |
sql.postgres.types.encode_float4 | function | |
sql.postgres.types.encode_float8 | function | |
sql.postgres.types.encode_int2 | function | |
sql.postgres.types.encode_int4 | function | |
sql.postgres.types.encode_int8 | function | |
sql.postgres.types.encode_json | function | |
sql.postgres.types.encode_jsonb | function | jsonb takes the same text behind the version byte the server expects. |
sql.postgres.types.encode_numeric | function | Encodes a Decimal, a number or a bigint as numeric. |
sql.postgres.types.encode_text | function | |
sql.postgres.types.encode_timestamp | function | |
sql.postgres.types.encode_uuid | function | |
sql.postgres.types.micros_from_date | function | The microseconds since the PostgreSQL epoch a date.Date stands for. |
sql.postgres.types.read_int16 | function | Reads a signed 16 bit big-endian integer. |
sql.postgres.types.read_int32 | function | Reads a signed 32 bit big-endian integer. |
sql.postgres.types.read_int64 | function | Reads a signed 64 bit big-endian integer. |
sql.postgres.types.read_uint16 | function | Reads an unsigned 16 bit big-endian integer. |
sql.postgres.types.read_uint32 | function | Reads an unsigned 32 bit big-endian integer. |
sql.postgres.types.text_of | function | A bytes slice as a UTF-8 string. |
sql.postgres.types.write_int16 | function | Writes a signed 16 bit big-endian integer. |
sql.postgres.types.write_int32 | function | Writes a signed 32 bit big-endian integer. |
sql.postgres.types.write_int64 | function | Writes a signed 64 bit big-endian integer. |
sql.raw | function | Builds a Raw. |
sql.register | function | Registers an adapter, so open() recognises its connection strings. |
sql.scan | function | What placeholders sql uses, without needing any values. |
sql.split_statements | function | Splits a script into its statements. |
sql.sqlite.Backup | class | A copy in progress between two connections. |
sql.sqlite.Blob | class | An open blob handle. |
sql.sqlite.DEFAULT_BUSY_TIMEOUT | constant | How long a statement waits for another writer by default. |
sql.sqlite.DRIVER | constant | The driver instance sql registers for this engine. |
sql.sqlite.MAX_PARAMETERS | constant | Most parameters one statement can bind, which is SQLite’s own limit. |
sql.sqlite.MEMORY | constant | The path that means a private database held in memory, which is discarded when the connection closes. |
sql.sqlite.SqliteConnection | class | An open SQLite database. |
sql.sqlite.SqliteCursor | class | A result being stepped. |
sql.sqlite.SqliteDriver | class | Opens SQLite databases. |
sql.sqlite.SqliteStatement | class | A prepared SQLite statement. |
sql.sqlite.backup.DEFAULT_PAGES | constant | How many pages a step copies when no size is given. |
sql.sqlite.base_type | function | The bare type name from a declaration, upper cased and without any size or precision. |
sql.sqlite.class_for | function | The class that best describes code. |
sql.sqlite.connect | function | Opens a SQLite database directly, without going through sql. |
sql.sqlite.connection.CACHE_LIMIT | constant | How many compiled statements a connection keeps for reuse. |
sql.sqlite.conversion_for | function | Which conversion a column’s declaration calls for: 'bool', 'date', 'json', or nil for a column that… |
sql.sqlite.driver.OPEN_CREATE | constant | |
sql.sqlite.driver.OPEN_FULLMUTEX | constant | |
sql.sqlite.driver.OPEN_MEMORY | constant | |
sql.sqlite.driver.OPEN_NOMUTEX | constant | |
sql.sqlite.driver.OPEN_PRIVATECACHE | constant | |
sql.sqlite.driver.OPEN_READONLY | constant | |
sql.sqlite.driver.OPEN_READWRITE | constant | |
sql.sqlite.driver.OPEN_SHAREDCACHE | constant | |
sql.sqlite.driver.OPEN_URI | constant | |
sql.sqlite.errors.primary | function | An extended code’s primary code, which is its low byte. |
sql.sqlite.reraise | function | Re-raises an error from the native layer as the right class. |
sql.sqlite.schema.SqliteSchema | class | Answers introspection questions about a SQLite database. |
sql.sqlite.split_message | function | Splits the [code] message form the native layer raises. |
sql.sqlite.statement.bind_all | function | Binds values to a reset statement, in order. |
sql.sqlite.statement.describe | function | Describes the columns a compiled statement returns. |
sql.sqlite.types.BOOLEAN_TYPES | constant | Declared types that mean a boolean. |
sql.sqlite.types.JSON_TYPES | constant | Declared types that mean JSON held in a text column. |
sql.sqlite.types.TEMPORAL_TYPES | constant | Declared types that mean a point in time. |
sql.sqlite.types.conversions_for | function | The conversions for a whole result, one per column, worked out once so each row does not have to look at the… |
sql.sqlite.types.decode | function | Applies one column’s conversion to one stored value. |
sql.sqlite.types.decode_row | function | Applies a result’s conversions to one row of stored values. |
sql.time | function | Builds a Time. |
sql.time_from_seconds | function | Builds a Time of total seconds, splitting it into parts. |
sql.translate | function | Rewrites sql into style and puts the values in binding order. |
sql.types.ISO_DATE_FORMAT | constant | The same, without the time, for a column that holds only a date. |
sql.types.ISO_FORMAT | constant | The format this module writes timestamps in: ISO 8601 with microsecond precision and an explicit UTC offset. |
sql.types.flatten | function | Reduces value to something an engine with no richer type can store: a timestamp becomes ISO text, a list or… |
sql.types.from_iso | function | Reads ISO 8601 text back into a date.Date. |
sql.types.from_json | function | Decodes JSON text, returning nil for anything that will not parse. |
sql.types.integer_from_text | function | Reads an integer, however long, as a number where one holds it exactly and a bigint where it does not. |
sql.types.is_interval | function | Whether value is a Time. |
sql.types.is_structured | function | Whether value is something this module stores as JSON. |
sql.types.is_temporal | function | Whether value is a date.Date. |
sql.types.to_iso | function | Formats a date.Date as ISO 8601 text. |
sql.types.to_json | function | Encodes value as JSON text. |
Submodules
| Module | Reached as | Summary |
|---|---|---|
sql.connection | sql.connection.* | The connection a program holds. |
sql.crud | sql.crud.* | Building the four statements that are the same everywhere. |
sql.cursor | sql.cursor.* | Reading a result without holding all of it. |
sql.decimal | sql.decimal.* | Exact decimal numbers, for the money column that must not be a float. |
sql.driver | sql.driver.* | The contract every database adapter implements, and the capability flags that let one adapter differ from… |
sql.errors | sql.* | Every error a database raises, under one root, in one shape, whatever engine it came from. |
sql.mysql | import sql.mysql | The MySQL and MariaDB adapter. |
sql.params | sql.params.* | Rewriting a statement’s placeholders into whatever the active adapter expects. |
sql.pool | sql.pool.* | Keeping connections open and lending them out. |
sql.postgres | import sql.postgres | The PostgreSQL adapter. |
sql.result | sql.result.* | What a statement hands back: the rows of a query, or the count of a statement that changed something. |
sql.schema | sql.schema.* | Asking a database what is in it. |
sql.sqlite | import sql.sqlite | The SQLite adapter. |
sql.statement | sql.statement.* | A statement compiled once and run many times. |
sql.transaction | sql.transaction.* | Transactions, and the nesting that savepoints make possible. |
sql.types | sql.types.* | How Zuri values and database values correspond, for the parts that are the same whichever engine is… |
Functions
register()
sql.register(driver) -> Driver
Registers an adapter, so open() recognises its connection strings.
An adapter is anything implementing Driver. Registering one under a
name already taken replaces it, which is how a program swaps in an
adapter of its own for an engine sql already knows.
sql.register(MyOracleDriver())
var db = sql.open('oracle://localhost/app')
Parameters
driver(Driver)
Returns Driver — The driver, so a registration can be an
expression.
Raises SqlError if driver is not one.
drivers()
sql.drivers() -> list[string]
The names of every registered adapter.
Returns list[string]
driver()
sql.driver(name: string) -> Driver
The adapter registered under name.
Parameters
name(string)
Returns Driver
Raises SqlError if nothing is registered under that name.
driver_for()
sql.driver_for(dsn: string) -> Driver
The adapter a connection string names.
The scheme decides: sqlite:// and postgres:// reach the adapters of
those names, and a string with no scheme at all is taken as a SQLite
path, which is the only form that could not be anything else.
Parameters
dsn(string)
Returns Driver
Raises SqlError if no adapter claims the scheme.
open()
sql.open(dsn, options) -> Connection
Opens a connection.
sql.open('sqlite://./app.db')
sql.open('postgres://alice:secret@localhost/app')
sql.open('./app.db')
An options dictionary works too, and needs driver to say which adapter
it is for:
sql.open({ driver: 'sqlite', path: './app.db', journal_mode: 'wal' })
Parameters
dsn(string|dict) — A connection string, or options.options(dict|nil) — Extra options, merged over whatever the connection string carried. Anything an adapter accepts in its own options goes here.
Returns Connection
Raises ConnectionError if the database cannot be reached.
pool()
sql.pool(dsn, options) -> Pool
Opens a pool of connections.
Parameters
dsn(string|dict) — Asopen()takes.options(dict|nil) — The pool’s own settings, and any the adapter accepts. SeePoolfor what the pool reads.
Returns Pool
2026, Richard Ore and Zuri contributors