- WIP on insert/update statement
This commit is contained in:
1 parent
bad5123447
commit
828daffec8
13 files changed
+311
-36
No files matched your search
@@ -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)));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user