AclQueryBuilder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. $sqlOrderByField = $this->getPDO()->identifierQuote($orderBy[0]);
  222. $sortByList[] = "t.{$sqlOrderByField} {$orderBy[1]}";
  223. }
  224. return (!empty($sortByList))
  225. ? "order by " . implode(", ", $sortByList)
  226. : '';
  227. }
  228. public function limit($limit) {
  229. $this->limit = (int)$limit;
  230. return $this;
  231. }
  232. public function offset($offset) {
  233. $this->offset = (int)$offset;
  234. return $this;
  235. }
  236. public function isInstance($instances) {
  237. $this->isInstances = (is_array($instances)) ? $instances : [ $instances ];
  238. return $this;
  239. }
  240. public function isNotInstance($instances) {
  241. $this->isNotInstances = (is_array($instances)) ? $instances : [ $instances ];
  242. return $this;
  243. }
  244. public function _parseJoinParams($params) {
  245. if (array_key_exists('rawJoin', $params)) return $params['rawJoin'];
  246. throw new Exception("Not implemented JOIN params '".json_encode($params)."'");
  247. }
  248. public function _getTableName($source) {
  249. if (is_scalar($source)) return $source;
  250. if ($source instanceof Core_AclBase) return $source->getRootTableName();
  251. throw new Exception("Not implemented FROM type '".get_class($source)."'");
  252. }
  253. public function execute() {
  254. return $this->fetchAll();
  255. }
  256. public function getPDO() {
  257. $idDatabase = $this->from->getDB();
  258. return DB::getPDO($idDatabase);
  259. }
  260. public function fetchAll() {
  261. $sql = $this->generateSql();
  262. DBG::log((array)$this, 'array', "AclQueryBuilder::fetchAll");
  263. return $this->getPDO()->fetchAll($sql);
  264. }
  265. public function fetchTotal() {
  266. $sql = $this->generateSql("count(*) as cnt");
  267. return $this->getPDO()->fetchValue($sql);
  268. }
  269. public function fetchValue() {
  270. $sql = $this->generateSql();
  271. DBG::log(['sql'=>$sql,'this'=>(array)$this], 'array', "AclQueryBuilder::fetchValue");
  272. return $this->getPDO()->fetchValue($sql);
  273. }
  274. public function fetchFirst() {
  275. $sql = $this->generateSql();
  276. DBG::log(['sql'=>$sql,'this'=>(array)$this], 'array', "AclQueryBuilder::fetchFirst");
  277. return $this->getPDO()->fetchFirst($sql);
  278. }
  279. public function generateSql($select = null) {
  280. if (!$this->from) throw new Exception("Missing FROM");
  281. $tableName = $this->_getTableName($this->from);
  282. if (!$tableName) throw new Exception("Missing FROM table name");
  283. $sqlPk = ($this->from instanceof Core_AclBase)
  284. ? $this->from->getSqlPrimaryKeyField()
  285. : 'ID';
  286. $sqlJoin = [];
  287. DBG::log($this, 'array', '$this');
  288. foreach ($this->isInstances as $k => $ns) {
  289. $idInstance = ACL::getInstanceId($ns);
  290. $instanceTable = ACL::getInstanceTable($ns);
  291. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  292. $prefix = "is_inst_{$k}";
  293. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  294. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  295. // and {$prefix}.idInstance = {$idInstance}
  296. // )";
  297. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} in (
  298. select {$prefix}.pk
  299. from `{$instanceTable}` {$prefix}
  300. where {$prefix}.idInstance = {$idInstance}
  301. )";
  302. DBG::log("{$this->_fromPrefix}.{$sqlPk} in (
  303. select {$prefix}.pk
  304. from `{$instanceTable}` {$prefix}
  305. where {$prefix}.idInstance = {$idInstance}
  306. )", 'string', "\$this->where[] =");
  307. }
  308. foreach ($this->isNotInstances as $k => $ns) {
  309. $idInstance = ACL::getInstanceId($ns);
  310. $instanceTable = ACL::getInstanceTable($ns);
  311. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  312. $prefix = "is_inst_{$k}";
  313. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  314. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  315. // and {$prefix}.idInstance = {$idInstance}
  316. // )";
  317. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} not in (
  318. select {$prefix}.pk
  319. from `{$instanceTable}` {$prefix}
  320. where {$prefix}.idInstance = {$idInstance}
  321. )";
  322. }
  323. // join `{$instanceTable}` i on(i.pk = t.{$sqlPk} and i.idInstance = {$idInstance})
  324. foreach ($this->_joinPrefix as $prefix => $join) {
  325. $joinName = $this->_getTableName($join);
  326. $sqlJoin[] = " join `{$joinName}` {$prefix} on(" . $this->_parseJoinParams($this->_joinParams[$prefix]) . ")";
  327. }
  328. $sqlJoin = (!empty($sqlJoin)) ? implode("\n\t", $sqlJoin) : "";
  329. DBG::log($this->where, 'array', "generateSql \$this->where");
  330. $sqlWhere = array_filter(
  331. array_map([$this, '_generateWhereMain'], $this->where),
  332. 'is_string'
  333. );
  334. $sqlWhere = (!empty($sqlWhere)) ? "where " . implode("\n\t and ", $sqlWhere) : '';
  335. $limit = ($this->limit < 0) ? 0 : $this->limit;
  336. $offset = ($this->offset < 0) ? 0 : $this->offset;
  337. $sqlLimit = ($limit > 0) ? "limit {$limit} offset {$offset}" : '';
  338. // TODO: split select to local and remote (use field isLocal)
  339. // TODO: $this->_hasSelectRemoteFields = false;
  340. // TODO: $this->_hasQueryRemoteFields = false;
  341. if ($select) {
  342. $sqlSelect = $select; // used for example fetchTotal -> generateSql("count(*) as cnt")
  343. } else {
  344. $sqlSelect = array_filter(
  345. array_map([$this, '_generateSelectMain'], $this->select, array_keys($this->select)),
  346. 'is_string'
  347. );
  348. $sqlSelect = (!empty($sqlSelect))
  349. ? implode(",\n\t", $sqlSelect)
  350. : "{$this->_fromPrefix}.*"
  351. ;
  352. }
  353. $sqlOrderBy = $this->generateOrderBySql();
  354. $sqlTableName = (is_object($this->from) && method_exists($this->from, 'getSqlTableFrom'))
  355. ? $this->from->getSqlTableFrom()
  356. : $this->getPDO()->tableNameQuote( $tableName );
  357. return "
  358. select {$sqlSelect}
  359. from {$sqlTableName} {$this->_fromPrefix}
  360. {$sqlJoin}
  361. {$sqlWhere}
  362. {$sqlOrderBy}
  363. {$sqlLimit}
  364. ";
  365. }
  366. public function parseSelectFieldValueToSql($fieldName, $prefix = 't') {
  367. if (false !== strpos($fieldName, '/')) {
  368. DBG::log($fieldName, 'string', "TODO: select by xpath");
  369. throw new Exception("Not implemented select by xpath ('{$fieldName}')");
  370. }
  371. $fieldType = $this->from->getXsdFieldType($fieldName);
  372. @list($typePrefix, $typeName, $retTypeName) = explode(':', $fieldType);
  373. $sqlFieldName = $this->getPDO()->identifierQuote($fieldName);
  374. switch ($typePrefix) {
  375. case 'xsd': {
  376. switch ($typeName) {
  377. case 'base64Binary': return "IF({$prefix}.{$sqlFieldName} is not null, 1, 0) as {$sqlFieldName}";
  378. default: return "{$prefix}.{$sqlFieldName}";
  379. }
  380. }
  381. // 'gml:PolygonPropertyType':
  382. // 'gml:PointPropertyType':
  383. // 'gml:LineStringPropertyType':
  384. // 'gml:GeometryPropertyType':
  385. case 'gml': return "AsWKT({$prefix}.{$sqlFieldName}) as {$sqlFieldName}";
  386. case 'p5': {
  387. switch ($typeName) {
  388. case 'text': return "{$prefix}.{$sqlFieldName}";
  389. case 'price': return "{$prefix}.{$sqlFieldName}";
  390. case 'enum': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote
  391. case 'www_link': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  392. case 'string': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  393. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  394. }
  395. }
  396. case 'p5Type': {
  397. switch ($typeName) {
  398. case 'text': return "{$prefix}.{$sqlFieldName}";
  399. case 'price': return "{$prefix}.{$sqlFieldName}";
  400. case 'enum': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote
  401. case 'www_link': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  402. case 'string': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  403. case 'date':
  404. case 'dateTime':
  405. case 'decimal':
  406. case 'double':
  407. case 'float':
  408. case 'hexBinary':
  409. case 'polygon':
  410. case 'text':
  411. case 'time':
  412. case 'year':
  413. case 'integer': return "{$prefix}.{$sqlFieldName}"; // TODO: check if local or remote?
  414. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  415. }
  416. }
  417. default: throw new Exception("Not implemented field type in select '{$fieldType}' (field: '{$fieldName}')");
  418. }
  419. return null;
  420. }
  421. }