Parcourir la source

updated AclQueryFeatures - parse recurence

Piotr Labudda il y a 9 ans
Parent
commit
331ada5a0a
2 fichiers modifiés avec 46 ajouts et 30 suppressions
  1. 29 11
      SE/se-lib/AclQueryBuilder.php
  2. 17 19
      SE/se-lib/AclQueryFeatures.php

+ 29 - 11
SE/se-lib/AclQueryBuilder.php

@@ -64,18 +64,36 @@ class AclQueryBuilder {
       , 'like', 'not like'
       , 'like', 'not like'
       , 'is not null', 'is null'
       , 'is not null', 'is null'
       , 'Intersects', 'GeometryType'
       , 'Intersects', 'GeometryType'
+      , 'or' // $value = [ comparisons for $fieldName ... or null ]
+      , 'and' // $value = [ comparisons for $fieldName ... or null ]
     ])) {
     ])) {
       throw new Exception("Not implemented comparisonSign '{$comparisonSign}'");
       throw new Exception("Not implemented comparisonSign '{$comparisonSign}'");
     }
     }
-    $sqlFieldName = $fieldName; // TODO: getSqlFieldName
+    $this->where[] = [$fieldName, $comparisonSign, $value];
+    return $this;
+  }
+  public function _generateWhereMain($where) { // @returns string
+    list($fieldName, $comparisonSign, $value) = $where;
+    $sqlFieldName = $fieldName; // TODO: getSqlFieldName // TODO: get sql field name with table prefix from join list to replace "{$this->_fromPrefix}.{$sqlFieldName}" below
     switch ($comparisonSign) {
     switch ($comparisonSign) {
-      case 'is not null': $this->where[] = "{$this->_fromPrefix}.{$sqlFieldName} is not null"; break;
-      case 'is null': $this->where[] = "{$this->_fromPrefix}.{$sqlFieldName} is null"; break;
-      case 'Intersects': $this->where[] = "Intersects(GeomFromText('{$value}'), {$this->_fromPrefix}.`{$sqlFieldName}`)=1"; break;
-      case 'GeometryType': $this->where[] = "GeometryType({$this->_fromPrefix}.`{$sqlFieldName}`)='{$value}'"; break;
-      default: $this->where[] = "{$this->_fromPrefix}.{$sqlFieldName} {$comparisonSign} " . DB::getPDO()->quote($value);
+      case 'is not null': return "{$this->_fromPrefix}.{$sqlFieldName} is not null";
+      case 'is null': return "{$this->_fromPrefix}.{$sqlFieldName} is null";
+      case 'Intersects': return "Intersects(GeomFromText('{$value}'), {$this->_fromPrefix}.`{$sqlFieldName}`)=1";
+      case 'GeometryType': return "GeometryType({$this->_fromPrefix}.`{$sqlFieldName}`)='{$value}'";
+      case 'or': return $this->_generateWhereBlock($where);
+      case 'and': return $this->_generateWhereBlock($where);
+      default: return "{$this->_fromPrefix}.{$sqlFieldName} {$comparisonSign} " . DB::getPDO()->quote($value);
     }
     }
-    return $this;
+    return null;
+  }
+  public function _generateWhereBlock($where) { // @returns string
+    list($fieldName, $sqlGlue, $list) = $where;
+    $sqlList = array_filter(
+      array_map([$this, '_generateWhereMain'], $list),
+      'is_string'
+    );
+    if (1 === count($sqlList)) return $sqlList[0];
+    else return "( " . implode(" {$sqlGlue} ", $sqlList) . " )";
   }
   }
   public function whereIsNotNull($fieldName) {
   public function whereIsNotNull($fieldName) {
     $this->where[] = "{$this->_fromPrefix}.{$fieldName} is not null";
     $this->where[] = "{$this->_fromPrefix}.{$fieldName} is not null";
@@ -236,10 +254,10 @@ class AclQueryBuilder {
 
 
     $sqlJoin = (!empty($sqlJoin)) ? implode("\n\t", $sqlJoin) : "";
     $sqlJoin = (!empty($sqlJoin)) ? implode("\n\t", $sqlJoin) : "";
 
 
-    $sqlWhere = [];
-    foreach ($this->where as $where) {
-      $sqlWhere[] = $where;
-    }
+    $sqlWhere = array_filter(
+      array_map([$this, '_generateWhereMain'], $this->where),
+      'is_string'
+    );
     $sqlWhere = (!empty($sqlWhere)) ? "where " . implode("\n\t and ", $sqlWhere) : '';
     $sqlWhere = (!empty($sqlWhere)) ? "where " . implode("\n\t and ", $sqlWhere) : '';
 
 
     $limit = ($this->limit < 0) ? 0 : $this->limit;
     $limit = ($this->limit < 0) ? 0 : $this->limit;

+ 17 - 19
SE/se-lib/AclQueryFeatures.php

@@ -1,6 +1,7 @@
 <?php
 <?php
 
 
 Lib::loadClass('ACL');
 Lib::loadClass('ACL');
+Lib::loadClass('SqlQueryWhereBuilder');
 
 
 // usage: (Acl class)::buildQuery($params): return new AclQueryFeatures($this, $params);
 // usage: (Acl class)::buildQuery($params): return new AclQueryFeatures($this, $params);
 //        (view): $queryFeatures = $acl->buildQuery($params);
 //        (view): $queryFeatures = $acl->buildQuery($params);
@@ -24,7 +25,7 @@ class AclQueryFeatures {
     // TODO: _legacyMode = ($from instanceof simple schema or another programmed objects)
     // TODO: _legacyMode = ($from instanceof simple schema or another programmed objects)
   }
   }
 
 
-  public function parseQueryValue($searchQuery, $fieldType = 'xsd:string') {
+  public function parseQueryValue($fieldName, $searchQuery, $fieldType = 'xsd:string') {
     if ('!NULL' === $searchQuery) return ['is not null', null];
     if ('!NULL' === $searchQuery) return ['is not null', null];
     if ('IS NOT NULL' === $searchQuery) return ['is not null', null];
     if ('IS NOT NULL' === $searchQuery) return ['is not null', null];
     if ('NULL' === $searchQuery) return ['is null', null];
     if ('NULL' === $searchQuery) return ['is null', null];
@@ -61,23 +62,20 @@ class AclQueryFeatures {
           default:  return ['not like', substr($searchQuery, 1)];
           default:  return ['not like', substr($searchQuery, 1)];
         }
         }
       default: {
       default: {
-        if (false !== strpos($searchQuery, '%')) return ['like', $searchQuery];
-        if ('xsd:number' === $fieldType) return ['=', $searchQuery];
-        // if ($acl->isColTypeNumber($fieldName)) return ['=', $searchQuery];
-        // else {
-        //   $queryWhereBuilder = new SqlQueryWhereBuilder();
-        //   $searchWords = $queryWhereBuilder->splitQueryToWords($v);
-        //   $sqlWhereWords = array();
-        //   if (!empty($searchWords)) {
-        //     foreach ($searchWords as $word) {
-        //       $sqlWord = $this->_db->_($word);
-        //       $sqlWhereWords[] = "t.`{$fldName}` like '%{$sqlWord}%'";
-        //     }
-        //   }
-        //   if (!empty($searchWords)) {
-        //     $sql_where_and[] = "(" . implode(" and ", $sqlWhereWords) . ")";
-        //   }
-        // }
+        switch ($fieldType) {
+          case 'xsd:number':
+          case 'xsd:integer': return ['=', $searchQuery];
+          default: {
+            if (false !== strpos($searchQuery, '%')) return ['like', $searchQuery];
+            $queryWhereBuilder = new SqlQueryWhereBuilder();
+            return ['and'
+              , array_map(function ($word) use ($fieldName) {
+                  return [$fieldName, 'like', "%{$word}%"];
+                }, $queryWhereBuilder->splitQueryToWords($searchQuery)
+              )
+            ];
+          }
+        }
         return ['=', $searchQuery];
         return ['=', $searchQuery];
       }
       }
     }
     }
@@ -147,7 +145,7 @@ class AclQueryFeatures {
       if ('f_' === substr($k, 0, 2) && strlen($k) > 3) {
       if ('f_' === substr($k, 0, 2) && strlen($k) > 3) {
         $fieldName = substr($k, 2);
         $fieldName = substr($k, 2);
         $fieldType = $this->_acl->getXsdFieldType($fieldName);
         $fieldType = $this->_acl->getXsdFieldType($fieldName);
-        list($comparisonSign, $value) = $this->parseQueryValue($v, $fieldType);
+        list($comparisonSign, $value) = $this->parseQueryValue($fieldName, $v, $fieldType);
         DBG::log([ $fieldName, $fieldType, $comparisonSign, $value ], 'array', "parseQueryValue");
         DBG::log([ $fieldName, $fieldType, $comparisonSign, $value ], 'array', "parseQueryValue");
         $this->_query->where($fieldName, $comparisonSign, $value);
         $this->_query->where($fieldName, $comparisonSign, $value);
       } else if ('sf_' === substr($k, 0, 3) && strlen($k) > 4) {
       } else if ('sf_' === substr($k, 0, 3) && strlen($k) > 4) {