AclQueryBuilder.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 ('@primaryKey' === $select) {
  156. return "{$this->_fromPrefix}.{$sqlPk} as `@primaryKey`";
  157. }
  158. if ('@instances' === $select) {
  159. if (!($this->from instanceof Core_AclBase)) throw new Exception("select @instances allowed only for Acl object");
  160. $instanceTable = ACL::getInstanceTable($this->from->getNamespace());
  161. return "
  162. (
  163. select GROUP_CONCAT(inst_conf.namespace)
  164. from `{$instanceTable}` inst_tbl
  165. join `CRM_INSTANCE_CONFIG` inst_conf on (inst_conf.id = inst_tbl.idInstance)
  166. where inst_tbl.pk = {$this->_fromPrefix}.{$sqlPk}
  167. ) as `@instances`
  168. ";
  169. }
  170. if (is_array($select)) {
  171. // TODO: [ '__backRef' => [ ... ] ]
  172. DBG::log($select, 'array', "TODO: select array");
  173. return null;
  174. }
  175. if (is_string($select)) {
  176. // TODO: only real table field
  177. // TODO: if geometry type then `AsWKT(t.{$sqlFieldName}) as {$fieldName}`
  178. try {
  179. return $this->parseSelectFieldValueToSql($select, $this->_fromPrefix);
  180. } catch (Exception $e) {
  181. DBG::log($e);
  182. }
  183. }
  184. return null;
  185. }
  186. public function join($join, $prefix, $params) {
  187. if (array_key_exists($prefix, $this->_joinPrefix)) throw new Exception("Prefix already used!");
  188. $this->_joinPrefix[$prefix] = $join;
  189. $this->_joinParams[$prefix] = $params;
  190. return $this;
  191. }
  192. public function orderBy($orderBy) { // TODO: ogc: order by ...
  193. if (null !== $this->orderBy) throw new Exception("OrderBy already set!");
  194. $this->orderBy = [];
  195. if (!$orderBy) return $this;
  196. // ID A,COL_X D,COL_Y A,...
  197. if (false !== strpos($orderBy, '+')) $orderBy = str_replace('+', ' ', $orderBy);
  198. $sortByEx = array_map('trim', explode(',', $orderBy));
  199. $sortByEx = array_filter($sortByEx, ['V', 'filterNotEmpty']);
  200. foreach ($sortByEx as $sortPart) {
  201. $sortPartEx = explode(' ', $sortPart);
  202. if (count($sortPartEx) > 2) throw new Exception("SortBy parse error #" . __LINE__);
  203. $fieldName = trim($sortPartEx[0]);
  204. if (!$this->isFieldAllowedToOrderBy($fieldName)) throw new Exception("SortBy parse error for field '{$fieldName}' #" . __LINE__);
  205. $colSortDir = 'ASC';
  206. if (count($sortPartEx) == 2) {
  207. if ('A' == strtoupper($sortPartEx[1]) || 'ASC' == strtoupper($sortPartEx[1])) {
  208. } else if ('D' == strtoupper($sortPartEx[1]) || 'DESC' == strtoupper($sortPartEx[1])) {
  209. $colSortDir = 'DESC';
  210. } else throw new Exception("SortBy parse error - unknown sort order '{$sortPartEx[1]}' #" . __LINE__);
  211. }
  212. $this->orderBy[] = [$fieldName, $colSortDir];
  213. DBG::log($this->orderBy, 'array', "sortBy");
  214. }
  215. return $this;
  216. }
  217. public function isFieldAllowedToOrderBy($fieldName) {
  218. return true; // TODO:? only local fields in $this->from
  219. }
  220. public function generateOrderBySql() {
  221. if (empty($this->orderBy)) return '';
  222. $sortByList = [];
  223. foreach ($this->orderBy as $orderBy) {
  224. list($fieldName, $orderDir) = $orderBy;
  225. if (false !== strpos($fieldName, '.')) {
  226. list($joinPrefix, $joinFieldName) = explode('.', $fieldName, 2);
  227. $sqlOrderByField = $this->getPDO()->identifierQuote($joinFieldName);
  228. if ('t' !== $joinPrefix && !array_key_exists($joinPrefix, $this->_joinParams)) throw new Exception("Missing table prefix '{$joinPrefix}' in orderBy");
  229. $sortByList[] = "{$joinPrefix}.{$sqlOrderByField} {$orderDir}";
  230. } else {
  231. $sqlOrderByField = $this->getPDO()->identifierQuote($fieldName);
  232. $sortByList[] = "t.{$sqlOrderByField} {$orderDir}";
  233. }
  234. }
  235. return (!empty($sortByList))
  236. ? "order by " . implode(", ", $sortByList)
  237. : '';
  238. }
  239. public function limit($limit) {
  240. $this->limit = (int)$limit;
  241. return $this;
  242. }
  243. public function offset($offset) {
  244. $this->offset = (int)$offset;
  245. return $this;
  246. }
  247. public function isInstance($instances) {
  248. $this->isInstances = (is_array($instances)) ? $instances : [ $instances ];
  249. return $this;
  250. }
  251. public function isNotInstance($instances) {
  252. $this->isNotInstances = (is_array($instances)) ? $instances : [ $instances ];
  253. return $this;
  254. }
  255. public function _parseJoinParams($params) {
  256. if (array_key_exists('rawJoin', $params)) return $params['rawJoin'];
  257. throw new Exception("Not implemented JOIN params '".json_encode($params)."'");
  258. }
  259. public function _getTableName($source) {
  260. if (is_scalar($source)) return $source;
  261. if ($source instanceof Core_AclBase) return $source->getRootTableName();
  262. throw new Exception("Not implemented FROM type '".get_class($source)."'");
  263. }
  264. public function execute() {
  265. return $this->fetchAll();
  266. }
  267. public function getPDO() {
  268. $idDatabase = $this->from->getDB();
  269. return DB::getPDO($idDatabase);
  270. }
  271. public function fetchAll() {
  272. $sql = $this->generateSql();
  273. DBG::log((array)$this, 'array', "AclQueryBuilder::fetchAll");
  274. return $this->getPDO()->fetchAll($sql);
  275. }
  276. public function fetchTotal() {
  277. $sql = $this->generateSql("count(*) as cnt");
  278. return $this->getPDO()->fetchValue($sql);
  279. }
  280. public function fetchValue() {
  281. $sql = $this->generateSql();
  282. DBG::log(['sql'=>$sql,'this'=>(array)$this], 'array', "AclQueryBuilder::fetchValue");
  283. return $this->getPDO()->fetchValue($sql);
  284. }
  285. public function fetchFirst() {
  286. $sql = $this->generateSql();
  287. DBG::log(['sql'=>$sql,'this'=>(array)$this], 'array', "AclQueryBuilder::fetchFirst");
  288. return $this->getPDO()->fetchFirst($sql);
  289. }
  290. public function generateSql($select = null) {
  291. if (!$this->from) throw new Exception("Missing FROM");
  292. $tableName = $this->_getTableName($this->from);
  293. if (!$tableName) throw new Exception("Missing FROM table name");
  294. $sqlPk = ($this->from instanceof Core_AclBase)
  295. ? $this->from->getSqlPrimaryKeyField()
  296. : 'ID';
  297. $sqlJoin = [];
  298. DBG::log($this, 'array', '$this');
  299. foreach ($this->isInstances as $k => $ns) {
  300. $idInstance = ACL::getInstanceId($ns);
  301. $instanceTable = ACL::getInstanceTable($ns);
  302. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  303. $prefix = "is_inst_{$k}";
  304. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  305. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  306. // and {$prefix}.idInstance = {$idInstance}
  307. // )";
  308. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} in (
  309. select {$prefix}.pk
  310. from `{$instanceTable}` {$prefix}
  311. where {$prefix}.idInstance = {$idInstance}
  312. )";
  313. DBG::log("{$this->_fromPrefix}.{$sqlPk} in (
  314. select {$prefix}.pk
  315. from `{$instanceTable}` {$prefix}
  316. where {$prefix}.idInstance = {$idInstance}
  317. )", 'string', "\$this->where[] =");
  318. }
  319. foreach ($this->isNotInstances as $k => $ns) {
  320. $idInstance = ACL::getInstanceId($ns);
  321. $instanceTable = ACL::getInstanceTable($ns);
  322. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  323. $prefix = "is_inst_{$k}";
  324. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  325. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  326. // and {$prefix}.idInstance = {$idInstance}
  327. // )";
  328. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} not in (
  329. select {$prefix}.pk
  330. from `{$instanceTable}` {$prefix}
  331. where {$prefix}.idInstance = {$idInstance}
  332. )";
  333. }
  334. // join `{$instanceTable}` i on(i.pk = t.{$sqlPk} and i.idInstance = {$idInstance})
  335. foreach ($this->_joinPrefix as $prefix => $join) {
  336. $joinName = $this->_getTableName($join);
  337. $sqlJoin[] = " join `{$joinName}` {$prefix} on(" . $this->_parseJoinParams($this->_joinParams[$prefix]) . ")";
  338. }
  339. $sqlJoin = (!empty($sqlJoin)) ? implode("\n\t", $sqlJoin) : "";
  340. DBG::log($this->where, 'array', "generateSql \$this->where");
  341. $sqlWhere = array_filter(
  342. array_map([$this, '_generateWhereMain'], $this->where),
  343. 'is_string'
  344. );
  345. $sqlWhere = (!empty($sqlWhere)) ? "where " . implode("\n\t and ", $sqlWhere) : '';
  346. $limit = ($this->limit < 0) ? 0 : $this->limit;
  347. $offset = ($this->offset < 0) ? 0 : $this->offset;
  348. $sqlLimit = ($limit > 0) ? "limit {$limit} offset {$offset}" : '';
  349. // TODO: split select to local and remote (use field isLocal)
  350. // TODO: $this->_hasSelectRemoteFields = false;
  351. // TODO: $this->_hasQueryRemoteFields = false;
  352. if ($select) {
  353. $sqlSelect = $select; // used for example fetchTotal -> generateSql("count(*) as cnt")
  354. } else {
  355. $sqlSelect = array_filter(
  356. array_map([$this, '_generateSelectMain'], $this->select, array_keys($this->select)),
  357. 'is_string'
  358. );
  359. $sqlSelect = (!empty($sqlSelect))
  360. ? implode(",\n\t", $sqlSelect)
  361. : "{$this->_fromPrefix}.*"
  362. ;
  363. }
  364. $sqlOrderBy = $this->generateOrderBySql();
  365. $sqlTableName = (is_object($this->from) && method_exists($this->from, 'getSqlTableFrom'))
  366. ? $this->from->getSqlTableFrom()
  367. : $this->getPDO()->tableNameQuote( $tableName );
  368. return "
  369. select {$sqlSelect}
  370. from {$sqlTableName} {$this->_fromPrefix}
  371. {$sqlJoin}
  372. {$sqlWhere}
  373. {$sqlOrderBy}
  374. {$sqlLimit}
  375. ";
  376. }
  377. public function parseSelectFieldValueToSql($fieldName, $prefix = 't') {
  378. if (false !== strpos($fieldName, '/')) {
  379. DBG::log($fieldName, 'string', "TODO: select by xpath");
  380. throw new Exception("Not implemented select by xpath ('{$fieldName}')");
  381. }
  382. $fieldType = $this->from->getXsdFieldType($fieldName);
  383. @list($typePrefix, $typeName, $retTypeName) = explode(':', $fieldType);
  384. $sqlFieldName = $this->getPDO()->identifierQuote($fieldName);
  385. switch ($typePrefix) {
  386. case 'xsd': {
  387. switch ($typeName) {
  388. case 'base64Binary': return "IF({$prefix}.{$sqlFieldName} is not null, 1, 0) as {$sqlFieldName}";
  389. default: return "{$prefix}.{$sqlFieldName}";
  390. }
  391. }
  392. // 'gml:PolygonPropertyType':
  393. // 'gml:PointPropertyType':
  394. // 'gml:LineStringPropertyType':
  395. // 'gml:GeometryPropertyType':
  396. case 'gml': return "AsWKT({$prefix}.{$sqlFieldName}) as {$sqlFieldName}";
  397. case 'p5': {
  398. switch ($typeName) {
  399. case 'text': return "{$prefix}.{$sqlFieldName}";
  400. case 'price': return "{$prefix}.{$sqlFieldName}";
  401. case 'enum': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote
  402. case 'www_link': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  403. case 'string': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  404. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  405. }
  406. }
  407. case 'p5Type': {
  408. switch ($typeName) {
  409. case 'text': return "{$prefix}.{$sqlFieldName}";
  410. case 'price': return "{$prefix}.{$sqlFieldName}";
  411. case 'enum': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote
  412. case 'www_link': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  413. case 'string': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  414. case 'date':
  415. case 'dateTime':
  416. case 'decimal':
  417. case 'double':
  418. case 'float':
  419. case 'hexBinary':
  420. case 'polygon':
  421. case 'text':
  422. case 'time':
  423. case 'year':
  424. case 'integer': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  425. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  426. }
  427. }
  428. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  429. }
  430. return null;
  431. }
  432. }