Part III — Advanced Systems · Chapter 17 · Lesson 1 of 7 · 8 min read
Lesson 1: SQLite and Optional Database Adapter Contracts
Lesson level: Advanced
Purpose: Connect PeaLang data work to relational databases through explicit, testable contracts while separating the built-in SQLite foundation from optional adapters that require external drivers and live validation.
Release note. The project materials describe a SQL foundation with a SQLite implementation and adapter contracts for DuckDB, PostgreSQL, MySQL/MariaDB, SQL Server, and Oracle. They also record live provider integration certification as an external release gate that remains pending in RC2. Module paths, class names, and method spellings in this chapter are illustrative. Verify them against the installed reference before running a lab.
Learning objectives
By the end of this lesson, you should be able to distinguish an implemented adapter from an adapter contract; describe the responsibilities of a database connection boundary; choose SQLite for local learning and deterministic tests; design configuration without embedding credentials; and state what evidence is needed before claiming that a provider adapter works.
1.1 Why a database boundary needs a contract
A database is not just another file format. It has identity, permissions, concurrency, transactions, server-side types, network behavior, and operational ownership. A PeaLang module that queries it inherits all of those properties. Treat the database connection as a host-controlled capability, not as an incidental helper.
An adapter contract states what every supported database integration must provide: how a connection is opened and closed, how parameters are bound, how results are typed, how transactions are expressed, how errors are classified, and how metadata is discovered. A concrete adapter implements that contract for one engine.
The distinction matters. A contract can be complete while an implementation is untested against a real service. A unit test with a fake connection can verify the PeaLang side of the boundary without proving driver compatibility, network configuration, authentication, isolation semantics, or server version behavior.
1.2 Responsibilities on each side
Host responsibilities: select approved databases; supply connection configuration and credentials through an approved secret provider; enforce network policy; grant database capability; set timeouts and resource budgets; record audit events; and own operational monitoring.
Adapter responsibilities: translate the contract into driver calls; bind parameters safely; map database types to portable values or DataFrame types; report failures consistently; and release connections, cursors, and transactions deterministically.
PeaLang module responsibilities: express queries with parameters; declare expected result schemas; validate returned data; handle documented errors; and avoid assumptions about engine-specific behavior unless the module explicitly targets that engine.
Common mistake: passing an unrestricted connection object across the host boundary. Expose a narrow capability—such as approved read queries against one schema—when the task does not require broader authority.
1.3 Why SQLite is the learning baseline
SQLite is an embedded database engine stored in an ordinary file or created in memory. It needs no separate server, which makes it well suited to repeatable lessons, isolated unit tests, and small local applications. In the project materials, it is the adapter with an implementation rather than only a contract.
That convenience does not make SQLite a universal substitute. Server engines differ in type systems, identifier quoting, transaction isolation, locking, date handling, decimal precision, upsert syntax, and error codes. A query that succeeds on SQLite can fail or behave differently on another engine.
Use SQLite to establish the PeaLang-side contract: parameter binding, schema expectations, transaction boundaries, result conversion, and error handling. Then validate each production engine separately with its approved driver, version, configuration, and test data.
Illustrative local connection. A host creates a database handle for learning.sqlite in an approved workspace directory, grants the module read and write access only to that handle, sets a short operation timeout, and closes the handle after the lab. Confirm the actual constructor, options, and lifetime method before implementation.
In-memory databases. An in-memory database is useful for deterministic tests because each test can start from an empty state. Remember that its contents disappear when the connection closes. Do not use it to demonstrate persistence or crash recovery.
1.4 Configuration without secrets in source
Database configuration has two categories. Non-secret settings include engine, host name, port, database name, schema, timeout, and pool size. Secrets include passwords, tokens, private keys, and client certificates. Keep the categories separate.
Store non-secret configuration in versioned, reviewable project configuration where policy permits. Resolve secrets at runtime through an approved provider. Never commit secrets in .pea source, test fixtures, sample connection strings, logs, or error messages.
Illustrative profile: engine = sqlite; path = approved local file; read_only = false; timeout_ms = bounded value; secret_ref = none. A server profile would replace the path with host, port, and database fields and identify a secret reference rather than embed its value.
Guided practice. Classify each setting as public configuration, environment-specific configuration, or secret: database name, password, port, schema, client key, maximum pool size, and read-only flag.
Review prompt. Why should an adapter error message identify the connection profile without displaying the full connection string?
1.5 Mapping database types to PeaLang values
Database results must cross into PeaLang as portable values or DataFrame columns. Every adapter therefore needs a type-mapping table. The table should state the source database type, the PeaLang representation, null handling, precision or range limits, and failure behavior.
Integers usually map naturally, but range still matters. Exact decimal values need special care: converting a currency amount to binary floating point can introduce rounding error. Date, time, timestamp, and time-zone types require explicit conventions. Binary large objects, arrays, JSON columns, and engine-specific types may need host-side conversion or rejection.
Illustrative mapping policy. Integer columns map to integer values within the supported range; exact decimal columns map to a declared exact representation or fail if none is available; text maps to string; boolean maps to boolean; database null maps to null; unsupported types fail with a clear schema error instead of being converted to text silently.
SQLite deserves particular attention because its storage model is flexible. A column declaration does not necessarily prevent values of another storage class from being inserted. Validate result types at the PeaLang boundary rather than trusting the table definition alone.
Guided practice. Build a mapping table for identifier, display name, active flag, created timestamp, credit limit, optional notes, and metadata JSON. Identify which mappings need a policy decision.
1.6 Capability descriptors and adapter selection
An adapter should describe what it supports: transactions, savepoints, batch execution, upsert, schema discovery, streaming results, read-only connections, and parameter styles. A caller can then check requirements before running a workflow.
Capability checks prevent false portability. If a pipeline requires merge semantics, it should fail clearly on an adapter that lacks a verified upsert path rather than emulate merge with an unsafe sequence of reads and writes.
Illustrative selection rule: require transaction support and parameter binding for every write workflow; require upsert only for synchronization workflows; require discovery only for administrative inspection; deny DDL capability in ordinary application modules.
Common mistakes. Assuming every SQL engine accepts the same placeholder style; relying on implicit autocommit; treating a contract stub as a tested driver; and allowing an application role to create or drop tables when it only needs to read and write rows.
Review prompt. What evidence distinguishes a declared capability from a capability that has been certified against a live service?
1.7 Hands-on lab: A local SQLite boundary
Scenario. Build a small product catalog boundary for local development. Use synthetic records only.
- Have the host create an approved SQLite database in the learning workspace.
- Create a products table with an integer identifier, text SKU, text name, exact or explicitly documented price representation, active flag, and update timestamp.
- Expose one read capability for active products and one write capability for inserting a product. Do not expose arbitrary SQL execution to the PeaLang module.
- Declare the expected result schema and validate each returned row.
- Test a valid insert, a duplicate SKU, a missing required field, an unsupported value type, and a closed connection.
- Record which failures come from validation, the adapter, or the database.
Acceptance criteria. The module uses parameters for all variable data; credentials are absent; type conversions follow the mapping table; failures are classified; connections are closed after success and failure; and the lab notes do not claim that any server adapter was tested.
Review questions. What is an adapter contract? Why is SQLite suitable for deterministic lessons? Which settings are secrets? Why should unsupported types fail clearly? How does a capability descriptor protect a pipeline?
Applied challenge. Write a portability note for moving the catalog from SQLite to an approved server database. List the type, transaction, constraint, upsert, authentication, network, and performance checks required before production use.
Lesson summary. Database integration is a capability boundary. Use SQLite to develop and test the contract locally, keep secrets outside source, map types explicitly, and require engine-specific evidence before relying on optional adapters.
Common mistakes
- Treating adapter contracts as working drivers: in v0.63 SQLite is the implemented reference backend; the other databases are adapter contracts that need a real driver and certification before use.
- Putting credentials in connection strings: resolve secrets at runtime from a secret provider.
- Assuming type mappings are lossless: check decimal, date-time, and boolean mappings for each backend.
- Ignoring capability descriptors: check what a backend supports before relying on transactions, upserts, or schema discovery.
- Using an on-disk test database shared between tests: use an isolated in-memory or temporary SQLite database per test.
Review questions
- What responsibilities belong to the PeaLang side and to the adapter side of a database boundary?
- Why is SQLite used as the learning and test backend?
- What does it mean that other databases are adapter contracts in v0.63?
- How should configuration be supplied without exposing secrets?
- Why must type mappings be tested per backend?
Applied challenge
Build a small SQLite-backed product catalogue with configuration read from the environment, a capability check at start-up, and tests that run against an in-memory database. Write a one-page note listing what would need to be added and certified before the same code could use another database adapter.
Lesson summary
A database boundary separates PeaLang logic from storage details. SQLite is the implemented backend in v0.63; other databases are contracts until a driver is supplied and certified. Keep secrets out of configuration, check capabilities, and test type mappings explicitly.
Lesson status: Ready for review.