-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySQLConnection.php
141 lines (109 loc) · 4.06 KB
/
MySQLConnection.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
<?php
require_once('AbstractDBConnection.php');
class MySQLConnection extends AbstractDBConnection{
var $dbName;
var $persistentConnection = true;
var $pdo;
public function __construct($hostname, $username, $password, $dbName){
parent::__construct($hostname, $username, $password);
$this->dbName = $dbName;
}
public function getPDO() {
if(!isset($this->pdo)){
$dsn = 'mysql:dbname=' . $this->dbName . ';host=' . $this->hostname . ';';
$options = array(PDO::ATTR_PERSISTENT => $this->isPersistentConnection());
$this->pdo = new PDO($dsn, $this->username, $this->password, $options);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
return $this->pdo;
}
public function closePDO() {
$this->pdo = null;
}
public function query($query, $parameters=array()){
if(!is_array($parameters)){
//Assume that there is only one parameter in the query, and it has been passed in directly instead of in an array.
$parameters = array($parameters);
}
$adjustedArguments = $this->adjustQueryArguments($query, $parameters);
$query = $adjustedArguments[0];
$parameters = $adjustedArguments[1];
$stmt = $this->getPDO()->prepare($query);
if(!$stmt->execute($parameters)){
$errorInfo = $stmt->errorInfo();
$errorMessage = $errorInfo[2];
throw new Exception("An error occurred while executing the following query: $query <br/>With the following parameters: " . print_r($parameters, true) . "<br/>Error: $errorMessage");
}
return $stmt;
}
public function insert($tableName, $parameters){
return $this->insertOrUpdate($tableName, $parameters, false);
}
/**
* This function was based on the pdo_insert() function here:
* http://stackoverflow.com/questions/4587988/how-to-pass-an-array-of-rows-to-pdo-to-insert-them
*
* It seems like PDO would have this feature built in.....
*/
public function insertOrUpdate($table, $parameters=array(), $orUpdate=true){
if (!is_array($parameters) || !count($parameters)){
throw new Exception("Invalid parameters: " . print_r($parameters, true));
}
$bind = ':'.implode(',:', array_keys($parameters));
$sql = 'insert into '.$table.' (`'.implode('`,`', array_keys($parameters)).'`) '.
'values ('.$bind.')';
if($orUpdate){
$parameterUpdateSQL = '';
foreach($parameters as $name=>$value){
if(!empty($parameterUpdateSQL)){
$parameterUpdateSQL .= ',';
}
$parameterUpdateSQL .= "`$name`=:$name";
}
$sql .= " on duplicate key update $parameterUpdateSQL";
}
$stmt = $this->query($sql, $parameters);
/**
* An update will only count as changing rows if the update changed a value,
* so don't throw an exception on update if the row count didn't change.
*/
if(!$orUpdate && $stmt->rowCount() != 1){
throw new Exception("The following insert did not affect the expected number of rows: $sql <br/>\nWith Parameters: " . print_r($parameters, true));
}
return $stmt;
}
/**
* Adjust the query parameters to support array arguments
*/
public function adjustQueryArguments($query, $parameters){
$newParameters = array();
foreach($parameters as $key=>$var){
if(is_array($var)){
$paramList = "";
foreach($var as $index=>$arrayVar){
$arrayParamName = "$key$index";
if(!empty($paramList)){
$paramList .= ',';
}
$paramList .= ":$arrayParamName";
$newParameters[$arrayParamName] = $arrayVar;
}
$query = str_replace(":$key", "($paramList)", $query);
}else{
$newParameters[$key] = $var;
}
}
return array($query, $newParameters);
}
public function isPersistentConnection()
{
return $this->persistentConnection;
}
public function setPersistentConnection($persistentConnection)
{
if(isset($this->pdo)){
throw new Exception("This connection has already been initialized, so it is not possible to change whether or not it is persistent.");
}
$this->persistentConnection = $persistentConnection;
}
}