|
15 | 15 | use StellarWP\DB\DB; |
16 | 16 | use StellarWP\Shepherd\Config; |
17 | 17 | use StellarWP\Shepherd\Tables\Utility\Safe_Dynamic_Prefix; |
18 | | -use StellarWP\Shepherd\Traits\Custom_Table_Query_Methods; |
19 | | -use DateTimeInterface; |
20 | 18 |
|
21 | 19 | /** |
22 | 20 | * Class Table_Abstract |
|
26 | 24 | * @package StellarWP\Shepherd\Abstracts |
27 | 25 | */ |
28 | 26 | abstract class Table_Abstract extends Table { |
29 | | - use Custom_Table_Query_Methods; |
30 | | - |
31 | | - /** |
32 | | - * The PHP type for an integer. |
33 | | - * |
34 | | - * @since 0.0.1 |
35 | | - * |
36 | | - * @var string |
37 | | - */ |
38 | | - public const PHP_TYPE_INT = 'int'; |
39 | | - |
40 | | - /** |
41 | | - * The PHP type for a string. |
42 | | - * |
43 | | - * @since 0.0.1 |
44 | | - * |
45 | | - * @var string |
46 | | - */ |
47 | | - public const PHP_TYPE_STRING = 'string'; |
48 | | - |
49 | | - /** |
50 | | - * The PHP type for a float. |
51 | | - * |
52 | | - * @since 0.0.1 |
53 | | - * |
54 | | - * @var string |
55 | | - */ |
56 | | - public const PHP_TYPE_FLOAT = 'float'; |
57 | | - |
58 | | - /** |
59 | | - * The PHP type for a boolean. |
60 | | - * |
61 | | - * @since 0.0.1 |
62 | | - * |
63 | | - * @var string |
64 | | - */ |
65 | | - public const PHP_TYPE_BOOL = 'bool'; |
66 | | - |
67 | | - /** |
68 | | - * The PHP type for a datetime. |
69 | | - * |
70 | | - * @since 0.0.1 |
71 | | - * |
72 | | - * @var string |
73 | | - */ |
74 | | - public const PHP_TYPE_DATETIME = DateTimeInterface::class; |
75 | | - |
76 | | - /** |
77 | | - * The column type for a bigint. |
78 | | - * |
79 | | - * @since 0.0.1 |
80 | | - * |
81 | | - * @var string |
82 | | - */ |
83 | | - public const COLUMN_TYPE_BIGINT = 'bigint'; |
84 | | - |
85 | | - /** |
86 | | - * The column type for a varchar. |
87 | | - * |
88 | | - * @since 0.0.1 |
89 | | - * |
90 | | - * @var string |
91 | | - */ |
92 | | - public const COLUMN_TYPE_VARCHAR = 'varchar'; |
93 | | - |
94 | | - /** |
95 | | - * The column type for a text. |
96 | | - * |
97 | | - * @since 0.0.1 |
98 | | - * |
99 | | - * @var string |
100 | | - */ |
101 | | - public const COLUMN_TYPE_TEXT = 'text'; |
102 | | - |
103 | | - /** |
104 | | - * The column type for a longtext. |
105 | | - * |
106 | | - * @since 0.0.1 |
107 | | - * |
108 | | - * @var string |
109 | | - */ |
110 | | - public const COLUMN_TYPE_LONGTEXT = 'longtext'; |
111 | | - |
112 | | - /** |
113 | | - * The column type for a timestamp. |
114 | | - * |
115 | | - * @since 0.0.1 |
116 | | - * |
117 | | - * @var string |
118 | | - */ |
119 | | - public const COLUMN_TYPE_TIMESTAMP = 'timestamp'; |
120 | | - |
121 | | - public const SQL_RESERVED_DEFAULTS = [ |
122 | | - 'CURRENT_TIMESTAMP', |
123 | | - 'CURRENT_DATE', |
124 | | - 'CURRENT_TIME', |
125 | | - ]; |
126 | | - |
127 | | - /** |
128 | | - * The indexes for the table. |
129 | | - * |
130 | | - * @since 0.0.1 |
131 | | - * |
132 | | - * @var array<array<string, string>> |
133 | | - */ |
134 | | - public const INDEXES = []; |
135 | | - |
136 | 27 | /** |
137 | 28 | * Constructor. |
138 | 29 | * |
@@ -174,130 +65,6 @@ public static function get_schema_slug(): string { |
174 | 65 | return sprintf( static::$schema_slug, Config::get_hook_prefix() ); |
175 | 66 | } |
176 | 67 |
|
177 | | - /** |
178 | | - * An array of all the columns in the table. |
179 | | - * |
180 | | - * @since 0.0.1 |
181 | | - * |
182 | | - * @return array<string, array<string, string>> |
183 | | - */ |
184 | | - abstract public static function get_columns(): array; |
185 | | - |
186 | | - /** |
187 | | - * An array of all the columns that are searchable. |
188 | | - * |
189 | | - * @since 0.0.1 |
190 | | - * |
191 | | - * @return string[] |
192 | | - */ |
193 | | - public static function get_searchable_columns(): array { |
194 | | - return []; |
195 | | - } |
196 | | - |
197 | | - /** |
198 | | - * Helper method to check and add an index to a table. |
199 | | - * |
200 | | - * @since 0.0.1 |
201 | | - * |
202 | | - * @param array $results The results array to track changes. |
203 | | - * @param string $index_name The name of the index. |
204 | | - * @param string $columns The columns to index. |
205 | | - * |
206 | | - * @return array The updated results array. |
207 | | - */ |
208 | | - protected function check_and_add_index( array $results, string $index_name, string $columns ): array { |
209 | | - $index_name = esc_sql( $index_name ); |
210 | | - |
211 | | - // Add index only if it does not exist. |
212 | | - if ( $this->has_index( $index_name ) ) { |
213 | | - return $results; |
214 | | - } |
215 | | - |
216 | | - $columns = esc_sql( $columns ); |
217 | | - |
218 | | - DB::query( |
219 | | - DB::prepare( "ALTER TABLE %i ADD INDEX `{$index_name}` ( {$columns} )", esc_sql( static::table_name( true ) ) ) |
220 | | - ); |
221 | | - |
222 | | - return $results; |
223 | | - } |
224 | | - |
225 | | - /** |
226 | | - * Returns the table creation SQL in the format supported |
227 | | - * by the `dbDelta` function. |
228 | | - * |
229 | | - * @since 0.0.1 |
230 | | - * @since 0.0.3 Updated to remove an empty line after the columns and before the primary key. |
231 | | - * |
232 | | - * @return string The table creation SQL, in the format supported |
233 | | - * by the `dbDelta` function. |
234 | | - */ |
235 | | - public function get_definition() { |
236 | | - global $wpdb; |
237 | | - $table_name = static::table_name( true ); |
238 | | - $charset_collate = $wpdb->get_charset_collate(); |
239 | | - $uid_column = static::uid_column(); |
240 | | - |
241 | | - $columns = static::get_columns(); |
242 | | - |
243 | | - $columns_definitions = []; |
244 | | - foreach ( $columns as $column => $definition ) { |
245 | | - $column_sql = "`{$column}` {$definition['type']}"; |
246 | | - |
247 | | - if ( ! empty( $definition['length'] ) ) { |
248 | | - $column_sql .= "({$definition['length']})"; |
249 | | - } |
250 | | - |
251 | | - if ( ! empty( $definition['unsigned'] ) ) { |
252 | | - $column_sql .= ' UNSIGNED'; |
253 | | - } |
254 | | - |
255 | | - $column_sql .= ! empty( $definition['nullable'] ) ? ' NULL' : ' NOT NULL'; |
256 | | - |
257 | | - if ( ! empty( $definition['auto_increment'] ) ) { |
258 | | - $column_sql .= ' AUTO_INCREMENT'; |
259 | | - } |
260 | | - |
261 | | - if ( ! empty( $definition['default'] ) ) { |
262 | | - $column_sql .= ' DEFAULT ' . ( in_array( $definition['default'], self::SQL_RESERVED_DEFAULTS, true ) || in_array( $definition['php_type'], [ self::PHP_TYPE_INT, self::PHP_TYPE_BOOL, self::PHP_TYPE_FLOAT ], true ) ? $definition['default'] : "'{$definition['default']}'" ); |
263 | | - } |
264 | | - |
265 | | - $columns_definitions[] = $column_sql; |
266 | | - } |
267 | | - |
268 | | - $columns_sql = implode( ',' . PHP_EOL, $columns_definitions ); |
269 | | - |
270 | | - return " |
271 | | - CREATE TABLE `{$table_name}` ( |
272 | | - {$columns_sql}, |
273 | | - PRIMARY KEY (`{$uid_column}`) |
274 | | - ) {$charset_collate}; |
275 | | - "; |
276 | | - } |
277 | | - |
278 | | - /** |
279 | | - * Add indexes after table creation. |
280 | | - * |
281 | | - * @since 0.0.1 |
282 | | - * |
283 | | - * @param array<string,string> $results A map of results in the format |
284 | | - * returned by the `dbDelta` function. |
285 | | - * |
286 | | - * @return array<string,string> A map of results in the format returned by |
287 | | - * the `dbDelta` function. |
288 | | - */ |
289 | | - protected function after_update( array $results ) { |
290 | | - if ( empty( static::INDEXES ) || ! is_array( static::INDEXES ) ) { |
291 | | - return $results; |
292 | | - } |
293 | | - |
294 | | - foreach ( static::INDEXES as $index ) { |
295 | | - $this->check_and_add_index( $results, $index['name'], $index['columns'] ); |
296 | | - } |
297 | | - |
298 | | - return $results; |
299 | | - } |
300 | | - |
301 | 68 | /** |
302 | 69 | * Returns the base table name without the dynamic prefix. |
303 | 70 | * |
|
0 commit comments