Browse Source

refactoring

Maurits van der Schee 5 years ago
parent
commit
505492f3f1

+ 10
- 21
api.php View File

1402
             return '';
1402
             return '';
1403
         }
1403
         }
1404
         switch ($this->driver) {
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
 
1415
 
1416
     public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
1416
     public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
1417
     {
1417
     {
1418
+        if (count($columnOrdering)==0) {
1419
+            return '';
1420
+        }
1418
         $results = array();
1421
         $results = array();
1419
         foreach ($columnOrdering as $i => list($columnName, $ordering)) {
1422
         foreach ($columnOrdering as $i => list($columnName, $ordering)) {
1420
             $column = $table->getColumn($columnName);
1423
             $column = $table->getColumn($columnName);
1421
             $quotedColumnName = $this->quoteColumnName($column);
1424
             $quotedColumnName = $this->quoteColumnName($column);
1422
             $results[] = $quotedColumnName . ' ' . $ordering;
1425
             $results[] = $quotedColumnName . ' ' . $ordering;
1423
         }
1426
         }
1424
-        return implode(',', $results);
1427
+        return ' ORDER BY '.implode(',', $results);
1425
     }
1428
     }
1426
 
1429
 
1427
     public function getSelect(ReflectedTable $table, array $columnNames): String
1430
     public function getSelect(ReflectedTable $table, array $columnNames): String
1941
         return $stmt->fetchColumn(0);
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
     public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
1947
     public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
1959
     {
1948
     {
1960
         if ($limit == 0) {
1949
         if ($limit == 0) {
1967
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
1956
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
1968
         $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
1957
         $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
1969
         $offsetLimit = $this->columns->getOffsetLimit($offset, $limit);
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
         $stmt = $this->query($sql, $parameters);
1960
         $stmt = $this->query($sql, $parameters);
1972
         $records = $stmt->fetchAll();
1961
         $records = $stmt->fetchAll();
1973
         $this->converter->convertRecords($table, $columnNames, $records);
1962
         $this->converter->convertRecords($table, $columnNames, $records);
5032
             $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
5021
             $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
5033
         }
5022
         }
5034
         $condition = OrCondition::fromArray($conditions);
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
             $records[] = $record;
5025
             $records[] = $record;
5037
         }
5026
         }
5038
     }
5027
     }
5078
         $pkIds = implode(',', array_keys($pkValues));
5067
         $pkIds = implode(',', array_keys($pkValues));
5079
         $condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
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
         foreach ($records as $record) {
5071
         foreach ($records as $record) {
5083
             $val1 = $record[$fk1Name];
5072
             $val1 = $record[$fk1Name];
5084
             $val2 = $record[$fk2Name];
5073
             $val2 = $record[$fk2Name];

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

21
             return '';
21
             return '';
22
         }
22
         }
23
         switch ($this->driver) {
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
 
34
 
35
     public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
35
     public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
36
     {
36
     {
37
+        if (count($columnOrdering)==0) {
38
+            return '';
39
+        }
37
         $results = array();
40
         $results = array();
38
         foreach ($columnOrdering as $i => list($columnName, $ordering)) {
41
         foreach ($columnOrdering as $i => list($columnName, $ordering)) {
39
             $column = $table->getColumn($columnName);
42
             $column = $table->getColumn($columnName);
40
             $quotedColumnName = $this->quoteColumnName($column);
43
             $quotedColumnName = $this->quoteColumnName($column);
41
             $results[] = $quotedColumnName . ' ' . $ordering;
44
             $results[] = $quotedColumnName . ' ' . $ordering;
42
         }
45
         }
43
-        return implode(',', $results);
46
+        return ' ORDER BY '.implode(',', $results);
44
     }
47
     }
45
 
48
 
46
     public function getSelect(ReflectedTable $table, array $columnNames): String
49
     public function getSelect(ReflectedTable $table, array $columnNames): String

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

185
         return $stmt->fetchColumn(0);
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
     public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
188
     public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
203
     {
189
     {
204
         if ($limit == 0) {
190
         if ($limit == 0) {
211
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
197
         $whereClause = $this->conditions->getWhereClause($condition, $parameters);
212
         $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
198
         $orderBy = $this->columns->getOrderBy($table, $columnOrdering);
213
         $offsetLimit = $this->columns->getOffsetLimit($offset, $limit);
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
         $stmt = $this->query($sql, $parameters);
201
         $stmt = $this->query($sql, $parameters);
216
         $records = $stmt->fetchAll();
202
         $records = $stmt->fetchAll();
217
         $this->converter->convertRecords($table, $columnNames, $records);
203
         $this->converter->convertRecords($table, $columnNames, $records);

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

208
             $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
208
             $conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
209
         }
209
         }
210
         $condition = OrCondition::fromArray($conditions);
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
             $records[] = $record;
212
             $records[] = $record;
213
         }
213
         }
214
     }
214
     }
254
         $pkIds = implode(',', array_keys($pkValues));
254
         $pkIds = implode(',', array_keys($pkValues));
255
         $condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
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
         foreach ($records as $record) {
258
         foreach ($records as $record) {
259
             $val1 = $record[$fk1Name];
259
             $val1 = $record[$fk1Name];
260
             $val2 = $record[$fk2Name];
260
             $val2 = $record[$fk2Name];

Loading…
Cancel
Save