|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Query |
|
4
|
|
|
* |
|
5
|
|
|
* SQL Query Builder / Database Abstraction Layer |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7 |
|
8
|
|
|
* |
|
9
|
|
|
* @package Query |
|
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
|
11
|
|
|
* @copyright 2012 - 2016 Timothy J. Warren |
|
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
13
|
|
|
* @link https://git.timshomepage.net/aviat4ion/Query |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Query\Drivers; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Abstract class defining database / table creation methods |
|
20
|
|
|
* |
|
21
|
|
|
* @method string quoteIdent(string $sql) |
|
22
|
|
|
* @method string quoteTable(string $sql) |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractUtil { |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Reference to the current connection object |
|
28
|
|
|
* @var DriverInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $conn; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Save a reference to the connection object for later use |
|
34
|
|
|
* |
|
35
|
|
|
* @param DriverInterface $conn |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(DriverInterface $conn) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->conn = $conn; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the driver object for the current connection |
|
44
|
|
|
* |
|
45
|
|
|
* @return DriverInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getDriver() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->conn; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Convenience public function to generate sql for creating a db table |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $name |
|
56
|
|
|
* @param array $fields |
|
57
|
|
|
* @param array $constraints |
|
58
|
|
|
* @param bool $ifNotExists |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function createTable($name, $fields, array $constraints=[], $ifNotExists=TRUE) |
|
62
|
|
|
{ |
|
63
|
|
|
$existsStr = ($ifNotExists) ? ' IF NOT EXISTS ' : ' '; |
|
64
|
|
|
|
|
65
|
|
|
// Reorganize into an array indexed with column information |
|
66
|
|
|
// Eg $columnArray[$colname] = array( |
|
67
|
|
|
// 'type' => ..., |
|
68
|
|
|
// 'constraint' => ..., |
|
69
|
|
|
// 'index' => ..., |
|
70
|
|
|
// ) |
|
71
|
|
|
$columnArray = \array_zipper([ |
|
72
|
|
|
'type' => $fields, |
|
73
|
|
|
'constraint' => $constraints |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
// Join column definitions together |
|
77
|
|
|
$columns = []; |
|
78
|
|
|
foreach($columnArray as $n => $props) |
|
79
|
|
|
{ |
|
80
|
|
|
$str = $this->getDriver()->quoteIdent($n); |
|
81
|
|
|
$str .= (isset($props['type'])) ? " {$props['type']}" : ""; |
|
82
|
|
|
$str .= (isset($props['constraint'])) ? " {$props['constraint']}" : ""; |
|
83
|
|
|
|
|
84
|
|
|
$columns[] = $str; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Generate the sql for the creation of the table |
|
88
|
|
|
$sql = 'CREATE TABLE'.$existsStr.$this->getDriver()->quoteTable($name).' ('; |
|
89
|
|
|
$sql .= implode(', ', $columns); |
|
90
|
|
|
$sql .= ')'; |
|
91
|
|
|
|
|
92
|
|
|
return $sql; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Drop the selected table |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $name |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function deleteTable($name) |
|
102
|
|
|
{ |
|
103
|
|
|
return 'DROP TABLE IF EXISTS '.$this->getDriver()->quoteTable($name); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// -------------------------------------------------------------------------- |
|
107
|
|
|
// ! Abstract Methods |
|
108
|
|
|
// -------------------------------------------------------------------------- |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return an SQL file with the database table structure |
|
112
|
|
|
* |
|
113
|
|
|
* @abstract |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
abstract public function backupStructure(); |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return an SQL file with the database data as insert statements |
|
120
|
|
|
* |
|
121
|
|
|
* @abstract |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
abstract public function backupData(); |
|
125
|
|
|
|
|
126
|
|
|
} |