Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ $result2 = $result->where($array) // for each key-value pair, call $result->w
$result2 = $result->whereNot($array) // for each key-value pair, call $result->whereNot($key, $value)
```

## Selected columns, Order and Limit
## Selected columns, Group, Order and Limit

Note that you can order association results, but you cannot use `LIMIT` on them.

```php
$result2 = $result->select($expr) // identfiers NOT escaped, so expressions are possible
// multiple calls are joined with a comma

// $column group by
$result2 = $result->groupBy($column);

// $column will be escaped
$result2 = $result->orderBy($column);
$result2 = $result->orderBy($column, 'ASC');
Expand Down
11 changes: 9 additions & 2 deletions src/LessQL/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ public function setIdentifierDelimiter($d)
* @param string $table
* @param mixed $exprs
* @param array $where
* @param array $groupBy
* @param array $orderBy
* @param int|null $limitCount
* @param int|null $limitOffset
Expand All @@ -464,6 +465,7 @@ public function select($table, $options = array())
$options = array_merge(array(
'expr' => null,
'where' => array(),
'groupBy' => array(),
'orderBy' => array(),
'limitCount' => null,
'limitOffset' => null,
Expand All @@ -484,7 +486,7 @@ public function select($table, $options = array())
$table = $this->rewriteTable($table);
$query .= " FROM " . $this->quoteIdentifier($table);

$query .= $this->getSuffix($options['where'], $options['orderBy'], $options['limitCount'], $options['limitOffset']);
$query .= $this->getSuffix($options['where'], $options['groupBy'], $options['orderBy'], $options['limitCount'], $options['limitOffset']);

$this->onQuery($query, $options['params']);

Expand Down Expand Up @@ -762,19 +764,24 @@ public function delete($table, $where = array(), $params = array())
* Return WHERE/LIMIT/ORDER suffix for queries
*
* @param array $where
* @param array $groupBy
* @param array $orderBy
* @param int|null $limitCount
* @param int|null $limitOffset
* @return string
*/
public function getSuffix($where, $orderBy = array(), $limitCount = null, $limitOffset = null)
public function getSuffix($where,$groupBy = array(), $orderBy = array(), $limitCount = null, $limitOffset = null)
{
$suffix = "";

if (!empty($where)) {
$suffix .= " WHERE (" . implode(") AND (", $where) . ")";
}

if (!empty($groupBy)) {
$suffix .= " GROUP BY " . implode(", ", $groupBy);
}

if (!empty($orderBy)) {
$suffix .= " ORDER BY " . implode(", ", $orderBy);
}
Expand Down