Skip to content

Commit 505492f

Browse files
committed
refactoring
1 parent 6861fc2 commit 505492f

File tree

4 files changed

+20
-42
lines changed

4 files changed

+20
-42
lines changed

api.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,9 +1402,9 @@ public function getOffsetLimit(int $offset, int $limit): String
14021402
return '';
14031403
}
14041404
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";
14081408
}
14091409
}
14101410

@@ -1415,13 +1415,16 @@ private function quoteColumnName(ReflectedColumn $column): String
14151415

14161416
public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
14171417
{
1418+
if (count($columnOrdering)==0) {
1419+
return '';
1420+
}
14181421
$results = array();
14191422
foreach ($columnOrdering as $i => list($columnName, $ordering)) {
14201423
$column = $table->getColumn($columnName);
14211424
$quotedColumnName = $this->quoteColumnName($column);
14221425
$results[] = $quotedColumnName . ' ' . $ordering;
14231426
}
1424-
return implode(',', $results);
1427+
return ' ORDER BY '.implode(',', $results);
14251428
}
14261429

14271430
public function getSelect(ReflectedTable $table, array $columnNames): String
@@ -1941,20 +1944,6 @@ public function selectCount(ReflectedTable $table, Condition $condition): int
19411944
return $stmt->fetchColumn(0);
19421945
}
19431946

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-
19581947
public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
19591948
{
19601949
if ($limit == 0) {
@@ -1967,7 +1956,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
19671956
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
19681957
$orderBy = $this->columns->getOrderBy($table, $columnOrdering);
19691958
$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;
19711960
$stmt = $this->query($sql, $parameters);
19721961
$records = $stmt->fetchAll();
19731962
$this->converter->convertRecords($table, $columnNames, $records);
@@ -5032,7 +5021,7 @@ private function addPkRecords(ReflectedTable $t1, ReflectedTable $t2, array $pkV
50325021
$conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
50335022
}
50345023
$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) {
50365025
$records[] = $record;
50375026
}
50385027
}
@@ -5078,7 +5067,7 @@ private function getHabtmEmptyValues(ReflectedTable $t1, ReflectedTable $t2, Ref
50785067
$pkIds = implode(',', array_keys($pkValues));
50795068
$condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
50805069

5081-
$records = $db->selectAllUnordered($t3, $columnNames, $condition);
5070+
$records = $db->selectAll($t3, $columnNames, $condition, array(), 0, -1);
50825071
foreach ($records as $record) {
50835072
$val1 = $record[$fk1Name];
50845073
$val2 = $record[$fk2Name];

src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function getOffsetLimit(int $offset, int $limit): String
2121
return '';
2222
}
2323
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";
2727
}
2828
}
2929

@@ -34,13 +34,16 @@ private function quoteColumnName(ReflectedColumn $column): String
3434

3535
public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
3636
{
37+
if (count($columnOrdering)==0) {
38+
return '';
39+
}
3740
$results = array();
3841
foreach ($columnOrdering as $i => list($columnName, $ordering)) {
3942
$column = $table->getColumn($columnName);
4043
$quotedColumnName = $this->quoteColumnName($column);
4144
$results[] = $quotedColumnName . ' ' . $ordering;
4245
}
43-
return implode(',', $results);
46+
return ' ORDER BY '.implode(',', $results);
4447
}
4548

4649
public function getSelect(ReflectedTable $table, array $columnNames): String

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,6 @@ public function selectCount(ReflectedTable $table, Condition $condition): int
185185
return $stmt->fetchColumn(0);
186186
}
187187

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-
202188
public function selectAll(ReflectedTable $table, array $columnNames, Condition $condition, array $columnOrdering, int $offset, int $limit): array
203189
{
204190
if ($limit == 0) {
@@ -211,7 +197,7 @@ public function selectAll(ReflectedTable $table, array $columnNames, Condition $
211197
$whereClause = $this->conditions->getWhereClause($condition, $parameters);
212198
$orderBy = $this->columns->getOrderBy($table, $columnOrdering);
213199
$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;
215201
$stmt = $this->query($sql, $parameters);
216202
$records = $stmt->fetchAll();
217203
$this->converter->convertRecords($table, $columnNames, $records);

src/Tqdev/PhpCrudApi/Record/RelationJoiner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private function addPkRecords(ReflectedTable $t1, ReflectedTable $t2, array $pkV
208208
$conditions[] = new ColumnCondition($fk, 'in', $pkValueKeys);
209209
}
210210
$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) {
212212
$records[] = $record;
213213
}
214214
}
@@ -254,7 +254,7 @@ private function getHabtmEmptyValues(ReflectedTable $t1, ReflectedTable $t2, Ref
254254
$pkIds = implode(',', array_keys($pkValues));
255255
$condition = new ColumnCondition($t3->getColumn($fk1Name), 'in', $pkIds);
256256

257-
$records = $db->selectAllUnordered($t3, $columnNames, $condition);
257+
$records = $db->selectAll($t3, $columnNames, $condition, array(), 0, -1);
258258
foreach ($records as $record) {
259259
$val1 = $record[$fk1Name];
260260
$val2 = $record[$fk2Name];

0 commit comments

Comments
 (0)