Skip to content

Commit ad9e0fe

Browse files
committed
#14 - Add SQL restriction and projection as fallbacks
1 parent 8027c28 commit ad9e0fe

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace TgDatabase\Criterion;
4+
5+
use TgDatabase\Criteria;
6+
use TgDatabase\Criterion;
7+
8+
class SqlExpression implements Criterion {
9+
10+
public function __construct($sql) {
11+
$this->sql = $sql;
12+
}
13+
14+
/**
15+
* Render the SQL fragment.
16+
* @param Criteria $localCriteria - local criteria object (e.g. subquery)
17+
* @param Criteria $overallCriteria - overall criteria object
18+
* @return string - the SQL fragment representing this criterion.
19+
*/
20+
public function toSqlString($localCriteria, $overallCriteria) {
21+
return $this->sql;
22+
}
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace TgDatabase\Criterion;
4+
5+
use TgDatabase\Criteria;
6+
use TgDatabase\Projection;
7+
8+
class SqlProjection implements Projection {
9+
10+
public function __construct($sql) {
11+
$this->sql = $sql;
12+
}
13+
14+
/**
15+
* Render the SQL fragment.
16+
* @param Criteria $localCriteria - local criteria object (e.g. subquery)
17+
* @param Criteria $overallCriteria - overall criteria object
18+
* @return string - the SQL fragment representing this criterion.
19+
*/
20+
public function toSqlString($localCriteria, $overallCriteria) {
21+
return $this->sql;
22+
}
23+
24+
}

src/TgDatabase/Projections.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use TgDatabase\Criterion\CountProjection;
88
use TgDatabase\Criterion\AggregateProjection;
99
use TgDatabase\Criterion\AliasedProjection;
10+
use TgDatabase\Criterion\SqlProjection;
1011

1112
class Projections {
1213

@@ -45,4 +46,10 @@ public static function sum($propertyName) {
4546
public static function alias($projection, $alias) {
4647
return new AliasedProjection($projection, $alias);
4748
}
49+
50+
public static function sql($sql) {
51+
return new SqlProjection($sql);
52+
}
53+
54+
4855
}

src/TgDatabase/Restrictions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use TgDatabase\Criterion\NotInExpression;
1111
use TgDatabase\Criterion\PropertyExpression;
1212
use TgDatabase\Criterion\BetweenExpression;
13+
use TgDatabase\Criterion\SqlExpression;
1314

1415
/**
1516
* Provides the built-in citerions.
@@ -172,5 +173,12 @@ public static function leProperty($propertyName1, $propertyName2) {
172173
return new PropertyExpression($propertyName1, $propertyName2, '<=');
173174
}
174175

176+
/**
177+
* Apply a SQL constraint. (Exists as fallback to enable other expressions not supported yet)
178+
*/
179+
public static function sql($sql) {
180+
return new SqlExpression($sql);
181+
}
175182

183+
176184
}

0 commit comments

Comments
 (0)