Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00%
0 / 1
0.00%
0 / 13
CRAP
0.00%
0 / 109
Db
0.00%
0 / 1
0.00%
0 / 13
812
0.00%
0 / 109
 connect($dsn, $user, $password)
0.00%
0 / 1
2
0.00%
0 / 6
 create($dsn, $user, $password)
0.00%
0 / 1
42
0.00%
0 / 18
 getProvider($dsn)
0.00%
0 / 1
2
0.00%
0 / 4
 __construct($dsn, $user, $password)
0.00%
0 / 1
2
0.00%
0 / 8
 getDbh()
0.00%
0 / 1
2
0.00%
0 / 3
 getDb()
0.00%
0 / 1
6
0.00%
0 / 7
 cleanup()
0.00%
0 / 1
2
0.00%
0 / 3
 load($sql)
0.00%
0 / 1
42
0.00%
0 / 30
 insert($table, array $data)
0.00%
0 / 1
2
0.00%
0 / 5
 select($column, $table, array $criteria)
0.00%
0 / 1
6
0.00%
0 / 9
 deleteQuery($table, $id)
0.00%
0 / 1
2
0.00%
0 / 5
 sqlLine($sql)
0.00%
0 / 1
20
0.00%
0 / 7
 sqlQuery($query)
0.00%
0 / 1
2
0.00%
0 / 4
<?php
namespace Codeception\Util\Driver;
use Codeception\Exception\Module as ModuleException;
class Db
{
protected $dbh;
protected $dsn;
public static function connect($dsn, $user, $password)
{
$dbh = new \PDO($dsn, $user, $password);
$dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $dbh;
}
/**
* @static
* @param $dsn
* @param $user
* @param $password
* @return Db|MsSql|MySql|Oracle|PostgreSql|Sqlite
*/
public static function create($dsn, $user, $password)
{
$provider = self::getProvider($dsn);
switch ($provider) {
case 'sqlite':
return new Sqlite($dsn, $user, $password);
case 'mysql':
return new MySql($dsn, $user, $password);
case 'pgsql':
return new PostgreSql($dsn, $user, $password);
case 'mssql':
return new MsSql($dsn, $user, $password);
case 'oracle':
return new Oracle($dsn, $user, $password);
default:
return new Db($dsn, $user, $password);
}
}
public static function getProvider($dsn)
{
return substr($dsn, 0, strpos($dsn, ':'));
}
public function __construct($dsn, $user, $password)
{
$this->dbh = new \PDO($dsn, $user, $password);
$this->dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
}
public function getDbh() {
return $this->dbh;
}
public function getDb()
{
$matches = array();
$matched = preg_match('~dbname=(.*);~s', $this->dsn, $matches);
if (!$matched) return false;
return $matches[1];
}
public function cleanup()
{
}
public function load($sql)
{
$query = '';
$delimiter = ';';
$delimiterLength = 1;
foreach ($sql as $sqlLine) {
if (preg_match('/DELIMITER ([\;\$\|\\\\]+)/i', $sqlLine, $match)) {
$delimiter = $match[1];
$delimiterLength = strlen($delimiter);
continue;
}
$parsed = $this->sqlLine($sqlLine);
if ($parsed) {
continue;
}
$query .= rtrim($sqlLine);
if (substr($query, - 1 * $delimiterLength, $delimiterLength) == $delimiter) {
$queryToBeExecuted = substr($query, 0, - 1 * $delimiterLength);
try {
$this->sqlQuery($queryToBeExecuted);
} catch (\PDOException $e) {
throw new ModuleException(
__CLASS__,
$e->getMessage() . "\nSQL query being executed: " . $queryToBeExecuted
);
}
$query = "";
}
}
}
public function insert($table, array $data)
{
$query = "insert into %s (%s) values (%s)";
return sprintf($query, $table, implode(', ', array_keys($data)), implode(', ', array_fill(0, count($data),'?')));
}
public function select($column, $table, array $criteria) {
$query = "select %s from %s where %s";
$params = array();
foreach ($criteria as $k => $v) {
$params[] = "$k = ? ";
}
$params = implode('AND ', $params);
return sprintf($query, $column, $table, $params);
}
public function deleteQuery($table, $id)
{
$query = "delete from $table where id = $id";
$this->sqlQuery($query);
}
protected function sqlLine($sql)
{
if (trim($sql) == "") return true;
if (trim($sql) == ";") return true;
if (preg_match('~^((--.*?)|(#))~s', $sql)) return true;
return false;
}
protected function sqlQuery($query)
{
$this->dbh->exec($query);
}
}