Repository navigation
Expand file tree
/
Copy pathDb.php
More file actions
162 lines (134 loc) · 4.06 KB
/
Copy pathDb.php
File metadata and controls
162 lines (134 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Codeception\Util\Driver;
use Codeception\Exception\Module as ModuleException;
class Db
{
protected $dbh;
protected $dsn;
public $sqlToRun;
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 .= "\n" . rtrim($sqlLine);
if (substr($query, - 1 * $delimiterLength, $delimiterLength) == $delimiter) {
$this->sqlToRun = substr($query, 0, - 1 * $delimiterLength);
$this->sqlQuery($this->sqlToRun);
$query = "";
}
}
}
public function insert($tableName, array $data)
{
$columns = array_map(
array($this, 'getQuotedName'),
array_keys($data)
);
return sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
$this->getQuotedName($tableName),
implode(', ', $columns),
implode(', ', array_fill(0, count($data), '?'))
);
}
public function select($column, $table, array $criteria) {
$where = $criteria ? "where %s" : '';
$query = "select %s from `%s` $where";
$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);
}
public function lastInsertId($table) {
return $this->getDbh()->lastInsertId();
}
public function getQuotedName($name)
{
return $name;
}
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);
}
}