PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/database/postgresql/statement.php

https://github.com/cbandy/real-database
PHP | 123 lines | 50 code | 9 blank | 64 comment | 0 complexity | bf5df027bb498ddfca578272f7602533 MD5 | raw file
  1. <?php
  2. /**
  3. * Prepared statement for [Database_PostgreSQL]. Parameters are positional
  4. * literals that map to named positions: $1, $2, etc.
  5. *
  6. * @package RealDatabase
  7. * @subpackage PostgreSQL
  8. * @category Prepared Statements
  9. *
  10. * @author Chris Bandy
  11. * @copyright (c) 2011 Chris Bandy
  12. * @license http://www.opensource.org/licenses/isc-license.txt
  13. *
  14. * @link http://www.postgresql.org/docs/current/static/libpq-exec.html#LIBPQ-PQPREPARE
  15. *
  16. * @see Database_PostgreSQL::prepare_statement()
  17. */
  18. class Database_PostgreSQL_Statement extends Database_Statement
  19. {
  20. /**
  21. * @var Database_PostgreSQL
  22. */
  23. protected $_db;
  24. /**
  25. * @var string Statement name
  26. */
  27. protected $_name;
  28. /**
  29. * @var string Original SQL of this statement
  30. */
  31. public $statement;
  32. /**
  33. * @param Database_PostgreSQL $db
  34. * @param string $name Statement name
  35. * @param array $parameters Unquoted literal parameters
  36. */
  37. public function __construct($db, $name, $parameters = array())
  38. {
  39. $this->_db = $db;
  40. $this->_name = $name;
  41. $this->_parameters = $parameters;
  42. $this->_statement =& $this->statement;
  43. }
  44. public function __toString()
  45. {
  46. return $this->_name;
  47. }
  48. /**
  49. * Deallocate this this prepared statement
  50. *
  51. * If you do not explicitly deallocate a prepared statement, it is
  52. * deallocated when the session ends.
  53. *
  54. * @throws Database_Exception
  55. * @return void
  56. */
  57. public function deallocate()
  58. {
  59. $this->_db->execute_command(
  60. 'DEALLOCATE '.$this->_db->quote_identifier($this->_name)
  61. );
  62. }
  63. /**
  64. * Execute the statement, returning the number of rows affected.
  65. *
  66. * @throws Database_Exception
  67. * @return integer Number of affected rows
  68. */
  69. public function execute_command()
  70. {
  71. return $this->_db->execute_prepared_command(
  72. $this->_name,
  73. $this->_parameters
  74. );
  75. }
  76. /**
  77. * Execute the statement, returning the value of a column from the first
  78. * row.
  79. *
  80. * @throws Database_Exception
  81. * @param mixed $identity Converted to SQL_Column
  82. * @param string|boolean $as_object Class as which to return row results, TRUE for stdClass or FALSE for associative array
  83. * @param array $arguments Arguments to pass to the row class constructor
  84. * @return array List including number of affected rows and a value from the first row
  85. */
  86. public function execute_insert($identity, $as_object = FALSE, $arguments = array())
  87. {
  88. return $this->_db->execute_prepared_insert(
  89. $this->_name,
  90. $identity,
  91. $this->_parameters,
  92. $as_object,
  93. $arguments
  94. );
  95. }
  96. /**
  97. * Execute the statement, returning the result set or NULL when the
  98. * statement is not a query (e.g., a DELETE statement).
  99. *
  100. * @throws Database_Exception
  101. * @param string|boolean $as_object Class as which to return row results, TRUE for stdClass or FALSE for associative array
  102. * @param array $arguments Arguments to pass to the row class constructor
  103. * @return Database_PostgreSQL_Result Result set or NULL
  104. */
  105. public function execute_query($as_object = FALSE, $arguments = array())
  106. {
  107. return $this->_db->execute_prepared_query(
  108. $this->_name,
  109. $this->_parameters,
  110. $as_object,
  111. $arguments
  112. );
  113. }
  114. }