| 1 |
<?php |
| 2 |
|
| 3 |
namespace CodesVault\Howdyqb\Statement; |
| 4 |
|
| 5 |
use CodesVault\Howdyqb\Clause\SqlCore; |
| 6 |
use CodesVault\Howdyqb\Clause\WhereClause; |
| 7 |
use CodesVault\Howdyqb\SqlGenerator; |
| 8 |
use CodesVault\Howdyqb\Utilities; |
| 9 |
use CodesVault\Howdyqb\Validation\IdentifierValidator; |
| 10 |
|
| 11 |
class Insert |
| 12 |
{ |
| 13 |
// bring all SQL Clause |
| 14 |
use SqlCore, WhereClause; |
| 15 |
|
| 16 |
protected $db; |
| 17 |
protected $data = []; |
| 18 |
protected $insert_sql = []; |
| 19 |
protected $sql = []; |
| 20 |
public $test = []; |
| 21 |
protected $params = []; |
| 22 |
private $table_name; |
| 23 |
|
| 24 |
public function __construct($db, string $table_name, array $data) |
| 25 |
{ |
| 26 |
$this->db = $db; |
| 27 |
$this->data = $data; |
| 28 |
$this->table_name = $table_name; |
| 29 |
|
| 30 |
$this->start(); |
| 31 |
$this->insert_sql['insert_table_name'] = $this->getTableName(); |
| 32 |
$this->insert_sql['insert_columns'] = $this->getColumns(); |
| 33 |
$this->insert_sql['value_placeholders'] = $this->getValuePlaceholders(); |
| 34 |
$this->params = $this->getParams(); |
| 35 |
} |
| 36 |
|
| 37 |
private function driverExecute($sql) |
| 38 |
{ |
| 39 |
$driver = $this->db; |
| 40 |
if (class_exists('wpdb') && $driver instanceof \wpdb) { |
| 41 |
return $driver->query($driver->prepare($sql, $this->params)); |
| 42 |
} |
| 43 |
|
| 44 |
try { |
| 45 |
$data = $driver->prepare($sql); |
| 46 |
return $data->execute($this->params); |
| 47 |
} catch (\Exception $exception) { |
| 48 |
Utilities::throughException($exception); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
private function start() |
| 53 |
{ |
| 54 |
$this->insert_sql['start'] = 'INSERT INTO'; |
| 55 |
} |
| 56 |
|
| 57 |
private function getTableName() |
| 58 |
{ |
| 59 |
$prefix = Utilities::get_db_configs()->prefix; |
| 60 |
return IdentifierValidator::validateTableName($prefix . $this->table_name); |
| 61 |
} |
| 62 |
|
| 63 |
private function getColumns() |
| 64 |
{ |
| 65 |
if (empty($this->data)) return; |
| 66 |
|
| 67 |
if (! is_array($this->data[0])) { |
| 68 |
// Validate and escape column names for INSERT...SELECT |
| 69 |
$escapedColumns = array_map(function ($col) { |
| 70 |
return IdentifierValidator::validateColumnName($col); |
| 71 |
}, $this->data); |
| 72 |
return '(' . implode(', ', $escapedColumns) . ')'; |
| 73 |
} |
| 74 |
|
| 75 |
$columns = IdentifierValidator::validateColumnNames(array_keys($this->data[0])); |
| 76 |
|
| 77 |
return '(' . implode(', ', $columns) . ')'; |
| 78 |
} |
| 79 |
|
| 80 |
private function getValuePlaceholders() |
| 81 |
{ |
| 82 |
if (empty($this->data) || ! is_array($this->data[0])) return; |
| 83 |
|
| 84 |
$placeholders = []; |
| 85 |
|
| 86 |
if (count($this->data) > 1) { |
| 87 |
foreach ($this->data as $row) { |
| 88 |
$placeholders[] = '(' . implode(',', array_fill(0, count($row), Utilities::get_placeholder($this->db, $row))) . ')'; |
| 89 |
} |
| 90 |
} else { |
| 91 |
$placeholders[] = '(' . implode(',', array_fill(0, count($this->data[0]), Utilities::get_placeholder($this->db, $this->data))) . ')'; |
| 92 |
} |
| 93 |
return 'VALUES ' . implode(',', $placeholders); |
| 94 |
} |
| 95 |
|
| 96 |
private function getParams() |
| 97 |
{ |
| 98 |
if (empty($this->data) || ! is_array($this->data[0])) return []; |
| 99 |
|
| 100 |
$params = []; |
| 101 |
foreach ($this->data as $value) { |
| 102 |
foreach ($value as $val) { |
| 103 |
$params[] = $val; |
| 104 |
} |
| 105 |
} |
| 106 |
return $params; |
| 107 |
} |
| 108 |
|
| 109 |
public function ignoreDuplicates(): self |
| 110 |
{ |
| 111 |
$this->insert_sql['start'] = 'INSERT IGNORE INTO'; |
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
public function select(...$columns): self |
| 116 |
{ |
| 117 |
$this->sql['start']['select'] = 'SELECT'; |
| 118 |
if (! empty($columns)) { |
| 119 |
$this->columns(...$columns); |
| 120 |
return $this; |
| 121 |
} |
| 122 |
|
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
public function getSql() |
| 127 |
{ |
| 128 |
$this->setStartExpression(); |
| 129 |
return [ |
| 130 |
'query' => SqlGenerator::insert($this->insert_sql, $this->sql), |
| 131 |
'params' => $this->params, |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
public function execute() |
| 136 |
{ |
| 137 |
$this->setStartExpression(); |
| 138 |
$query = SqlGenerator::insert($this->insert_sql, $this->sql); |
| 139 |
return $this->driverExecute($query); |
| 140 |
} |
| 141 |
} |
| 142 |
|