/**
  * Returns the values associated with the provided cache tag and key.
  *
  * @param string $cacheTag cache tag to use for lookup
  * @param string $key      key to use for lookup
  *
  * @return mixed
  */
 private function getCached($cacheTag, $key)
 {
     $sql = $this->adapter->getSQL();
     if (strpos($sql, '-- keep-cache') !== strlen($sql) - 13) {
         // If SQL has been taken place outside of this method then something else then
         // a select query might have happened! (or instruct to keep cache)
         $this->cache = array();
     } else {
         if (isset($this->cache[$cacheTag][$key])) {
             return $this->cache[$cacheTag][$key];
         }
     }
     return NULL;
 }
Esempio n. 2
0
 /**
  * Implementation of the onEvent() method for Observer interface.
  * If a query gets executed this method gets invoked because the
  * adapter will send a signal to the attached logger.
  *
  * @param  string                    $eventName ID of the event (name)
  * @param  RedBean_Adapter_DBAdapter $adapter   adapter that sends the signal
  *
  * @return void
  */
 public function onEvent($eventName, $adapter)
 {
     if ($eventName == 'sql_exec') {
         $this->logs[] = $adapter->getSQL();
     }
 }
Esempio n. 3
0
 /**
  * @see RedBean_QueryWriter::selectRecord
  */
 public function selectRecord($type, $conditions, $addSql = null, $delete = null, $inverse = false, $all = false)
 {
     if (!is_array($conditions)) {
         throw new Exception('Conditions must be an array');
     }
     if (!$delete && $this->flagUseCache) {
         $key = serialize(array($type, $conditions, $addSql, $inverse, $all));
         $sql = $this->adapter->getSQL();
         if (strpos($sql, '-- keep-cache') !== strlen($sql) - 13) {
             //If SQL has been taken place outside of this method then something else then
             //a select query might have happened! (or instruct to keep cache)
             $this->cache = array();
         } else {
             if (isset($this->cache[$key])) {
                 return $this->cache[$key];
             }
         }
     }
     $table = $this->esc($type);
     $sqlConditions = array();
     $bindings = array();
     foreach ($conditions as $column => $values) {
         if (!count($values)) {
             continue;
         }
         $sql = $this->esc($column);
         $sql .= ' ' . ($inverse ? ' NOT ' : '') . ' IN ( ';
         //If its safe to not use bindings please do... (fixes SQLite PDO issue limit 256 bindings)
         if (is_array($conditions) && count($conditions) === 1 && isset($conditions['id']) && is_array($values) && preg_match('/^\\d+$/', implode('', $values))) {
             $sql .= implode(',', $values) . ') ';
             $sqlConditions[] = $sql;
         } else {
             $sql .= implode(',', array_fill(0, count($values), '?')) . ') ';
             $sqlConditions[] = $sql;
             if (!is_array($values)) {
                 $values = array($values);
             }
             foreach ($values as $k => $v) {
                 $values[$k] = strval($v);
             }
             $bindings = array_merge($bindings, $values);
         }
     }
     if (is_array($addSql)) {
         if (count($addSql) > 1) {
             $bindings = array_merge($bindings, $addSql[1]);
         } else {
             $bindings = array();
         }
         $addSql = $addSql[0];
     }
     $sql = '';
     if (is_array($sqlConditions) && count($sqlConditions) > 0) {
         $sql = implode(' AND ', $sqlConditions);
         $sql = " WHERE ( {$sql} ) ";
         if ($addSql) {
             $sql .= ($all ? '' : ' AND ') . " {$addSql} ";
         }
     } elseif ($addSql) {
         if ($all) {
             $sql = " {$addSql} ";
         } else {
             $sql = " WHERE {$addSql} ";
         }
     }
     $sql = ($delete ? 'DELETE FROM ' : 'SELECT * FROM ') . $table . $sql;
     $rows = $this->adapter->get($sql . ($delete ? '' : ' -- keep-cache'), $bindings);
     if (!$delete && $this->flagUseCache) {
         $this->cache[$key] = $rows;
     }
     return $rows;
 }