DebugPDOStatement.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * PDOStatement that provides some enhanced functionality needed by Propel.
  11. *
  12. * Simply adds the ability to count the number of queries executed and log the queries/method calls.
  13. *
  14. * @author Oliver Schonrock <oliver@realtsp.com>
  15. * @author Jarno Rantanen <jarno.rantanen@tkk.fi>
  16. * @since 2007-07-12
  17. * @package propel.runtime.connection
  18. */
  19. class DebugPDOStatement extends PDOStatement
  20. {
  21. /**
  22. * The PDO connection from which this instance was created.
  23. *
  24. * @var PropelPDO
  25. */
  26. protected $pdo;
  27. /**
  28. * Hashmap for resolving the PDO::PARAM_* class constants to their human-readable names.
  29. *
  30. * This is only used in logging the binding of variables.
  31. *
  32. * @see self::bindValue()
  33. * @var array
  34. */
  35. protected static $typeMap = array(
  36. PDO::PARAM_BOOL => "PDO::PARAM_BOOL",
  37. PDO::PARAM_INT => "PDO::PARAM_INT",
  38. PDO::PARAM_STR => "PDO::PARAM_STR",
  39. PDO::PARAM_LOB => "PDO::PARAM_LOB",
  40. PDO::PARAM_NULL => "PDO::PARAM_NULL",
  41. );
  42. /**
  43. * @var array The values that have been bound
  44. */
  45. protected $boundValues = array();
  46. /**
  47. * Construct a new statement class with reference to main DebugPDO object from
  48. * which this instance was created.
  49. *
  50. * @param DebugPDO $pdo Reference to the parent PDO instance.
  51. */
  52. protected function __construct(PropelPDO $pdo)
  53. {
  54. $this->pdo = $pdo;
  55. }
  56. public function getExecutedQueryString()
  57. {
  58. $sql = $this->queryString;
  59. $matches = array();
  60. if (preg_match_all('/(:p[0-9]+\b)/', $sql, $matches)) {
  61. $size = count($matches[1]);
  62. for ($i = $size-1; $i >= 0; $i--) {
  63. $pos = $matches[1][$i];
  64. $sql = str_replace($pos, $this->boundValues[$pos], $sql);
  65. }
  66. }
  67. return $sql;
  68. }
  69. /**
  70. * Executes a prepared statement. Returns a boolean value indicating success.
  71. *
  72. * Overridden for query counting and logging.
  73. *
  74. * @return bool
  75. */
  76. public function execute($input_parameters = null)
  77. {
  78. $debug = $this->pdo->getDebugSnapshot();
  79. $return = parent::execute($input_parameters);
  80. $sql = $this->getExecutedQueryString();
  81. $this->pdo->log($sql, null, __METHOD__, $debug);
  82. $this->pdo->setLastExecutedQuery($sql);
  83. $this->pdo->incrementQueryCount();
  84. return $return;
  85. }
  86. /**
  87. * Binds a value to a corresponding named or question mark placeholder in the SQL statement
  88. * that was use to prepare the statement. Returns a boolean value indicating success.
  89. *
  90. * @param int $pos Parameter identifier (for determining what to replace in the query).
  91. * @param mixed $value The value to bind to the parameter.
  92. * @param int $type Explicit data type for the parameter using the PDO::PARAM_* constants. Defaults to PDO::PARAM_STR.
  93. * @return boolean
  94. */
  95. public function bindValue($pos, $value, $type = PDO::PARAM_STR)
  96. {
  97. $debug = $this->pdo->getDebugSnapshot();
  98. $typestr = isset(self::$typeMap[$type]) ? self::$typeMap[$type] : '(default)';
  99. $return = parent::bindValue($pos, $value, $type);
  100. $valuestr = $type == PDO::PARAM_LOB ? '[LOB value]' : var_export($value, true);
  101. $msg = "Binding $valuestr at position $pos w/ PDO type $typestr";
  102. $this->boundValues[$pos] = $valuestr;
  103. $this->pdo->log($msg, null, __METHOD__, $debug);
  104. return $return;
  105. }
  106. }