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

/core/lib/Drupal/Core/Database/Query/SelectExtender.php

https://gitlab.com/reasonat/test8
PHP | 536 lines | 237 code | 76 blank | 223 comment | 4 complexity | 554253601d2a2d601f90ac627e02dd65 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\Database\Query;
  3. use Drupal\Core\Database\Connection;
  4. /**
  5. * The base extender class for Select queries.
  6. */
  7. class SelectExtender implements SelectInterface {
  8. /**
  9. * The Select query object we are extending/decorating.
  10. *
  11. * @var \Drupal\Core\Database\Query\SelectInterface
  12. */
  13. protected $query;
  14. /**
  15. * The connection object on which to run this query.
  16. *
  17. * @var \Drupal\Core\Database\Connection
  18. */
  19. protected $connection;
  20. /**
  21. * A unique identifier for this query object.
  22. */
  23. protected $uniqueIdentifier;
  24. /**
  25. * The placeholder counter.
  26. */
  27. protected $placeholder = 0;
  28. public function __construct(SelectInterface $query, Connection $connection) {
  29. $this->uniqueIdentifier = uniqid('', TRUE);
  30. $this->query = $query;
  31. $this->connection = $connection;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function uniqueIdentifier() {
  37. return $this->uniqueIdentifier;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function nextPlaceholder() {
  43. return $this->placeholder++;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function addTag($tag) {
  49. $this->query->addTag($tag);
  50. return $this;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function hasTag($tag) {
  56. return $this->query->hasTag($tag);
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function hasAllTags() {
  62. return call_user_func_array(array($this->query, 'hasAllTags'), func_get_args());
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function hasAnyTag() {
  68. return call_user_func_array(array($this->query, 'hasAnyTag'), func_get_args());
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function addMetaData($key, $object) {
  74. $this->query->addMetaData($key, $object);
  75. return $this;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getMetaData($key) {
  81. return $this->query->getMetaData($key);
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function condition($field, $value = NULL, $operator = '=') {
  87. $this->query->condition($field, $value, $operator);
  88. return $this;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function &conditions() {
  94. return $this->query->conditions();
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function arguments() {
  100. return $this->query->arguments();
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function where($snippet, $args = array()) {
  106. $this->query->where($snippet, $args);
  107. return $this;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function compile(Connection $connection, PlaceholderInterface $queryPlaceholder) {
  113. return $this->query->compile($connection, $queryPlaceholder);
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function compiled() {
  119. return $this->query->compiled();
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function havingCondition($field, $value = NULL, $operator = '=') {
  125. $this->query->havingCondition($field, $value, $operator);
  126. return $this;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function &havingConditions() {
  132. return $this->query->havingConditions();
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function havingArguments() {
  138. return $this->query->havingArguments();
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function having($snippet, $args = array()) {
  144. $this->query->having($snippet, $args);
  145. return $this;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function havingCompile(Connection $connection) {
  151. return $this->query->havingCompile($connection);
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function havingIsNull($field) {
  157. $this->query->havingIsNull($field);
  158. return $this;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function havingIsNotNull($field) {
  164. $this->query->havingIsNotNull($field);
  165. return $this;
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function havingExists(SelectInterface $select) {
  171. $this->query->havingExists($select);
  172. return $this;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function havingNotExists(SelectInterface $select) {
  178. $this->query->havingNotExists($select);
  179. return $this;
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function extend($extender_name) {
  185. $class = $this->connection->getDriverClass($extender_name);
  186. return new $class($this, $this->connection);
  187. }
  188. /* Alter accessors to expose the query data to alter hooks. */
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function &getFields() {
  193. return $this->query->getFields();
  194. }
  195. /**
  196. * {@inheritdoc}
  197. */
  198. public function &getExpressions() {
  199. return $this->query->getExpressions();
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function &getOrderBy() {
  205. return $this->query->getOrderBy();
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function &getGroupBy() {
  211. return $this->query->getGroupBy();
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function &getTables() {
  217. return $this->query->getTables();
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function &getUnion() {
  223. return $this->query->getUnion();
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function escapeLike($string) {
  229. return $this->query->escapeLike($string);
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function escapeField($string) {
  235. $this->query->escapeField($string);
  236. return $this;
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function getArguments(PlaceholderInterface $queryPlaceholder = NULL) {
  242. return $this->query->getArguments($queryPlaceholder);
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function isPrepared() {
  248. return $this->query->isPrepared();
  249. }
  250. /**
  251. * {@inheritdoc}
  252. */
  253. public function preExecute(SelectInterface $query = NULL) {
  254. // If no query object is passed in, use $this.
  255. if (!isset($query)) {
  256. $query = $this;
  257. }
  258. return $this->query->preExecute($query);
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function execute() {
  264. // By calling preExecute() here, we force it to preprocess the extender
  265. // object rather than just the base query object. That means
  266. // hook_query_alter() gets access to the extended object.
  267. if (!$this->preExecute($this)) {
  268. return NULL;
  269. }
  270. return $this->query->execute();
  271. }
  272. /**
  273. * {@inheritdoc}
  274. */
  275. public function distinct($distinct = TRUE) {
  276. $this->query->distinct($distinct);
  277. return $this;
  278. }
  279. /**
  280. * {@inheritdoc}
  281. */
  282. public function addField($table_alias, $field, $alias = NULL) {
  283. return $this->query->addField($table_alias, $field, $alias);
  284. }
  285. /**
  286. * {@inheritdoc}
  287. */
  288. public function fields($table_alias, array $fields = array()) {
  289. $this->query->fields($table_alias, $fields);
  290. return $this;
  291. }
  292. /**
  293. * {@inheritdoc}
  294. */
  295. public function addExpression($expression, $alias = NULL, $arguments = array()) {
  296. return $this->query->addExpression($expression, $alias, $arguments);
  297. }
  298. /**
  299. * {@inheritdoc}
  300. */
  301. public function join($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  302. return $this->query->join($table, $alias, $condition, $arguments);
  303. }
  304. /**
  305. * {@inheritdoc}
  306. */
  307. public function innerJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  308. return $this->query->innerJoin($table, $alias, $condition, $arguments);
  309. }
  310. /**
  311. * {@inheritdoc}
  312. */
  313. public function leftJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  314. return $this->query->leftJoin($table, $alias, $condition, $arguments);
  315. }
  316. /**
  317. * {@inheritdoc}
  318. */
  319. public function rightJoin($table, $alias = NULL, $condition = NULL, $arguments = array()) {
  320. return $this->query->rightJoin($table, $alias, $condition, $arguments);
  321. }
  322. /**
  323. * {@inheritdoc}
  324. */
  325. public function addJoin($type, $table, $alias = NULL, $condition = NULL, $arguments = array()) {
  326. return $this->query->addJoin($type, $table, $alias, $condition, $arguments);
  327. }
  328. /**
  329. * {@inheritdoc}
  330. */
  331. public function orderBy($field, $direction = 'ASC') {
  332. $this->query->orderBy($field, $direction);
  333. return $this;
  334. }
  335. /**
  336. * {@inheritdoc}
  337. */
  338. public function orderRandom() {
  339. $this->query->orderRandom();
  340. return $this;
  341. }
  342. /**
  343. * {@inheritdoc}
  344. */
  345. public function range($start = NULL, $length = NULL) {
  346. $this->query->range($start, $length);
  347. return $this;
  348. }
  349. /**
  350. * {@inheritdoc}
  351. */
  352. public function union(SelectInterface $query, $type = '') {
  353. $this->query->union($query, $type);
  354. return $this;
  355. }
  356. /**
  357. * {@inheritdoc}
  358. */
  359. public function groupBy($field) {
  360. $this->query->groupBy($field);
  361. return $this;
  362. }
  363. /**
  364. * {@inheritdoc}
  365. */
  366. public function forUpdate($set = TRUE) {
  367. $this->query->forUpdate($set);
  368. return $this;
  369. }
  370. /**
  371. * {@inheritdoc}
  372. */
  373. public function countQuery() {
  374. return $this->query->countQuery();
  375. }
  376. /**
  377. * {@inheritdoc}
  378. */
  379. function isNull($field) {
  380. $this->query->isNull($field);
  381. return $this;
  382. }
  383. /**
  384. * {@inheritdoc}
  385. */
  386. function isNotNull($field) {
  387. $this->query->isNotNull($field);
  388. return $this;
  389. }
  390. /**
  391. * {@inheritdoc}
  392. */
  393. public function exists(SelectInterface $select) {
  394. $this->query->exists($select);
  395. return $this;
  396. }
  397. /**
  398. * {@inheritdoc}
  399. */
  400. public function notExists(SelectInterface $select) {
  401. $this->query->notExists($select);
  402. return $this;
  403. }
  404. /**
  405. * {@inheritdoc}
  406. */
  407. public function __toString() {
  408. return (string) $this->query;
  409. }
  410. /**
  411. * {@inheritdoc}
  412. */
  413. public function __clone() {
  414. $this->uniqueIdentifier = uniqid('', TRUE);
  415. // We need to deep-clone the query we're wrapping, which in turn may
  416. // deep-clone other objects. Exciting!
  417. $this->query = clone($this->query);
  418. }
  419. /**
  420. * Magic override for undefined methods.
  421. *
  422. * If one extender extends another extender, then methods in the inner extender
  423. * will not be exposed on the outer extender. That's because we cannot know
  424. * in advance what those methods will be, so we cannot provide wrapping
  425. * implementations as we do above. Instead, we use this slower catch-all method
  426. * to handle any additional methods.
  427. */
  428. public function __call($method, $args) {
  429. $return = call_user_func_array(array($this->query, $method), $args);
  430. // Some methods will return the called object as part of a fluent interface.
  431. // Others will return some useful value. If it's a value, then the caller
  432. // probably wants that value. If it's the called object, then we instead
  433. // return this object. That way we don't "lose" an extender layer when
  434. // chaining methods together.
  435. if ($return instanceof SelectInterface) {
  436. return $this;
  437. }
  438. else {
  439. return $return;
  440. }
  441. }
  442. /**
  443. * {@inheritdoc}
  444. */
  445. public function conditionGroupFactory($conjunction = 'AND') {
  446. return new Condition($conjunction);
  447. }
  448. /**
  449. * {@inheritdoc}
  450. */
  451. public function andConditionGroup() {
  452. return $this->conditionGroupFactory('AND');
  453. }
  454. /**
  455. * {@inheritdoc}
  456. */
  457. public function orConditionGroup() {
  458. return $this->conditionGroupFactory('OR');
  459. }
  460. }