context.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Defines a behaviour scope for the database
  4. * driver that lasts until the object is destroyed.
  5. */
  6. class DatabaseContext {
  7. /**
  8. * Conection that this context is applied to.
  9. *
  10. * @var \DatabaseConnection_sqlsrv
  11. */
  12. var $connection;
  13. /**
  14. * Bypass SQL Server specific query preprocessing.
  15. *
  16. * @var bool
  17. */
  18. var $state_bypass = NULL;
  19. /**
  20. * Use DIRECT_QUERY feature of the driver so that statements are not prepared.
  21. *
  22. * @var bool
  23. */
  24. var $state_direct = NULL;
  25. /**
  26. * Prepared statement caching enabled for this connection. Incompatible
  27. * with direct queries.
  28. *
  29. * @var bool
  30. */
  31. var $statement_caching = NULL;
  32. /**
  33. * Define the behaviour of the database driver during the scope of the
  34. * life of this instance.
  35. *
  36. * @param DatabaseConnection_sqlsrv $connection
  37. *
  38. * Instance of the connection to be configured. Leave null to use the
  39. * current default connection.
  40. *
  41. * @param mixed $bypass_queries
  42. *
  43. * Do not preprocess the query before execution.
  44. *
  45. * @param mixed $direct_query
  46. *
  47. * Prepare statements with SQLSRV_ATTR_DIRECT_QUERY = TRUE.
  48. *
  49. * @param mixed $statement_caching
  50. *
  51. * Enable prepared statement caching. Cached statements are reused even
  52. * after the context has expired.
  53. *
  54. */
  55. public function __construct(\DatabaseConnection_sqlsrv $connection = NULL,
  56. $bypass_queries = NULL,
  57. $direct_query = NULL,
  58. $statement_caching = NULL) {
  59. if ($connection == NULL) {
  60. $connection = Database::getConnection();
  61. }
  62. $this->connection = $connection;
  63. $this->state_bypass = $this->connection->bypassQueryPreprocess;
  64. $this->state_direct = $this->connection->directQuery;
  65. $this->statement_caching = $this->connection->statementCaching;
  66. if ($bypass_queries !== NULL) {
  67. $this->connection->bypassQueryPreprocess = $bypass_queries;
  68. }
  69. if ($direct_query !== NULL) {
  70. $this->connection->directQuery = $direct_query;
  71. }
  72. if ($statement_caching !== NULL) {
  73. $this->connection->statementCaching = $statement_caching;
  74. }
  75. }
  76. public function __destruct() {
  77. // Restore previous driver configuration.
  78. $this->connection->bypassQueryPreprocess = $this->state_bypass;
  79. $this->connection->directQuery = $this->state_direct;
  80. $this->connection->statementCaching = $this->statement_caching;
  81. }
  82. }