8000 [ProfilerBundle] Allows the BundleStorage to use PDO_SQLITE as well a… · CodingFabian/symfony@dad7552 · GitHub
[go: up one dir, main page]

Skip to content

Commit dad7552

Browse files
Seldaekfabpot
authored andcommitted
[ProfilerBundle] Allows the BundleStorage to use PDO_SQLITE as well as SQLite3 and fixes a bug in purge()
Signed-off-by: Jordi Boggiano <j.boggiano@seld.be>
1 parent e182853 commit dad7552

File tree

1 file changed

+77
-14
lines changed

1 file changed

+77
-14
lines changed

src/Symfony/Framework/ProfilerBundle/ProfilerStorage.php

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,99 @@ public function getToken()
5757

5858
protected function read()
5959
{
60-
$db = $this->initDb(SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READ);
61-
$data = $db->querySingle(sprintf("SELECT data FROM data WHERE token = '%s' LIMIT 1 ORDER BY created_at DESC", $db->escapeString($this->token)));
62-
63-
$this->data = unserialize(pack('H*', $data));
64-
65-
$db->close();
60+
$db = $this->initDb();
61+
$args = array(':token' => $this->token);
62+
$data = $this->exec($db, "SELECT data FROM data WHERE token = :token ORDER BY created_at DESC LIMIT 1", $args);
63+
$this->close($db);
64+
if (isset($data[0]['data']))
65+
{
66+
return unserialize(pack('H*', $data[0]['data']));
67+
}
6668
}
6769

6870
public function write($data)
6971
{
7072
$unpack = unpack('H*', serialize($data));
7173
$data = $unpack[1];
7274

73-
$db = $this->initDb(SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
74-
$db->exec(sprintf("INSERT INTO data (token, data, created_at) VALUES ('%s', '%s', %s)", $db->escapeString($this->token), $db->escapeString($data), time()));
75-
$db->close();
75+
$db = $this->initDb(false);
76+
$args = array(
77+
':token' => $this->token,
78+
':data' => (string) $data,
79+
':time' => time()
80+
);
81+
$this->exec($db, "INSERT INTO data (token, data, created_at) VALUES (:token, :data, :time)", $args);
82+
$this->close($db);
7683
}
7784

78-
protected function initDb($flags)
85+
protected function initDb($readOnly = true)
7986
{
80-
$db = new \SQLite3($this->store, $flags);
81-
$db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, created_at TIMESTAMP)');
87+
if (class_exists('\SQLite3'))
88+
{
89+
$flags = $readOnly ? \SQLITE3_OPEN_READONLY : \SQLITE3_OPEN_READWRITE;
90+
$flags |= \SQLITE3_OPEN_CREATE;
91+
$db = new \SQLite3($this->store, $flags);
92+
}
93+
elseif (class_exists('\PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true))
94+
{
95+
$db = new \PDO('sqlite:'.$this->store);
96+
}
97+
else
98+
{
99+
throw new \RuntimeException('You need to enable either the SQLite or PDO_SQLite extension for the ProfilerBundle to run properly.');
100+
}
101+
102+
$db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, created_at INTEGER)');
82103
$db->exec('CREATE INDEX IF NOT EXISTS data_data ON data (created_at)');
83104

84105
return $db;
85106
}
86107

108+
protected function exec($db, $query, array $args = array())
109+
{
110+
$return = array();
111+
$stmt = $db->prepare($query);
112+
113+
if ($db instanceof \SQLite3)
114+
{
115+
foreach ($args as $arg => $val)
116+
{
117+
$stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
118+
}
119+
$res = $stmt->execute();
120+
while ($row = $res->fetchArray(\SQLITE3_ASSOC))
121+
{
122+
$return[] = $row;
123+
}
124+
$res->finalize();
125+
$stmt->close();
126+
}
127+
else
128+
{
129+
foreach ($args as $arg => $val)
130+
{
131+
$stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
132+
}
133+
$stmt->execute();
134+
$return = $stmt->fetchAll(\PDO::FETCH_ASSOC);
135+
}
136+
137+
return $return;
138+
}
139+
140+
protected function close($db)
141+
{
142+
if ($db instanceof \SQLite3)
143+
{
144+
$db->close();
145+
}
146+
}
147+
87148
public function purge($lifetime)
88149
{
89-
$db = $this->initDb(SQLITE3_OPEN_CREATE | SQLITE3_OPEN_READWRITE);
90-
$db->exec(sprintf("DELETE FROM data WHERE strftime('%%s', 'now') - created_at > %d", $lifetime));
150+
$db = $this->initDb(false);
151+
$args = array(':time' => time() - (int) $lifetime);
152+
$this->exec($db, "DELETE FROM data WHERE created_at &l 41DD t; :time", $args);
153+
$this->close($db);
91154
}
92155
}

0 commit comments

Comments
 (0)
0