コード例 #1
0
 /**
  * Wraps an SQL query.
  *
  * @param array $aColumns
  * @param array $aTables
  * @param array $aLimitations
  * @param array $aGroupColumns
  * @param string $primaryKey
  * @param array $aLeftJoinedTables
  * @return integer  The number of rows affected by the query
  */
 function _select($aColumns, $aTables, $aLimitations, $aGroupColumns, $primaryKey, $aLeftJoinedTables = null)
 {
     $conf = $GLOBALS['_MAX']['CONF'];
     $oDbh = OA_DB::singleton();
     $columns = '';
     if (is_array($aColumns)) {
         foreach ($aColumns as $column => $alias) {
             $columns .= $columns == '' ? "SELECT {$column} AS {$alias}" : ",{$column} AS {$alias}";
         }
     } else {
         $columns = "SELECT {$aColumns}";
     }
     $prev_aliases = array();
     $tables = '';
     if (is_array($aTables)) {
         $x = 0;
         if (!is_null($aLeftJoinedTables)) {
             $aTables = array_diff($aTables, $aLeftJoinedTables);
         }
         $joinType = 'INNER';
         while (count($aTables) && $x < 50) {
             $x++;
             foreach ($aTables as $tableKey => $alias) {
                 $table = $tableKey;
                 //  check for prefix
                 if (!empty($conf['table']['prefix']) && strpos($table, $conf['table']['prefix']) != 0) {
                     $table = $conf['table']['prefix'] . $table;
                 }
                 $qTable = $oDbh->quoteIdentifier($table, true);
                 $joinLimitation = '';
                 if (count($prev_aliases)) {
                     if (is_array($aLimitations)) {
                         foreach ($aLimitations as $limitationKey => $limitation) {
                             if (preg_match("/({$alias}\\.[a-z0-9_]+ *= *(" . join('|', $prev_aliases) . ")\\..+|(" . join('|', $prev_aliases) . ")\\.[a-z0-9_]+ *= *{$alias}\\..+)/", $limitation)) {
                                 $joinLimitation = $limitation;
                                 unset($aLimitations[$limitationKey]);
                                 break;
                             }
                         }
                     }
                 } else {
                     $tables .= " FROM {$qTable} AS {$alias}";
                 }
                 if ($joinLimitation) {
                     $tables .= " {$joinType} JOIN {$qTable} AS {$alias} ON ({$joinLimitation})";
                 } elseif (count($prev_aliases)) {
                     continue;
                 }
                 $prev_aliases[] = $alias;
                 unset($aTables[$tableKey]);
             }
             if (!is_null($aLeftJoinedTables) && !count($aTables)) {
                 $aTables = $aLeftJoinedTables;
                 $aLeftJoinedTables = null;
                 $joinType = 'LEFT';
             }
         }
     } else {
         $tables = "FROM " . $oDbh->quoteIdentifier($aTables, true);
     }
     $where = '';
     if (is_array($aLimitations)) {
         foreach ($aLimitations as $limitation) {
             $where .= $where == '' ? " WHERE {$limitation}" : " AND {$limitation}";
         }
     } else {
         $where = " WHERE {$aLimitations}";
     }
     $group = '';
     if (is_array($aGroupColumns) && count($aGroupColumns) > 0) {
         $group = ' GROUP BY ' . implode(',', $aGroupColumns);
     }
     $query = $columns . $tables . $where . $group;
     //var_dump($query);
     return SqlBuilder::_query($query, $primaryKey);
 }