Skip to content

Commit 0079add

Browse files
author
rkr
committed
- It's not possible to add parameters to Insert::addExpr, Insert::updateExpr and Insert::addOrUpdateExpr.
Example: ``` $stmt = $insert ->into('test') ->addExpr('a=?', 'a') ->updateExpr('b=?', 'b') ->addOrUpdateExpr('c=?', 'c'); ```
1 parent c04073a commit 0079add

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/Builder/Insert.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,21 @@ public function addOrUpdate($field, $value) {
8484

8585
/**
8686
* @param string $str
87+
* @param string ...$params
8788
* @return $this
8889
*/
8990
public function addExpr($str) {
90-
$this->fields[] = $str;
91+
$this->fields[] = func_get_args();
9192
return $this;
9293
}
9394

9495
/**
9596
* @param string $str
97+
* @param string ...$params
9698
* @return $this
9799
*/
98100
public function updateExpr($str) {
99-
$this->update[] = $str;
101+
$this->update[] = func_get_args();
100102
return $this;
101103
}
102104

@@ -105,8 +107,8 @@ public function updateExpr($str) {
105107
* @return $this
106108
*/
107109
public function addOrUpdateExpr($str) {
108-
$this->addExpr($str);
109-
$this->updateExpr($str);
110+
$this->fields[] = func_get_args();
111+
$this->update[] = func_get_args();
110112
return $this;
111113
}
112114

@@ -285,4 +287,17 @@ private function clearValues(array $values) {
285287

286288
return $result;
287289
}
290+
291+
/**
292+
* @param string $expression
293+
* @param array $args
294+
* @return string
295+
*/
296+
private function formatExtraArgs($expression, $args) {
297+
if(count($args) > 1) {
298+
$args = array_slice($args, 1);
299+
$expression = $this->db()->quoteExpression($expression, $args);
300+
}
301+
return $expression;
302+
}
288303
}

src/Builder/Statement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function aliasReplacer() {
5050
}
5151

5252
/**
53-
* @return Database
53+
* @return MySQL
5454
*/
5555
protected function db() {
5656
return $this->db;

tests/Builder/InsertTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,14 @@ public function testMask() {
139139
->asString();
140140
$this->assertEquals("INSERT INTO\n\ttest\nSET\n\t`field1`='1'\nON DUPLICATE KEY UPDATE\n\t`field1`='1'\n", $sql);
141141
}
142+
143+
public function testExprWithParams() {
144+
$sql = TestInsert::create()
145+
->into('test')
146+
->addExpr('a=?', 'a')
147+
->updateExpr('b=?', 'b')
148+
->addOrUpdateExpr('c=?', 'c')
149+
->asString();
150+
$this->assertEquals("INSERT INTO\n\ttest\nSET\n\ta='a',\n\tb='b'\n", $sql);
151+
}
142152
}

0 commit comments

Comments
 (0)