Browse Source

refactoring

Maurits van der Schee 5 years ago
parent
commit
505492f3f1

+ 10
- 21
api.php View File

@@ -1402,9 +1402,9 @@ class ColumnsBuilder
1402 1402
             return '';
1403 1403
         }
1404 1404
         switch ($this->driver) {
1405
-            case 'mysql':return "LIMIT $offset, $limit";
1406
-            case 'pgsql':return "LIMIT $limit OFFSET $offset";
1407
-            case 'sqlsrv':return "OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
1405
+            case 'mysql':return " LIMIT $offset, $limit";
1406
+            case 'pgsql':return " LIMIT $limit OFFSET $offset";
1407
+            case 'sqlsrv':return " OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
1408 1408
         }
1409 1409
     }
1410 1410
 
@@ -1415,13 +1415,16 @@ class ColumnsBuilder
1415 1415
 
1416 1416
     public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
1417 1417
     {
1418
+        if (count($columnOrdering)==0) {
1419
+            return '';
1420
+        }
1418 1421
         $results = array();
1419 1422
         foreach ($columnOrdering as $i => list($columnName, $ordering)) {
1420 1423
             $column = $table->getColumn($columnName);
1421 1424
             $quotedColumnName = $this->quoteColumnName($column);
1422 1425
             $results[] = $quotedColumnName . ' ' . $ordering;
1423 1426
         }
1424
-        return implode(',', $results);
1427
+        return ' ORDER BY '.implode(',', $results);
1425 1428
     }
1426 1429
 
1427 1430
     public function getSelect(ReflectedTable $table, array $columnNames): String
@@ -1941,20 +1944,6 @@ class GenericDB
1941 1944
         return $stmt->fetchColumn(0);
1942 1945
     }
1943 1946
 
1944
-    public function selectAllUnordered(ReflectedTable $table, array $columnNames, Condition $condition): array
1945
-    {
1946
-        $selectColumns = $this->columns->getSelect($table, $columnNames);
1947
-        $tableName = $table->getName();
1948
-        $condition = $this->addMiddlewareConditions($tableName, $condition);
1949
-        $parameters = array();
1950
-        $whereClause = $this->conditions->getWhereClause($condition, $parameters);
1951
-        $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause;
1952
-        $stmt = $this->query($sql, $parameters);
1953
-        $records = $stmt->fetchAll();
1954
-        $this->converter->convertRecords($table, $columnNames, $records);
1955
-        return $records;
1956
-    }
1957
-
1958 1947
     public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
1959 1948
     {
1960 1949
         if ($limit == 0) {
@@ -1967,7 +1956,7 @@ class GenericDB
1967 1956
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
1968 1957
         $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
1969 1958
         $offsetLimit = $this->columns->getOffsetLimit($offset, $limit);
1970
-        $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause . ' ORDER BY ' . $orderBy . ' ' . $offsetLimit;
1959
+        $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause . $orderBy . $offsetLimit;
1971 1960
         $stmt = $this->query($sql, $parameters);
1972 1961
         $records = $stmt->fetchAll();
1973 1962
         $this->converter->convertRecords($table, $columnNames, $records);
@@ -5032,7 +5021,7 @@ class RelationJoiner
5032 5021
             $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
5033 5022
         }
5034 5023
         $condition = OrCondition::fromArray($conditions);
5035
-        foreach ($db->selectAllUnordered($t2, $columnNames, $condition) as $record) {
5024
+        foreach ($db->selectAll($t2, $columnNames, $condition, array(), 0, -1) as $record) {
5036 5025
             $records[] = $record;
5037 5026
         }
5038 5027
     }
@@ -5078,7 +5067,7 @@ class RelationJoiner
5078 5067
         $pkIds = implode(',', array_keys($pkValues));
5079 5068
         $condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
5080 5069
 
5081
-        $records = $db->selectAllUnordered($t3, $columnNames, $condition);
5070
+        $records = $db->selectAll($t3, $columnNames, $condition, array(), 0, -1);
5082 5071
         foreach ($records as $record) {
5083 5072
             $val1 = $record[$fk1Name];
5084 5073
             $val2 = $record[$fk2Name];

+ 7
- 4
src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php View File

@@ -21,9 +21,9 @@ class ColumnsBuilder
21 21
             return '';
22 22
         }
23 23
         switch ($this->driver) {
24
-            case 'mysql':return "LIMIT $offset, $limit";
25
-            case 'pgsql':return "LIMIT $limit OFFSET $offset";
26
-            case 'sqlsrv':return "OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
24
+            case 'mysql':return " LIMIT $offset, $limit";
25
+            case 'pgsql':return " LIMIT $limit OFFSET $offset";
26
+            case 'sqlsrv':return " OFFSET $offset ROWS FETCH NEXT $limit ROWS ONLY";
27 27
         }
28 28
     }
29 29
 
@@ -34,13 +34,16 @@ class ColumnsBuilder
34 34
 
35 35
     public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
36 36
     {
37
+        if (count($columnOrdering)==0) {
38
+            return '';
39
+        }
37 40
         $results = array();
38 41
         foreach ($columnOrdering as $i => list($columnName, $ordering)) {
39 42
             $column = $table->getColumn($columnName);
40 43
             $quotedColumnName = $this->quoteColumnName($column);
41 44
             $results[] = $quotedColumnName . ' ' . $ordering;
42 45
         }
43
-        return implode(',', $results);
46
+        return ' ORDER BY '.implode(',', $results);
44 47
     }
45 48
 
46 49
     public function getSelect(ReflectedTable $table, array $columnNames): String

+ 1
- 15
src/Tqdev/PhpCrudApi/Database/GenericDB.php View File

@@ -185,20 +185,6 @@ class GenericDB
185 185
         return $stmt->fetchColumn(0);
186 186
     }
187 187
 
188
-    public function selectAllUnordered(ReflectedTable $table, array $columnNames, Condition $condition): array
189
-    {
190
-        $selectColumns = $this->columns->getSelect($table, $columnNames);
191
-        $tableName = $table->getName();
192
-        $condition = $this->addMiddlewareConditions($tableName, $condition);
193
-        $parameters = array();
194
-        $whereClause = $this->conditions->getWhereClause($condition, $parameters);
195
-        $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause;
196
-        $stmt = $this->query($sql, $parameters);
197
-        $records = $stmt->fetchAll();
198
-        $this->converter->convertRecords($table, $columnNames, $records);
199
-        return $records;
200
-    }
201
-
202 188
     public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
203 189
     {
204 190
         if ($limit == 0) {
@@ -211,7 +197,7 @@ class GenericDB
211 197
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
212 198
         $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
213 199
         $offsetLimit = $this->columns->getOffsetLimit($offset, $limit);
214
-        $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause . ' ORDER BY ' . $orderBy . ' ' . $offsetLimit;
200
+        $sql = 'SELECT ' . $selectColumns . ' FROM "' . $tableName . '"' . $whereClause . $orderBy . $offsetLimit;
215 201
         $stmt = $this->query($sql, $parameters);
216 202
         $records = $stmt->fetchAll();
217 203
         $this->converter->convertRecords($table, $columnNames, $records);

+ 2
- 2
src/Tqdev/PhpCrudApi/Record/RelationJoiner.php View File

@@ -208,7 +208,7 @@ class RelationJoiner
208 208
             $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
209 209
         }
210 210
         $condition = OrCondition::fromArray($conditions);
211
-        foreach ($db->selectAllUnordered($t2, $columnNames, $condition) as $record) {
211
+        foreach ($db->selectAll($t2, $columnNames, $condition, array(), 0, -1) as $record) {
212 212
             $records[] = $record;
213 213
         }
214 214
     }
@@ -254,7 +254,7 @@ class RelationJoiner
254 254
         $pkIds = implode(',', array_keys($pkValues));
255 255
         $condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
256 256
 
257
-        $records = $db->selectAllUnordered($t3, $columnNames, $condition);
257
+        $records = $db->selectAll($t3, $columnNames, $condition, array(), 0, -1);
258 258
         foreach ($records as $record) {
259 259
             $val1 = $record[$fk1Name];
260 260
             $val2 = $record[$fk2Name];

Loading…
Cancel
Save