Skip to content

feat: QueryBuilder insert/update support Time object #6456

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ parameters:
Database:
- Entity
- Events
- I18n
Email:
- Events
Entity:
Expand Down
7 changes: 6 additions & 1 deletion system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Closure;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\I18n\Time;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -2698,7 +2699,11 @@ protected function objectToArray($object)
$array = [];

foreach (get_object_vars($object) as $key => $val) {
if ((! is_object($val) || $val instanceof RawSql) && ! is_array($val)) {
if ($val instanceof RawSql) {
$array[$key] = $val;
} elseif ($val instanceof Time) {
$array[$key] = $val;
} elseif (! is_object($val) && ! is_array($val)) {
$array[$key] = $val;
}
}
Expand Down
5 changes: 5 additions & 0 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Closure;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Events\Events;
use CodeIgniter\I18n\Time;
use stdClass;
use Throwable;

Expand Down Expand Up @@ -1247,6 +1248,10 @@ public function escape($str)
return $str->__toString();
}

if ($str instanceof Time) {
$str = $str->toDatabase();
}

return "'" . $this->escapeString($str) . "'";
}

Expand Down
5 changes: 5 additions & 0 deletions system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\RawSql;
use CodeIgniter\I18n\Time;
use ErrorException;
use stdClass;

Expand Down Expand Up @@ -186,6 +187,10 @@ public function escape($str)
return $str->__toString();
}

if ($str instanceof Time) {
$str = $str->toDatabase();
}

return pg_escape_literal($this->connID, $str);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/system/Database/Builder/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Query;
use CodeIgniter\Database\RawSql;
use CodeIgniter\I18n\Time;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use Locale;

/**
* @internal
Expand Down Expand Up @@ -101,6 +103,26 @@ public function testInsertObjectWithRawSql()
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
}

public function testInsertObjectWithTimeWithLocaleFa()
{
$currentLocale = Locale::getDefault();
Locale::setDefault('fa');

$builder = $this->db->table('jobs');

$time = Time::parse('2022-08-29 13:00');
$insertData = (object) [
'id' => 1,
'insert_at' => $time,
];
$builder->testMode()->insert($insertData, true);

$expectedSQL = 'INSERT INTO "jobs" ("id", "insert_at") VALUES (1, \'2022-08-29 13:00:00\')';
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));

Locale::setDefault($currentLocale);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5365
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/system/Database/Builder/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\I18n\Time;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use CodeIgniter\Test\Mock\MockQuery;
use Locale;

/**
* @internal
Expand Down Expand Up @@ -168,6 +170,34 @@ public function testUpdateWithSetAsBoolean()
$this->assertSame($expectedBinds, $builder->getBinds());
}

public function testUpdateWithSetAsTimeWithLocaleFa()
{
$currentLocale = Locale::getDefault();
Locale::setDefault('fa');

$builder = new BaseBuilder('users', $this->db);

$time = Time::parse('2022-08-29 13:00');
$builder->testMode()->set('last_active', $time)->where('id', 1)->update();

$expectedSQL = 'UPDATE "users" SET "last_active" = \'2022-08-29 13:00:00\' WHERE "id" = 1';
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledUpdate()));

$expectedBinds = [
'last_active' => [
$time,
true,
],
'id' => [
1,
true,
],
];
$this->assertSame($expectedBinds, $builder->getBinds());

Locale::setDefault($currentLocale);
}

public function testUpdateWithSetAsArray()
{
$builder = new BaseBuilder('jobs', $this->db);
Expand Down