| 1 |
<?php |
| 2 |
|
| 3 |
namespace CodesVault\Howdyqb\Statement; |
| 4 |
|
| 5 |
use CodesVault\Howdyqb\Api\TableInterface; |
| 6 |
use CodesVault\Howdyqb\Utilities; |
| 7 |
use CodesVault\Howdyqb\Validation\IdentifierValidator; |
| 8 |
|
| 9 |
class Table implements TableInterface |
| 10 |
{ |
| 11 |
protected $db; |
| 12 |
protected $params = []; |
| 13 |
private $table_name; |
| 14 |
|
| 15 |
public function __construct($db, string $table_name) |
| 16 |
{ |
| 17 |
$this->db = $db; |
| 18 |
$prefix = Utilities::get_db_configs()->prefix; |
| 19 |
$this->table_name = IdentifierValidator::validateTableName($prefix . $table_name); |
| 20 |
} |
| 21 |
|
| 22 |
public function drop() |
| 23 |
{ |
| 24 |
$sql = 'DROP TABLE ' . $this->table_name; |
| 25 |
$this->driverExecute($sql); |
| 26 |
} |
| 27 |
|
| 28 |
public function dropIfExists() |
| 29 |
{ |
| 30 |
$sql = 'DROP TABLE IF EXISTS ' . $this->table_name; |
| 31 |
$this->driverExecute($sql); |
| 32 |
} |
| 33 |
|
| 34 |
public function truncate() |
| 35 |
{ |
| 36 |
$sql = 'TRUNCATE TABLE ' . $this->table_name; |
| 37 |
$this->driverExecute($sql); |
| 38 |
} |
| 39 |
|
| 40 |
private function driverExecute($sql) |
| 41 |
{ |
| 42 |
$driver = $this->db; |
| 43 |
if (class_exists('wpdb') && $driver instanceof \wpdb) { |
| 44 |
return $driver->query($sql); |
| 45 |
} |
| 46 |
|
| 47 |
$data = $driver->prepare($sql); |
| 48 |
try { |
| 49 |
return $data->execute($this->params); |
| 50 |
} catch (\Exception $exception) { |
| 51 |
Utilities::throughException($exception); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|