PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / vendor / codesvault / howdy-qb / src / Statement / Create.php

Create.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.6, at vendor/codesvault/howdy-qb/src/Statement/Create.php

257 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace CodesVault\Howdyqb\Statement;
4
5 use CodesVault\Howdyqb\Api\CreateInterface;
6 use CodesVault\Howdyqb\SqlGenerator;
7 use CodesVault\Howdyqb\Utilities;
8
9 class Create implements CreateInterface
10 {
11 protected $db;
12 public $sql = [];
13 protected $params = [];
14 protected $column_name;
15 protected $wpdb_object;
16 protected $table_name;
17
18 public function __construct($db, string $table_name)
19 {
20 $this->db = $db;
21 $this->table_name = $table_name;
22
23 $this->start();
24 $this->sql['table_name'] = $this->get_table_name();
25 }
26
27 public function column(string $column_name): self
28 {
29 $this->column_name = $column_name;
30 $this->sql['columns'][$column_name] = [];
31 return $this;
32 }
33
34 public function int(int $size = 255): self
35 {
36 $this->sql['columns'][$this->column_name]['int'] = "INT($size)";
37 return $this;
38 }
39
40 public function bigInt(int $size = 255): self
41 {
42 $this->sql['columns'][$this->column_name]['bigInt'] = "BIGINT($size)";
43 return $this;
44 }
45
46 public function double(): self
47 {
48 $this->sql['columns'][$this->column_name]['double'] = "DOUBLE";
49 return $this;
50 }
51
52 public function boolean(): self
53 {
54 $this->sql['columns'][$this->column_name]['boolean'] = "BOOLEAN";
55 return $this;
56 }
57
58 public function string(int $size = 255): self
59 {
60 $this->sql['columns'][$this->column_name]['string'] = "VARCHAR($size)";
61 return $this;
62 }
63
64 public function text(int $size = 10000): self
65 {
66 $this->sql['columns'][$this->column_name]['text'] = "TEXT($size)";
67 return $this;
68 }
69
70 public function longText(): self
71 {
72 $this->sql['columns'][$this->column_name]['longText'] = "LONGTEXT";
73 return $this;
74 }
75
76 public function json(): self
77 {
78 $this->sql['columns'][$this->column_name]['json'] = "JSON";
79 return $this;
80 }
81
82 public function required(): self
83 {
84 $this->sql['columns'][$this->column_name]['required'] = "NOT NULL";
85 return $this;
86 }
87
88 public function nullable(): self
89 {
90 return $this->default('NULL');
91 }
92
93 public function primary($columns = []): self
94 {
95 if (! empty($columns)) {
96 $this->sql['primary'] = "PRIMARY KEY (" . implode(',', $columns) . ")";
97 return $this;
98 }
99 $this->sql['columns'][$this->column_name]['primary'] = "PRIMARY KEY";
100 return $this;
101 }
102
103 public function index(array $columns): self
104 {
105 $this->sql['index'] = "INDEX (" . implode(',', $columns) . ")";
106 return $this;
107 }
108
109 public function date(): self
110 {
111 $this->sql['columns'][$this->column_name]['date'] = "DATE";
112 return $this;
113 }
114
115 public function dateTime(): self
116 {
117 $this->sql['columns'][$this->column_name]['dateTime'] = "DATETIME";
118 return $this;
119 }
120
121 public function timestamp($default = null, $on_update = null): self
122 {
123 $this->sql['columns'][$this->column_name]['timestamp'] = "TIMESTAMP";
124
125 if ($default === 'now') {
126 $this->sql['columns'][$this->column_name]['default'] = "DEFAULT CURRENT_TIMESTAMP";
127 } elseif ($default && $default !== 'now') {
128 $this->sql['columns'][$this->column_name]['default'] = "DEFAULT " . $default;
129 }
130
131 if ($on_update === 'current') {
132 $this->sql['columns'][$this->column_name]['onUpdate'] = "ON UPDATE CURRENT_TIMESTAMP";
133 } elseif ($on_update && $on_update !== 'current') {
134 $this->sql['columns'][$this->column_name]['onUpdate'] = "ON UPDATE " . $on_update;
135 }
136
137 return $this;
138 }
139
140 public function unsigned(): self
141 {
142 $this->sql['columns'][$this->column_name]['unsigned'] = "UNSIGNED";
143 return $this;
144 }
145
146 public function autoIncrement(): self
147 {
148 $this->sql['columns'][$this->column_name]['autoIncrement'] = "AUTO_INCREMENT";
149 return $this;
150 }
151
152 public function default($value): self
153 {
154 $val = is_string($value) ? "'$value'" : $value;
155 $this->sql['columns'][$this->column_name]['default'] = "DEFAULT $val";
156 return $this;
157 }
158
159 public function foreignKey(string $column, string $ref_table_column, ?string $on_delete = null): self
160 {
161 $ref_table_column = explode('.', $ref_table_column);
162 $table_name = Utilities::get_db_configs()->prefix . $ref_table_column[0];
163 $this->sql['foreignKey'] = "FOREIGN KEY ($column) REFERENCES $table_name ($ref_table_column[1])";
164
165 if ($on_delete) {
166 $this->sql['foreignKey'] .= " ON DELETE " . strtoupper($on_delete);
167 }
168 return $this;
169 }
170
171 public function onDelete(string $action): self
172 {
173 $this->sql['onDelete'] = "ON DELETE " . strtoupper($action);
174 return $this;
175 }
176
177 public function enum(array $allowed): self
178 {
179 $list = '';
180 foreach ($allowed as $value) {
181 if (gettype($value) === 'string') {
182 $list .= "'$value', ";
183 } else {
184 $list .= $value . ", ";
185 }
186 }
187
188 $list = substr(trim($list), 0, -1);
189
190 $this->sql['columns'][$this->column_name]['enum'] = "ENUM(" . $list . ")";
191 return $this;
192 }
193
194 public function decimal($precision = 8, $scale = 2): self
195 {
196 $this->sql['columns'][$this->column_name]['decimal'] = "DECIMAL($precision, $scale)";
197 return $this;
198 }
199
200 public function float(): self
201 {
202 $this->sql['columns'][$this->column_name]['float'] = "FLOAT";
203 return $this;
204 }
205
206 public function unique(): self
207 {
208 if (! isset($this->sql['unique'])) {
209 $this->sql['unique'] = [];
210 }
211 array_push($this->sql['unique'], $this->column_name);
212 return $this;
213 }
214
215 protected function start()
216 {
217 $this->sql['start'] = 'CREATE TABLE IF NOT EXISTS';
218 }
219
220 protected function get_table_name()
221 {
222 return Utilities::get_db_configs()->prefix . $this->table_name;
223 }
224
225 // get only sql query string
226 public function getSql()
227 {
228 $this->start();
229 $query = [
230 'query' => SqlGenerator::create($this->sql),
231 ];
232 return $query;
233 }
234
235 private function driver_execute($sql)
236 {
237 $driver = $this->db;
238 if (class_exists('wpdb') && $driver instanceof \wpdb) {
239 return $driver->query($sql);
240 }
241
242 try {
243 return $driver->exec($sql);
244 } catch (\PDOException $exception) {
245 Utilities::throughException($exception);
246 }
247 }
248
249 public function execute()
250 {
251 $this->start();
252 $query = SqlGenerator::create($this->sql);
253
254 $this->driver_execute($query);
255 }
256 }
257