/**
  * Quotes the field (and table) names within an order by clause with the quote
  * character suitable for the DB being used
  *
  * @param	string		An order by clause that can by parsed by parseFieldList
  * @return	string		Usable order by clause with quoted field/table names
  */
 protected function quoteOrderBy($orderBy)
 {
     if ($orderBy === '') {
         return '';
     }
     if ($this->runningNative()) {
         return $orderBy;
     }
     $orderBy = $this->SQLparser->parseFieldList($orderBy);
     $orderBy = $this->_quoteOrderBy($orderBy);
     return $this->SQLparser->compileFieldList($orderBy);
 }
 /**
  * Remaps table/field names in a SELECT query's parts
  * Notice: All arguments are passed by reference!
  *
  * @param	string		List of fields to select from the table. This is what comes right after "SELECT ...". Required value.
  * @param	string		Table(s) from which to select. This is what comes right after "FROM ...". Require value.
  * @param	string		Where clause. This is what comes right after "WHERE ...". Can be blank.
  * @param	string		Group by field(s)
  * @param	string		Order by field(s)
  * @return	void
  * @see exec_SELECTquery()
  */
 protected function map_remapSELECTQueryParts(&$select_fields, &$from_table, &$where_clause, &$groupBy, &$orderBy)
 {
     // Backup current mapping as it may be altered if aliases on mapped tables are found
     $backupMapping = $this->mapping;
     // Tables:
     $tables = $this->SQLparser->parseFromTables($from_table);
     $defaultTable = $tables[0]['table'];
     // Prepare mapping for aliased tables. This will copy the definition of the original table name.
     // The alias is prefixed with a database-incompatible character to prevent naming clash with real table name
     // Further access to $this->mapping should be made through $this->getMappingKey() method
     foreach ($tables as $k => $v) {
         if ($v['as'] && is_array($this->mapping[$v['table']]['mapFieldNames'])) {
             $mappingKey = $this->getFreeMappingKey($v['as']);
             $this->mapping[$mappingKey]['mapFieldNames'] =& $this->mapping[$v['table']]['mapFieldNames'];
         }
         if (is_array($v['JOIN'])) {
             foreach ($v['JOIN'] as $joinCnt => $join) {
                 if ($join['as'] && is_array($this->mapping[$join['withTable']]['mapFieldNames'])) {
                     $mappingKey = $this->getFreeMappingKey($join['as']);
                     $this->mapping[$mappingKey]['mapFieldNames'] =& $this->mapping[$join['withTable']]['mapFieldNames'];
                 }
             }
         }
     }
     foreach ($tables as $k => $v) {
         $tableKey = $this->getMappingKey($v['table']);
         if ($this->mapping[$tableKey]['mapTableName']) {
             $tables[$k]['table'] = $this->mapping[$tableKey]['mapTableName'];
         }
         // Mapping JOINS
         if (is_array($v['JOIN'])) {
             foreach ($v['JOIN'] as $joinCnt => $join) {
                 // Mapping withTable of the JOIN
                 $withTableKey = $this->getMappingKey($join['withTable']);
                 if ($this->mapping[$withTableKey]['mapTableName']) {
                     $tables[$k]['JOIN'][$joinCnt]['withTable'] = $this->mapping[$withTableKey]['mapTableName'];
                 }
                 $onPartsArray = array();
                 // Mapping ON parts of the JOIN
                 if (is_array($tables[$k]['JOIN'][$joinCnt]['ON'])) {
                     foreach ($tables[$k]['JOIN'][$joinCnt]['ON'] as &$condition) {
                         // Left side of the comparator
                         $leftTableKey = $this->getMappingKey($condition['left']['table']);
                         if (isset($this->mapping[$leftTableKey]['mapFieldNames'][$condition['left']['field']])) {
                             $condition['left']['field'] = $this->mapping[$leftTableKey]['mapFieldNames'][$condition['left']['field']];
                         }
                         if (isset($this->mapping[$leftTableKey]['mapTableName'])) {
                             $condition['left']['table'] = $this->mapping[$leftTableKey]['mapTableName'];
                         }
                         // Right side of the comparator
                         $rightTableKey = $this->getMappingKey($condition['right']['table']);
                         if (isset($this->mapping[$rightTableKey]['mapFieldNames'][$condition['right']['field']])) {
                             $condition['right']['field'] = $this->mapping[$rightTableKey]['mapFieldNames'][$condition['right']['field']];
                         }
                         if (isset($this->mapping[$rightTableKey]['mapTableName'])) {
                             $condition['right']['table'] = $this->mapping[$rightTableKey]['mapTableName'];
                         }
                     }
                 }
             }
         }
     }
     $from_table = $this->SQLparser->compileFromTables($tables);
     // Where clause:
     $whereParts = $this->SQLparser->parseWhereClause($where_clause);
     $this->map_sqlParts($whereParts, $defaultTable);
     $where_clause = $this->SQLparser->compileWhereClause($whereParts, FALSE);
     // Select fields:
     $expFields = $this->SQLparser->parseFieldList($select_fields);
     $this->map_sqlParts($expFields, $defaultTable);
     $select_fields = $this->SQLparser->compileFieldList($expFields, FALSE, FALSE);
     // Group By fields
     $expFields = $this->SQLparser->parseFieldList($groupBy);
     $this->map_sqlParts($expFields, $defaultTable);
     $groupBy = $this->SQLparser->compileFieldList($expFields);
     // Order By fields
     $expFields = $this->SQLparser->parseFieldList($orderBy);
     $this->map_sqlParts($expFields, $defaultTable);
     $orderBy = $this->SQLparser->compileFieldList($expFields);
     // Restore the original mapping
     $this->mapping = $backupMapping;
 }