Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
nosqlprimaryselector.php
1<?php
11
18{
25 public static function checkQuery(\Bitrix\Main\Data\Connection $connection, Query $query)
26 {
27 // check interface
28 if (!($connection instanceof INosqlPrimarySelector))
29 {
30 return false;
31 }
32
33 // no expressions in select
34 foreach ($query->getSelectChains() as $selectChain)
35 {
36 if ($selectChain->getLastElement()->getValue() instanceof ExpressionField)
37 {
38 return false;
39 }
40 }
41
42 // skip empty select - useless case for nosql api
43 if (empty($query->getSelect()))
44 {
45 return false;
46 }
47
48 // if empty joinmap, group, order and simple filter
49 if (!count($query->getJoinMap()) && !count($query->getGroupChains()) && !count($query->getOrderChains()) && !count($query->getHavingChains()))
50 {
51 $entityPrimary = $query->getEntity()->getPrimary();
52
53 // check for primary singularity
54 if (!is_array($entityPrimary))
55 {
56 // check if only primary is in filter
57 if (count($query->getFilterChains()) == 1 && key($query->getFilterChains()) === $entityPrimary)
58 {
59 $passFilter = true;
60
61 // check if only equality operations & 1-level filter
62 if ($query->getFilter())
63 {
64 foreach ($query->getFilter() as $filterElement => $filterValue)
65 {
66 if (is_numeric($filterElement) && is_array($filterValue))
67 {
68 // filter has subfilters. not ok
69 $passFilter = false;
70 break;
71 }
72
73 // no multiple values for HSPHP
74 if (is_array($filterValue))
75 {
76 $passFilter = false;
77 break;
78 }
79
80 // skip system keys
81 if ($filterElement === 'LOGIC')
82 {
83 continue;
84 }
85
86 $operation = substr($filterElement, 0, 1);
87
88 if ($operation !== '=')
89 {
90 // only equal operation allowed. not ok
91 $passFilter = false;
92 break;
93 }
94 }
95 }
96 elseif ($query->getFilterHandler()->hasConditions())
97 {
98 foreach ($query->getFilterHandler()->getConditions() as $condition)
99 {
100 if ($condition->getOperator() !== '=' || !is_scalar($condition->getValue()))
101 {
102 $passFilter = false;
103 break;
104 }
105 }
106 }
107
108
109 // fine!
110 if ($passFilter)
111 {
112 return true;
113 }
114 }
115 }
116 }
117
118 return false;
119 }
120
121 public static function relayQuery(\Bitrix\Main\Data\Connection $connection, Query $query)
122 {
123 // prepare select
124 $select = array();
125
126 foreach ($query->getSelectChains() as $selectChain)
127 {
128 $select[] = $selectChain->getLastElement()->getValue()->getName();
129 }
130
131 // prepare filter
132 $filter = array();
133
134 if ($query->getFilter())
135 {
136 foreach ($query->getFilter() as $filterElem)
137 {
138 if (is_array($filterElem))
139 {
140 $filter = array_merge($filter, $filterElem);
141 }
142 else
143 {
144 $filter[] = $filterElem;
145 }
146 }
147 }
148 elseif ($query->getFilterHandler()->hasConditions())
149 {
150 foreach ($query->getFilterHandler()->getConditions() as $condition)
151 {
152 $filter[] = $condition->getValue();
153 }
154 }
155
156 $result = $connection->getEntityByPrimary($query->getEntity(), $filter, $select);
157
158 return $result;
159 }
160}
static relayQuery(\Bitrix\Main\Data\Connection $connection, Query $query)
static checkQuery(\Bitrix\Main\Data\Connection $connection, Query $query)