Skip to content

Commit f889fce

Browse files
committed
Add SQL order clause
1 parent 09b29dd commit f889fce

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/TgDatabase/Order.php

+20-12
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
*/
88
class Order {
99

10-
public function __construct($propertyName, $ascending = TRUE) {
10+
public function __construct($propertyName, $ascending = TRUE, $isSql = FALSE) {
1111
$this->propertyName = $propertyName;
1212
$this->ascending = $ascending;
1313
$this->ignoreCase = FALSE;
14+
$this->sql = $isSql;
1415
}
1516

1617
/*
@@ -31,16 +32,20 @@ public function toSqlString($localQuery, $overallQuery) {
3132
$lower = $this->ignoreCase;
3233

3334
$rc = '';
34-
if ($lower) {
35-
$rc .= 'LOWER(';
36-
}
37-
$rc .= $overallQuery->quoteName($localQuery->getAlias(), $this->propertyName);
38-
if ($lower) {
39-
$rc .= ')';
40-
}
35+
if (!$this->sql) {
36+
if ($lower) {
37+
$rc .= 'LOWER(';
38+
}
39+
$rc .= $overallQuery->quoteName($localQuery->getAlias(), $this->propertyName);
40+
if ($lower) {
41+
$rc .= ')';
42+
}
4143

42-
if (!$this->ascending) {
43-
$rc .= ' DESC';
44+
if (!$this->ascending) {
45+
$rc .= ' DESC';
46+
}
47+
} else {
48+
$rc = $this->propertyName;
4449
}
4550
return $rc;
4651
}
@@ -53,6 +58,10 @@ public static function desc($propertyName) {
5358
return new Order($propertyName, FALSE);
5459
}
5560

61+
public static function sql($sql) {
62+
return new Order($sql, TRUE, TRUE);
63+
}
64+
5665
/**
5766
* Creates the order object.
5867
* @param mixed $orders - string or order object (fieldname ASC/DESC)
@@ -67,9 +76,8 @@ public static function toOrder($order) {
6776
$lastWord = strtolower(substr($s, $pos+1));
6877
if ($lastWord == 'desc') return Order::desc(substr($s, 0, $pos));
6978
if ($lastWord == 'asc') return Order::asc(substr($s, 0, $pos));
70-
return Order::asc($s);
7179
}
72-
return Order::asc($s);
80+
return Order::sql($s);
7381
}
7482

7583
}

0 commit comments

Comments
 (0)