- Still working on Update and Insert statements
This commit is contained in:
1 parent
828daffec8
commit
3dc82b1f0d
11 files changed
+243
-62
No files matched your search
+15
-9
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace Ulmus\Query;
|
||||
|
||||
use Ulmus\QueryBuilder;
|
||||
|
||||
class Values extends Fragment {
|
||||
|
||||
public int $order = 0;
|
||||
|
||||
public ? int $fieldCount = null;
|
||||
|
||||
public array $rows;
|
||||
|
||||
public QueryBuilder $queryBuilder;
|
||||
@@ -17,36 +21,38 @@ class Values extends Fragment {
|
||||
|
||||
public function add(array $row) : self
|
||||
{
|
||||
$this->rows[] = $row;
|
||||
if ($this->fieldCount === null) {
|
||||
$this->fieldCount = count($row);
|
||||
}
|
||||
|
||||
$this->rows[] = array_values($row);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render() : string
|
||||
{
|
||||
$this->queryBuilder->addParameter($this->flattenRowsArray());
|
||||
$this->queryBuilder->addValues($this->flattenRowsArray());
|
||||
|
||||
return $this->renderSegments([
|
||||
'VALUES', '(' . $this->renderParameterPlaceholders() . ')',
|
||||
'VALUES', $this->renderParameterPlaceholders(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function renderParameterPlaceholders() : string
|
||||
{
|
||||
$return = [];
|
||||
$count = null;
|
||||
|
||||
foreach($this->rows as $row) {
|
||||
if ($count === null) {
|
||||
$count = count($row);
|
||||
if ($this->fieldCount === null) {
|
||||
}
|
||||
else {
|
||||
if (count($row) !== $count) {
|
||||
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->parameterCount, '?'));
|
||||
$return[] = implode(',', array_fill(0, $this->fieldCount, '?'));
|
||||
}
|
||||
|
||||
return "(" . implode('),(', $return) . ")";
|
||||
@@ -54,6 +60,6 @@ class Values extends Fragment {
|
||||
|
||||
public function flattenRowsArray() : array
|
||||
{
|
||||
return iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($this->rows)));
|
||||
return \iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->rows)));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user