Skip to content

Commit

Permalink
fix: Model's getCompiledUpdate() & getCompiledInsert() throws Databas…
Browse files Browse the repository at this point in the history
…eException

Fixes codeigniter4#5549
  • Loading branch information
kenjis committed Jan 11, 2022
1 parent d531506 commit d6b453d
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
56 changes: 56 additions & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,60 @@ public static function classToArray($data, $primaryKey = null, string $dateForma

return $properties;
}

/**
* Compiles an insert query and returns the sql
*
* @throws DatabaseException
*
* @return bool|string
*/
public function getCompiledInsert(bool $reset = true)
{
$builder = $this->builder();

if (empty($this->tempData['data'])) {
return $builder->getCompiledInsert($reset);
}

$data = $this->tempData['data'];
$escape = $this->tempData['escape'];

if ($reset) {
$this->tempData = [];
}

foreach ($data as $key => $value) {
$builder->set($key, $value, $escape[$key]);
}

return $builder->getCompiledInsert($reset);
}

/**
* Compiles an update query and returns the sql
*
* @return bool|string
*/
public function getCompiledUpdate(bool $reset = true)
{
$builder = $this->builder();

if (empty($this->tempData['data'])) {
return $builder->getCompiledUpdate($reset);
}

$data = $this->tempData['data'];
$escape = $this->tempData['escape'];

if ($reset) {
$this->tempData = [];
}

foreach ($data as $key => $value) {
$builder->set($key, $value, $escape[$key]);
}

return $builder->getCompiledUpdate($reset);
}
}
79 changes: 79 additions & 0 deletions tests/system/Models/GetCompiledModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Models;

use CodeIgniter\Model;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use Tests\Support\Models\UserObjModel;

/**
* @internal
*/
final class GetCompiledModelTest extends CIUnitTestCase
{
/**
* @var Model
*/
private $model;

/**
* Create an instance of Model for use in testing.
*/
private function createModel(string $modelName): Model
{
$db = new MockConnection([]);
$this->model = new $modelName($db);

return $this->model;
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5549
*/
public function testGetCompiledInsert(): void
{
$this->createModel(UserObjModel::class);

$sql = $this->model
->set('name', 'Mark')
->set('email', 'mark@example.com')
->getCompiledInsert();

$this->assertSame(
<<<'SQL'
INSERT INTO "user" ("name", "email") VALUES ('Mark', 'mark@example.com')
SQL,
$sql
);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5549
*/
public function testGetCompiledUpdate(): void
{
$this->createModel(UserObjModel::class);

$sql = $this->model
->set('name', 'Mark')
->set('email', 'mark@example.com')
->getCompiledUpdate();

$this->assertSame(
<<<'SQL'
UPDATE "user" SET "name" = 'Mark', "email" = 'mark@example.com'
SQL,
$sql
);
}
}

0 comments on commit d6b453d

Please sign in to comment.