|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\ConfigInterface; |
|
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Database |
|
10
|
|
|
* |
|
11
|
|
|
* Drop and Create the Actual Database using raw mysqli |
|
12
|
|
|
* |
|
13
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Schema |
|
14
|
|
|
*/ |
|
15
|
|
|
class Database |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @see https://dev.mysql.com/doc/refman/5.7/en/identifiers.html |
|
20
|
|
|
*/ |
|
21
|
|
|
public const MAX_IDENTIFIER_LENGTH = 64; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ConfigInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var null|\mysqli |
|
30
|
|
|
*/ |
|
31
|
|
|
private $link; |
|
32
|
|
|
|
|
33
|
1 |
|
public function __construct(ConfigInterface $config) |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->config = $config; |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return \mysqli |
|
40
|
|
|
* @throws DoctrineStaticMetaException |
|
41
|
|
|
*/ |
|
42
|
|
|
private function connect(): \mysqli |
|
43
|
|
|
{ |
|
44
|
|
|
if (null === $this->link) { |
|
45
|
|
|
$this->link = mysqli_connect( |
|
46
|
|
|
$this->config->get(ConfigInterface::PARAM_DB_HOST), |
|
47
|
|
|
$this->config->get(ConfigInterface::PARAM_DB_USER), |
|
48
|
|
|
$this->config->get(ConfigInterface::PARAM_DB_PASS) |
|
49
|
|
|
); |
|
50
|
|
|
if (!$this->link) { |
|
51
|
|
|
throw new DoctrineStaticMetaException('Failed getting connection in '.__METHOD__); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->link; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function throwUnsure(): void |
|
59
|
|
|
{ |
|
60
|
|
|
throw new \InvalidArgumentException('You must pass in `true` to show you are sure'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* You have to pass in true to confirm you really want to do it |
|
65
|
|
|
* |
|
66
|
|
|
* @param bool $sure |
|
67
|
|
|
* |
|
68
|
|
|
* @return Database |
|
69
|
|
|
* @throws \InvalidArgumentException |
|
70
|
|
|
* @throws DoctrineStaticMetaException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function drop($sure): Database |
|
73
|
|
|
{ |
|
74
|
|
|
if (true !== $sure) { |
|
75
|
|
|
$this->throwUnsure(); |
|
76
|
|
|
} |
|
77
|
|
|
$link = $this->connect(); |
|
78
|
|
|
$sql = "DROP DATABASE IF EXISTS `{$this->config->get(ConfigInterface::PARAM_DB_NAME)}`"; |
|
79
|
|
|
if (true !== mysqli_query($link, $sql)) { |
|
80
|
|
|
throw new DoctrineStaticMetaException( |
|
81
|
|
|
'Failed to drop the database ' |
|
82
|
|
|
.$this->config->get(ConfigInterface::PARAM_DB_NAME) |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* You have to pass in true to confirm you really want to do it |
|
91
|
|
|
* |
|
92
|
|
|
* @param bool $sure |
|
93
|
|
|
* |
|
94
|
|
|
* @return Database |
|
95
|
|
|
* @throws \InvalidArgumentException |
|
96
|
|
|
* @throws DoctrineStaticMetaException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function create($sure): Database |
|
99
|
|
|
{ |
|
100
|
|
|
if (true !== $sure) { |
|
101
|
|
|
$this->throwUnsure(); |
|
102
|
|
|
} |
|
103
|
|
|
$link = $this->connect(); |
|
104
|
|
|
$sql = 'CREATE DATABASE IF NOT EXISTS `' |
|
105
|
|
|
.$this->config->get(ConfigInterface::PARAM_DB_NAME) |
|
106
|
|
|
.'` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci'; |
|
107
|
|
|
if (true !== mysqli_query($link, $sql)) { |
|
108
|
|
|
throw new DoctrineStaticMetaException( |
|
109
|
|
|
'Failed to create the database ' |
|
110
|
|
|
.$this->config->get(ConfigInterface::PARAM_DB_NAME) |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function close(): void |
|
118
|
|
|
{ |
|
119
|
|
|
if (null !== $this->link) { |
|
120
|
|
|
mysqli_close($this->link); |
|
121
|
|
|
$this->link = null; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function __destruct() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->close(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|