Source for file SqlRecordset.php
Documentation is available at SqlRecordset.php
* @copyright Copyright (c) 2003-2006 Mirchev Ideas Ltd. All rights reserved.
* Handle and manage the reading of data from database.
* This class only read data from a given database table with necessary
* ordering and filtering.
* @copyright Copyright (c) 2003-2006 Mirchev Ideas Ltd. All rights reserved.
* miSqlRecordset constructor
* $recordset = new miSqlRecordset('tableName');
* $recordset = new miSqlRecordset(array('firstTable', 'secondTable'));
* @param string|array$table database table name, or several table names
* @param string $joinType the type of the join, eg. INNER, LEFT
* @param string $joinTable the table to be joined
* @param string $joinCondition the condition of the join
* Sets the field by which the result of the query will be ordered and
* the direction of ordering - ascending or descending.
* $recordset = new miSqlRecordset('tableName');
* $recordset->setOrder($tableField, 'ASC');
* @param string $sortBy database table field name
* @param string $sortDir direction of ordering - ascending or descending
public function setOrder($sortBy, $sortDir)
* The param is directly added after the GROUP BY clause
* @param string the group by fields
* $recordset = new miSqlRecordset('tableName');
* $recordset->addFilter($filter);
* @param array $filter a filter object
public function addFilter(miSqlFilter $filter)
* @param array $filters array of filter objects
* Sets the names of the filters to be used in a HAVING clause instead of WHERE
* @param array $filterNames array of filter names to be used in a HAVING clause instead of WHERE
* Defaults to * (all columns)
* Sets how many rows to be read from the databse
* $recordset = new miSqlRecordset('tableName');
* $recordset->setRecordsLimit(5, 20);
* @param int $from from which record index to start
* @param int $num how many rows to read
* Return the order clause
* $recordset = new miSqlRecordset('tableName');
* $recordset->setOrder($tableField, 'ASC');
* $order = $recordset->getSqlOrderClause();
* @return string order clause
* Return all the specified filter clauses as a string
* @return string all the specified filter clauses as a string
* @throws miConfigurationException
// Validate the filter fields
$filterFields =
$filter->getSqlFields();
foreach ($filterFields as $filterField) {
$sql =
$filter->getSql();
return implode(' AND ', $filtersSql);
* Return all the specified HAVING filter clauses as a string
* @return string all the specified HAVING filter clauses as a string
* @throws miConfigurationException
$sql =
$filter->getSql();
return implode(' AND ', $filtersSql);
* Return the limit as a sql code
* @param int $fromIndex from which record index to start
* @param int $numRecords number of rows to be read
* @return string the limit as a sql code
$fromIndex = (int)
$fromIndex;
$numRecords = (int)
$numRecords;
return ' LIMIT ' .
$fromIndex .
',' .
$numRecords;
* Return the join conditions sql
* @return string the join conditions sql
$sql .=
' ' .
$joinCondition[0] .
' JOIN ' .
$joinCondition[1] .
' ' .
$joinCondition[2];
* Returns the table sql clause
* @return string the table clause
return ' FROM ' .
$this->_table;
* Returns the group by clause
* @return string sql clause
* Returns the query to select all records for the recordset (without a LIMIT clause)
* @return string sql clause
$filterClause =
' WHERE ' .
$filterClause;
if ($filterHavingClause !=
'')
$filterHavingClause =
' HAVING ' .
$filterHavingClause;
* Return the number of all rows in a database table that reply to the
* $recordset = new miSqlRecordset('tableName');
* $recordset->addFilter($arrayFilter);
* $count = $recordset->getRecordsCount();
* @return int number of all rows in a database table
$filterClause =
' WHERE ' .
$filterClause;
// If there are fields for the HAVING clause, the only way to get the number of records is to execute the actual query as a subselect
$query =
'SELECT COUNT(*) AS N FROM ('.
$this->getSelectQuery().
') SubQuery';
return (int)
$rows[0]['N'];
* Read rows from a database table.
* The result reply to the given filter clause and is returned in order
* specify by order clause
* $recordset = new miSqlRecordset('tableName');
* $recordset->setOrder($tableField, 'ASC');
* $recordset->addFilter($filter);
* $rows = $recordset->getRecords();
* @return array rows returned from database
* Read fixed number of rows from a given index in a database table.
* The result reply to the given filter clause and is returned in order
* specify by order clause
* If both $fromIndex and $numRecords are false returns all records
* $recordset = new miSqlRecordset('tableName');
* $recordset->setOrder($tableField, 'ASC');
* $rows = $recordset->getRecordsByIndex($fromIndex, $numRecords);
* @param int $fromIndex from which record index to start
* @param int $numRecords number of rows to be read
* @return array rows returned from database
if (($fromIndex !==
false) &&
($numRecords !==
false))
* Read all records from the database table
* The result is limited to the given filter clause and is returned in order
* specify by order clause
* @return array rows returned from database
* Returns the list of all selected fields
* An empty array is returned of no query has been executed
* @return array the selected field names
* Returns all fields of the recordset table(s)
* The result of the function is cached. Call only after all join conditions have been added.
* @return array the field names
$tables =
array($this->_table);
$tables[] =
$joinCondition[1];
foreach ($tables as $table)
* Returns the table name for the current recordset
Documentation generated on Thu, 08 May 2008 16:57:38 +0300 by phpDocumentor 1.4.1