- WIP on insert/update statement

This commit is contained in:
Dave Mc Nicoll committed 2020-02-05 16:19:57 -05:00
1 parent bad5123447
commit 828daffec8
13 files changed
+311 -36

No files matched your search

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