Skip to content

Commit e848e00

Browse files
committed
Add RawSql to BaseConnection->escape()
1 parent cef5e53 commit e848e00

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

system/Database/BaseConnection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,10 @@ public function escape($str)
12381238
}
12391239

12401240
if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
1241+
if (is_a($str, RawSql::class)) {
1242+
return $str->__toString();
1243+
}
1244+
12411245
return "'" . $this->escapeString($str) . "'";
12421246
}
12431247

system/Database/Postgre/Connection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use CodeIgniter\Database\BaseConnection;
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
16+
use CodeIgniter\Database\RawSql;
1617
use ErrorException;
1718
use stdClass;
1819

@@ -181,6 +182,10 @@ public function escape($str)
181182
}
182183

183184
if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
185+
if (is_a($str, RawSql::class)) {
186+
return $str->__toString();
187+
}
188+
184189
return pg_escape_literal($this->connID, $str);
185190
}
186191

tests/system/Database/BaseConnectionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ public function testMagicGetMissing()
165165
$this->assertNull($db->foobar);
166166
}
167167

168+
public function testEscape()
169+
{
170+
$db = new MockConnection($this->options);
171+
172+
$stringArray = [' A simple string ', new RawSql('CURRENT_TIMESTAMP()'), false, null];
173+
174+
$escapedString = $db->escape($stringArray);
175+
176+
$this->assertSame("' A simple string '", $escapedString[0]);
177+
$this->assertSame('CURRENT_TIMESTAMP()', $escapedString[1]);
178+
$this->assertSame(0, $escapedString[2]);
179+
$this->assertSame('NULL', $escapedString[3]);
180+
}
181+
168182
/**
169183
* These tests are intended to confirm the current behavior.
170184
* We do not know if all of these are the correct behavior.

user_guide_src/source/database/call_function.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,18 @@ database result ID. The connection ID can be accessed using:
3737
The result ID can be accessed from within your result object, like this:
3838

3939
.. literalinclude:: call_function/004.php
40+
41+
$db->escape();
42+
============================
43+
44+
This function enables you to escape a string for database calls. The method
45+
is used by ``BaseBuilder`` for many built in functions. It accepts a string,
46+
array, object or ``CodeIgniter\Database\RawSql``. When ``RawSql`` is used
47+
the string is not escaped. This allows you to call SQL functions and
48+
constants.
49+
50+
.. literalinclude:: call_function/005.php
51+
52+
Here is an example using methods such as ``insert()`` to pass a SQL function.
53+
54+
.. literalinclude:: call_function/006.php
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
$stringArray = [' A simple string ', new RawSql('CURRENT_TIMESTAMP()'), false, null];
4+
5+
$escapedString = $db->escape($stringArray);
6+
7+
$this->assertSame("'A simple string'", $escapedString[0]); // adds quotes
8+
$this->assertSame('CURRENT_TIMESTAMP()', $escapedString[1]); // does not add quotes
9+
$this->assertSame(0, $escapedString[2]); // converts bool to 1 or 0
10+
$this->assertSame('NULL', $escapedString[3]); // null returns NULL without quotes
11+
$this->assertSame("'{braces}'", $escapedString[4]); // actual braces
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$data = [
4+
'id' => new RawSql('DEFAULT'),
5+
'title' => 'My title',
6+
'name' => 'My Name',
7+
'date' => '2022-01-01',
8+
'last_update' => new RawSql('CURRENT_TIMESTAMP()'),
9+
];
10+
11+
$builder->insert($data);
12+
/* Produces:
13+
INSERT INTO mytable (id, title, name, date, last_update)
14+
VALUES (DEFAULT, 'My title', 'My name', '2022-01-01', CURRENT_TIMESTAMP())
15+
*/

0 commit comments

Comments
 (0)