1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Subreality\Dilmun\Enki; |
4
|
|
|
|
5
|
|
|
use Subreality\Dilmun\LoggedClassTrait; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class MySql |
9
|
|
|
* @package Subreality\Dilmun\Enki |
10
|
|
|
*/ |
11
|
|
|
class MySql implements EnkiInterface |
12
|
|
|
{ |
13
|
|
|
use LoggedClassTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \PDO |
17
|
|
|
*/ |
18
|
|
|
private $pdo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Provides a MySQL connection interface that catches a PDOException on failure. |
22
|
|
|
* |
23
|
|
|
* @param string $host The host name for the MySQL connection |
24
|
|
|
* @param string $user The username for the MySQL connection |
25
|
|
|
* @param string $pass The password for the MySQL connection |
26
|
|
|
* @param string $name The database name for the MySQL connection |
27
|
|
|
* |
28
|
|
|
* @return bool Returns true if the connection was successful; returns false otherwise |
29
|
|
|
*/ |
30
|
14 |
|
public function connect($host, $user, $pass, $name) |
31
|
|
|
{ |
32
|
14 |
|
$dsn = "mysql:host={$host};dbname={$name}"; |
33
|
|
|
|
34
|
|
|
try { |
35
|
14 |
|
$this->pdo = new \PDO($dsn, $user, $pass); |
36
|
13 |
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
37
|
|
|
|
38
|
13 |
|
$connected = true; |
39
|
14 |
|
} catch (\PDOException $pdo_e) { |
40
|
1 |
|
$context = array(); |
41
|
|
|
|
42
|
1 |
|
$context["exception"] = $pdo_e; |
43
|
|
|
|
44
|
1 |
|
$this->updateLog("error", "MySQL connection failed with the following message: {message}", $context); |
45
|
|
|
|
46
|
1 |
|
$connected = false; |
47
|
|
|
} |
48
|
|
|
|
49
|
14 |
|
return $connected; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Destroys an existing PDO connection |
54
|
|
|
* |
55
|
|
|
* @return bool Returns true if the connection existed and was destroyed; returns false otherwise |
56
|
|
|
*/ |
57
|
2 |
|
public function disconnect() |
58
|
|
|
{ |
59
|
2 |
|
if ($this->pdo instanceof \PDO) { |
60
|
1 |
|
$this->pdo = null; |
61
|
|
|
|
62
|
1 |
|
$disconnected = true; |
63
|
1 |
|
} else { |
64
|
1 |
|
$disconnected = false; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
return $disconnected; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Gets the column names for all primary keys for a table or returns false if no keys exist |
72
|
|
|
* |
73
|
|
|
* @param string $table The table from which keys will be retrieved |
74
|
|
|
* |
75
|
|
|
* @return mixed Returns an array of column names if the table has a compound primary key; |
76
|
|
|
* returns a single column name if the table has only one primary key; |
77
|
|
|
* returns false if the table has no primary keys |
78
|
|
|
*/ |
79
|
9 |
|
public function getPrimaryKeys($table) |
80
|
|
|
{ |
81
|
9 |
|
$table_exists = $this->tableExists($table); |
82
|
|
|
|
83
|
9 |
|
if ($table_exists) { |
84
|
8 |
|
$keys_query = "SHOW KEYS FROM {$table} WHERE Key_name='PRIMARY'"; |
85
|
|
|
|
86
|
8 |
|
$statement = $this->pdo->prepare($keys_query); |
87
|
|
|
|
88
|
8 |
|
$statement->execute(); |
89
|
|
|
|
90
|
8 |
|
$result = $statement->fetchAll(); |
91
|
|
|
|
92
|
8 |
|
$row_count = $statement->rowCount(); |
93
|
|
|
|
94
|
8 |
|
if ($row_count > 1) { |
95
|
2 |
|
$keys = array(); |
96
|
|
|
|
97
|
2 |
|
foreach ($result as $row) { |
98
|
2 |
|
$keys[] = $row['Column_name']; |
99
|
2 |
|
} |
100
|
8 |
|
} elseif ($row_count == 1) { |
101
|
4 |
|
$keys = $result[0]['Column_name']; |
102
|
4 |
|
} else { |
103
|
2 |
|
$keys = false; |
104
|
|
|
} |
105
|
8 |
|
} else { |
106
|
1 |
|
$keys = false; |
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
return $keys; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieves a row represented by a single primary key |
114
|
|
|
* |
115
|
|
|
* @param int|string $id A unique id for a table |
116
|
|
|
* @param string $table The table from which a row will be retrieved |
117
|
|
|
* |
118
|
|
|
* @return array|false Returns an associative array representing the row corresponding with the primary key; |
119
|
|
|
* returns false if the table does not exist, if the provided id does not correspond with a |
120
|
|
|
* record, or if the table has a compound primary key |
121
|
|
|
*/ |
122
|
5 |
|
public function getRowById($id, $table) |
123
|
|
|
{ |
124
|
5 |
|
$key = $this->getPrimaryKeys($table); |
125
|
|
|
|
126
|
5 |
|
if (is_string($id)) { |
127
|
1 |
|
$query_id = "'{$id}'"; |
128
|
1 |
|
} else { |
129
|
4 |
|
$query_id = $id; |
130
|
|
|
} |
131
|
|
|
|
132
|
5 |
|
if ($key === false) { |
133
|
1 |
|
$row = false; |
134
|
5 |
|
} elseif (is_array($key)) { |
135
|
1 |
|
$row = false; |
136
|
1 |
|
} else { |
137
|
3 |
|
$query = "SELECT * FROM {$table} |
138
|
3 |
|
WHERE {$key} = {$query_id}"; |
139
|
|
|
|
140
|
3 |
|
$statement = $this->pdo->prepare($query); |
141
|
|
|
|
142
|
3 |
|
$statement->execute(); |
143
|
|
|
|
144
|
3 |
|
$result = $statement->fetchAll(\PDO::FETCH_ASSOC); |
145
|
|
|
|
146
|
3 |
|
if (empty($result)) { |
147
|
1 |
|
$row = false; |
148
|
1 |
|
} else { |
149
|
2 |
|
$row = $result[0]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
5 |
|
return $row; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Determines whether a given table exists |
158
|
|
|
* |
159
|
|
|
* @param string $table The table that will be checked for existence |
160
|
|
|
* @return bool Returns true if the table exists; returns false otherwise |
161
|
|
|
*/ |
162
|
11 |
|
public function tableExists($table) |
163
|
|
|
{ |
164
|
11 |
|
$table_query = "SHOW TABLES LIKE '{$table}'"; |
165
|
|
|
|
166
|
11 |
|
$statement = $this->pdo->prepare($table_query); |
167
|
|
|
|
168
|
11 |
|
$statement->execute(); |
169
|
|
|
|
170
|
11 |
|
$row_count = $statement->rowCount(); |
171
|
|
|
|
172
|
11 |
|
if ($row_count == 0) { |
|
|
|
|
173
|
2 |
|
return false; |
174
|
|
|
} else { |
175
|
9 |
|
return true; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|