|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.5 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Factories; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
|
18
|
|
|
use Quantum\Libraries\Database\Schemas\Table; |
|
19
|
|
|
use Quantum\Libraries\Database\Database; |
|
20
|
|
|
use PDOException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class TableFactory |
|
24
|
|
|
* @package Quantum\Factory |
|
25
|
|
|
*/ |
|
26
|
|
|
class TableFactory |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Creates new table |
|
31
|
|
|
* @param string $name |
|
32
|
|
|
* @return Table |
|
33
|
|
|
* @throws DatabaseException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function create(string $name): Table |
|
36
|
|
|
{ |
|
37
|
|
|
if ($this->checkTableExists($name)) { |
|
38
|
|
|
throw DatabaseException::tableAlreadyExists($name); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->createInstance($name)->setAction(Table::CREATE); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the table |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @return Table |
|
48
|
|
|
* @throws DatabaseException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function get(string $name): Table |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$this->checkTableExists($name)) { |
|
53
|
|
|
throw DatabaseException::tableDoesNotExists($name); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->createInstance($name)->setAction(Table::ALTER); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Renames the table |
|
61
|
|
|
* @param string $oldName |
|
62
|
|
|
* @param string $newName |
|
63
|
|
|
* @return bool |
|
64
|
|
|
* @throws DatabaseException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function rename(string $oldName, string $newName): bool |
|
67
|
|
|
{ |
|
68
|
|
|
if (!$this->checkTableExists($oldName)) { |
|
69
|
|
|
throw DatabaseException::tableDoesNotExists($oldName); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->createInstance($oldName)->setAction(Table::RENAME, ['newName' => $newName]); |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Drops the table |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* @return bool |
|
80
|
|
|
* @throws DatabaseException |
|
81
|
|
|
*/ |
|
82
|
|
|
public function drop(string $name): bool |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->checkTableExists($name)) { |
|
85
|
|
|
throw DatabaseException::tableDoesNotExists($name); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->createInstance($name)->setAction(Table::DROP); |
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Checks if the DB table exists |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @return bool |
|
96
|
|
|
* @throws DatabaseException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function checkTableExists(string $name): bool |
|
99
|
|
|
{ |
|
100
|
|
|
try { |
|
101
|
|
|
Database::query('SELECT 1 FROM ' . $name); |
|
102
|
|
|
} catch (PDOException $e) { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Creates new Table instance |
|
111
|
|
|
* @param string $name |
|
112
|
|
|
* @return Table |
|
113
|
|
|
*/ |
|
114
|
|
|
private function createInstance(string $name): Table |
|
115
|
|
|
{ |
|
116
|
|
|
return new Table($name); |
|
117
|
|
|
} |
|
118
|
|
|
} |