TYPO3 CMS  TYPO3_6-2
PdoBackend.php
Go to the documentation of this file.
1 <?php
3 
24 
28  protected $dataSourceName;
29 
33  protected $username;
34 
38  protected $password;
39 
43  protected $databaseHandle;
44 
48  protected $pdoDriver;
49 
57  public function setDataSourceName($DSN) {
58  $this->dataSourceName = $DSN;
59  }
60 
68  public function setUsername($username) {
69  $this->username = $username;
70  }
71 
79  public function setPassword($password) {
80  $this->password = $password;
81  }
82 
88  public function initializeObject() {
89  $this->connect();
90  }
91 
105  public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
106  if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
107  throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1259515600);
108  }
109  if (!is_string($data)) {
110  throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1259515601);
111  }
112  $this->remove($entryIdentifier);
113  $lifetime = $lifetime === NULL ? $this->defaultLifetime : $lifetime;
114  $statementHandle = $this->databaseHandle->prepare('INSERT INTO "cache" ("identifier", "context", "cache", "created", "lifetime", "content") VALUES (?, ?, ?, ?, ?, ?)');
115  $result = $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier, $GLOBALS['EXEC_TIME'], $lifetime, $data));
116  if ($result === FALSE) {
117  throw new \TYPO3\CMS\Core\Cache\Exception('The cache entry "' . $entryIdentifier . '" could not be written.', 1259530791);
118  }
119  $statementHandle = $this->databaseHandle->prepare('INSERT INTO "tags" ("identifier", "context", "cache", "tag") VALUES (?, ?, ?, ?)');
120  foreach ($tags as $tag) {
121  $result = $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier, $tag));
122  if ($result === FALSE) {
123  throw new \TYPO3\CMS\Core\Cache\Exception('The tag "' . $tag . ' for cache entry "' . $entryIdentifier . '" could not be written.', 1259530751);
124  }
125  }
126  }
127 
135  public function get($entryIdentifier) {
136  $statementHandle = $this->databaseHandle->prepare('SELECT "content" FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?' . $this->getNotExpiredStatement());
137  $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
138  return $statementHandle->fetchColumn();
139  }
140 
148  public function has($entryIdentifier) {
149  $statementHandle = $this->databaseHandle->prepare('SELECT COUNT("identifier") FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?' . $this->getNotExpiredStatement());
150  $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
151  return $statementHandle->fetchColumn() > 0;
152  }
153 
163  public function remove($entryIdentifier) {
164  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "tags" WHERE "identifier"=? AND "context"=? AND "cache"=?');
165  $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
166  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?');
167  $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
168  return $statementHandle->rowCount() > 0;
169  }
170 
177  public function flush() {
178  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "tags" WHERE "context"=? AND "cache"=?');
179  $statementHandle->execute(array($this->context, $this->cacheIdentifier));
180  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "cache" WHERE "context"=? AND "cache"=?');
181  $statementHandle->execute(array($this->context, $this->cacheIdentifier));
182  }
183 
191  public function flushByTag($tag) {
192  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "cache" WHERE "context"=? AND "cache"=? AND "identifier" IN (SELECT "identifier" FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?)');
193  $statementHandle->execute(array($this->context, $this->cacheIdentifier, $this->context, $this->cacheIdentifier, $tag));
194  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?');
195  $statementHandle->execute(array($this->context, $this->cacheIdentifier, $tag));
196  }
197 
206  public function findIdentifiersByTag($tag) {
207  $statementHandle = $this->databaseHandle->prepare('SELECT "identifier" FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?');
208  $statementHandle->execute(array($this->context, $this->cacheIdentifier, $tag));
209  return $statementHandle->fetchAll(\PDO::FETCH_COLUMN);
210  }
211 
218  public function collectGarbage() {
219  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "tags" WHERE "context"=? AND "cache"=? AND "identifier" IN ' . '(SELECT "identifier" FROM "cache" WHERE "context"=? AND "cache"=? AND "lifetime" > 0 AND "created" + "lifetime" < ' . $GLOBALS['EXEC_TIME'] . ')');
220  $statementHandle->execute(array($this->context, $this->cacheIdentifier, $this->context, $this->cacheIdentifier));
221  $statementHandle = $this->databaseHandle->prepare('DELETE FROM "cache" WHERE "context"=? AND "cache"=? AND "lifetime" > 0 AND "created" + "lifetime" < ' . $GLOBALS['EXEC_TIME']);
222  $statementHandle->execute(array($this->context, $this->cacheIdentifier));
223  }
224 
230  protected function getNotExpiredStatement() {
231  return ' AND ("lifetime" = 0 OR "created" + "lifetime" >= ' . $GLOBALS['EXEC_TIME'] . ')';
232  }
233 
240  protected function connect() {
241  try {
242  $splitdsn = explode(':', $this->dataSourceName, 2);
243  $this->pdoDriver = $splitdsn[0];
244  if ($this->pdoDriver === 'sqlite' && !file_exists($splitdsn[1])) {
245  $this->databaseHandle = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('PDO', $this->dataSourceName, $this->username, $this->password);
246  $this->createCacheTables();
247  } else {
248  $this->databaseHandle = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('PDO', $this->dataSourceName, $this->username, $this->password);
249  }
250  $this->databaseHandle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
251  if (substr($this->pdoDriver, 0, 5) === 'mysql') {
252  $this->databaseHandle->exec('SET SESSION sql_mode=\'ANSI\';');
253  }
254  } catch (\PDOException $e) {
255  throw new \RuntimeException('Could not connect to cache table with DSN "' . $this->dataSourceName . '". PDO error: ' . $e->getMessage(), 1334736164);
256  }
257  }
258 
265  protected function createCacheTables() {
266  try {
268  $this->databaseHandle,
269  $this->pdoDriver,
270  \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') .
271  'Resources/Private/Sql/Cache/Backend/PdoBackendCacheAndTags.sql'
272  );
273  } catch (\PDOException $e) {
274  throw new \RuntimeException('Could not create cache tables with DSN "' . $this->dataSourceName . '". PDO error: ' . $e->getMessage(), 1259576985);
275  }
276  }
277 
278 }
static importSql(\PDO $databaseHandle, $pdoDriver, $pathAndFilename)
Definition: PdoHelper.php:37
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]