Native databases
Connect through native PostgreSQL, MySQL, SQL Server and MongoDB clients, keep credentials in aner.secrets, and materialize bounded data for analysis.
aner.db connects Aner programs directly to databases through native client libraries. This is the module entry point for PostgreSQL, MySQL, Microsoft SQL Server and MongoDB. Its connector namespaces contain the database specific factories and operations; the shared db API owns sessions, SQL queries and results. aner.data owns typed tables and tensor preparation for analysis.
Start here for the shared contract, then open the appropriate connector guide for connection arguments, TLS configuration, parameter syntax, result types, limits and a complete runnable example.
Choose a connector
All four connectors are implemented as optional features of the native executable. A particular installed package can omit a connector. The client runtime must also be available, and SQL Server needs its separately installed vendor driver.
| Connector namespace | Native client | Detailed guide | Runnable example |
|---|---|---|---|
aner.db.postgres | PostgreSQL libpq | PostgreSQL | Walkthrough, source |
aner.db.mysql | MariaDB Connector/C 3.4 or newer, using the MySQL protocol | MySQL and MariaDB | Walkthrough, source |
aner.db.mssql | Native ODBC with Microsoft ODBC Driver 18 for SQL Server | Microsoft SQL Server | Walkthrough, source |
aner.db.mongodb | MongoDB C driver and BSON library | MongoDB | Walkthrough, source |
Inspect your installed executable before configuring a connection:
import aner.db;
print(db.available("postgres"));
print(db.available("mysql"));
print(db.available("mssql"));
print(db.available("mongodb"));db.available reports compiled support. It does not test a server, credentials, TLS trust or SQL Server's vendor driver. If unavailable, use a compatible native Aner package containing that connector. Importing a connector enables its factory and shared db operations.
Keep credentials private
Create a credential in a local terminal using Aner's hidden prompt:
aner secrets set demo-db-password --store system
aner secrets status demo-db-password --store systemThe program holds only secrets.stored("demo-db-password"), an opaque SecretRef. Connection factories require that reference instead of a password String. Native macOS Keychain and Windows Credential Manager adapters are implemented; Linux system storage is unavailable. For a headless process, inject a credential privately and select secrets.env("ANER_DB_PASSWORD"). Aner does not discover or load .env files.
The explicit POSIX file fallback pairs aner secrets set demo-db-password --store file with secrets.file("demo-db-password"). It stores plaintext in files accessible only to their owner, with directory mode 0700 and file mode 0600; it is not an encrypted vault. See Credentials for availability, permissions, removal and rotation.
Examples use fictional .invalid hosts, database names and users. Replace them privately and keep edited settings, credentials, SQL values and outputs out of public source, notebooks and websites. Use verified TLS with a trusted CA and matching server hostname. Private network addresses still require an explicit transport policy.
Connection workflow
- Check
db.availablefor your chosen connector and read its native runtime requirements. - Configure the database role, trusted CA and fictional example settings privately. Store the password with
aner.secrets. - Import
aner.db.<connector>andaner.secrets, then call that connector'sconnectfactory with aSecretRef. The connector import also enables the shareddboperations. - Bind SQL values separately, or supply reviewed MongoDB JSON objects. Set explicit result limits before materializing data.
- Inspect missing values and types, convert suitable results for analysis, and close the session when finished.
The connector guides provide complete connection programs for each database. The SQL examples read a prepared synthetic fixture; the MongoDB example writes and then reads its demo documents.
Shared SQL interface
PostgreSQL, MySQL and SQL Server return DBResult values through the same operations:
| Call | Behavior |
|---|---|
db.query(connection, sql, parameters, max_rows: 100000, max_bytes: 67108864) | Materialize one query result with checked limits. Parameters are a List of String or null. |
db.execute(connection, sql, parameters) | Execute a command and return its reported affected row count. Transaction syntax follows the server. |
db.rows(result), db.cols(result) | Inspect result dimensions. |
db.column_name(result, column), db.column_type(result, column) | Inspect column metadata. |
db.is_null(result, row, column) | Check a missing cell before reading it. |
db.integer, db.number, db.boolean, db.text | Read a nonmissing cell using the appropriate column type. Indices start at zero. |
db.to_table(result, memory_bytes) | Copy the result to a separately bounded DataTable. |
db.connected(connection), db.close(connection) | Inspect or close the native session. Closing is idempotent. |
Parameters represent values, never identifiers or SQL fragments. PostgreSQL uses $1, $2 and SQL casts; MySQL and SQL Server use ?. Supply parameters separately instead of interpolating user input into SQL. Use a least privilege database role for your workload.
Copies of a connection share one session, so closing one invalidates every alias. Operations on one session serialize. Use separate connections for independent concurrent workers. Completed results remain readable after the session closes. A failed statement inside a transaction may need an explicit rollback according to the connector and server.
Native MongoDB interface
MongoDB uses the same DBConnection, DBResult, db.rows, db.text, db.to_table and close operations, with document operations supplied by its connector:
| Call | Behavior |
|---|---|
mongodb.find(connection, collection, filter_json, options_json: "{}", max_documents: 100000, max_bytes: 67108864) | Read a bounded native cursor result. Filter and options must each be one JSON object. |
mongodb.document(result, row) | Read one document as canonical Extended JSON, preserving BSON types. |
mongodb.insert_one(connection, collection, document_json) | Insert one document and return 1 after an acknowledged write. |
mongodb.command(connection, command_json, max_bytes: 67108864) | Run a database command with the account's permissions and return canonical Extended JSON. |
A MongoDB result has one String column named document. Table conversion retains those strings; it does not flatten fields or create numeric feature tensors. Choose and transform numeric attributes explicitly before training. Commands can write data, and a successful write can remain applied when its response is lost or exceeds a byte limit. See the MongoDB guide for supported options, input validation, timeouts and write semantics.
From SQL to a feature tensor
Select complete numeric features in SQL, then convert explicitly:
import aner.data;
import aner.tensor;
let table: DataTable = db.to_table(features, 1048576);
let matrix = data.to_tensor(table, 1048576);
print(tensor.rows(matrix));This fragment uses a features query result from an active SQL connection. See each complete source example for connection setup and the query. String columns, exact decimal text and missing values need an explicit preparation policy before tensor conversion. Use unique SQL aliases valid as aner.data column names.
Result limits bound retained Aner storage rather than total process memory. Native client buffers, allocator capacity and transient conversion data can use additional memory. Reduce remote work with projection, selective filters, server row limits and the server's timeout controls. Overflow returns no partial result; the connector guide describes recovery and unsupported operations.
Synthetic data and complete examples
The SQL fixtures contain 400 deterministic samples, three numeric features and four class labels, 0 through 3. They create demo class and sample tables using a database administration tool. PostgreSQL deliberately fails on repeated table creation; MySQL and SQL Server preserve existing rows and add missing IDs. Review the SQL in a dedicated development database before running it.
- PostgreSQL setup SQL and Aner source
- MySQL setup SQL and Aner source
- SQL Server setup SQL and Aner source
- MongoDB source, which appends four documents per run
Check and run the downloaded source after replacing its settings privately:
aner check examples/db_postgres.aner
aner run examples/db_postgres.anerChecking makes no database connection. Running connects to the configured server. SQL examples read the prepared demo tables; the MongoDB example writes to a dedicated collection. These records teach connector use and are not measured performance or classification benchmarks.
Choose limits and resolve connection problems
A connector being available does not establish server reachability or authentication. Check the selected runtime, private endpoint, database role and trusted certificate configuration when a connection fails. Keep diagnostics and real connection values private. Every connector guide describes its own transport policy and sanitized error format.
SQL rows can contain exact integers, floating point values, booleans, text and missing cells. Exact decimals and database specific types follow each connector's documented mapping. Inspect metadata before reading cells or converting data. MongoDB preserves BSON inside canonical Extended JSON rather than assuming a tabular schema.
Choose row and byte limits for the retained result and a separate budget for db.to_table. These caps exclude native driver buffers and conversion overhead. Use server filtering, projection, row limits and supported server timeout options to reduce remote work. The connector guides explain recovery after overflows, transaction errors and unsupported operations.
Related references: typed data preparation, Tensor and private credentials.