2020-04-02 00:02:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Driver\SQL\Query;
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
use Driver\SQL\SQL;
|
2020-04-03 17:39:58 +02:00
|
|
|
use Driver\SQL\Strategy\Strategy;
|
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
class Insert extends Query {
|
|
|
|
|
2020-04-03 17:39:58 +02:00
|
|
|
private string $tableName;
|
|
|
|
private array $columns;
|
|
|
|
private array $rows;
|
|
|
|
private ?Strategy $onDuplicateKey;
|
|
|
|
private ?string $returning;
|
2020-04-02 00:02:51 +02:00
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function __construct(SQL $sql, string $name, array $columns = array()) {
|
2020-04-02 00:02:51 +02:00
|
|
|
parent::__construct($sql);
|
|
|
|
$this->tableName = $name;
|
|
|
|
$this->columns = $columns;
|
|
|
|
$this->rows = array();
|
|
|
|
$this->onDuplicateKey = NULL;
|
2020-04-02 01:48:46 +02:00
|
|
|
$this->returning = NULL;
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function addRow(...$values): Insert {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->rows[] = $values;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function onDuplicateKeyStrategy(Strategy $strategy): Insert {
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->onDuplicateKey = $strategy;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function returning(string $column): Insert {
|
2020-04-02 01:48:46 +02:00
|
|
|
$this->returning = $column;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:29:47 +02:00
|
|
|
public function execute() {
|
|
|
|
$fetchResult = !empty($this->sql->getReturning($this->returning));
|
|
|
|
return $this->sql->executeQuery($this, $fetchResult);
|
2020-04-02 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function getTableName(): string { return $this->tableName; }
|
|
|
|
public function getColumns(): array { return $this->columns; }
|
|
|
|
public function getRows(): array { return $this->rows; }
|
|
|
|
public function onDuplicateKey(): ?Strategy { return $this->onDuplicateKey; }
|
|
|
|
public function getReturning(): ?string { return $this->returning; }
|
2021-04-08 18:29:47 +02:00
|
|
|
|
|
|
|
public function build(array &$params, Query $context = NULL): ?string {
|
|
|
|
$tableName = $this->sql->tableName($this->getTableName());
|
|
|
|
$columns = $this->getColumns();
|
|
|
|
$rows = $this->getRows();
|
|
|
|
|
|
|
|
if (empty($rows)) {
|
|
|
|
$this->sql->setLastError("No rows to insert given.");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($columns) || empty($columns)) {
|
|
|
|
$columnStr = "";
|
|
|
|
} else {
|
|
|
|
$columnStr = " (" . $this->sql->columnName($columns) . ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = array();
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$rowPlaceHolder = array();
|
|
|
|
foreach ($row as $val) {
|
|
|
|
$rowPlaceHolder[] = $this->sql->addValue($val, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
$values[] = "(" . implode(",", $rowPlaceHolder) . ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = implode(",", $values);
|
|
|
|
|
|
|
|
$onDuplicateKey = $this->sql->getOnDuplicateStrategy($this->onDuplicateKey(), $params);
|
|
|
|
if ($onDuplicateKey === FALSE) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$returningCol = $this->getReturning();
|
|
|
|
$returning = $this->sql->getReturning($returningCol);
|
|
|
|
return "INSERT INTO $tableName$columnStr VALUES $values$onDuplicateKey$returning";
|
|
|
|
}
|
2020-04-03 17:39:58 +02:00
|
|
|
}
|