Skip to content

Commit db17f8b

Browse files
committed
Move user guide and move test to live
1 parent e848e00 commit db17f8b

File tree

8 files changed

+40
-50
lines changed

8 files changed

+40
-50
lines changed

tests/system/Database/BaseConnectionTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,6 @@ 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-
182168
/**
183169
* These tests are intended to confirm the current behavior.
184170
* We do not know if all of these are the correct behavior.

tests/system/Database/Live/EscapeTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Database\Live;
1313

14+
use CodeIgniter\Database\RawSql;
1415
use CodeIgniter\Test\CIUnitTestCase;
1516
use CodeIgniter\Test\DatabaseTestTrait;
1617

@@ -78,4 +79,22 @@ public function testEscapeLikeStringDirect()
7879
$this->expectNotToPerformAssertions();
7980
}
8081
}
82+
83+
public function testEscapeStringArray()
84+
{
85+
$stringArray = [' A simple string ', new RawSql('CURRENT_TIMESTAMP()'), false, null];
86+
87+
$escapedString = $this->db->escape($stringArray);
88+
89+
$this->assertSame("' A simple string '", $escapedString[0]);
90+
$this->assertSame('CURRENT_TIMESTAMP()', $escapedString[1]);
91+
92+
if ($this->db->DBDriver === 'Postgre') {
93+
$this->assertSame('FALSE', $escapedString[2]);
94+
} else {
95+
$this->assertSame(0, $escapedString[2]);
96+
}
97+
98+
$this->assertSame('NULL', $escapedString[3]);
99+
}
81100
}

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Enhancements
4343
- Added ``spark filter:check`` command to check the filters for a route. See :ref:`Controller Filters <spark-filter-check>` for the details.
4444
- Now **Encryption** can decrypt data encrypted with CI3's Encryption. See :ref:`encryption-compatible-with-ci3`.
4545
- Now ``spark routes`` command shows route names. See :ref:`URI Routing <routing-spark-routes>`.
46+
- ``BaseConnection::escape()`` now excludes the ``RawSql`` data type. This allows passing SQL strings into data. See :ref:`database-queries-db-escape`.
4647

4748
Changes
4849
*******

user_guide_src/source/database/call_function.rst

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,3 @@ 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

user_guide_src/source/database/call_function/005.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

user_guide_src/source/database/queries.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,17 @@ this:
102102
1. $db->escape()
103103
================
104104

105-
This function determines the data type so
106-
that it can escape only string data. It also automatically adds
107-
single quotes around the data so you don't have to:
105+
This function determines the data type so that it can escape only string
106+
data. It also automatically adds single quotes around the data so you
107+
don't have to:
108108

109109
.. literalinclude:: queries/009.php
110110

111+
When ``RawSql`` is used the string is not escaped. This allows you to call
112+
SQL functions and constants:
113+
114+
.. literalinclude:: queries/030.php
115+
111116
2. $db->escapeString()
112117
======================
113118

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
<?php
22

3-
$data = [
4-
'title' => 'My title',
5-
'name' => 'My Name',
6-
'date' => 'My date',
7-
];
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+
];
810

9-
$builder->insert($data);
10-
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
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)