AclQueryBuilder.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. public $orderBy;
  11. public $groupBy;
  12. public $limit;
  13. public $offset;
  14. public $_fromPrefix;
  15. public $_joinPrefix;
  16. public $_joinParams;
  17. public $isInstances;
  18. public $isNotInstances;
  19. public function __construct() {
  20. $this->select = [];
  21. $this->from = null; // (ACL | tableName)
  22. $this->where = [];
  23. $this->orderBy = null;
  24. $this->groupBy = null;
  25. $this->limit = null;
  26. $this->offset = null;
  27. $this->_fromPrefix = 't'; // prefix for this->from - default 't'
  28. $this->_joinPrefix = []; // prefix => from (Acl | tableName)
  29. $this->_joinParams = []; // prefx => params
  30. $this->isInstances = [];
  31. $this->isNotInstances = [];
  32. }
  33. public function select($propertyName) { // TODO: ogc:propertyName, *, xpath, @instances, etc...
  34. if (!empty($propertyName) && !in_array($propertyName, $this->select)) $this->select[] = $propertyName;
  35. return $this;
  36. }
  37. public function from($from, $prefix = 't') {
  38. DBG::log([
  39. 'from instanceof Core_AclBase' => ($from instanceof Core_AclBase),
  40. 'from instanceof AntAclBase' => ($from instanceof AntAclBase),
  41. ], 'array', "\$from class(".get_class($from).")");
  42. if ($this->from) throw new Exception("Duplicate FROM");
  43. $this->from = $from;
  44. $this->_fromPrefix = $prefix;
  45. return $this;
  46. }
  47. public function where($params) {
  48. if (!empty($params['rawWhere'])) {
  49. $this->where[] = $params['rawWhere'];
  50. } else {
  51. DBG::log($params, 'array', "Where not supported");
  52. throw new Exception("Where not supported");
  53. }
  54. }
  55. public function join($join, $prefix, $params) {
  56. if (array_key_exists($prefix, $this->_joinPrefix)) throw new Exception("Prefix already used!");
  57. $this->_joinPrefix[$prefix] = $join;
  58. $this->_joinParams[$prefix] = $params;
  59. return $this;
  60. }
  61. public function orderBy($orderBy) { // TODO: ogc: order by ...
  62. return $this;
  63. }
  64. public function limit($limit) {
  65. $this->limit = (int)$limit;
  66. return $this;
  67. }
  68. public function offset($offset) {
  69. $this->offset = (int)$offset;
  70. return $this;
  71. }
  72. public function isInstance($instances) {
  73. $this->isInstances = (is_array($instances)) ? $instances : [ $instances ];
  74. return $this;
  75. }
  76. public function isNotInstance($instances) {
  77. $this->isNotInstances = (is_array($instances)) ? $instances : [ $instances ];
  78. return $this;
  79. }
  80. public function _parseJoinParams($params) {
  81. if (array_key_exists('rawJoin', $params)) return $params['rawJoin'];
  82. throw new Exception("Not implemented JOIN params '".json_encode($params)."'");
  83. }
  84. public function _getTableName($source) {
  85. if (is_scalar($source)) return $source;
  86. if ($source instanceof Core_AclBase) return $source->getRootTableName();
  87. throw new Exception("Not implemented FROM type '".get_class($source)."'");
  88. }
  89. public function execute() {
  90. $sql = $this->generateSql();
  91. DBG::log((array)$this, 'array', "AclQueryBuilder");
  92. return DB::getPDO()->fetchAll($sql);
  93. }
  94. public function generateSql() {
  95. if (!$this->from) throw new Exception("Missing FROM");
  96. $tableName = $this->_getTableName($this->from);
  97. if (!$tableName) throw new Exception("Missing FROM table name");
  98. $sqlPk = 'ID';
  99. if ($this->from instanceof Core_AclBase) $sqlPk = $this->from->getSqlPrimaryKeyField();
  100. $sqlJoin = [];
  101. foreach ($this->isInstances as $k => $ns) {
  102. $idInstance = ACL::getInstanceId($ns);
  103. $instanceTable = ACL::getInstanceTable($ns);
  104. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  105. $prefix = "is_inst_{$k}";
  106. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  107. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  108. // and {$prefix}.idInstance = {$idInstance}
  109. // )";
  110. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} in (
  111. select {$prefix}.pk
  112. from `{$instanceTable}` {$prefix}
  113. where {$prefix}.idInstance = {$idInstance}
  114. )";
  115. DBG::log("{$this->_fromPrefix}.{$sqlPk} in (
  116. select {$prefix}.pk
  117. from `{$instanceTable}` {$prefix}
  118. where {$prefix}.idInstance = {$idInstance}
  119. )", 'string', "\$this->where[] =");
  120. }
  121. foreach ($this->isNotInstances as $k => $ns) {
  122. $idInstance = ACL::getInstanceId($ns);
  123. $instanceTable = ACL::getInstanceTable($ns);
  124. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPk} and i.idInstance = {$idInstance}" ])
  125. $prefix = "is_inst_{$k}";
  126. // $sqlJoin[] = " inner join `{$joinName}` {$prefix} on (
  127. // {$prefix}.pk = {$this->_fromPrefix}.{$sqlPk}
  128. // and {$prefix}.idInstance = {$idInstance}
  129. // )";
  130. $this->where[] = "{$this->_fromPrefix}.{$sqlPk} not in (
  131. select {$prefix}.pk
  132. from `{$instanceTable}` {$prefix}
  133. where {$prefix}.idInstance = {$idInstance}
  134. )";
  135. }
  136. // join `{$instanceTable}` i on(i.pk = t.{$sqlPk} and i.idInstance = {$idInstance})
  137. foreach ($this->_joinPrefix as $prefix => $join) {
  138. $joinName = $this->_getTableName($join);
  139. $sqlJoin[] = " join `{$joinName}` {$prefix} on(" . $this->_parseJoinParams($this->_joinParams[$prefix]) . ")";
  140. }
  141. $sqlJoin = (!empty($sqlJoin)) ? implode("\n\t", $sqlJoin) : "";
  142. $sqlWhere = [];
  143. foreach ($this->where as $where) {
  144. $sqlWhere[] = $where;
  145. }
  146. $sqlWhere = (!empty($sqlWhere)) ? "where " . implode("\n\t and ", $sqlWhere) : '';
  147. $limit = ($this->limit < 0) ? 0 : $this->limit;
  148. $offset = ($this->offset < 0) ? 0 : $this->offset;
  149. $sqlLimit = ($limit > 0) ? "limit {$limit} offset {$offset}" : '';
  150. $sqlSelect = [];
  151. $sqlSelect[] = "{$this->_fromPrefix}.{$sqlPk}";
  152. if (in_array('*', $this->select)) $sqlSelect[] = "{$this->_fromPrefix}.*";
  153. if (in_array('@instances', $this->select)) {
  154. if (!($this->from instanceof Core_AclBase)) throw new Exception("select @instances allowed only for Acl object");
  155. $instanceTable = ACL::getInstanceTable($this->from->getNamespace());
  156. $sqlSelect[] = "
  157. (
  158. select GROUP_CONCAT(inst_conf.namespace)
  159. from `{$instanceTable}` inst_tbl
  160. join `CRM_INSTANCE_CONFIG` inst_conf on (inst_conf.id = inst_tbl.idInstance)
  161. where inst_tbl.pk = {$this->_fromPrefix}.{$sqlPk}
  162. ) as `@instances`
  163. ";
  164. }
  165. $sqlSelect = (!empty($sqlSelect)) ? implode(", ", $sqlSelect) : "{$this->_fromPrefix}.*";
  166. return "
  167. select {$sqlSelect}
  168. from `{$tableName}` {$this->_fromPrefix}
  169. {$sqlJoin}
  170. {$sqlWhere}
  171. {$sqlLimit}
  172. ";
  173. }
  174. }