-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdo-extended.php
62 lines (51 loc) · 2.19 KB
/
pdo-extended.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
<?php
class PDO_Extended extends PDO {
private $current_database = null;
public function __construct($dsn, $username = null, $password = null, $options = null) {
parent::__construct($dsn, $username, $password, $options);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected to MySQL\n";
}
public function get_innodb_status() {
$mysql_engine_status_query = $this->query('SHOW ENGINE INNODB STATUS');
$mysql_engine_status = $mysql_engine_status_query->fetchAll(PDO::FETCH_ASSOC);
return $mysql_engine_status[0]['Status'];
}
public function get_global_variables() : array {
$mysql_global_variables_query = $this->query('SHOW GLOBAL VARIABLES');
$mysql_global_variables = [];
foreach ($mysql_global_variables_query->fetchAll(PDO::FETCH_ASSOC) as $variable) {
$mysql_global_variables[$variable['Variable_name']] = $variable['Value'];
}
return $mysql_global_variables;
}
public function set_global_variable($variable_name, $variable_value) {
if (false === in_array($variable_value, ['ON', 'OFF']) && is_string($variable_value)) {
$variable_value = "'$variable_value'";
}
$this->query("SET GLOBAL $variable_name = $variable_value");
}
public function database_exists($database_name) {
$show_databases_query = $this->query('SHOW DATABASES');
$databases = $show_databases_query->fetchAll(PDO::FETCH_COLUMN);
return in_array($database_name, $databases);
}
public function show_databases() {
$show_databases_query = $this->query('SHOW DATABASES');
return $show_databases_query->fetchAll(PDO::FETCH_COLUMN);
}
public function use_database($database_name) : bool {
if ($this->current_database == $database_name) {
return true;
}
if (!$this->database_exists($database_name)) {
return false;
}
$this->query("USE `$database_name`");
return true;
}
public function explain($query) {
$explain_query = $this->query("EXPLAIN $query");
return $explain_query->fetchAll(PDO::FETCH_ASSOC);
}
}