Ejemplo n.º 1
0
 /**
  * Generische Schnittstelle für Datenbankabfragen. Anstatt vieler Parameter wird hier ein
  * Hash als Parameter verwendet, der mögliche Informationen aufnimmt.
  * Es sind die folgenden Parameter zulässig:
  * <pre>
  * - 'where' - the Where-Clause
  * - 'groupby' - the GroupBy-Clause
  * - 'orderby' - the OrderBy-Clause
  * - 'sqlonly' - returns the generated SQL statement. No database access.
  * - 'limit' - limits the number of result rows
  * - 'wrapperclass' - A wrapper for each result rows
  * - 'pidlist' - A list of page-IDs to search for records
  * - 'recursive' - the recursive level to search for records in pages
  * - 'enablefieldsoff' - deactivate enableFields check
  * - 'enablefieldsbe' - force enableFields check for BE (this usually ignores hidden records)
  * - 'enablefieldsfe' - force enableFields check for FE
  * - 'db' - external database: tx_rnbase_util_db_IDatabase
  * - 'ignorei18n' - do not translate record to fe language
  * - 'i18nolmode' - translation mode, possible value: 'hideNonTranslated'
  * </pre>
  * @param string $what requested columns
  * @param string $from either the name of on table or an array with index 0 the from clause
  *              and index 1 the requested tablename and optional index 2 a table alias to use.
  * @param array $arr the options array
  * @param boolean $debug = 0 Set to 1 to debug sql-String
  */
 public function doSelect($what, $from, $arr, $debug = 0)
 {
     $debug = $debug ? $debug : intval($arr['debug']) > 0;
     if ($debug) {
         $time = microtime(TRUE);
         $mem = memory_get_usage();
     }
     $tableName = $from;
     $fromClause = $from;
     if (is_array($from)) {
         $tableName = $from[1];
         $fromClause = $from[0];
         $tableAlias = isset($from[2]) && strlen(trim($from[2])) > 0 ? trim($from[2]) : FALSE;
     }
     $where = is_string($arr['where']) ? $arr['where'] : '1=1';
     $groupBy = is_string($arr['groupby']) ? $arr['groupby'] : '';
     if ($groupBy) {
         $groupBy .= is_string($arr['having']) > 0 ? ' HAVING ' . $arr['having'] : '';
     }
     $orderBy = is_string($arr['orderby']) ? $arr['orderby'] : '';
     $offset = intval($arr['offset']) > 0 ? intval($arr['offset']) : 0;
     $limit = intval($arr['limit']) > 0 ? intval($arr['limit']) : '';
     $pidList = is_string($arr['pidlist']) ? $arr['pidlist'] : '';
     $recursive = intval($arr['recursive']) ? intval($arr['recursive']) : 0;
     $i18n = is_string($arr['i18n']) > 0 ? $arr['i18n'] : '';
     $sqlOnly = intval($arr['sqlonly']) > 0 ? intval($arr['sqlonly']) : '';
     $union = is_string($arr['union']) > 0 ? $arr['union'] : '';
     // offset und limit kombinieren
     // bei gesetztem limit ist offset optional
     if ($limit) {
         $limit = $offset > 0 ? $offset . ',' . $limit : $limit;
     } elseif ($offset) {
         $limit = $limit > 0 ? $offset . ',' . $limit : $offset . ',1000';
     } else {
         $limit = '';
     }
     $where .= $this->handleEnableFieldsOptions($arr, $tableName, $tableAlias);
     // Das sollte wegfallen. Die OL werden weiter unten geladen
     if (strlen($i18n) > 0) {
         $i18n = implode(',', tx_rnbase_util_Strings::intExplode(',', $i18n));
         $where .= ' AND ' . ($tableAlias ? $tableAlias : $tableName) . '.sys_language_uid IN (' . $i18n . ')';
     }
     if (strlen($pidList) > 0) {
         $where .= ' AND ' . ($tableAlias ? $tableAlias : $tableName) . '.pid IN (' . tx_rnbase_util_DB::_getPidList($pidList, $recursive) . ')';
     }
     if (strlen($union) > 0) {
         $where .= ' UNION ' . $union;
     }
     $database = $this->getDatabaseConnection($arr);
     if ($debug || $sqlOnly) {
         $sql = $database->SELECTquery($what, $fromClause, $where, $groupBy, $orderBy, $limit);
         if ($sqlOnly) {
             return $sql;
         }
         if ($debug) {
             tx_rnbase_util_Debug::debug($sql, 'SQL');
             tx_rnbase_util_Debug::debug(array($what, $from, $arr));
         }
     }
     $storeLastBuiltQuery = $database->store_lastBuiltQuery;
     $database->store_lastBuiltQuery = TRUE;
     $res = $this->watchOutDB($database->exec_SELECTquery($what, $fromClause, $where, $groupBy, $orderBy, $limit), $database);
     $database->store_lastBuiltQuery = $storeLastBuiltQuery;
     // use classic arrays or the array object
     // should be ever an object, but for backward compatibility is ts an array by default
     $rows = empty($arr['array_object']) ? array() : new ArrayObject();
     if ($this->testResource($res)) {
         $wrapper = is_string($arr['wrapperclass']) ? trim($arr['wrapperclass']) : 0;
         $callback = isset($arr['callback']) ? $arr['callback'] : FALSE;
         while ($row = $database->sql_fetch_assoc($res)) {
             // Workspacesupport
             $this->lookupWorkspace($row, $tableName, $arr);
             $this->lookupLanguage($row, $tableName, $arr);
             if (!is_array($row)) {
                 continue;
             }
             $item = $wrapper ? tx_rnbase::makeInstance($wrapper, $row) : $row;
             if ($item instanceof Tx_Rnbase_Domain_Model_DynamicTableInterface || $item instanceof tx_rnbase_model_base) {
                 $item->setTablename($tableName);
             }
             if ($callback) {
                 call_user_func($callback, $item);
                 unset($item);
             } else {
                 if (is_array($rows)) {
                     $rows[] = $item;
                 } else {
                     $rows->append($item);
                 }
             }
         }
         $database->sql_free_result($res);
     }
     if ($debug) {
         tx_rnbase_util_Debug::debug(array('Rows retrieved ' => count($rows), 'Time ' => microtime(TRUE) - $time, 'Memory consumed ' => memory_get_usage() - $mem), 'SQL statistics');
     }
     return $rows;
 }