Skip to content

Commit e37d11c

Browse files
author
rkr
committed
- You can now set a DBExpr as a field-value which will not be quoted when a query is generated
1 parent 38549ae commit e37d11c

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

src/Builder/DBExpr.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Kir\MySQL\Builder;
4+
5+
class DBExpr {
6+
/** @var string */
7+
private $expression;
8+
9+
/**
10+
* @param $expression
11+
*/
12+
public function __construct($expression) {
13+
$this->expression = $expression;
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getExpression() {
20+
return $this->expression;
21+
}
22+
23+
/**
24+
* @return string
25+
*/
26+
public function __toString() {
27+
return $this->expression;
28+
}
29+
}

src/Databases/MySQL.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public function quoteExpression($expression, array $arguments = array()) {
151151
public function quote($value) {
152152
if(is_null($value)) {
153153
$result = 'NULL';
154+
} elseif($value instanceof Builder\DBExpr) {
155+
$result = $value->getExpression();
154156
} elseif($value instanceof Builder\Select) {
155157
$result = sprintf('(%s)', (string) $value);
156158
} elseif(is_array($value)) {

tests/Builder/DeleteTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public function testWhere() {
3838
$this->assertEquals("DELETE FROM\n\ttravis_test.test1\nWHERE\n\t(field1='1')\n\tAND\n\t(field2 != field1)\n", $sql);
3939
}
4040

41+
public function testDBExpr() {
42+
$sql = TestDelete::create()
43+
->from('travis#test1')
44+
->where('field1=?', new DBExpr('NOW()'))
45+
->asString();
46+
$this->assertEquals("DELETE FROM\n\ttravis_test.test1\nWHERE\n\t(field1=NOW())\n", $sql);
47+
}
48+
4149
public function testOrderBy() {
4250
$sql = TestDelete::create()
4351
->from('travis#test1')

tests/Builder/InsertTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,14 @@ public function testExprWithParams() {
149149
->asString();
150150
$this->assertEquals("INSERT INTO\n\ttest\nSET\n\ta='a',\n\tc='c'\nON DUPLICATE KEY UPDATE\n\tb='b',\n\tc='c'\n", $sql);
151151
}
152+
153+
public function testDBExpr() {
154+
$sql = TestInsert::create()
155+
->into('test')
156+
->addExpr('a=?', new DBExpr('NOW()'))
157+
->updateExpr('b=?', new DBExpr('NOW()'))
158+
->addOrUpdateExpr('c=?', new DBExpr('NOW()'))
159+
->asString();
160+
$this->assertEquals("INSERT INTO\n\ttest\nSET\n\ta=NOW(),\n\tc=NOW()\nON DUPLICATE KEY UPDATE\n\tb=NOW(),\n\tc=NOW()\n", $sql);
161+
}
152162
}

tests/Builder/SelectTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ public function testHaving() {
7474
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nHAVING\n\t(a+1<2)\n", $str);
7575
}
7676

77+
public function testDBExpr() {
78+
$str = TestSelect::create()
79+
->field('a')
80+
->from('t', 'test')
81+
->where('a=?', new DBExpr('NOW()'))
82+
->having('b=?', new DBExpr('NOW()'))
83+
->asString();
84+
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(a=NOW())\nHAVING\n\t(b=NOW())\n", $str);
85+
86+
$str = TestSelect::create()
87+
->field('a')
88+
->from('t', 'test')
89+
->where('a < ?', 1000)
90+
->asString();
91+
$this->assertEquals("SELECT\n\ta\nFROM\n\ttest t\nWHERE\n\t(a < '1000')\n", $str);
92+
}
93+
7794
public function testOrder() {
7895
$str = TestSelect::create()
7996
->field('a')

tests/Builder/UpdateTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,14 @@ public function testMask() {
130130
->asString();
131131
$this->assertEquals("UPDATE\n\ttest1\nSET\n\t`field1`='1'\nLIMIT\n\t10\n", $sql);
132132
}
133+
134+
public function testDBExpr() {
135+
$sql = TestUpdate::create()
136+
->table('test1')
137+
->set('field1', 1)
138+
->set('field2', new DBExpr('NOW()'))
139+
->limit(10)
140+
->asString();
141+
$this->assertEquals("UPDATE\n\ttest1\nSET\n\t`field1`='1',\n\t`field2`=NOW()\nLIMIT\n\t10\n", $sql);
142+
}
133143
}

0 commit comments

Comments
 (0)