-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add RawSql to BaseConnection->escape() #6332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bece503
Add RawSql to BaseConnection->escape()
sclubricants baa49b3
Move user guide and move test to live
sclubricants 447e655
Fixes
sclubricants d0023b1
remove documentation and add notes
sclubricants dc76fea
fix objectToArray()
sclubricants accdfee
Update BaseBuilder.php
sclubricants c6e7bd3
Create RawSqlTest.php
sclubricants d218e86
fix sql
sclubricants 87af5f8
Fix logic
sclubricants ad3e088
fix is_a to instanceof
sclubricants f11edae
add testInsertObjectWithRawSql()
sclubricants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Database\Live\MySQLi; | ||
|
||
use CodeIgniter\Database\RawSql; | ||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\DatabaseTestTrait; | ||
use stdclass; | ||
use Tests\Support\Database\Seeds\CITestSeeder; | ||
|
||
/** | ||
* @group DatabaseLive | ||
* | ||
* @internal | ||
*/ | ||
final class RawSqlTest extends CIUnitTestCase | ||
{ | ||
use DatabaseTestTrait; | ||
|
||
protected $refresh = true; | ||
protected $seed = CITestSeeder::class; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
if ($this->db->DBDriver !== 'MySQLi') { | ||
$this->markTestSkipped('Only MySQLi has its own implementation.'); | ||
} else { | ||
$this->addSqlFunction(); | ||
} | ||
} | ||
|
||
protected function addSqlFunction() | ||
{ | ||
$this->db->query('DROP FUNCTION IF EXISTS setDateTime'); | ||
|
||
$sql = "CREATE FUNCTION setDateTime ( setDate varchar(20) ) | ||
RETURNS DATETIME | ||
READS SQL DATA | ||
DETERMINISTIC | ||
BEGIN | ||
RETURN CONVERT(CONCAT(setDate,' ','01:01:11'), DATETIME); | ||
END;"; | ||
|
||
$this->db->query($sql); | ||
} | ||
|
||
public function testRawSqlUpdateObject() | ||
{ | ||
$data = []; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
sclubricants marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$row->created_at = new RawSql("setDateTime('2022-01-01')"); | ||
$data[] = $row; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-01-01')"); | ||
$data[] = $row; | ||
|
||
$this->db->table('user')->updateBatch($data, 'email'); | ||
|
||
$row->created_at = new RawSql("setDateTime('2022-01-11')"); | ||
|
||
$this->db->table('user')->update($row, "email = '[email protected]'"); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-01-01 01:01:11']); | ||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-01-11 01:01:11']); | ||
} | ||
|
||
public function testRawSqlSetUpdateObject() | ||
{ | ||
$data = []; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-02-01')"); | ||
$data[] = $row; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-02-01')"); | ||
$data[] = $row; | ||
|
||
$this->db->table('user')->setUpdateBatch($data, 'email')->updateBatch(null, 'email'); | ||
|
||
$row->created_at = new RawSql("setDateTime('2022-02-11')"); | ||
|
||
$this->db->table('user')->set($row)->update(null, "email = '[email protected]'"); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-02-01 01:01:11']); | ||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-02-11 01:01:11']); | ||
} | ||
|
||
public function testRawSqlUpdateArray() | ||
{ | ||
$data = [ | ||
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-03-01')")], | ||
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-03-01')")], | ||
]; | ||
|
||
$this->db->table('user')->updateBatch($data, 'email'); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-03-01 01:01:11']); | ||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-03-01 01:01:11']); | ||
|
||
$data = ['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-03-11')")]; | ||
|
||
$this->db->table('user')->update($data, "email = '[email protected]'"); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-03-11 01:01:11']); | ||
} | ||
|
||
public function testRawSqlInsertArray() | ||
{ | ||
$data = [ | ||
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-04-01')")], | ||
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-04-01')")], | ||
]; | ||
|
||
$this->db->table('user')->insertBatch($data); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-04-01 01:01:11']); | ||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-04-01 01:01:11']); | ||
|
||
$data = ['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-04-11')")]; | ||
|
||
$this->db->table('user')->insert($data); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-04-11 01:01:11']); | ||
} | ||
|
||
public function testRawSqlInsertObject() | ||
{ | ||
$data = []; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-05-01')"); | ||
$data[] = $row; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-05-01')"); | ||
$data[] = $row; | ||
|
||
$this->db->table('user')->insertBatch($data); | ||
|
||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-05-11')"); | ||
|
||
$this->db->table('user')->insert($row); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-05-01 01:01:11']); | ||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-05-01 01:01:11']); | ||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-05-11 01:01:11']); | ||
} | ||
|
||
public function testRawSqlSetInsertObject() | ||
{ | ||
$data = []; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-06-01')"); | ||
$data[] = $row; | ||
|
||
$row = new stdclass(); | ||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-06-01')"); | ||
$data[] = $row; | ||
|
||
$this->db->table('user')->setInsertBatch($data)->insertBatch(); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-01 01:01:11']); | ||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-01 01:01:11']); | ||
|
||
$row->email = '[email protected]'; | ||
$row->created_at = new RawSql("setDateTime('2022-06-11')"); | ||
|
||
$this->db->table('user')->set($row)->insert(); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-11 01:01:11']); | ||
|
||
$this->db->table('user') | ||
->set('email', '[email protected]') | ||
->set('created_at', new RawSql("setDateTime('2022-06-13')")) | ||
->insert(); | ||
|
||
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-13 01:01:11']); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
<?php | ||
|
||
use CodeIgniter\Database\RawSql; | ||
|
||
$data = [ | ||
'title' => 'My title', | ||
'name' => 'My Name', | ||
'date' => 'My date', | ||
'id' => new RawSql('DEFAULT'), | ||
'title' => 'My title', | ||
'name' => 'My Name', | ||
'date' => '2022-01-01', | ||
'last_update' => new RawSql('CURRENT_TIMESTAMP()'), | ||
]; | ||
|
||
$builder->insert($data); | ||
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') | ||
/* Produces: | ||
INSERT INTO mytable (id, title, name, date, last_update) | ||
VALUES (DEFAULT, 'My title', 'My name', '2022-01-01', CURRENT_TIMESTAMP()) | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.