1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Query |
4
|
|
|
* |
5
|
|
|
* SQL Query Builder / Database Abstraction Layer |
6
|
|
|
* |
7
|
|
|
* PHP version 5.4 |
8
|
|
|
* |
9
|
|
|
* @package Query |
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
11
|
|
|
* @copyright 2012 - 2015 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
|
|
|
|
17
|
|
|
// -------------------------------------------------------------------------- |
18
|
|
|
|
19
|
|
|
namespace Query\Drivers; |
20
|
|
|
|
21
|
|
|
// -------------------------------------------------------------------------- |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Abstract class defining database / table creation methods |
25
|
|
|
* |
26
|
|
|
* @package Query |
27
|
|
|
* @subpackage Drivers |
28
|
|
|
* @method string quote_ident(string $sql) |
29
|
|
|
* @method string quote_table(string $sql) |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractUtil { |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Reference to the current connection object |
35
|
|
|
* @var DriverInterface |
36
|
|
|
*/ |
37
|
|
|
private $conn; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Save a reference to the connection object for later use |
41
|
|
|
* |
42
|
|
|
* @param DriverInterface $conn |
43
|
|
|
*/ |
44
|
|
|
public function __construct(DriverInterface $conn) |
45
|
|
|
{ |
46
|
|
|
$this->conn = $conn; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// -------------------------------------------------------------------------- |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the driver object for the current connection |
53
|
|
|
* |
54
|
|
|
* @return Driver_Interface |
55
|
|
|
*/ |
56
|
|
|
public function get_driver() |
57
|
|
|
{ |
58
|
|
|
return $this->conn; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// -------------------------------------------------------------------------- |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Convenience public function to generate sql for creating a db table |
65
|
|
|
* |
66
|
|
|
* @param string $name |
67
|
|
|
* @param array $fields |
68
|
|
|
* @param array $constraints |
69
|
|
|
* @param bool $if_not_exists |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function create_table($name, $fields, array $constraints=[], $if_not_exists=TRUE) |
73
|
|
|
{ |
74
|
|
|
$exists_str = ($if_not_exists) ? ' IF NOT EXISTS ' : ' '; |
75
|
|
|
|
76
|
|
|
// Reorganize into an array indexed with column information |
77
|
|
|
// Eg $column_array[$colname] = array( |
78
|
|
|
// 'type' => ..., |
79
|
|
|
// 'constraint' => ..., |
80
|
|
|
// 'index' => ..., |
81
|
|
|
// ) |
82
|
|
|
$column_array = \array_zipper([ |
83
|
|
|
'type' => $fields, |
84
|
|
|
'constraint' => $constraints |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
// Join column definitions together |
88
|
|
|
$columns = []; |
89
|
|
|
foreach($column_array as $n => $props) |
90
|
|
|
{ |
91
|
|
|
$str = $this->get_driver()->quote_ident($n); |
92
|
|
|
$str .= (isset($props['type'])) ? " {$props['type']}" : ""; |
93
|
|
|
$str .= (isset($props['constraint'])) ? " {$props['constraint']}" : ""; |
94
|
|
|
|
95
|
|
|
$columns[] = $str; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Generate the sql for the creation of the table |
99
|
|
|
$sql = 'CREATE TABLE'.$exists_str.$this->get_driver()->quote_table($name).' ('; |
100
|
|
|
$sql .= implode(', ', $columns); |
101
|
|
|
$sql .= ')'; |
102
|
|
|
|
103
|
|
|
return $sql; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// -------------------------------------------------------------------------- |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Drop the selected table |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function delete_table($name) |
115
|
|
|
{ |
116
|
|
|
return 'DROP TABLE IF EXISTS '.$this->get_driver()->quote_table($name); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
// -------------------------------------------------------------------------- |
121
|
|
|
// ! Abstract Methods |
122
|
|
|
// -------------------------------------------------------------------------- |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return an SQL file with the database table structure |
126
|
|
|
* |
127
|
|
|
* @abstract |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
abstract public function backup_structure(); |
131
|
|
|
|
132
|
|
|
// -------------------------------------------------------------------------- |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return an SQL file with the database data as insert statements |
136
|
|
|
* |
137
|
|
|
* @abstract |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
abstract public function backup_data(); |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
// End of abstract_util.php |