4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Profiler;
14 @trigger_error('The '.__NAMESPACE__.'\SqliteProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
17 * SqliteProfilerStorage stores profiling information in a SQLite database.
19 * @author Fabien Potencier <fabien@symfony.com>
21 * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
22 * Use {@link FileProfilerStorage} instead.
24 class SqliteProfilerStorage extends PdoProfilerStorage
27 * @throws \RuntimeException When neither of SQLite3 or PDO_SQLite extension is enabled
29 protected function initDb()
31 if (null === $this->db || $this->db instanceof \SQLite3) {
32 if (0 !== strpos($this->dsn, 'sqlite')) {
33 throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Sqlite with an invalid dsn "%s". The expected format is "sqlite:/path/to/the/db/file".', $this->dsn));
35 if (class_exists('SQLite3')) {
36 $db = new \SQLite3(substr($this->dsn, 7, strlen($this->dsn)), \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
37 if (method_exists($db, 'busyTimeout')) {
38 // busyTimeout only exists for PHP >= 5.3.3
39 $db->busyTimeout(1000);
41 } elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
42 $db = new \PDO($this->dsn);
44 throw new \RuntimeException('You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.');
47 $db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
48 $db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER, status_code INTEGER)');
49 $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
50 $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
51 $db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');
52 $db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)');
53 $db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)');
54 $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)');
62 protected function exec($db, $query, array $args = array())
64 if ($db instanceof \SQLite3) {
65 $stmt = $this->prepareStatement($db, $query);
66 foreach ($args as $arg => $val) {
67 $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
70 $res = $stmt->execute();
72 throw new \RuntimeException(sprintf('Error executing SQLite query "%s"', $query));
76 parent::exec($db, $query, $args);
80 protected function fetch($db, $query, array $args = array())
84 if ($db instanceof \SQLite3) {
85 $stmt = $this->prepareStatement($db, $query);
86 foreach ($args as $arg => $val) {
87 $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
89 $res = $stmt->execute();
90 while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
96 $return = parent::fetch($db, $query, $args);
105 protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
110 if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
111 $criteria[] = 'ip LIKE :ip';
112 $args[':ip'] = '%'.$ip.'%';
116 $criteria[] = 'url LIKE :url ESCAPE "\"';
117 $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
121 $criteria[] = 'method = :method';
122 $args[':method'] = $method;
125 if (!empty($start)) {
126 $criteria[] = 'time >= :start';
127 $args[':start'] = $start;
131 $criteria[] = 'time <= :end';
132 $args[':end'] = $end;
135 return array($criteria, $args);
138 protected function close($db)
140 if ($db instanceof \SQLite3) {