private function sanitizeQuery($query)
 {
     $query['explainable'] = true;
     $query['params'] = (array) $query['params'];
     foreach ($query['params'] as $j => &$param) {
         $key = is_int($j) ? $j + 1 : $j;
         if (isset($query['types'][$key])) {
             // Transform the param according to the type
             $type = $query['types'][$key];
             if (is_string($type)) {
                 $type = Type::getType($type);
             }
             if ($type instanceof Type) {
                 $query['types'][$key] = $type->getBindingType();
                 $param = $type->convertToDatabaseValue($param, $this->connection->getDatabasePlatform());
             }
         }
         list($param, $explainable) = $this->sanitizeParam($param);
         if (!$explainable) {
             $query['explainable'] = false;
         }
     }
     if ($query['explainable'] && ($explanation = $this->explain($query))) {
         $query['explain'] = $explanation;
     }
     return $query;
 }
 /**
  * Returns a merge/upsert (i.e. insert or update) SQL query when supported by the database.
  *
  * @return string|null The SQL string or null when not supported
  */
 protected function getMergeSql()
 {
     $platform = $this->connection->getDatabasePlatform();
     if ($platform instanceof MySqlPlatform) {
         return "INSERT INTO {$this->table} (id, data, time) VALUES (:id, :data, :time) " . "ON DUPLICATE KEY UPDATE data = VALUES(data), time = CASE WHEN time = :time THEN (VALUES(time) + INTERVAL 1 SECOND) ELSE VALUES(time) END";
     } elseif ($platform instanceof SqlitePlatform) {
         return "INSERT OR REPLACE INTO {$this->table} (id, data, time) VALUES (:id, :data, :time)";
     }
 }
Пример #3
0
 /**
  * Creates the "select" SQL string from the query parts.
  *
  * @return string
  */
 protected function getSQLForSelect()
 {
     extract($this->parts);
     $query = sprintf('SELECT %s FROM ' . $from, $select ? implode(', ', $select) : '*');
     foreach ($join as $j) {
         $query .= sprintf(' %s JOIN %s ON %s', strtoupper($j['type']), $j['table'], (string) $j['condition']);
     }
     if ($where) {
         $query .= ' WHERE ' . $where;
     }
     if ($group) {
         $query .= ' GROUP BY ' . implode(', ', $group);
     }
     if ($having) {
         $query .= ' HAVING ' . $having;
     }
     if ($order) {
         $query .= ' ORDER BY ' . implode(', ', $order);
     }
     return $limit === null && $offset === null ? $query : $this->connection->getDatabasePlatform()->modifyLimitQuery($query, $limit, $offset);
 }
Пример #4
0
 /**
  * Sets an option value.
  *
  * @param  string $name
  * @param  mixed $value
  * @param  boolean $autoload
  * @throws \InvalidArgumentException
  */
 public function set($name, $value, $autoload = null)
 {
     $name = trim($name);
     if (empty($name)) {
         throw new \InvalidArgumentException('Empty option name given.');
     }
     if (in_array($name, $this->protected)) {
         throw new \InvalidArgumentException(sprintf('"%s" is a protected option and may not be modified.', $name));
     }
     if (!$this->connection->isConnected()) {
         throw new \RuntimeException('Database is not connected.');
     }
     $old_value = $this->get($name);
     if ($value !== $old_value) {
         $this->options[$name] = $value;
         $data = ['name' => $name, 'value' => json_encode($value)];
         if ($autoload !== null) {
             $data['autoload'] = $autoload ? '1' : '0';
         }
         if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
             if ($autoload === null) {
                 $query = "INSERT INTO {$this->table} (name, value) VALUES (:name, :value) ON DUPLICATE KEY UPDATE value = :value";
             } else {
                 $query = "INSERT INTO {$this->table} (name, value, autoload) VALUES (:name, :value, :autoload) ON DUPLICATE KEY UPDATE value = :value, autoload = :autoload";
             }
             $this->connection->executeQuery($query, $data);
         } elseif (!$this->connection->update($this->table, $data, compact('name'))) {
             $this->connection->insert($this->table, $data);
         }
         $this->cache->delete($this->prefix . (isset($this->autoload[$name]) ? 'Autoload' : $name));
     }
     if (isset($this->ignore[$name])) {
         unset($this->ignore[$name]);
         $this->cache->save($this->prefix . 'Ignore', $this->ignore);
     }
 }
 /**
  * @return \Doctrine\DBAL\Platforms\AbstractPlatform
  */
 public function getDatabasePlatform()
 {
     return $this->connection->getDatabasePlatform();
 }