AclQueryBuilder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. Lib::loadClass('P5');
  3. Lib::loadClass('Core_AclBase');
  4. Lib::loadClass('AntAclBase');
  5. Lib::loadClass('ACL');
  6. class AclQueryBuilder {
  7. public $select;
  8. public $from;
  9. public $where;
  10. /* where: array of [ $fieldName, $comparisonSign, $value ]
  11. * $fieldName - field name, TODO: xpath, null for groups (or, and)
  12. * $comparisonSign - @see where() to check allowed (implemented) signs or function names
  13. * $value - string or another $where if group (or, and)
  14. * where examples: [ $fieldName, $comparisonSign, $value ]
  15. [ 'ID', '=', '1' ] // where ID = '1'
  16. [ 'LABEL', 'like', '%abc%' ] // where LABEL like '%abc%'
  17. [ null, 'or', [] $values ] // where ( $values[0] or $values[1] or ... ) // or where $values[0] when 1 === count($values)
  18. [ null, 'and', [] $values ] // where ( $values[0] and $values[1] and ... ) // or where $values[0] when 1 === count($values)
  19. */
  20. public $orderBy;
  21. public $groupBy;
  22. public $limit;
  23. public $offset;
  24. public $_fromPrefix;
  25. public $_joinPrefix;
  26. public $_joinParams;
  27. public $isInstances;
  28. public $isNotInstances;
  29. public $_hasSelectRemoteFields;
  30. public $_hasQueryRemoteFields;
  31. public function __construct() {
  32. $this->select = [];
  33. $this->from = null; // (ACL | tableName)
  34. $this->where = [];
  35. $this->orderBy = null;
  36. $this->groupBy = null;
  37. $this->limit = null;
  38. $this->offset = null;
  39. $this->_fromPrefix = 't'; // prefix for this->from - default 't'
  40. $this->_joinPrefix = []; // prefix => from (Acl | tableName)
  41. $this->_joinParams = []; // prefx => params
  42. $this->isInstances = [];
  43. $this->isNotInstances = [];
  44. $this->_hasSelectRemoteFields = false;
  45. $this->_hasQueryRemoteFields = false;
  46. }
  47. public function from($from, $prefix = 't') {
  48. DBG::log([
  49. 'from instanceof Core_AclBase' => ($from instanceof Core_AclBase),
  50. 'from instanceof AntAclBase' => ($from instanceof AntAclBase),
  51. 'from instanceof TableAcl' => ($from instanceof TableAcl),
  52. ], 'array', "\$from class(".get_class($from).")");
  53. if ($this->from) throw new Exception("Duplicate FROM");
  54. $this->from = $from;
  55. $this->_fromPrefix = $prefix;
  56. return $this;
  57. }
  58. public function select($propertyName) { // TODO: ogc:propertyName, *, xpath, @instances, etc...
  59. if (empty($propertyName)) return $this; // SKIP empty values, null's, etc.
  60. DBG::log($propertyName, 'array', "AclQueryBuilder->select");
  61. if (is_array($propertyName)) {
  62. $hasWildcard = in_array('*', $propertyName);
  63. foreach ($propertyName as $k => $v) {
  64. if ('rawSelect' === $k) $this->select['__rawSelect__'] = $v;
  65. else if ('*' === $v) continue;
  66. else $this->select[] = $v;
  67. }
  68. if ($hasWildcard) array_unshift($this->select, '*');
  69. return $this;
  70. }
  71. if (!in_array($propertyName, $this->select)) $this->select[] = $propertyName; // TODO: split by property source
  72. return $this;
  73. }
  74. public function where($where) {
  75. if (null === $where) return $this;
  76. if (is_string($where)) return $this->whereRaw($where);
  77. list($fieldName, $comparisonSign, $value) = $where;
  78. if (!in_array($comparisonSign, [ // validation
  79. '=', '<', '>', '<=', '>=', '<>', '!='
  80. , 'like', 'not like'
  81. , 'is not null', 'is null'
  82. , 'Intersects', 'GeometryType'
  83. , 'or' // $value = [ comparisons for $fieldName ... or null ]
  84. , 'and' // $value = [ comparisons for $fieldName ... or null ]
  85. ])) {
  86. throw new Exception("Not implemented comparisonSign '{$comparisonSign}'");
  87. }
  88. $this->where[] = [$fieldName, $comparisonSign, $value];
  89. return $this;
  90. }
  91. public function _generateWhereMain($where) { // @returns string
  92. if (is_string($where)) return $where; // whereRaw
  93. list($fieldName, $comparisonSign, $value) = $where;
  94. // $sqlFieldName = $fieldName; // TODO: getSqlFieldName // TODO: get sql field name with table prefix from join list to replace "{$this->_fromPrefix}.{$sqlFieldName}" below
  95. $sqlFieldName = $this->getPDO()->identifierQuote($fieldName);
  96. switch ($comparisonSign) {
  97. case 'is not null': return "{$this->_fromPrefix}.{$sqlFieldName} is not null";
  98. case 'is null': return "{$this->_fromPrefix}.{$sqlFieldName} is null";
  99. case 'Intersects': return "Intersects(GeomFromText('{$value}'), {$this->_fromPrefix}.{$sqlFieldName})=1";
  100. case 'GeometryType': return "GeometryType({$this->_fromPrefix}.{$sqlFieldName})='{$value}'";
  101. case 'or': return $this->_generateWhereBlock($where);
  102. case 'and': return $this->_generateWhereBlock($where);
  103. case 'UNIX_TIMESTAMP_LESS_THAN_NOW': return "
  104. COALESCE(UNIX_TIMESTAMP({$this->_fromPrefix}.{$sqlFieldName}), 0) < UNIX_TIMESTAMP()
  105. and {$this->_fromPrefix}.{$sqlFieldName} != ''
  106. and {$this->_fromPrefix}.{$sqlFieldName} != '0000-00-00 00:00:00'
  107. ";
  108. case 'UNIX_TIMESTAMP_NOW_3600': return "
  109. COALESCE(UNIX_TIMESTAMP({$this->_fromPrefix}.{$sqlFieldName}), 0) < UNIX_TIMESTAMP()+3600
  110. and COALESCE(UNIX_TIMESTAMP({$this->_fromPrefix}.{$sqlFieldName}), 0) > UNIX_TIMESTAMP()-3600
  111. ";
  112. case 'UNIX_TIMESTAMP_GREATER_THAN': return " COALESCE(UNIX_TIMESTAMP({$this->_fromPrefix}.{$sqlFieldName}), 0) > '{$value}' ";
  113. case 'UNIX_TIMESTAMP_LESS_THAN': return " COALESCE(UNIX_TIMESTAMP({$this->_fromPrefix}.{$sqlFieldName}), 0) < '{$value}' ";
  114. default: return "{$this->_fromPrefix}.{$sqlFieldName} {$comparisonSign} " . $this->getPDO()->quote($value);
  115. }
  116. return null;
  117. }
  118. public function _generateWhereBlock($where) { // @returns string
  119. list($fieldName, $sqlGlue, $list) = $where;
  120. $list = array_filter($list, function ($value) { return null !== $value; });
  121. $sqlList = array_filter(
  122. array_map([$this, '_generateWhereMain'], $list),
  123. 'is_string'
  124. );
  125. if (1 === count($sqlList)) return $sqlList[0];
  126. else return "( " . implode(" {$sqlGlue} ", $sqlList) . " )";
  127. }
  128. public function whereIsNotNull($fieldName) {
  129. $this->where[] = "{$this->_fromPrefix}.{$fieldName} is not null";
  130. return $this;
  131. }
  132. public function whereRaw($rawWhere) { // add where without validation
  133. if (!$rawWhere) return $this;
  134. $this->where[] = $rawWhere;
  135. return $this;
  136. }
  137. // public function whereFunction($column, $operator = null, $value = null) {
  138. public function whereFunction($fieldName, $comparisonSign = null, $value = null) {
  139. throw new Exception("whereFunction not supported");
  140. $this->where[] = '';
  141. return $this;
  142. }
  143. public function generateWhereSql() { // @return string - sql without where keyword
  144. $sqlWhere = array_filter(
  145. array_map([$this, '_generateWhereMain'], $this->where),
  146. 'is_string'
  147. );
  148. return (!empty($sqlWhere)) ? implode("\n\t and ", $sqlWhere) : '';
  149. }
  150. public function _generateSelectMain($select, $key) {
  151. if ('__rawSelect__' === $key) return $select;
  152. $sqlPk = 'ID';
  153. if ($this->from instanceof Core_AclBase) $sqlPk = $this->from->getSqlPrimaryKeyField();
  154. if ('*' === $select) return "{$this->_fromPrefix}.*";
  155. if ('@instances' === $select) {
  156. if (!($this->from instanceof Core_AclBase)) throw new Exception("select @instances allowed only for Acl object");
  157. $instanceTable = ACL::getInstanceTable($this->from->getNamespace());
  158. return "
  159. (
  160. select GROUP_CONCAT(inst_conf.namespace)
  161. from `{$instanceTable}` inst_tbl
  162. join `CRM_INSTANCE_CONFIG` inst_conf on (inst_conf.id = inst_tbl.idInstance)
  163. where inst_tbl.pk = {$this->_fromPrefix}.{$sqlPk}
  164. ) as `@instances`
  165. ";
  166. }
  167. if (is_array($select)) {
  168. // TODO: [ '__backRef' => [ ... ] ]
  169. DBG::log($select, 'array', "TODO: select array");
  170. return null;
  171. }
  172. if (is_string($select)) {
  173. // TODO: only real table field
  174. // TODO: if geometry type then `AsWKT(t.{$sqlFieldName}) as {$fieldName}`
  175. try {
  176. return $this->parseSelectFieldValueToSql($select, $this->_fromPrefix);
  177. } catch (Exception $e) {
  178. DBG::log($e);
  179. }
  180. }
  181. return null;
  182. }
  183. public function join($join, $prefix, $params) {
  184. if (array_key_exists($prefix, $this->_joinPrefix)) throw new Exception("Prefix already used!");
  185. $this->_joinPrefix[$prefix] = $join;
  186. $this->_joinParams[$prefix] = $params;
  187. return $this;
  188. }
  189. public function orderBy($orderBy) { // TODO: ogc: order by ...
  190. if (null !== $this->orderBy) throw new Exception("OrderBy already set!");
  191. $this->orderBy = [];
  192. if (!$orderBy) return $this;
  193. // ID A,COL_X D,COL_Y A,...
  194. if (false !== strpos($orderBy, '+')) $orderBy = str_replace('+', ' ', $orderBy);
  195. $sortByEx = array_map('trim', explode(',', $orderBy));
  196. $sortByEx = array_filter($sortByEx, ['V', 'filterNotEmpty']);
  197. foreach ($sortByEx as $sortPart) {
  198. $sortPartEx = explode(' ', $sortPart);
  199. if (count($sortPartEx) > 2) throw new Exception("SortBy parse error #" . __LINE__);
  200. $fieldName = trim($sortPartEx[0]);
  201. if (!$this->isFieldAllowedToOrderBy($fieldName)) throw new Exception("SortBy parse error for field '{$fieldName}' #" . __LINE__);
  202. $colSortDir = 'ASC';
  203. if (count($sortPartEx) == 2) {
  204. if ('A' == strtoupper($sortPartEx[1]) || 'ASC' == strtoupper($sortPartEx[1])) {
  205. } else if ('D' == strtoupper($sortPartEx[1]) || 'DESC' == strtoupper($sortPartEx[1])) {
  206. $colSortDir = 'DESC';
  207. } else throw new Exception("SortBy parse error - unknown sort order '{$sortPartEx[1]}' #" . __LINE__);
  208. }
  209. $this->orderBy[] = [$fieldName, $colSortDir];
  210. DBG::log($this->orderBy, 'array', "sortBy");
  211. }
  212. return $this;
  213. }
  214. public function isFieldAllowedToOrderBy($fieldName) {
  215. return true; // TODO:? only local fields in $this->from
  216. }
  217. public function generateOrderBySql() {
  218. if (empty($this->orderBy)) return '';
  219. $sortByList = [];
  220. foreach ($this->orderBy as $orderBy) {
  221. list($fieldName, $orderDir) = $orderBy;
  222. if (false !== strpos($fieldName, '.')) {
  223. list($joinPrefix, $joinFieldName) = explode('.', $fieldName, 2);
  224. $sqlOrderByField = $this->getPDO()->identifierQuote($joinFieldName);
  225. if ('t' !== $joinPrefix && !array_key_exists($joinPrefix, $this->_joinParams)) throw new Exception("Missing table prefix '{$joinPrefix}' in orderBy");
  226. $sortByList[] = "{$joinPrefix}.{$sqlOrderByField} {$orderDir}";
  227. } else {
  228. $sqlOrderByField = $this->getPDO()->identifierQuote($fieldName);
  229. $sortByList[] = "t.{$sqlOrderByField} {$orderDir}";
  230. }
  231. }
  232. return (!empty($sortByList))
  233. ? "order by " . implode(", ", $sortByList)
  234. : '';
  235. }
  236. public function limit($limit) {
  237. $this->limit = (int)$limit;
  238. return $this;
  239. }
  240. public function offset($offset) {
  241. $this->offset = (int)$offset;
  242. return $this;
  243. }
  244. public function isInstance($instances) {
  245. $this->isInstances = (is_array($instances)) ? $instances : [ $instances ];
  246. return $this;
  247. }
  248. public function isNotInstance($instances) {
  249. $this->isNotInstances = (is_array($instances)) ? $instances : [ $instances ];
  250. return $this;
  251. }
  252. public function _parseJoinParams($params) {
  253. if (array_key_exists('rawJoin', $params)) return $params['rawJoin'];
  254. throw new Exception("Not implemented JOIN params '".json_encode($params)."'");
  255. }
  256. public function _getTableName($source) {
  257. if (is_scalar($source)) return $source;
  258. if ($source instanceof Core_AclBase) return $source->getRootTableName();
  259. throw new Exception("Not implemented FROM type '".get_class($source)."'");
  260. }
  261. public function execute() {
  262. return $this->fetchAll();
  263. }
  264. public function getPDO() {
  265. $idDatabase = $this->from->getDB();
  266. return DB::getPDO($idDatabase);
  267. }
  268. public function fetchAll() {
  269. $sql = $this->generateSql();
  270. DBG::log((array)$this, 'array', "AclQueryBuilder::fetchAll");
  271. return $this->getPDO()->fetchAll($sql);
  272. }
  273. public function fetchTotal() {
  274. $sql = $this->generateSql("count(*) as cnt");
  275. return $this->getPDO()->fetchValue($sql);
  276. }
  277. public function fetchValue() {
  278. $sql = $this->generateSql();
  279. DBG::log(['sql'=>$sql,'this'=>(array)$this], 'array', "AclQueryBuilder::fetchValue");
  280. return $this->getPDO()->fetchValue($sql);
  281. }
  282. public function fetchFirst() {
  283. $sql = $this->generateSql();
  284. DBG::log(['sql'=>$sql,'this'=>(array)$this], 'array', "AclQueryBuilder::fetchFirst");
  285. return $this->getPDO()->fetchFirst($sql);
  286. }
  287. public function generateSql($select = null) {
  288. if (!$this->from) throw new Exception("Missing FROM");
  289. $tableName = $this->_getTableName($this->from);
  290. if (!$tableName) throw new Exception("Missing FROM table name");
  291. $sqlPk = ($this->from instanceof Core_AclBase)
  292. ? $this->from->getSqlPrimaryKeyField()
  293. : 'ID';
  294. $sqlJoin = [];
  295. DBG::log($this, 'array', '$this');
  296. foreach ($this->isInstances as $k => $ns) {
  297. $idInstance = ACL::getInstanceId($ns);
  298. $instanceTable = ACL::getInstanceTable($ns);
  299. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  300. $prefix = "is_inst_{$k}";
  301. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  302. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  303. // and {$prefix}.idInstance = {$idInstance}
  304. // )";
  305. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} in (
  306. select {$prefix}.pk
  307. from `{$instanceTable}` {$prefix}
  308. where {$prefix}.idInstance = {$idInstance}
  309. )";
  310. DBG::log("{$this->_fromPrefix}.{$sqlPk} in (
  311. select {$prefix}.pk
  312. from `{$instanceTable}` {$prefix}
  313. where {$prefix}.idInstance = {$idInstance}
  314. )", 'string', "\$this->where[] =");
  315. }
  316. foreach ($this->isNotInstances as $k => $ns) {
  317. $idInstance = ACL::getInstanceId($ns);
  318. $instanceTable = ACL::getInstanceTable($ns);
  319. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  320. $prefix = "is_inst_{$k}";
  321. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  322. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  323. // and {$prefix}.idInstance = {$idInstance}
  324. // )";
  325. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} not in (
  326. select {$prefix}.pk
  327. from `{$instanceTable}` {$prefix}
  328. where {$prefix}.idInstance = {$idInstance}
  329. )";
  330. }
  331. // join `{$instanceTable}` i on(i.pk = t.{$sqlPk} and i.idInstance = {$idInstance})
  332. foreach ($this->_joinPrefix as $prefix => $join) {
  333. $joinName = $this->_getTableName($join);
  334. $sqlJoin[] = " join `{$joinName}` {$prefix} on(" . $this->_parseJoinParams($this->_joinParams[$prefix]) . ")";
  335. }
  336. $sqlJoin = (!empty($sqlJoin)) ? implode("\n\t", $sqlJoin) : "";
  337. DBG::log($this->where, 'array', "generateSql \$this->where");
  338. $sqlWhere = array_filter(
  339. array_map([$this, '_generateWhereMain'], $this->where),
  340. 'is_string'
  341. );
  342. $sqlWhere = (!empty($sqlWhere)) ? "where " . implode("\n\t and ", $sqlWhere) : '';
  343. $limit = ($this->limit < 0) ? 0 : $this->limit;
  344. $offset = ($this->offset < 0) ? 0 : $this->offset;
  345. $sqlLimit = ($limit > 0) ? "limit {$limit} offset {$offset}" : '';
  346. // TODO: split select to local and remote (use field isLocal)
  347. // TODO: $this->_hasSelectRemoteFields = false;
  348. // TODO: $this->_hasQueryRemoteFields = false;
  349. if ($select) {
  350. $sqlSelect = $select; // used for example fetchTotal -> generateSql("count(*) as cnt")
  351. } else {
  352. $sqlSelect = array_filter(
  353. array_map([$this, '_generateSelectMain'], $this->select, array_keys($this->select)),
  354. 'is_string'
  355. );
  356. $sqlSelect = (!empty($sqlSelect))
  357. ? implode(",\n\t", $sqlSelect)
  358. : "{$this->_fromPrefix}.*"
  359. ;
  360. }
  361. $sqlOrderBy = $this->generateOrderBySql();
  362. $sqlTableName = (is_object($this->from) && method_exists($this->from, 'getSqlTableFrom'))
  363. ? $this->from->getSqlTableFrom()
  364. : $this->getPDO()->tableNameQuote( $tableName );
  365. return "
  366. select {$sqlSelect}
  367. from {$sqlTableName} {$this->_fromPrefix}
  368. {$sqlJoin}
  369. {$sqlWhere}
  370. {$sqlOrderBy}
  371. {$sqlLimit}
  372. ";
  373. }
  374. public function parseSelectFieldValueToSql($fieldName, $prefix = 't') {
  375. if (false !== strpos($fieldName, '/')) {
  376. DBG::log($fieldName, 'string', "TODO: select by xpath");
  377. throw new Exception("Not implemented select by xpath ('{$fieldName}')");
  378. }
  379. $fieldType = $this->from->getXsdFieldType($fieldName);
  380. @list($typePrefix, $typeName, $retTypeName) = explode(':', $fieldType);
  381. $sqlFieldName = $this->getPDO()->identifierQuote($fieldName);
  382. switch ($typePrefix) {
  383. case 'xsd': {
  384. switch ($typeName) {
  385. case 'base64Binary': return "IF({$prefix}.{$sqlFieldName} is not null, 1, 0) as {$sqlFieldName}";
  386. default: return "{$prefix}.{$sqlFieldName}";
  387. }
  388. }
  389. // 'gml:PolygonPropertyType':
  390. // 'gml:PointPropertyType':
  391. // 'gml:LineStringPropertyType':
  392. // 'gml:GeometryPropertyType':
  393. case 'gml': return "AsWKT({$prefix}.{$sqlFieldName}) as {$sqlFieldName}";
  394. case 'p5': {
  395. switch ($typeName) {
  396. case 'text': return "{$prefix}.{$sqlFieldName}";
  397. case 'price': return "{$prefix}.{$sqlFieldName}";
  398. case 'enum': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote
  399. case 'www_link': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  400. case 'string': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  401. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  402. }
  403. }
  404. case 'p5Type': {
  405. switch ($typeName) {
  406. case 'text': return "{$prefix}.{$sqlFieldName}";
  407. case 'price': return "{$prefix}.{$sqlFieldName}";
  408. case 'enum': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote
  409. case 'www_link': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  410. case 'string': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  411. case 'date':
  412. case 'dateTime':
  413. case 'decimal':
  414. case 'double':
  415. case 'float':
  416. case 'hexBinary':
  417. case 'polygon':
  418. case 'text':
  419. case 'time':
  420. case 'year':
  421. case 'integer': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  422. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  423. }
  424. }
  425. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  426. }
  427. return null;
  428. }
  429. }