| 1 |
<?php |
| 2 |
|
| 3 |
namespace CodesVault\Howdyqb\Statement; |
| 4 |
|
| 5 |
use CodesVault\Howdyqb\Api\DeleteInterface; |
| 6 |
use CodesVault\Howdyqb\Clause\WhereClause; |
| 7 |
use CodesVault\Howdyqb\SqlGenerator; |
| 8 |
use CodesVault\Howdyqb\Utilities; |
| 9 |
|
| 10 |
class Delete implements DeleteInterface |
| 11 |
{ |
| 12 |
// bring all SQL Clause |
| 13 |
use WhereClause; |
| 14 |
|
| 15 |
protected $db; |
| 16 |
public $sql = []; |
| 17 |
protected $params = []; |
| 18 |
protected $table_name; |
| 19 |
|
| 20 |
public function __construct($db, string $table_name) |
| 21 |
{ |
| 22 |
$this->db = $db; |
| 23 |
$this->table_name = Utilities::get_db_configs()->prefix . $table_name; |
| 24 |
} |
| 25 |
|
| 26 |
protected function start() |
| 27 |
{ |
| 28 |
$this->sql['start'] = 'DELETE FROM ' . $this->table_name; |
| 29 |
} |
| 30 |
|
| 31 |
public function drop() |
| 32 |
{ |
| 33 |
$this->sql['drop'] = 'DROP TABLE ' . $this->table_name; |
| 34 |
return $this; |
| 35 |
} |
| 36 |
|
| 37 |
public function dropIfExists() |
| 38 |
{ |
| 39 |
$this->sql['drop'] = 'DROP TABLE IF EXISTS ' . $this->table_name; |
| 40 |
return $this; |
| 41 |
} |
| 42 |
|
| 43 |
private function driver_exicute($sql) |
| 44 |
{ |
| 45 |
$driver = $this->db; |
| 46 |
if (class_exists('wpdb') && $driver instanceof \wpdb) { |
| 47 |
if (empty($this->params)) { |
| 48 |
return $driver->query($sql); |
| 49 |
} |
| 50 |
return $driver->query($driver->prepare($sql, $this->params)); |
| 51 |
} |
| 52 |
|
| 53 |
$data = $driver->prepare($sql); |
| 54 |
try { |
| 55 |
return $data->execute($this->params); |
| 56 |
} catch (\PDOException $exception) { |
| 57 |
Utilities::throughException($exception); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
private function delete_data() |
| 62 |
{ |
| 63 |
$query = SqlGenerator::delete($this->sql); |
| 64 |
|
| 65 |
return $this->driver_exicute($query); |
| 66 |
} |
| 67 |
|
| 68 |
// get only sql query string |
| 69 |
public function getSql() |
| 70 |
{ |
| 71 |
$this->start(); |
| 72 |
$query = [ |
| 73 |
'query' => SqlGenerator::delete($this->sql), |
| 74 |
'params' => $this->params, |
| 75 |
]; |
| 76 |
return $query; |
| 77 |
} |
| 78 |
|
| 79 |
public function execute() |
| 80 |
{ |
| 81 |
$this->start(); |
| 82 |
$this->delete_data(); |
| 83 |
} |
| 84 |
} |
| 85 |
|