HomeDevCentral

New feature: Database engine
f5ae956e6554Unpublished

Unpublished Commit · Learn More

Repository Importing: This repository is still importing.

Description

New feature: Database engine

[ Feature description ]

A database abstraction layer, to be able to pick between MySQL,
MySQLi or PDO extension, and to.

A singleton pattern has also been added to better compartimentate
site components, but allowing to get easily an unique instance.

It will be used by Collection engine to provide a storage implementation
built on the top of the Database engine.

[ Context ]

The context has now a new public property: $db.

It's intended to replace:

global $db;

by:

$db = $this->context->db;

[ Database class ]

Database is based on the former sql_db class and offers
the following abstract methods: query, nextId, escape.

  • DatabaseResult Database::query(string $sql)
  • int Database::nextId()
  • string Database::escape($expression)

A simple implementation calling the DatabaseResult homonym
methods is provided for: fetchRow, numRows.

  • array Database::fetchRow (DatabaseResult $result)
  • int Database::numRows (DatabaseResult $result)

To replace sql_query_express, we provide a queryScalar method:

  • string Database::queryScalar($query, $errorMessage);

    It will return the scalar result (the value of the first field returned by the query or an empty string if there isn't any result) or die on SQL errors.

To create a new engine, inherits from this class.

[ DatabaseResult class ]

An abstract class with two functions to implement:

  • int DatabaseResult::numRows ()
  • int DatabaseResult::fetchRow ()

    It provides an associative array with TWO sets of key/values: one for numeric 0, 1, 2, ... access, one for by field access.

    e.g. for $result = $db->query("SELECT foo, bar FROM quux")

    ┌───────┬───────┐ │ foo │ bar │ ├───────┼───────┤ │ cat │ dog │ └───────┴───────┘

    $row = $db->fetchRow() will contain an array with 4 values:

    0 => 'cat' 1 => 'dog'

    'foo' => 'cat' 'bar' => 'dog'

An IteratorAggregate implementation, like a custom Iterator or a generator
has also to be implemented by inheriting classes.

[ DatabaseException class ]

An exception with a $query property to store the SQL query.

The $code property should contain the SQL error code, if existing.

[ Events implementation ]

  • QueryError event lets a method to be called when a query fails.

    To invoke it:

    $this->context->db->queryErrorEvents[] = <callable>; or Event::bind($this->context->db, 'QueryError', callable);
  • CantConnectToHost event is called on connection issue. Not really interesting to invoke right now, see bug #21.

[ MySQL implementation ]

The first implementation uses the legacy MySQL extension.

It replaces old includes/mysql.php sql_db class.

[ Compatibility ]

sql_db::sql_* methods are translated to correct calls.

e.g. $db->sql_escape will return $db->escape

[ Upstream ]

This will ship, maybe without the events in the next Keruald release.

[ L10n ]

  • New string: CantExecuteQuery, used when a query can't be executed as default Database::queryScalar message.

[ Unit testing ]

  • DatabaseException is fully covered
  • MysqlDatabase::escape() is convered, other methods no
  • MySQLDatabaseResult isn't unit tested

Details

Provenance
derecksonAuthored on
Parents
rOBSIDIANa31b74009930: MediaWikiMirror application: view, improve code
Branches
Unknown
Tags
Unknown