This repository has been archived by the owner on Aug 25, 2019. It is now read-only.
/
Connection.php
198 lines (172 loc) · 4.06 KB
/
Connection.php
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace UseBB\System\Database;
use Doctrine\DBAL\DriverManager;
/**
* Database connection.
*
* Represents a connection to the database.
*
* \author Dietrich Moerman
*/
class Connection {
private $type;
private $connection;
private $prefix;
/**
* Constructor.
*
* \param $dbConfig Database configuration array
*
* \exception \InvalidArgumentException When connection info is missing
* \exception \InvalidArgumentException On unknown database type
*/
public function __construct(array $dbConfig) {
if (!isset($dbConfig["type"])) {
throw new \InvalidArgumentException("Missing database connection info.");
}
$this->type = $dbConfig["type"];
$this->prefix = !empty($dbConfig["prefix"])
? $dbConfig["prefix"]
: "";
$driverMap = array(
"mysql" => "pdo_mysql",
"sqlite" => "pdo_sqlite",
"postgresql" => "pdo_pgsql",
"oracle" => "oci8", // pdo_oci gives problems with DBAL
"mssql" => "pdo_sqlsrv"
);
if (!isset($driverMap[$this->type])) {
throw new \InvalidArgumentException("Unknown database type.");
}
$params = array(
"driver" => $driverMap[$this->type],
"charset" => "utf8"
);
unset($dbConfig["type"], $dbConfig["prefix"]);
$params = array_merge($params, $dbConfig);
$this->connection = DriverManager::getConnection($params);
$this->connection->setCharset("utf8");
}
/**
* Get the table prefix.
*
* \returns Table prefix
*/
public function getPrefix() {
return $this->prefix;
}
/**
* Get the database's schema.
*
* \returns Schema instance
*/
public function getSchema() {
return new Schema($this->type, $this->connection, $this->prefix);
}
/**
* Begin a database transaction.
*/
public function beginTransaction() {
$this->connection->beginTransaction();
}
/**
* Commit the transaction.
*/
public function commit() {
$this->connection->commit();
}
/**
* Rollback the transaction.
*/
public function rollback() {
$this->connection->rollback();
}
/**
* Transactional run of a code block.
*
* \param $closure Closure or callback
*/
public function transactional($closure) {
$this->connection->transactional($closure);
}
/**
* Create a new query.
*
* \returns Query instance
*/
public function newQuery() {
return new Query($this->connection->createQueryBuilder(),
$this->prefix);
}
/**
* Convert DateTime instances to strings.
*
* \param $data Array
*/
private function setDateTimes(array &$data) {
$new = array();
foreach ($data as $k => $v) {
if ($v instanceof \DateTime) {
$v = $v->format("Y-m-d H:i:s");
}
$new[$k] = $v;
}
$data = $new;
}
/**
* Insert a row into a table.
*
* Example:
*
* \code
* $db->insert("test", array(
* "foo" => "bar"
* ));
* \endcode
*
* \param $tableName Table name
* \param $data Key/value pairs
* \param $hasDateTime Has DateTime instances
* \returns Number of affected rows
*/
public function insert($tableName, array $data, $hasDateTime = FALSE) {
if ($hasDateTime) {
$this->setDateTimes($data);
}
return $this->connection->insert($this->prefix . $tableName, $data);
}
/**
* Update data in a database table.
*
* \param $tableName Table name
* \param $data Key/value pairs
* \param $identifier Key/value pairs as update criteria
* \param $hasDateTime Has DateTime instances
* \returns Number of affected rows
*/
public function update($tableName, array $data, array $identifier,
$hasDateTime = FALSE) {
if ($hasDateTime) {
$this->setDateTimes($data);
}
return $this->connection->update($this->prefix . $tableName, $data,
$identifier);
}
/**
* Delete data from a database table.
*
* \param $tableName Table name
* \param $identifier Key/value pairs as update criteria
* \returns Number of affected rows
*/
public function delete($tableName, array $identifier) {
return $this->connection->delete($this->prefix . $tableName,
$identifier);
}
/**
* Close the database connection.
*/
public function close() {
$this->connection->close();
}
}