Runo

Runo — Overview & setup

Runo is the database engine for the Lunaris framework, but it works as a standalone library. It is intentionally not an ORM: a thin utility layer over PDO for executing plain SQL, binding values, hydrating rows into objects, casting, and managing transactions.

Philosophy

Runo does not provide high-level abstractions like other ORMs. Instead, it encourages writing queries in plain SQL while providing bare-metal utilities such as a PDO wrapper. Raw SQL through DB::prepare() is a first-class path; the optional Sql builder is a convenience, not a requirement.

Features

  • Static connection facade backed by one PDO instance
  • Fluent prepared statements with typed value binding
  • Plain SQL as the primary query path
  • Optional SQL builder for SELECT, INSERT, UPDATE, and DELETE
  • Reusable query classes with per-query casts and record classes
  • Automatic hydration of rows into Record objects
  • Column casting for integers, floats, booleans, JSON, and date/time classes
  • Result collections with iteration, counting, first(), pluck(), and toArray()
  • Query metadata: last insert ID, affected row count, and execution time
  • Closure-based and manual transaction APIs
  • No runtime dependencies beyond PHP and PDO
  • PSR-4 autoloading under the Runo\ namespace

Requirements

  • PHP 8.0 or newer (Docker image uses PHP 8.3)
  • The PDO extension and the PDO driver for your database (pdo_mysql required for MySQL/MariaDB)
  • MySQL or MariaDB
  • Composer for installation and autoloading

Installation

Install with Composer:

composer require lunaris/runo

Load the Composer autoloader:

require __DIR__ . '/vendor/autoload.php';

For local development against a checked-out copy, add a Composer path repository pointing to ../runo.

Quick start

<?php

require __DIR__ . '/vendor/autoload.php';

use Runo\DB;

DB::init([
    'driver'   => 'mysql',
    'host'     => '127.0.0.1',
    'port'     => 3306,
    'database' => 'app',
    'username' => 'root',
    'password' => 'secret',
]);

$posts = DB::prepare(
    'SELECT id, title FROM posts ORDER BY id DESC'
)->execute();

foreach ($posts as $post) {
    echo $post->id . ': ' . $post->title . PHP_EOL;
}

echo 'Rows: ' . $posts->count() . PHP_EOL;

Configuration

KeyRequiredExampleDescription
driverYesmysqlPDO driver used as the DSN prefix.
hostYes127.0.0.1Database server hostname or IP address.
portYes3306Database server port.
databaseYesappDatabase name.
usernameYesrootDatabase username.
passwordYessecretDatabase password. Can be an empty string.

The generated DSN is always {driver}:host={host};port={port};dbname={database};charset=utf8mb4. PDO is configured with ERRMODE_EXCEPTION, FETCH_OBJ, and EMULATE_PREPARES=false.

Connection lifecycle

  • The connection is stored in a static property and shared by every DB call.
  • DB::init() only creates a connection when none exists; later calls are silently ignored.
  • There is no public method to close, reset, or replace the connection.
  • DB::prepare() throws Exception("Database not initialized.") if DB::init() has not been called.
  • DB::commit() and DB::rollback() assume a connection already exists.

Architecture

Raw SQL or Runo\Support\Sql
            |
            v
      DB::prepare()
            |
            v
   Runo\Database\Statement  (args / cast / as)
            |
            v
   PDO prepares and executes
            |
            v
   Runo\Support\Hydrator
            |
            v
   Runo\Support\Result  (items: Record[], meta(): Attributes)

Project structure

runo/
├── composer.json
├── readme.md
├── src/
│   ├── DB.php
│   ├── Database/
│   │   ├── Query.php
│   │   └── Statement.php
│   └── Support/
│       ├── Attributes.php
│       ├── Hydrator.php
│       ├── Raw.php
│       ├── Record.php
│       ├── Result.php
│       └── Sql.php
└── docker/  (local dev environment)

Local development with Docker

From the project root: docker compose -f docker/docker-compose.yml up --build. Services include PHP 8.3 + Apache on :8080 and MariaDB 10.11 on :3306.

Known limitations

  • Only MySQL/MariaDB is targeted by the built-in DSN; one connection only.
  • All results are fetched eagerly; no lazy/streaming cursor.
  • Sql is a builder only (no execution). When used with Query, bindings must be passed explicitly.
  • Nested where() groups can collide with placeholder names; where() requires an explicit operator.
  • whereIn([]) produces invalid SQL; offset() without limit() may fail on MySQL.
  • update()/delete() without where() affect every row.
  • No nested transactions/savepoints; identifiers/Raw are not escaped.
  • Last insert ID is only detected for statements starting with INSERT.

Part of the Lunaris documentation · Source on Codeberg · MIT License

Part of the Lunaris documentation · Source on Codeberg · MIT License