| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Db_Queries; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 7 |
use Jet_Form_Builder\Migrations\Migration_Exception; |
| 8 |
use Jet_Form_Builder\Migrations\Versions\Base_Migration; |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
abstract class Base_Db_Model { |
| 16 |
|
| 17 |
const DB_TABLE_PREFIX = 'jet_fb_'; |
| 18 |
// phpcs:disable Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase |
| 19 |
const InnoDB = 'InnoDB'; |
| 20 |
const MyISAM = 'MyISAM'; |
| 21 |
// phpcs:enable Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase |
| 22 |
|
| 23 |
const PRIMARY_ID = 'id'; |
| 24 |
const CREATED_AT = 'created_at'; |
| 25 |
const UPDATED_AT = 'updated_at'; |
| 26 |
|
| 27 |
const DATETIME_FORMAT = 'Y-m-d H:i:s'; |
| 28 |
|
| 29 |
protected static $prefix = self::DB_TABLE_PREFIX; |
| 30 |
|
| 31 |
protected $silence = false; |
| 32 |
protected $gmt = 0; |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns table name |
| 36 |
* |
| 37 |
* @return [type] [description] |
| 38 |
*/ |
| 39 |
abstract public static function table_name(): string; |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns columns schema |
| 43 |
* |
| 44 |
* @return [type] [description] |
| 45 |
*/ |
| 46 |
abstract public static function schema(): array; |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns schemas options |
| 50 |
* Such as primary keys, charset |
| 51 |
* |
| 52 |
* @return [type] [description] |
| 53 |
*/ |
| 54 |
abstract public static function schema_keys(): array; |
| 55 |
|
| 56 |
public static function schema_columns( $prefix = '' ): array { |
| 57 |
$columns = array(); |
| 58 |
$generator = self::generate_scoped_columns( $prefix ); |
| 59 |
|
| 60 |
foreach ( $generator as $column ) { |
| 61 |
$generator->next(); |
| 62 |
|
| 63 |
$columns[] = $generator->current(); |
| 64 |
} |
| 65 |
|
| 66 |
return $columns; |
| 67 |
} |
| 68 |
|
| 69 |
public static function generate_scoped_columns( string $prefix = '' ): \Generator { |
| 70 |
$generator = self::generate_columns(); |
| 71 |
|
| 72 |
foreach ( $generator as $column ) { |
| 73 |
$item = sprintf( '`%s`.`%s`', static::table(), $column ); |
| 74 |
|
| 75 |
if ( $prefix ) { |
| 76 |
$item .= sprintf( " as '%s'", "{$prefix}.{$column}" ); |
| 77 |
} |
| 78 |
|
| 79 |
yield $column; |
| 80 |
yield array( 'as' => $item ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public static function generate_columns(): \Generator { |
| 85 |
$schema = static::schema(); |
| 86 |
|
| 87 |
foreach ( $schema as $column_name => $details ) { |
| 88 |
yield $column_name; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
public function create(): Base_Db_Model { |
| 94 |
Execution_Builder::instance()->safe_create( $this ); |
| 95 |
|
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* This method is used to create foreign keys. |
| 101 |
* And this is safe as the model will create |
| 102 |
* the outer table before it creates the current one. |
| 103 |
* |
| 104 |
* P.S. see |
| 105 |
* - \Jet_Form_Builder\Db_Queries\Base_Db_Model::before_create |
| 106 |
* - \Jet_Form_Builder\Db_Queries\Execution_Builder::create |
| 107 |
* |
| 108 |
* @return Base_Db_Constraint[] |
| 109 |
*/ |
| 110 |
public function foreign_relations(): array { |
| 111 |
return array(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* This method returns a list of migrations |
| 116 |
* that must be performed before |
| 117 |
* the sql query is executed. |
| 118 |
* |
| 119 |
* These migrations will be automatically |
| 120 |
* marked as "installed" after the current table is created |
| 121 |
* |
| 122 |
* @return Base_Migration[] |
| 123 |
*/ |
| 124 |
public function related_migrations(): array { |
| 125 |
return array(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Generate migrations which need to install |
| 130 |
* |
| 131 |
* @return \Generator |
| 132 |
*/ |
| 133 |
public function get_migrations(): \Generator { |
| 134 |
$migrations = $this->related_migrations(); |
| 135 |
|
| 136 |
if ( ! count( $migrations ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
foreach ( $this->related_migrations() as $migration ) { |
| 141 |
if ( ! $migration->is_installed() ) { |
| 142 |
yield $migration; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @param array $columns |
| 149 |
* @param null $format |
| 150 |
* |
| 151 |
* @return int |
| 152 |
* @throws Sql_Exception |
| 153 |
* @since 3.3.0 Added inner hooks |
| 154 |
*/ |
| 155 |
public function insert( $columns = array(), $format = null ): int { |
| 156 |
$columns = apply_filters( 'jet-form-builder/db/before-insert', $columns, $this ); |
| 157 |
$columns = apply_filters( "jet-form-builder/db/{$this::table_name()}/before-insert", $columns ); |
| 158 |
|
| 159 |
$id = Execution_Builder::instance()->insert( $this, $columns, $format ); |
| 160 |
|
| 161 |
do_action( 'jet-form-builder/db/after-insert', $id, $columns, $this ); |
| 162 |
do_action( "jet-form-builder/db/{$this::table_name()}/after-insert", $id, $columns ); |
| 163 |
|
| 164 |
return $id; |
| 165 |
} |
| 166 |
|
| 167 |
public function insert_soft( $columns = array(), $format = null ): int { |
| 168 |
try { |
| 169 |
return $this->insert( $columns, $format ); |
| 170 |
} catch ( Sql_Exception $exception ) { |
| 171 |
return 0; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @param $multi_columns |
| 177 |
* @param null $format |
| 178 |
* |
| 179 |
* @return int[] |
| 180 |
* @throws Sql_Exception |
| 181 |
*/ |
| 182 |
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 183 |
public function insert_many( $multi_columns, $format = null ) { |
| 184 |
$inserted_ids = array(); |
| 185 |
foreach ( $multi_columns as $columns ) { |
| 186 |
if ( ! is_array( $columns ) || empty( $columns ) ) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
$inserted_ids[] = $this->insert( $columns ); |
| 190 |
} |
| 191 |
|
| 192 |
return $inserted_ids; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @param $columns |
| 197 |
* @param $where |
| 198 |
* @param null $format |
| 199 |
* @param null $where_format |
| 200 |
* |
| 201 |
* @return int |
| 202 |
* @throws Sql_Exception |
| 203 |
*/ |
| 204 |
public function update( $columns, $where, $format = null, $where_format = null ): int { |
| 205 |
return Execution_Builder::instance()->update( $this, $columns, $where, $format, $where_format ); |
| 206 |
} |
| 207 |
|
| 208 |
public function update_soft( $columns, $where, $format = null, $where_format = null ): int { |
| 209 |
try { |
| 210 |
return $this->update( $columns, $where, $format, $where_format ); |
| 211 |
} catch ( Sql_Exception $exception ) { |
| 212 |
return 0; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @param $where |
| 218 |
* @param null $format |
| 219 |
* |
| 220 |
* @return int |
| 221 |
* @throws Sql_Exception |
| 222 |
*/ |
| 223 |
public function delete( $where, $format = null ) { |
| 224 |
return Execution_Builder::instance()->delete( $this, $where, $format ); |
| 225 |
} |
| 226 |
|
| 227 |
public function get_defaults(): array { |
| 228 |
$defaults = array(); |
| 229 |
$schema_keys = array_keys( static::schema() ); |
| 230 |
|
| 231 |
if ( in_array( self::CREATED_AT, $schema_keys, true ) ) { |
| 232 |
$defaults[ self::CREATED_AT ] = current_time( 'mysql', $this->gmt ); |
| 233 |
} |
| 234 |
|
| 235 |
return $defaults; |
| 236 |
} |
| 237 |
|
| 238 |
public function before_create() { |
| 239 |
$this->create_foreign_tables(); |
| 240 |
$this->mark_migrations_as_installed(); |
| 241 |
} |
| 242 |
|
| 243 |
protected function create_foreign_tables() { |
| 244 |
Execution_Builder::instance()->create_foreign_tables( $this ); |
| 245 |
} |
| 246 |
|
| 247 |
protected function mark_migrations_as_installed() { |
| 248 |
foreach ( $this->related_migrations() as $migration ) { |
| 249 |
$migration->install_automatic(); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
public function after_create() { |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* @throws Sql_Exception |
| 258 |
*/ |
| 259 |
public function before_insert() { |
| 260 |
$this->create(); |
| 261 |
$this->migrations_install(); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* @throws Sql_Exception |
| 266 |
*/ |
| 267 |
protected function migrations_install() { |
| 268 |
try { |
| 269 |
foreach ( $this->related_migrations() as $migration ) { |
| 270 |
$migration->install(); |
| 271 |
} |
| 272 |
} catch ( Migration_Exception $exception ) { |
| 273 |
throw new Sql_Exception( esc_html( $exception->getMessage() ) ); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
public function after_insert( $insert_columns ) { |
| 278 |
} |
| 279 |
|
| 280 |
public function before_update() { |
| 281 |
} |
| 282 |
|
| 283 |
public function update_columns( array $columns ): array { |
| 284 |
$schema_keys = array_keys( static::schema() ); |
| 285 |
|
| 286 |
if ( in_array( self::UPDATED_AT, $schema_keys, true ) && |
| 287 |
! array_key_exists( self::UPDATED_AT, $columns ) |
| 288 |
) { |
| 289 |
$columns[ self::UPDATED_AT ] = current_time( 'mysql', $this->gmt ); |
| 290 |
} |
| 291 |
|
| 292 |
return $columns; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* @throws Sql_Exception |
| 297 |
*/ |
| 298 |
public function before_delete() { |
| 299 |
if ( jet_form_builder()->has_module( 'cli' ) ) { |
| 300 |
return; |
| 301 |
} |
| 302 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 303 |
throw new Sql_Exception( |
| 304 |
esc_html( "Not enough capabilities for current user to delete rows in {$this::table()}" ) |
| 305 |
); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
public function schema_charset_collate() { |
| 310 |
return $this->wpdb()->get_charset_collate(); |
| 311 |
} |
| 312 |
|
| 313 |
public function schema_engine() { |
| 314 |
return self::InnoDB; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* You must you this function, |
| 319 |
* to get right name of table |
| 320 |
* |
| 321 |
* @return string table name |
| 322 |
*/ |
| 323 |
public static function table() { |
| 324 |
return static::wpdb()->prefix . static::$prefix . static::table_name(); |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* @param bool $silence |
| 329 |
* |
| 330 |
* @return Base_Db_Model |
| 331 |
*/ |
| 332 |
public function set_silence( bool $silence ): Base_Db_Model { |
| 333 |
$this->silence = $silence; |
| 334 |
|
| 335 |
return $this; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* @return bool |
| 340 |
*/ |
| 341 |
public function is_silence(): bool { |
| 342 |
return $this->silence; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Returns WPDB instance |
| 347 |
* |
| 348 |
* @return \QM_DB|\wpdb [description] |
| 349 |
*/ |
| 350 |
public static function wpdb() { |
| 351 |
global $wpdb; |
| 352 |
|
| 353 |
return $wpdb; |
| 354 |
} |
| 355 |
|
| 356 |
} |
| 357 |
|