Skip to content

Commit

Permalink
corrections/bug fix for transaction support, when used an exception w…
Browse files Browse the repository at this point in the history
…ill be throw that needs to be catch for rollback
  • Loading branch information
TheTechsTech committed Jun 8, 2019
1 parent c9f18c4 commit f3d22c2
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 37 deletions.
2 changes: 1 addition & 1 deletion lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* ezsqlModel Constants
*/
\defined('EZSQL_VERSION') or \define('EZSQL_VERSION', '4.0.4');
\defined('EZSQL_VERSION') or \define('EZSQL_VERSION', '4.0.5');
\defined('OBJECT') or \define('OBJECT', 'OBJECT');
\defined('ARRAY_A') or \define('ARRAY_A', 'ARRAY_A');
\defined('ARRAY_N') or \define('ARRAY_N', 'ARRAY_N');
Expand Down
13 changes: 12 additions & 1 deletion lib/Database/ez_mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ private function fetch_prepared_result(&$stmt, $query)
public function query_prepared(string $query, array $param = null)
{
$stmt = $this->dbh->prepare($query);
if (!$stmt instanceof \mysqli_stmt) {
if ($this->isTransactional)
throw new \Exception($this->getLast_Error());

return false;
}

$params = [];
$types = \array_reduce($param,
function ($string, &$arg) use (&$params) {
Expand Down Expand Up @@ -422,8 +429,12 @@ public function query(string $query, bool $use_prepare = false)

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

if ($this->processQueryResult($query) === false)
if ($this->processQueryResult($query) === false) {
if ($this->isTransactional)
throw new \Exception($this->getLast_Error());

return false;
}

// disk caching of queries
$this->store_cache($query, $this->is_insert);
Expand Down
6 changes: 5 additions & 1 deletion lib/Database/ez_pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,12 @@ public function query(string $query, bool $use_prepare = false)
$this->database->getIsFile());
}

if ($this->processQuery($query, $param) === false)
if ($this->processQuery($query, $param) === false) {
if ($this->isTransactional)
throw new \PDOException($this->getLast_Error());

return false;
}

// disk caching of queries
$this->store_cache($query, $this->is_insert);
Expand Down
6 changes: 5 additions & 1 deletion lib/Database/ez_pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,12 @@ public function query(string $query, bool $use_prepare = false)
$this->result = @\pg_query($this->dbh, $query);
}

if ($this->processQueryResult($query) === false)
if ($this->processQueryResult($query) === false) {
if ($this->isTransactional)
throw new \Exception($this->getLast_Error());

return false;
}

// disk caching of queries
$this->store_cache($query, $this->is_insert);
Expand Down
19 changes: 15 additions & 4 deletions lib/Database/ez_sqlite3.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public function getArgType($arg)
public function query_prepared(string $query, array $param = null)
{
$stmt = $this->dbh->prepare($query);
if (!$stmt instanceof \SQLite3Stmt) {
if ($this->isTransactional)
throw new \Exception($this->getLast_Error());

return false;
}

foreach ($param as $index => $val) {
// indexing start from 1 in Sqlite3 statement
if (\is_array($val)) {
Expand Down Expand Up @@ -272,8 +279,12 @@ public function query(string $query, bool $use_prepare = false)
$this->result = $this->dbh->query($query);
}

if ($this->processQueryResult($query) === false)
if ($this->processQueryResult($query) === false) {
if ($this->isTransactional)
throw new \Exception($this->getLast_Error());

return false;
}

if (!empty($param) && \is_array($param) && $this->isPrepareOn())
$this->result->finalize();
Expand Down Expand Up @@ -316,19 +327,19 @@ public function handle()
*/
public function beginTransaction()
{
$this->dbh->exec('BEGIN');
$this->dbh->exec('BEGIN;');
$this->isTransactional = true;
}

public function commit()
{
$this->dbh->exec('COMMIT');
$this->dbh->exec('COMMIT;');
$this->isTransactional = false;
}

public function rollback()
{
$this->dbh->exec('ROLLBACK');
$this->dbh->exec('ROLLBACK;');
$this->isTransactional = false;
}
}
6 changes: 5 additions & 1 deletion lib/Database/ez_sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,12 @@ public function query(string $query, bool $use_prepare = false)
$this->result = @\sqlsrv_query($this->dbh, $query);
}

if ($this->processQueryResult($query) === false)
if ($this->processQueryResult($query) === false) {
if ($this->isTransactional)
throw new \Exception($this->getLast_Error());

return false;
}

// disk caching of queries
$this->store_cache($query, $this->is_insert);
Expand Down
2 changes: 1 addition & 1 deletion lib/ezFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ function update($table = '', $keyValue, ...$args) {
: false;
}

function delete($table = '', ...$args) {
function deleting($table = '', ...$args) {
$ezQuery = \getInstance();
return ($ezQuery instanceOf DatabaseInterface)
? $ezQuery->delete($table, ...$args)
Expand Down
2 changes: 1 addition & 1 deletion lib/ezQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ public function create(string $table = null, ...$schemas)
if (empty($table) || empty($schemas) || empty($vendor))
return false;

$sql = 'CREATE TABLE '.$table.'( ';
$sql = 'CREATE TABLE IF NOT EXISTS '.$table.'( ';

$skipSchema = false;
if (\is_string($schemas[0])) {
Expand Down
4 changes: 2 additions & 2 deletions tests/ezFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ public function testUpdate() {
/**
* @test delete
*/
public function testDelete() {
$this->assertFalse(delete('field', 'data', 'data2'));
public function testDeleting() {
$this->assertFalse(deleting('field', 'data', 'data2'));
}

/**
Expand Down
87 changes: 87 additions & 0 deletions tests/mysqli/mysqliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,93 @@ public function testSelecting()
}
}

/**
* @covers ezsql\Database\ez_mysqli::commit
* @covers ezsql\Database\ez_mysqli::beginTransaction
* @covers ezsql\Database\ez_mysqli::query
* @covers ezsql\Database\ez_mysqli::processQueryResult
*/
public function testBeginTransactionCommit()
{
$this->object->connect();
$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');
$this->object->query('"ALTER TABLE unit_test Type=InnoDB"');

$commit = null;
try {
$commit = true;
$this->object->beginTransaction();
$this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'testing 1' ));
$this->object->insert('unit_test', array('id'=>'2', 'test_key'=>'testing 2' ));
$this->object->insert('unit_test', array('id'=>'3', 'test_key'=>'testing 3' ));
$this->object->commit();
} catch(\Exception $ex) {
$commit = false;
$this->object->rollback();
echo ("Error! This rollback message shouldn't have been displayed: ").$ex->getMessage();
}

if ($commit) {
$result = $this->object->selecting('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals($i, $row->id);
$this->assertEquals('testing ' . $i, $row->test_key);
++$i;
}

$this->assertEquals(0, $this->object->drop('unit_test'));
}
}

/**
* @covers ezsql\Database\ez_mysqli::rollback
* @covers ezsql\Database\ez_mysqli::beginTransaction
* @covers ezsql\Database\ez_mysqli::query
* @covers ezsql\Database\ez_mysqli::processQueryResult
*/
public function testBeginTransactionRollback()
{
$this->object->connect();
$this->object->query('CREATE TABLE unit_test(id int(11) NOT NULL AUTO_INCREMENT, test_key varchar(50), PRIMARY KEY (ID)');
$this->object->query('"ALTER TABLE unit_test Type=InnoDB"');

$commit = null;
try {
$commit = true;
$this->object->beginTransaction();
$this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'testing 1' ));
$this->object->insert('unit_test', array('id'=>'2', 'test_key'=>'testing 2' ));
$this->object->insert('unit_test', array('idx' => 1, 'test_key2'=>'testing 1' ));
$this->object->commit();
} catch(\Exception $ex) {
$commit = false;
$this->object->rollback();
}

if ($commit) {
echo ("Error! This message shouldn't have been displayed.");
$result = $this->object->selecting('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals('should not be seen ' . $i, $row->test_key);
++$i;
}
$this->object->drop('unit_test');
} else {
//echo ("Error! rollback.");
$result = $this->object->selecting('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals('should not be seen ' . $i, $row->test_key);
++$i;
}

$this->assertEquals(0, $result);
$this->object->drop('unit_test');
}
}

/**
* @covers ezsql\Database\ez_mysqli::query
* @covers ezsql\Database\ez_mysqli::processQueryResult
Expand Down
40 changes: 17 additions & 23 deletions tests/pdo/pdo_mysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,9 @@ public function testBeginTransactionCommit()
$this->object->insert('unit_test', array('id'=>'3', 'test_key'=>'testing 3' ));
$this->object->commit();
} catch(\PDOException $ex) {
$commit = false;
$this->object->rollback();
$this->fail("Error! This message shouldn't have been displayed.");
echo ("Error! This rollback message shouldn't have been displayed: ").$ex->getMessage();
}

if ($commit) {
Expand All @@ -333,23 +334,7 @@ public function testBeginTransactionCommit()
++$i;
}

$where = array('test_key', '=', 'testing 2');
$result = select('unit_test', 'id', $where);
foreach ($result as $row) {
$this->assertEquals(2, $row->id);
}

$result = $this->object->selecting('unit_test', 'test_key', array( 'id', '=', '3' ));
foreach ($result as $row) {
$this->assertEquals('testing 3', $row->test_key);
}

$result = $this->object->selecting('unit_test', array ('test_key'), eq('id', 1));
foreach ($result as $row) {
$this->assertEquals('testing 1', $row->test_key);
}

$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
$this->assertEquals(0, $this->object->drop('unit_test'));
}
}

Expand All @@ -372,26 +357,35 @@ public function testBeginTransactionRollback()
try {
$commit = true;
$this->object->beginTransaction();
$this->object->insert('unit_test', array( 0 => 3, 'test_key'=>'testing 3' ));
$this->object->insert('unit_test', array('id'=>'1', 'test_key'=>'testing 1' ));
$this->object->insert('unit_test', array('id'=>'2', 'test_key'=>'testing 2' ));
$this->object->insert('unit_test', array( 'idx' => 3, 'test_key'=>'testing 3' ));
$this->object->commit();
} catch(\PDOException $ex) {
$commit = false;
$this->object->rollback();
}

if ($commit) {
//echo ("Error! This message shouldn't have been displayed.");
echo ("Error! This message shouldn't have been displayed.");
$result = $this->object->selecting('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals('should be seen ' . $i, $row->test_key);
$this->assertEquals('should not be seen ' . $i, $row->test_key);
++$i;
}
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
$this->object->drop('unit_test');
} else {
//echo ("Error! rollback.");
$result = $this->object->selecting('unit_test');
$i = 1;
foreach ($result as $row) {
$this->assertEquals('should not be seen ' . $i, $row->test_key);
++$i;
}

$this->assertEquals(0, $result);
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
$this->object->drop('unit_test');
}
}

Expand Down
Loading

0 comments on commit f3d22c2

Please sign in to comment.