- 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
@@ -31,6 +31,6 @@ class Insert extends Fragment {
|
||||
|
||||
protected function renderTable() : string
|
||||
{
|
||||
return $this->table . ( $this->alias ? " {$this->alias}" : "" );
|
||||
return $this->table;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Query;
|
||||
|
||||
use Ulmus\QueryBuilder;
|
||||
|
||||
class Set extends Fragment {
|
||||
|
||||
public int $order = 0;
|
||||
|
||||
public array $dataset;
|
||||
|
||||
public QueryBuilder $queryBuilder;
|
||||
|
||||
public function __construct(QueryBuilder $queryBuilder)
|
||||
{
|
||||
$this->queryBuilder = $queryBuilder;
|
||||
}
|
||||
|
||||
public function set(array $dataset) : self
|
||||
{
|
||||
$this->dataset = $dataset;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render() : string
|
||||
{
|
||||
return $this->renderSegments([
|
||||
'SET', $this->renderParameterPlaceholders(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function renderParameterPlaceholders() : string
|
||||
{
|
||||
$keys = [];
|
||||
|
||||
foreach($this->dataset as $key =>$value) {
|
||||
$this->queryBuilder->addParameter($value, ":v_$key");
|
||||
$keys[] = "$key=:v_$key";
|
||||
}
|
||||
|
||||
return implode(",", $keys);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Query;
|
||||
|
||||
class Update extends Fragment {
|
||||
|
||||
public int $order = -100;
|
||||
|
||||
public bool $quick = false;
|
||||
|
||||
public bool $ignore = false;
|
||||
|
||||
public array $fieldlist = [];
|
||||
|
||||
public string $priority;
|
||||
|
||||
public string $table;
|
||||
|
||||
public ? string $alias = null;
|
||||
|
||||
public function render() : string
|
||||
{
|
||||
return $this->renderSegments([
|
||||
'UPDATE',
|
||||
( $this->priority ?? false ),
|
||||
( $this->ignore ? 'IGNORE' : false ),
|
||||
$this->table,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function renderTable() : string
|
||||
{
|
||||
return $this->table;
|
||||
}
|
||||
}
|
||||
+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