65 lines
1.6 KiB
PHP
65 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Query;
|
|
|
|
use Ulmus\QueryBuilder;
|
|
|
|
class Values extends Fragment {
|
|
|
|
public int $order = 0;
|
|
|
|
public ? int $fieldCount = null;
|
|
|
|
public array $rows;
|
|
|
|
public QueryBuilder $queryBuilder;
|
|
|
|
public function __construct(QueryBuilder $queryBuilder)
|
|
{
|
|
$this->queryBuilder = $queryBuilder;
|
|
}
|
|
|
|
public function add(array $row) : self
|
|
{
|
|
if ($this->fieldCount === null) {
|
|
$this->fieldCount = count($row);
|
|
}
|
|
|
|
$this->rows[] = array_values($row);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function render() : string
|
|
{
|
|
$this->queryBuilder->addValues($this->flattenRowsArray());
|
|
|
|
return $this->renderSegments([
|
|
'VALUES', $this->renderParameterPlaceholders(),
|
|
]);
|
|
}
|
|
|
|
public function renderParameterPlaceholders() : string
|
|
{
|
|
$return = [];
|
|
|
|
foreach($this->rows as $row) {
|
|
if ($this->fieldCount === null) {
|
|
}
|
|
else {
|
|
if (count($row) !== $this->fieldCount) {
|
|
throw new \Exception("Insert statement must contains the same number of values for each rows.");
|
|
}
|
|
}
|
|
|
|
$return[] = implode(',', array_fill(0, $this->fieldCount, '?'));
|
|
}
|
|
|
|
return "(" . implode('),(', $return) . ")";
|
|
}
|
|
|
|
public function flattenRowsArray() : array
|
|
{
|
|
return \iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->rows)));
|
|
}
|
|
} |