Skip to content

API Database

Dory Azar edited this page Nov 15, 2026 · 1 revision

Caligrafy\Database

Caligrafy Quill 2.26.0. Generated from the source by php qa/build-api-reference.php; the guides explain how to use these classes, this reference says exactly what each public method takes and returns. Back to the API reference.

Part of Data.

namespace Caligrafy;

class Database extends \stdClass

Source: framework/database/Database.php:19

The Database class handles all database connections and global functions that interface directly with the database

Methods

Database::connect()

public static function connect()

Connects to the database

Returns the: database connection

Source: framework/database/Database.php:39

Database::persistent()

public static function persistent($adapter)

Whether this process keeps its database connection between requests (2.26.0): DB_PERSISTENT, true by default for MySQL and PostgreSQL in a web server; never on the command line (tests, migrations, the queue: each run is fresh), never for SQLite. DB_PERSISTENT=false connects on every request, as before 2.26.0.

Parameters

  • $adapter

Source: framework/database/Database.php:62

Database::reset()

public static function reset($connection)

A reused connection starts clean: a transaction the previous request left open is rolled back (PHP rolls it back at the end of a request too; this is the second lock), and the session time zone is set again by alignTimeZone. PDO checks that a reused connection is alive and opens a new one if the database dropped it.

Parameters

  • $connection

Source: framework/database/Database.php:79

Database::alignTimeZone()

public static function alignTimeZone($connection, $adapter)

MySQL session time zone = PHP's (SERVER_TIMEZONE), so that NOW(), CURRENT_TIMESTAMP columns and the times PHP writes (the audit log) use the same clock. DB_TIMEZONE=server keeps the database server's own time zone.

Parameters

  • $connection
  • $adapter

Source: framework/database/Database.php:95

Database::dsn()

public static function dsn($adapter, $host, $dbName, $port = null)

Builds the PDO DSN from the DB_* settings DB_PORT is added unless DB_HOST already carries a ';port=' (older workaround)

Parameters

  • $adapter
  • $host
  • $dbName
  • $port, default null

Returns string: the DSN

Source: framework/database/Database.php:112

Database::execute()

public static function execute($query, $args = array())

Executes PDO Query

Parameters

  • $query string: defines the query to be executed
  • $args array, default array(): defines the argument that need to be bound to the query

Returns rendered: result in array or json format

Source: framework/database/Database.php:131

Database::render()

public static function render($result)

Result rendered

Parameters

  • $result array: defines the result coming from the database in an array format

Returns rendered: result in array or json format depending on the setting

Source: framework/database/Database.php:161

Database::toArray()

public static function toArray($result)

Forces the result to an array

Parameters

  • $result array: defines the result coming in either a json or array format

Returns array: $result the result rendered as an array

Source: framework/database/Database.php:179

Database::is_Json()

public static function is_Json($string)

Checks if a string i a JSON

Parameters

  • $string string: defines the string to be checked

Returns boolean: if the string is a JSON

Source: framework/database/Database.php:197

Database::connection()

public static function connection()

The PDO connection, connecting on first use (null when DB_ACTIVATE is not true); tests run in its transactions

Source: framework/database/Database.php:206

Database::endRequest()

public static function endRequest()

The end of a request in a process that serves many (2.26.0, the async server): a transaction left open is rolled back, the counters forgotten; the connection stays open for the next request

Source: framework/database/Database.php:230

Database::useConnection()

public static function useConnection($connection = null)

Uses this PDO connection (a test's in-memory SQLite, a worker's own connection); null connects again on next use

Parameters

  • $connection, default null

Source: framework/database/Database.php:242

Database::table()

public static function table($table)

A query builder on a table: Database::table('books')->where('price', '<', 20)->get()

Parameters

  • $table string: the table (letters, digits, _)

Returns Query

Source: framework/database/Database.php:253

Database::transaction()

public static function transaction(callable $callback)

Runs $callback in a transaction: committed when it returns, rolled back when it throws (the exception is thrown again). A transaction inside another one (or inside a test's transaction) just runs in the outer one.

Parameters

  • $callback callable

Returns mixed: what $callback returns

Source: framework/database/Database.php:263

Database::rowCount()

public static function rowCount()

The number of rows the last INSERT, UPDATE or DELETE changed

Source: framework/database/Database.php:291

Database::lastInsertId()

public static function lastInsertId()

Gets Last Inserted Id

Returns int: Last Inserted Id

Source: framework/database/Database.php:302

Caligrafy: Describe it. Approve it. Ship it. Your idea turned into a complete app: every feature built once, for web pages, Vue apps, a REST API and AI agents, and online.

Start here

Why Caligrafy: the benefits, the measurements, and what it is not yet

  1. Quickstart with Claude: describe it, approve it, ship it

    or the Quickstart: the same three steps by hand, in 5 minutes

  2. How Caligrafy works: the ideas in 5 minutes
  3. Your first capability: build a feature, page included

Capabilities

Experiences

Data and people

  1. Models
  2. Relationships
  3. Queries: filters, paging, related rows
  4. Sign-in, users and tokens

Built-in features

Put it online

Build with AI

Reference

Clone this wiki locally