public function explain($query) { if (!$query['explainable']) { return false; } try { return $this->connection->executeQuery('EXPLAIN ' . $query['sql'], $query['params'], isset($query['types']) ? $query['types'] : [])->fetchAll(\PDO::FETCH_ASSOC); } catch (\Exception $e) { return false; } }
/** * Execute the query as select, update or delete. * * @param string $type * @return mixed */ protected function executeQuery($type = 'select') { switch ($type) { case 'update': $sql = $this->getSQLForUpdate(); break; case 'delete': $sql = $this->getSQLForDelete(); break; default: $sql = $this->getSQLForSelect(); break; } if ($type == 'select') { return $this->connection->executeQuery($sql, $this->params, $this->guessParamTypes($this->params)); } else { return $this->connection->executeUpdate($sql, $this->params, $this->guessParamTypes($this->params)); } }
/** * {@inheritdoc} */ public function write($id, $data) { try { $params = ['id' => $id, 'data' => base64_encode($data), 'time' => date('Y-m-d H:i:s')]; if (null !== ($sql = $this->getMergeSql())) { $this->connection->executeQuery($sql, $params); return true; } $this->connection->beginTransaction(); try { $this->connection->delete($this->table, ['id' => $id]); $this->connection->insert($this->table, $params); $this->connection->commit(); } catch (ConnectionException $e) { $this->connection->rollback(); throw $e; } } catch (\PDOException $e) { throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e); } return true; }
/** * 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); } }