AclQueryBuilder.php 17 KB

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