Skip to content

Commit e83dad1

Browse files
committed
feat: QueryBuilder insert/update support Time
1 parent 8e6bff9 commit e83dad1

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

system/Database/BaseBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Closure;
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
1616
use CodeIgniter\Database\Exceptions\DataException;
17+
use CodeIgniter\I18n\Time;
1718
use InvalidArgumentException;
1819

1920
/**
@@ -2697,7 +2698,7 @@ protected function objectToArray($object)
26972698
$array = [];
26982699

26992700
foreach (get_object_vars($object) as $key => $val) {
2700-
if ((! is_object($val) || $val instanceof RawSql) && ! is_array($val)) {
2701+
if ((! is_object($val) || $val instanceof RawSql || $val instanceof Time) && ! is_array($val)) {
27012702
$array[$key] = $val;
27022703
}
27032704
}

system/Database/BaseConnection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Closure;
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
1616
use CodeIgniter\Events\Events;
17+
use CodeIgniter\I18n\Time;
1718
use stdClass;
1819
use Throwable;
1920

@@ -1247,6 +1248,10 @@ public function escape($str)
12471248
return $str->__toString();
12481249
}
12491250

1251+
if ($str instanceof Time) {
1252+
$str = $str->toDatabase();
1253+
}
1254+
12501255
return "'" . $this->escapeString($str) . "'";
12511256
}
12521257

tests/system/Database/Builder/InsertTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use CodeIgniter\Database\Exceptions\DatabaseException;
1515
use CodeIgniter\Database\Query;
1616
use CodeIgniter\Database\RawSql;
17+
use CodeIgniter\I18n\Time;
1718
use CodeIgniter\Test\CIUnitTestCase;
1819
use CodeIgniter\Test\Mock\MockConnection;
20+
use Locale;
1921

2022
/**
2123
* @internal
@@ -101,6 +103,26 @@ public function testInsertObjectWithRawSql()
101103
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
102104
}
103105

106+
public function testInsertObjectWithTimeWithLocaleFa()
107+
{
108+
$currentLocale = Locale::getDefault();
109+
Locale::setDefault('fa');
110+
111+
$builder = $this->db->table('jobs');
112+
113+
$time = Time::parse('2022-08-29 13:00');
114+
$insertData = (object) [
115+
'id' => 1,
116+
'insert_at' => $time,
117+
];
118+
$builder->testMode()->insert($insertData, true);
119+
120+
$expectedSQL = 'INSERT INTO "jobs" ("id", "insert_at") VALUES (1, \'2022-08-29 13:00:00\')';
121+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
122+
123+
Locale::setDefault($currentLocale);
124+
}
125+
104126
/**
105127
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5365
106128
*/

tests/system/Database/Builder/UpdateTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use CodeIgniter\Database\BaseBuilder;
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
16+
use CodeIgniter\I18n\Time;
1617
use CodeIgniter\Test\CIUnitTestCase;
1718
use CodeIgniter\Test\Mock\MockConnection;
1819
use CodeIgniter\Test\Mock\MockQuery;
20+
use Locale;
1921

2022
/**
2123
* @internal
@@ -168,6 +170,34 @@ public function testUpdateWithSetAsBoolean()
168170
$this->assertSame($expectedBinds, $builder->getBinds());
169171
}
170172

173+
public function testUpdateWithSetAsTimeWithLocaleFa()
174+
{
175+
$currentLocale = Locale::getDefault();
176+
Locale::setDefault('fa');
177+
178+
$builder = new BaseBuilder('users', $this->db);
179+
180+
$time = Time::parse('2022-08-29 13:00');
181+
$builder->testMode()->set('last_active', $time)->where('id', 1)->update();
182+
183+
$expectedSQL = 'UPDATE "users" SET "last_active" = \'2022-08-29 13:00:00\' WHERE "id" = 1';
184+
$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledUpdate()));
185+
186+
$expectedBinds = [
187+
'last_active' => [
188+
$time,
189+
true,
190+
],
191+
'id' => [
192+
1,
193+
true,
194+
],
195+
];
196+
$this->assertSame($expectedBinds, $builder->getBinds());
197+
198+
Locale::setDefault($currentLocale);
199+
}
200+
171201
public function testUpdateWithSetAsArray()
172202
{
173203
$builder = new BaseBuilder('jobs', $this->db);

0 commit comments

Comments
 (0)