|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
+-----------------------------------------------------------------------------+ |
|
6
|
|
|
| PHPPackage - PDO Wrapper |
|
7
|
|
|
+-----------------------------------------------------------------------------+ |
|
8
|
|
|
| Copyright (c)2018 (http://github.com/phppackage/pdo-wrapper) |
|
9
|
|
|
+-----------------------------------------------------------------------------+ |
|
10
|
|
|
| This source file is subject to MIT License |
|
11
|
|
|
| that is bundled with this package in the file LICENSE. |
|
12
|
|
|
| |
|
13
|
|
|
| If you did not receive a copy of the license and are unable to |
|
14
|
|
|
| obtain it through the world-wide-web, please send an email |
|
15
|
|
|
| to [email protected] so we can send you a copy immediately. |
|
16
|
|
|
+-----------------------------------------------------------------------------+ |
|
17
|
|
|
| Authors: |
|
18
|
|
|
| Lawrence Cherone <[email protected]> |
|
19
|
|
|
+-----------------------------------------------------------------------------+ |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace PHPPackage\PDOWrapper; |
|
23
|
|
|
|
|
24
|
|
|
class Database |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \PHPPackage\PDOWrapper\PDO |
|
28
|
|
|
*/ |
|
29
|
|
|
private $pdo; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array PDO attribute keys. |
|
33
|
|
|
*/ |
|
34
|
|
|
private $attributes = array( |
|
35
|
|
|
'AUTOCOMMIT', 'ERRMODE', 'CASE', 'CLIENT_VERSION', 'CONNECTION_STATUS', |
|
36
|
|
|
'ORACLE_NULLS', 'PERSISTENT', 'PREFETCH', 'SERVER_INFO', 'SERVER_VERSION', |
|
37
|
|
|
'TIMEOUT', 'DRIVER_NAME' |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
11 |
|
public function __construct(PDO $pdo) |
|
44
|
|
|
{ |
|
45
|
11 |
|
$this->pdo = $pdo; |
|
46
|
11 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Enumarate PDO attributes |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key Pick out a attribute by key |
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
6 |
|
public function info(string $key = null) |
|
55
|
|
|
{ |
|
56
|
6 |
|
$return = []; |
|
57
|
6 |
|
foreach ($this->attributes as $value) { |
|
58
|
|
|
try { |
|
59
|
6 |
|
$return['PDO::ATTR_'.$value] = $this->pdo->getAttribute(constant('PDO::ATTR_'.$value)); |
|
60
|
6 |
|
} catch (\PDOException $e) { |
|
61
|
6 |
|
$return['PDO::ATTR_'.$value] = null; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
6 |
|
return (!is_null($key) && isset($return[$key])) ? $return[$key] : $return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get database name from dsn |
|
69
|
|
|
* |
|
70
|
|
|
* @throws RuntimeException |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function name(string $dsn): string |
|
74
|
|
|
{ |
|
75
|
|
|
// match database from dsn & set working vars |
|
76
|
4 |
|
if (!preg_match('/dbname=(\w+);/', $dsn, $results)) { |
|
77
|
1 |
|
throw new \RuntimeException('Could not match database name from dsn'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
return $results[1]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create database |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function create(string $name, $username, $password): bool |
|
89
|
|
|
{ |
|
90
|
1 |
|
if (!in_array($name, $this->databases())) { |
|
91
|
1 |
|
return (bool) $this->pdo->exec(" |
|
92
|
1 |
|
CREATE DATABASE `$name`; |
|
93
|
1 |
|
CREATE USER '{$username}'@'%' IDENTIFIED BY '{$password}'; |
|
94
|
1 |
|
GRANT ALL ON `$name`.* TO '{$username}'@'%'; |
|
95
|
|
|
FLUSH PRIVILEGES; |
|
96
|
|
|
"); |
|
97
|
|
|
} |
|
98
|
1 |
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns an array of databases |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
2 |
|
public function databases(): array |
|
106
|
|
|
{ |
|
107
|
2 |
|
$stmt = $this->pdo->query("SHOW DATABASES"); |
|
108
|
|
|
|
|
109
|
1 |
|
$result = []; |
|
110
|
1 |
|
while ($row = $stmt->fetchColumn(0)) { |
|
111
|
1 |
|
$result[] = $row; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns an array of tables |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function tables(): array |
|
122
|
|
|
{ |
|
123
|
1 |
|
$stmt = $this->pdo->query("SHOW TABLES"); |
|
124
|
|
|
|
|
125
|
1 |
|
$result = []; |
|
126
|
1 |
|
while ($row = $stmt->fetchColumn(0)) { |
|
127
|
1 |
|
$result[] = $row; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|