This documentation is powered by the Dispute component based on the pj DataProvider and the pj DataProvider Framework respectively.

Basic Concepts:

  • The pj DataProvider is a general usage framework usable in any PHP environment;
  • IDataProvider defines a function set the actual database engine interface must provide;
  • MySqlDataProvider is the IDataProvider layer to the MySql database engine (mysqli).
    Other database layers have only to be developed (e.g. PostgreDataProvider, SqlServerDataProvider, OracleDataProvider etc.) and implemented in the place of MySqlDataProvider with no effects for the application code itself;
  • InfoBase, representing one single database table row, is a base class for extension's classes with specific functionality related to application's database tables.

Usage:

The developer writes own DataProvider class, which instantiates and opens a singleton IDataProvider database connection (MySqlDataProvider) and eventually implemments specific business layer functions.

Then, he/she designs business layer classes extending InfoBase corresponding to the database model and registers them at start-up in the DataProvider. It is not necessary to add any code to bare class declaration, although it is convenient to outsource specific  functionality there.

The framework offers full database access for registered InfoBase classes, including retrieving by ID or by queries passed either by string or by SearchKey structure. It recognizes changes of instance data and inserts, updates and deletes instances together with corresponding table rows, optionally in nested transactions. It ensures that at any time, there exists at most one unique instance of InfoBase class representing one database table row.

Defines database related provider functions the DataProvider class rely on. The IDataProvider declaration is located in the folder /libraries/pj/data.

There is only MySqlDataProvider implementation for mysqli database engine yet. Other implementations like SqlServerDataProvider, OracleDataProvider etc. could be developed.

General functions

function Open ($host, $username, $passwd, $dbname, $prefix = '')

Connects to the database engine with passed arguments:

$host: Can be either a host name or an IP address
$username: The database user name
$passwd: The database user password
$dbname: If provided, it will specify the default database to be used when performing queries
$prefix: Tables' and stored procedures' name prefix

Throws DataProviderException with database error number and description if connection failed.

function ExecuteQuery ($query, array $args = null)

Executes database query $query replacing its arguments with $args values.

Returns raw database engine response without any interpretation of the content.

Throws DataProviderException if there's no connection.
Throws DataProviderException with resolved query and error description on underlying database engine errors.
Throws underlying database exceptions.

function ExecuteScalar ($query, array $args = null)

Executes database query $query replacing its arguments with $args values.

Throws DataProviderException if there's no connection.
Throws DataProviderException with resolved query and error description on database engine errors.
function ExecuteScript ($script)

Executes $script.

Throws underlying database exceptions.

InfoBase registering functions

function IsClassRegistered ($cls)

Returns table name associated with the class or false if not registered.

$cls: class name or class instance

function RegisterClass ($cls, $tableName) 

Registers the association between a class and an underlying database table. Ignored if the class is already registered.

$cls: class name or class instance
$tableName: name of the associated table (without event. prefix)

Throws DataProviderException if there's no connection.

function UnregisterClass ($cls)

Removes the association between a class and the underlying database table.

$cls: class name or class instance

InfoBase functions

function GetItem ($cls, $id, $forceRetrieve = false)

Returns the $cls instance by its ID or null if it doesn't exists.

The instance contains all column values from the underlying table row, eventually converted to PHP variable types.

If the $id instance was allready retrieved, the function returns the previous one.

$cls: class name or class instance
$id: unique table row identifier of whatever type
$forceRetrieve: sets whether an existing instance's $data should be retrieved again and $isDirty reset.

Throws DataProviderException if there's no connection.
Throws DataProviderException if $cls isn't registered.
Throws underlying database exceptions.

function UpdateItem (InfoBase $item)

Inserts or updates values from $item->data array into the database table row.

When inserting a new item, its $item->ID() is set to the new row's unique identifier.

The $item->isDirty is reset.

Throws DataProviderException if there's no connection.
Throws DataProviderException if $cls isn't registered.
Throws underlying database exceptions.

function DeleteItem (InfoBase $item)

Deletes the item's corresponding row from the underlying database table. The $item instance remains, but subsequent calls to UpdateItem have no effect.

Throws DataProviderException if there's no connection.
Throws DataProviderException if the $item class isn't registered.
Throws underlying database exceptions.

function DeleteItems ($cls, $select)

Deletes items' corresponding rows from the underlying database table. Eventual class instances remain, but subsequent calls to UpdateItem have no effect.

$cls: class name or class instance
$select: one of

  • SearchKey
  • Array of either IDs or $cls class instances (mixing allowed). For tables having no singular primary key, only instances are allowed;
  • String containing the SQL WHERE specification (like id IN (10,11,12) OR crit_id IS NULL)

Throws DataProviderException if there's no connection.
Throws DataProviderException if $cls isn't registered.
Throws DataProviderException if the $select is of improper data type or if $select array contains other elements types then integers (tables with singular primary key) or appropriate InfoBase instance.
Throws underlying database exceptions.

function ListItems ($cls, $select = null, $orderBy = null, array $fields = null, $startPos = -1, $count = PHP_INT_MAX)

Returns an array of $cls instances selected by $select and ordered by $orderBy. Already retrieved instances are not overwritten.

$cls: class name or class instance;

$select: SQL string or a SearchKey structure. If string, tokens "#_" are replaced by dbprefix and the rest of arguments is ignored. If not provided, the function returns all $cls instances from the underlying table.

$orderBy: if provided, it contains one or more sorting field names either separated by comma or collected in an array.

$fields: if provided, it limits $cls retrieved fields. The table's primary id field will be included anyway.  Instances previously or subsequently retrieved otherwise contain full field set.

$count: Limits the number of instances retrieved starting at  $startPos (zero based). If $startPos < 0, both arguments are ignored.

Throws DataProviderException if there's no connection.
Throws DataProviderException if $cls isn't registered.
Throws DataProviderException if $select is of other type than string, SearchKey or null.
Throws underlying database exceptions.

function CountItems ($cls, $select = null)

Returns the count of $cls (name or instance) rows selected by $select.

$select: SQL string or a SearchKey structure. If string, tokens "#_" are replaced by dbprefix. If not provided, the function returns total count of the underlying table rows.

Throws DataProviderException if there's no connection.
Throws DataProviderException if $cls isn't registered.

function ListIDs ($cls, $select = null, $orderBy = null)

Returns an array of $cls's table primary keys of whatever type selected by $select and ordered by $orderBy. Returns an empty array if the table has no primary key.

$cls: class name or class instance;

$select: SQL string or a SearchKey structure. If string, tokens "#_" are replaced by dbprefix and the rest of arguments is ignored. If not provided, the function returns all primary keys from the underlying table.

$orderBy: if provided, it contains one or more sorting field names separated by comma or collected in an array.

Throws DataProviderException if there's no connection.
Throws DataProviderException if $cls isn't registered.
Throws DataProviderException if $select is of other type than string, SearchKey or null.
Throws underlying database exceptions.

Transaction functions

function BeginTransaction ()

Begins the database transaction mode and returns a transaction variable used for subsequent call to either CommitTransaction() or RollbackTransaction().

BeginTransaction can be called from inside a transaction block recursively, but on each level, it must terminate by either CommitTransaction() or RollbackTransaction() with the corresponding transaction variable. Only the topmost level block actually begins and terminates the underlying database transaction.

Throws DataProviderException if there's no connection.
Throws DataProviderException if called from inside a transaction block already rolled back.
Throws underlying database exceptions.

function CommitTransaction ($trans)

Commits the transaction $trans returned by previous call to BeginTransaction.

If called recursively, only the topmost level block actually commits the underlying database transaction.

Throws DataProviderException if there's no connection.
Throws DataProviderException if calledfrom inside a transaction block already rolled back.
Throws DataProviderException if $trans doesn't correspond with current recursion level (i.e. nested transaction wasn't terminated).
Throws underlying database exceptions.

 function RollbackTransaction ($trans)

Rolls back the transaction $trans returned by previous call to BeginTransaction.

If called recursively, only the topmost level block actually rolls back the underlying database transaction.

After rollback, all updated InfoBase's data are reset to the state as they entered the Update call. This ensures that ID field of new item has its previous value and its isNew() is set.

Throws DataProviderException if there's no connection.
Throws DataProviderException if $trans doesn't correspond with current recursion level (i.e. nested transaction wasn't terminated).
Throws underlying database exceptions.