Skip to content

Commit

Permalink
Transactions added
Browse files Browse the repository at this point in the history
  • Loading branch information
carduz committed Oct 5, 2015
1 parent e3930a9 commit 7f585ab
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,7 @@ The simplest and the most powerful library for mysql written in php:
* Log all actions performed on db
* All mysql error as exception
* Clone support
* Transactions support

# Download, install and use

Expand Down Expand Up @@ -87,6 +88,8 @@ You can find some examples under `examples` to run it:

There is a simple example `simpleExample.php` that shows you how to use the library in the simplest way

There are not examples about transactions yet (you have to remember to set autocommit to true after using transactions)

N.B. if you have added mysqltcs as composer dependency you will find example under `vendor/thecsea/mysqltcs/examples`

# By [thecsea.it](http://www.thecsea.it)
46 changes: 46 additions & 0 deletions src/Mysqltcs.php
Expand Up @@ -284,4 +284,50 @@ public function executeQuery($query)
return $results;
}
}

/**
* Set autocommit
* @param boolean $autocommit
* @throws MysqltcsException on error
*/
public function setAutocommit($autocommit)
{
if($this->mysqliRef->autocommit($autocommit)) {
$this->log("autocommit set to ".($autocommit?"true":"false"));
return;
}
$mex = "Mysql error: it is not possible set autocommit to ".($autocommit?"true":"false"). " state. ".$this->mysqliRef->error;
$this->log($mex);
throw new MysqltcsException($mex);
}

/**
* Commit the transaction
* @throws MysqltcsException on commit error
*/
public function commit()
{
if($this->mysqliRef->commit()) {
$this->log("commit");
return;
}
$mex = "Mysql error: it is not possible perform the commit. ".$this->mysqliRef->error;
$this->log($mex);
throw new MysqltcsException($mex);
}

/**
* Rollback (abort) the transaction
* @throws MysqltcsException on rollback error
*/
public function rollBack()
{
if($this->mysqliRef->rollback()) {
$this->log("rollback");
return;
}
$mex = "Mysql error: it is not possible perform the rollback. ".$this->mysqliRef->error;
$this->log($mex);
throw new MysqltcsException($mex);
}
}
83 changes: 83 additions & 0 deletions tests/TransactionTest.php
@@ -0,0 +1,83 @@
<?php
/**
* Created by PhpStorm.
* User: Claudio Cardinale <cardi@thecsea.it>
* Date: 05/10/15
* Time: 15.38
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace it\thecsea\mysqltcs;


/**
* Class TransactionTest
* @package it\thecsea\mysqltcs
* @author Claudio Cardinale <cardi@thecsea.it>
* @copyright 2015 Claudio Cardinale
* @version 1.0.0
*/
class TransactionTest extends \PHPUnit_Framework_TestCase
{
public function testSuccessfull()
{
$db = require(__DIR__ . "/config.php");
$mysqltcs = new Mysqltcs($db['host'], $db['user'], $db['psw'], $db['db']);
$mysqltcs2 = clone $mysqltcs;
$operations = new MysqltcsOperations($mysqltcs, $db['tables']['test1']);
$operations2 = clone $operations;
$operations2->setMysqltcs($mysqltcs2);
//commit
$mysqltcs->setAutocommit(false);
$operations->insert("value, value2", "'tt', 'kk'");
$this->assertEmpty($operations2->getList("*", "1"));
$mysqltcs2->commit();
$this->assertEmpty($operations2->getList("*", "1"));
$mysqltcs->commit();
$this->assertNotEmpty($operations2->getList("*", "1"));
$mysqltcs->setAutocommit(true);
$operations->deleteRows("1");
//autocommit
$mysqltcs->setAutocommit(false);
$operations->insert("value, value2", "'tt', 'kk'");
$this->assertEmpty($operations2->getList("*", "1"));
$mysqltcs->setAutocommit(true);
$this->assertNotEmpty($operations2->getList("*", "1"));
$operations->deleteRows("1");
//rollback
$mysqltcs->setAutocommit(false);
$operations->insert("value, value2", "'tt', 'kk'");
$this->assertEmpty($operations2->getList("*", "1"));
$mysqltcs->rollBack();
$this->assertEmpty($operations2->getList("*", "1"));
$mysqltcs->setAutocommit(true);
$this->assertEmpty($operations2->getList("*", "1"));
$operations->deleteRows("1"); //this to avoid problems in other tests, in normal situations thi lines is useless
}

public function testLogger()
{
$db = require(__DIR__ . "/config.php");
$mysqltcs = new Mysqltcs($db['host'], $db['user'], $db['psw'], $db['db']);
$mysqltcs->setSimpleLogger();
$mysqltcs->setAutocommit(false);
$logA = $mysqltcs->getLogger()->getLogArray();
$this->assertEquals("autocommit set to false", $logA[count($logA)-1]);
$mysqltcs->commit();
$logA = $mysqltcs->getLogger()->getLogArray();
$this->assertEquals("commit", $logA[count($logA)-1]);
$mysqltcs->rollBack();
$logA = $mysqltcs->getLogger()->getLogArray();
$this->assertEquals("rollback", $logA[count($logA)-1]);
}
}

0 comments on commit 7f585ab

Please sign in to comment.