/core/lib/Hunter/Database/Query.php

https://github.com/hunteryun/HunterPHP · PHP · 97 lines · 32 code · 15 blank · 50 comment · 0 complexity · 0a8e70b6d24204a4d814f8901f4fb87c MD5 · raw file

  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Query
  6. */
  7. namespace Hunter\Core\Database;
  8. class Query {
  9. /**
  10. * 库配置的目标
  11. *
  12. * @var string
  13. */
  14. protected $target;
  15. /**
  16. * 库连接
  17. *
  18. * @var Hunter\Core\Connection
  19. */
  20. protected $connection;
  21. /**
  22. * Query设置
  23. *
  24. * @var array
  25. */
  26. protected $queryOptions;
  27. /**
  28. * 该条query的唯一码
  29. *
  30. * @var string
  31. */
  32. protected $uniqueIdentifier;
  33. /**
  34. * 占位符计数器
  35. *
  36. * @var integer
  37. */
  38. protected $nextPlaceholder = 0;
  39. /**
  40. * 析构函数
  41. *
  42. */
  43. public function __construct(Connection $connection, $options) {
  44. $this->uniqueIdentifier = uniqid('', true);
  45. $this->connection = $connection;
  46. $this->target = $this->connection->getTarget();
  47. $this->queryOptions = $options;
  48. }
  49. /**
  50. * 魔术方法
  51. *
  52. * 被serialize的时候,断开连接
  53. */
  54. public function __sleep() {
  55. $keys = get_object_vars($this);
  56. unset($keys['connection']);
  57. return array_keys($keys);
  58. }
  59. /**
  60. * 魔术方法
  61. *
  62. * 被unserialize的时候,重新连接
  63. */
  64. public function __wakeup() {
  65. $this->connection = Database::getConnection($this->target);
  66. }
  67. public function __clone() {
  68. $this->uniqueIdentifier = uniqid('', true);
  69. }
  70. /**
  71. * 获取该query的唯一标识码
  72. */
  73. public function uniqueIdentifier() {
  74. return $this->uniqueIdentifier;
  75. }
  76. /**
  77. * 获取下一个占位符计数
  78. */
  79. public function nextPlaceholder() {
  80. return $this->nextPlaceholder++;
  81. }
  82. }