PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/HttpKernel/Profiler/SqliteProfilerStorage.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 124 lines | 89 code | 15 blank | 20 comment | 15 complexity | c3f5eb478567e518dfe7bfca3b745cf0 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Profiler;
  11. /**
  12. * SqliteProfilerStorage stores profiling information in a SQLite database.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class SqliteProfilerStorage extends PdoProfilerStorage
  17. {
  18. /**
  19. * @throws \RuntimeException When neither of SQLite3 or PDO_SQLite extension is enabled
  20. */
  21. protected function initDb()
  22. {
  23. if (null === $this->db || $this->db instanceof \SQLite3) {
  24. if (0 !== strpos($this->dsn, 'sqlite')) {
  25. throw new \RuntimeException('You are trying to use Sqlite with a wrong dsn. "'.$this->dsn.'"');
  26. }
  27. if (class_exists('SQLite3')) {
  28. $db = new \SQLite3(substr($this->dsn, 7, strlen($this->dsn)), \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
  29. if (method_exists($db, 'busyTimeout')) {
  30. // busyTimeout only exists for PHP >= 5.3.3
  31. $db->busyTimeout(1000);
  32. }
  33. } elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
  34. $db = new \PDO($this->dsn);
  35. } else {
  36. throw new \RuntimeException('You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.');
  37. }
  38. $db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
  39. $db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER)');
  40. $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
  41. $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
  42. $db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)');
  43. $db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)');
  44. $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)');
  45. $this->db = $db;
  46. }
  47. return $this->db;
  48. }
  49. protected function exec($db, $query, array $args = array())
  50. {
  51. if ($db instanceof \SQLite3) {
  52. $stmt = $this->prepareStatement($db, $query);
  53. foreach ($args as $arg => $val) {
  54. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  55. }
  56. $res = $stmt->execute();
  57. if (false === $res) {
  58. throw new \RuntimeException(sprintf('Error executing SQLite query "%s"', $query));
  59. }
  60. $res->finalize();
  61. } else {
  62. parent::exec($db, $query, $args);
  63. }
  64. }
  65. protected function fetch($db, $query, array $args = array())
  66. {
  67. $return = array();
  68. if ($db instanceof \SQLite3) {
  69. $stmt = $this->prepareStatement($db, $query, true);
  70. foreach ($args as $arg => $val) {
  71. $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
  72. }
  73. $res = $stmt->execute();
  74. while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
  75. $return[] = $row;
  76. }
  77. $res->finalize();
  78. $stmt->close();
  79. } else {
  80. $return = parent::fetch($db, $query, $args);
  81. }
  82. return $return;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. protected function buildCriteria($ip, $url, $limit)
  88. {
  89. $criteria = array();
  90. $args = array();
  91. if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
  92. $criteria[] = 'ip LIKE :ip';
  93. $args[':ip'] = '%'.$ip.'%';
  94. }
  95. if ($url) {
  96. $criteria[] = 'url LIKE :url ESCAPE "\"';
  97. $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
  98. }
  99. return array($criteria, $args);
  100. }
  101. protected function close($db)
  102. {
  103. if ($db instanceof \SQLite3) {
  104. $db->close();
  105. }
  106. }
  107. }