Query.class.php
<?php
class Query {
private $query;
private $prefixlist;
private $postfixlist;
private $table;
private $alias;
private $fieldlist;
private $joins;
private $parameters;
private $orderby;
private $values;
private $numberOfPages;
function Query($primarytable) {
$this->table = $primarytable;
$this->alias = $primarytable;
// SELECT {fieldlist}
// FROM {primarytable}
// JOIN
}
function SetDisplayList (array $fieldlist) {
$this->fieldlist = $fieldlist;
}
function SetTableAlias($alias) {
$this->alias = $alias;
}
function AddJoin ($typeofjoin, $jointable, $joinalias, $criteria) {
$defcriteria = "{$joinalias}.iCompanyID = {$this->alias}.iCompanyID AND {$joinalias}.iDivisionID = {$this->alias}.iDivisionID";
$newjoin = $typeofjoin.' '.$jointable.' '.$joinalias.' ON ('.$defcriteria;
if (strlen($criteria) > 0) {
$newjoin = $newjoin.' AND '.$criteria;
}
$newjoin = $newjoin.') ';
$this->joins[] = $newjoin;
/*
*
* JOIN Site_Page2Site_Section ON (Site_Page2Site_Section.iCompanyID = Site_Page.iCompanyID
AND Site_Page2Site_Section.iDivisionID = Site_Page.iDivisionID
AND Site_Page2Site_Section.iSite_PageID = Site_Page.iSite_PageID)
JOIN Site_Section ON (Site_Section.iCompanyID = Site_Page2Site_Section.iCompanyID
AND Site_Section.iDivisionID = Site_Page2Site_Section.iDivisionID
AND Site_Section.iSite_SectionID = Site_Page2Site_Section.iSite_SectionID)
*
*/
}
function AddParameter ($criteria1, $value) {
$this->parameters[] = $criteria1;
$this->values[] = $value;
}
function AddPrefixOption ($value) {
$this->prefix[] = $value;
}
function AddPostfixOption ($value) {
$this->prefix[] = $value;
}
function SetOrderBy ($orderby) {
$this->orderby = $orderby;
}
function ExecuteSelectQuery () {
global $db;
$select = $this->BuildSelectQueryString();
$select = $db->Prepare($select);
$ret = $db->Execute($select,$this->values);
return $ret;
}
function ExecuteQueryWithPaging($ar) {
global $db;
$select = $this->BuildSelectQueryString();
$select = $db->Prepare($select);
if (isset($ar['Page'])) {
$iPage = (int)$ar['Page'];
if (isset($ar['PageLength'])) {
$iPageLength = (int)$ar['PageLength'];
}
else {
$iPageLength = 10;
}
} else {
$iPage = 1;
$iPageLength = 10;
}
//var_dump("Page:$iPage PageLength:$iPageLength");
$ret = $db->PageExecute($select, $iPageLength, $iPage, $this->values);
$this->numberOfPages = ((int)$ret->_maxRecordCount / $iPageLength) + (($ret->_maxRecordCount % $iPageLength) > 0) ;
return $ret;
}
function BuildSelectQueryString () {
global $db;
$prefixoptions = '';
if (isset($this->prefixlist)) {
foreach($this->prefixlist as $prefix) {
$prefixoptions = $prefixoptions.' '.$prefix;
}
}
$postfixoptions = '';
if (isset($this->postfixlist)) {
foreach($this->postfixlist as $postfix) {
$postfixoptions = $postfixoptions.' '.$postfix;
}
}
$listoffields = '';
if (isset($this->fieldlist)) {
foreach($this->fieldlist as $field) {
$listoffields = $listoffields.', '.$field;
}
$listoffields = ltrim($listoffields,',');
}
$select = 'SELECT '.$prefixoptions.' '.$listoffields;
if (strcasecmp($this->table,$this->alias) == 0)
$from = ' FROM '.$this->table;
else
$from = ' FROM '.$this->table.' '.$this->alias;
$joinlist = '';
if (isset($this->joins)) {
$joinlist = '';
foreach($this->joins as $j) {
$joinlist = $joinlist.$j;
}
}
$where = '';
$parmlist = '';
if (isset($this->parameters)) {
$where = ' WHERE ';
foreach($this->parameters as $p) {
if ( strlen($parmlist) > 0 ) {
$parmlist = $parmlist.' AND '.$p;
} else {
$parmlist = $p;
}
}
}
$orderby = '';
if (isset($this->orderby)) {
$orderby = ' ORDER BY '.$this->orderby;
}
// $parmlist = '';
$where = $where.$parmlist;
$select = $select.' '.$from.' '.$joinlist.' '.$where.' '.$orderby.' '.$postfixoptions;
//var_dump($select);
//var_dump($this->values);
return $select;
}
function getNumberOfPages () {
return $this->numberOfPages;
}
// AddParameter
// - select clause
// SELECT {table1}.{fieldname1}, [table1}.{fieldname2}, {table2}.{fieldname3}
// FROM {table1}
// WHERE
// INNER JOIN/OUTER JOIN (...)
//
// SELECT [ ALL | DISTINCT ]
// [ TOP n | TOP n PERCENT | TOP n WITH TIES | TOP n PERCENT WITH TIES ]
// { [ table_name. | table_alias. | view_name. ]
// { * | column_name [[ AS ] column_alias ] | expression [[ AS ] column_alias | IDENTITYCOL [[ AS ] column_alias
// ROWGUIDCOL [[ AS ] column_alias | column_alias = expression
// }
// }
// [ ..., n ]
// [ INTO new_table ]
// [ FROM
// {
// derived_table | AS table_alas ]
// [ ... JOIN ... ]
// [ WHERE ... ]
// [ GROUP BY ... ]
// [ HAVING ... ]
// [ UNION ... ]
// [ ORDER BY ... ]
// [ COMPUTE ... ]
// [ FOR BROWSE ... ]
// [ OPTION ... ]
}
?>