Skip to content

Commit f3d22c2

Browse files
committed
corrections/bug fix for transaction support, when used an exception will be throw that needs to be catch for rollback
1 parent c9f18c4 commit f3d22c2

File tree

14 files changed

+449
-37
lines changed

14 files changed

+449
-37
lines changed

lib/Constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* ezsqlModel Constants
77
*/
8-
\defined('EZSQL_VERSION') or \define('EZSQL_VERSION', '4.0.4');
8+
\defined('EZSQL_VERSION') or \define('EZSQL_VERSION', '4.0.5');
99
\defined('OBJECT') or \define('OBJECT', 'OBJECT');
1010
\defined('ARRAY_A') or \define('ARRAY_A', 'ARRAY_A');
1111
\defined('ARRAY_N') or \define('ARRAY_N', 'ARRAY_N');

lib/Database/ez_mysqli.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ private function fetch_prepared_result(&$stmt, $query)
273273
public function query_prepared(string $query, array $param = null)
274274
{
275275
$stmt = $this->dbh->prepare($query);
276+
if (!$stmt instanceof \mysqli_stmt) {
277+
if ($this->isTransactional)
278+
throw new \Exception($this->getLast_Error());
279+
280+
return false;
281+
}
282+
276283
$params = [];
277284
$types = \array_reduce($param,
278285
function ($string, &$arg) use (&$params) {
@@ -422,8 +429,12 @@ public function query(string $query, bool $use_prepare = false)
422429

423430
$this->result = \mysqli_query($this->dbh, $query);
424431

425-
if ($this->processQueryResult($query) === false)
432+
if ($this->processQueryResult($query) === false) {
433+
if ($this->isTransactional)
434+
throw new \Exception($this->getLast_Error());
435+
426436
return false;
437+
}
427438

428439
// disk caching of queries
429440
$this->store_cache($query, $this->is_insert);

lib/Database/ez_pdo.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,12 @@ public function query(string $query, bool $use_prepare = false)
423423
$this->database->getIsFile());
424424
}
425425

426-
if ($this->processQuery($query, $param) === false)
426+
if ($this->processQuery($query, $param) === false) {
427+
if ($this->isTransactional)
428+
throw new \PDOException($this->getLast_Error());
429+
427430
return false;
431+
}
428432

429433
// disk caching of queries
430434
$this->store_cache($query, $this->is_insert);

lib/Database/ez_pgsql.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,12 @@ public function query(string $query, bool $use_prepare = false)
297297
$this->result = @\pg_query($this->dbh, $query);
298298
}
299299

300-
if ($this->processQueryResult($query) === false)
300+
if ($this->processQueryResult($query) === false) {
301+
if ($this->isTransactional)
302+
throw new \Exception($this->getLast_Error());
303+
301304
return false;
305+
}
302306

303307
// disk caching of queries
304308
$this->store_cache($query, $this->is_insert);

lib/Database/ez_sqlite3.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ public function getArgType($arg)
137137
public function query_prepared(string $query, array $param = null)
138138
{
139139
$stmt = $this->dbh->prepare($query);
140+
if (!$stmt instanceof \SQLite3Stmt) {
141+
if ($this->isTransactional)
142+
throw new \Exception($this->getLast_Error());
143+
144+
return false;
145+
}
146+
140147
foreach ($param as $index => $val) {
141148
// indexing start from 1 in Sqlite3 statement
142149
if (\is_array($val)) {
@@ -272,8 +279,12 @@ public function query(string $query, bool $use_prepare = false)
272279
$this->result = $this->dbh->query($query);
273280
}
274281

275-
if ($this->processQueryResult($query) === false)
282+
if ($this->processQueryResult($query) === false) {
283+
if ($this->isTransactional)
284+
throw new \Exception($this->getLast_Error());
285+
276286
return false;
287+
}
277288

278289
if (!empty($param) && \is_array($param) && $this->isPrepareOn())
279290
$this->result->finalize();
@@ -316,19 +327,19 @@ public function handle()
316327
*/
317328
public function beginTransaction()
318329
{
319-
$this->dbh->exec('BEGIN');
330+
$this->dbh->exec('BEGIN;');
320331
$this->isTransactional = true;
321332
}
322333

323334
public function commit()
324335
{
325-
$this->dbh->exec('COMMIT');
336+
$this->dbh->exec('COMMIT;');
326337
$this->isTransactional = false;
327338
}
328339

329340
public function rollback()
330341
{
331-
$this->dbh->exec('ROLLBACK');
342+
$this->dbh->exec('ROLLBACK;');
332343
$this->isTransactional = false;
333344
}
334345
}

lib/Database/ez_sqlsrv.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,12 @@ public function query(string $query, bool $use_prepare = false)
301301
$this->result = @\sqlsrv_query($this->dbh, $query);
302302
}
303303

304-
if ($this->processQueryResult($query) === false)
304+
if ($this->processQueryResult($query) === false) {
305+
if ($this->isTransactional)
306+
throw new \Exception($this->getLast_Error());
307+
305308
return false;
309+
}
306310

307311
// disk caching of queries
308312
$this->store_cache($query, $this->is_insert);

lib/ezFunctions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ function update($table = '', $keyValue, ...$args) {
450450
: false;
451451
}
452452

453-
function delete($table = '', ...$args) {
453+
function deleting($table = '', ...$args) {
454454
$ezQuery = \getInstance();
455455
return ($ezQuery instanceOf DatabaseInterface)
456456
? $ezQuery->delete($table, ...$args)

lib/ezQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ public function create(string $table = null, ...$schemas)
779779
if (empty($table) || empty($schemas) || empty($vendor))
780780
return false;
781781

782-
$sql = 'CREATE TABLE '.$table.'( ';
782+
$sql = 'CREATE TABLE IF NOT EXISTS '.$table.'( ';
783783

784784
$skipSchema = false;
785785
if (\is_string($schemas[0])) {

tests/ezFunctionsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ public function testUpdate() {
301301
/**
302302
* @test delete
303303
*/
304-
public function testDelete() {
305-
$this->assertFalse(delete('field', 'data', 'data2'));
304+
public function testDeleting() {
305+
$this->assertFalse(deleting('field', 'data', 'data2'));
306306
}
307307

308308
/**

tests/mysqli/mysqliTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,93 @@ public function testSelecting()
420420
}
421421
}
422422

423+
/**
424+
* @covers ezsql\Database\ez_mysqli::commit
425+
* @covers ezsql\Database\ez_mysqli::beginTransaction
426+
* @covers ezsql\Database\ez_mysqli::query
427+
* @covers ezsql\Database\ez_mysqli::processQueryResult
428+
*/
429+
public function testBeginTransactionCommit()
430+
{
431+
$this->object->connect();
432+
$this->object->query('CREATE TABLE unit_test(id int(11) NOT NULL AUTO_INCREMENT, test_key varchar(50), PRIMARY KEY (ID))ENGINE=MyISAM DEFAULT CHARSET=utf8');
433+
$this->object->query('"ALTER TABLE unit_test Type=InnoDB"');
434+
435+
$commit = null;
436+
try {
437+
$commit = true;
438+
$this->object->beginTransaction();
439+
$this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'testing 1' ));
440+
$this->object->insert('unit_test', array('id'=>'2', 'test_key'=>'testing 2' ));
441+
$this->object->insert('unit_test', array('id'=>'3', 'test_key'=>'testing 3' ));
442+
$this->object->commit();
443+
} catch(\Exception $ex) {
444+
$commit = false;
445+
$this->object->rollback();
446+
echo ("Error! This rollback message shouldn't have been displayed: ").$ex->getMessage();
447+
}
448+
449+
if ($commit) {
450+
$result = $this->object->selecting('unit_test');
451+
$i = 1;
452+
foreach ($result as $row) {
453+
$this->assertEquals($i, $row->id);
454+
$this->assertEquals('testing ' . $i, $row->test_key);
455+
++$i;
456+
}
457+
458+
$this->assertEquals(0, $this->object->drop('unit_test'));
459+
}
460+
}
461+
462+
/**
463+
* @covers ezsql\Database\ez_mysqli::rollback
464+
* @covers ezsql\Database\ez_mysqli::beginTransaction
465+
* @covers ezsql\Database\ez_mysqli::query
466+
* @covers ezsql\Database\ez_mysqli::processQueryResult
467+
*/
468+
public function testBeginTransactionRollback()
469+
{
470+
$this->object->connect();
471+
$this->object->query('CREATE TABLE unit_test(id int(11) NOT NULL AUTO_INCREMENT, test_key varchar(50), PRIMARY KEY (ID)');
472+
$this->object->query('"ALTER TABLE unit_test Type=InnoDB"');
473+
474+
$commit = null;
475+
try {
476+
$commit = true;
477+
$this->object->beginTransaction();
478+
$this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'testing 1' ));
479+
$this->object->insert('unit_test', array('id'=>'2', 'test_key'=>'testing 2' ));
480+
$this->object->insert('unit_test', array('idx' => 1, 'test_key2'=>'testing 1' ));
481+
$this->object->commit();
482+
} catch(\Exception $ex) {
483+
$commit = false;
484+
$this->object->rollback();
485+
}
486+
487+
if ($commit) {
488+
echo ("Error! This message shouldn't have been displayed.");
489+
$result = $this->object->selecting('unit_test');
490+
$i = 1;
491+
foreach ($result as $row) {
492+
$this->assertEquals('should not be seen ' . $i, $row->test_key);
493+
++$i;
494+
}
495+
$this->object->drop('unit_test');
496+
} else {
497+
//echo ("Error! rollback.");
498+
$result = $this->object->selecting('unit_test');
499+
$i = 1;
500+
foreach ($result as $row) {
501+
$this->assertEquals('should not be seen ' . $i, $row->test_key);
502+
++$i;
503+
}
504+
505+
$this->assertEquals(0, $result);
506+
$this->object->drop('unit_test');
507+
}
508+
}
509+
423510
/**
424511
* @covers ezsql\Database\ez_mysqli::query
425512
* @covers ezsql\Database\ez_mysqli::processQueryResult

0 commit comments

Comments
 (0)