Skip to content

Commit ca29a3b

Browse files
authored
Merge pull request #1917 from atishamte/dbexception
Database Test Cases
2 parents 9f8468a + 28638fa commit ca29a3b

12 files changed

Lines changed: 921 additions & 200 deletions

File tree

system/Database/BaseResult.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ public function getCustomResultObject(string $className)
154154
{
155155
return $this->customResultObject[$className];
156156
}
157-
elseif (! $this->resultID || $this->numRows === 0)
157+
158+
if (is_bool($this->resultID) || ! $this->resultID || $this->numRows === 0)
158159
{
159160
return [];
160161
}
@@ -215,7 +216,7 @@ public function getResultArray(): array
215216
// In the event that query caching is on, the result_id variable
216217
// will not be a valid resource so we'll simply return an empty
217218
// array.
218-
if (! $this->resultID || $this->numRows === 0)
219+
if (is_bool($this->resultID) || ! $this->resultID || $this->numRows === 0)
219220
{
220221
return [];
221222
}
@@ -258,7 +259,7 @@ public function getResultObject(): array
258259
// In the event that query caching is on, the result_id variable
259260
// will not be a valid resource so we'll simply return an empty
260261
// array.
261-
if (! $this->resultID || $this->numRows === 0)
262+
if (is_bool($this->resultID) || ! $this->resultID || $this->numRows === 0)
262263
{
263264
return [];
264265
}
@@ -300,7 +301,7 @@ public function getRow($n = 0, $type = 'object')
300301
if (! is_numeric($n))
301302
{
302303
// We cache the row data for subsequent uses
303-
is_array($this->rowData) || $this->row_data = $this->getRowArray(0);
304+
is_array($this->rowData) || $this->rowData = $this->getRowArray(0);
304305

305306
// array_key_exists() instead of isset() to allow for NULL values
306307
if (empty($this->rowData) || ! array_key_exists($n, $this->rowData))
@@ -421,7 +422,7 @@ public function setRow($key, $value = null)
421422
// We cache the row data for subsequent uses
422423
if (! is_array($this->rowData))
423424
{
424-
$this->row_data = $this->getRowArray(0);
425+
$this->rowData = $this->getRowArray(0);
425426
}
426427

427428
if (is_array($key))

system/Database/BaseUtils.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,21 @@ public function optimizeDatabase()
200200
}
201201

202202
// Build the result array...
203+
203204
$res = $res->getResultArray();
204-
$res = current($res);
205-
$key = str_replace($this->db->database . '.', '', current($res));
206-
$keys = array_keys($res);
207-
unset($res[$keys[0]]);
205+
206+
// Postgre & SQLite3 returns empty array
207+
if (empty($res))
208+
{
209+
$key = $table_name;
210+
}
211+
else
212+
{
213+
$res = current($res);
214+
$key = str_replace($this->db->database . '.', '', current($res));
215+
$keys = array_keys($res);
216+
unset($res[$keys[0]]);
217+
}
208218

209219
$result[$key] = $res;
210220
}
@@ -304,15 +314,16 @@ public function getXMLFromResult(ResultInterface $query, $params = [])
304314
extract($params);
305315

306316
// Load the xml helper
307-
helper('xml');
317+
helper('xml');
308318
// Generate the result
309319
$xml = '<' . $root . '>' . $newline;
310320
while ($row = $query->getUnbufferedRow())
311321
{
312322
$xml .= $tab . '<' . $element . '>' . $newline;
313323
foreach ($row as $key => $val)
314324
{
315-
$xml .= $tab . $tab . '<' . $key . '>' . xml_convert($val) . '</' . $key . '>' . $newline;
325+
$val = (!empty($val)) ? xml_convert($val) : '';
326+
$xml .= $tab . $tab . '<' . $key . '>' . $val . '</' . $key . '>' . $newline;
316327
}
317328
$xml .= $tab . '</' . $element . '>' . $newline;
318329
}

system/Database/Forge.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function createDatabase(string $db_name): bool
220220
{
221221
if ($this->db->DBDebug)
222222
{
223-
throw new DatabaseException('Unable to drop the specified database.');
223+
throw new DatabaseException('Unable to create the specified database.');
224224
}
225225

226226
return false;
@@ -388,13 +388,13 @@ public function addField($field)
388388
* @param string $fieldName
389389
* @param string $tableName
390390
* @param string $tableField
391-
* @param boolean $onUpdate
392-
* @param boolean $onDelete
391+
* @param string $onUpdate
392+
* @param string $onDelete
393393
*
394394
* @return \CodeIgniter\Database\Forge
395395
* @throws \CodeIgniter\Database\Exceptions\DatabaseException
396396
*/
397-
public function addForeignKey(string $fieldName = '', string $tableName = '', string $tableField = '', bool $onUpdate = false, bool $onDelete = false)
397+
public function addForeignKey(string $fieldName = '', string $tableName = '', string $tableField = '', string $onUpdate = '', string $onDelete = '')
398398
{
399399
if (! isset($this->fields[$fieldName]))
400400
{

system/Database/SQLite3/Connection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class Connection extends BaseConnection implements ConnectionInterface
6464
*/
6565
protected $_random_keyword = [
6666
'RANDOM()',
67-
'RANDOM()',
6867
];
6968

7069
//--------------------------------------------------------------------

system/Database/SQLite3/Result.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getFieldData(): array
111111
/**
112112
* Frees the current result.
113113
*
114-
* @return mixed
114+
* @return void
115115
*/
116116
public function freeResult()
117117
{
@@ -138,12 +138,9 @@ public function dataSeek($n = 0)
138138
{
139139
if ($n !== 0)
140140
{
141-
if ($this->db->DBDebug)
142-
{
143-
throw new DatabaseException('SQLite3 doesn\'t support seeking to other offset.');
144-
}
145-
return false;
141+
throw new DatabaseException('SQLite3 doesn\'t support seeking to other offset.');
146142
}
143+
147144
return $this->resultID->reset();
148145
}
149146

system/Language/en/Database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'featureUnavailable' => 'This feature is not available for the database you are using.',
2727
'tableNotFound' => 'Table `{0}` was not found in the current database.',
2828
'noPrimaryKey' => '`{0}` model class does not specify a Primary Key.',
29-
'forEmptyInputGiven' => 'Empty statement is given for the field `{0}`',
29+
'fieldNotExists' => 'Field `{0}` not found.',
30+
'forEmptyInputGiven' => 'Empty statement is given for the field `{0}`',
3031
'forFindColumnHaveMultipleColumns' => 'Only single column allowed in Column name.',
3132
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Tests\Support\Database;
4+
5+
class MockTestClass
6+
{
7+
}

tests/system/Database/BaseQueryTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ public function testStoresDuration()
4343

4444
//--------------------------------------------------------------------
4545

46+
public function testGetStartTime()
47+
{
48+
$query = new Query($this->db);
49+
50+
$start = round(microtime(true));
51+
52+
$query->setDuration($start, $start + 5);
53+
54+
$this->assertEquals($start, $query->getStartTime(true));
55+
}
56+
57+
//--------------------------------------------------------------------
58+
59+
public function testGetStartTimeNumberFormat()
60+
{
61+
$query = new Query($this->db);
62+
63+
$start = microtime(true);
64+
65+
$query->setDuration($start, $start + 5);
66+
67+
$this->assertEquals(number_format($start, 6), $query->getStartTime());
68+
}
69+
70+
//--------------------------------------------------------------------
71+
4672
public function testsStoresErrorInformation()
4773
{
4874
$query = new Query($this->db);

0 commit comments

Comments
 (0)