- 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
@@ -5,21 +5,18 @@ namespace Ulmus\Query;
|
||||
class Delete extends Fragment {
|
||||
|
||||
public int $order = -100;
|
||||
|
||||
public bool $lowPriority = false;
|
||||
|
||||
public bool $quick = false;
|
||||
|
||||
public bool $ignore = false;
|
||||
|
||||
public string $alias;
|
||||
|
||||
public string $priority;
|
||||
|
||||
public function render() : string
|
||||
{
|
||||
return $this->renderSegments([
|
||||
'DELETE',
|
||||
( $this->alias ?? false ),
|
||||
( $this->lowPriority ? 'LOW_PRIORITY' : false ),
|
||||
( $this->priority ?? false ),
|
||||
( $this->quick ? 'QUICK' : false ),
|
||||
( $this->ignore ? 'IGNORE' : false ),
|
||||
]);
|
||||
|
||||
+3
-11
@@ -2,28 +2,20 @@
|
||||
|
||||
namespace Ulmus\Query;
|
||||
|
||||
use Ulmus\QueryBuilder;
|
||||
|
||||
class From extends Fragment {
|
||||
|
||||
public int $order = -80;
|
||||
|
||||
protected $tables = [];
|
||||
|
||||
public QueryBuilder $queryBuilder;
|
||||
public array $tables = [];
|
||||
|
||||
public function __construct(QueryBuilder $queryBuilder)
|
||||
{
|
||||
$this->queryBuilder = $queryBuilder;
|
||||
}
|
||||
|
||||
public function set(array $tables) : self
|
||||
{
|
||||
$this->tables = $tables;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function add($table) : self
|
||||
public function add(/* array|string */ $table) : self
|
||||
{
|
||||
foreach((array) $table as $alias => $name) {
|
||||
$this->tables[$alias] = $name;
|
||||
|
||||
+28
-1
@@ -4,6 +4,33 @@ namespace Ulmus\Query;
|
||||
|
||||
class Insert extends Fragment {
|
||||
|
||||
public bool $ignore = false;
|
||||
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([
|
||||
'INSERT',
|
||||
( $this->priority ?? false ),
|
||||
( $this->ignore ? 'IGNORE' : false ),
|
||||
'INTO', $this->renderTable(),
|
||||
"(" . implode(',', $this->fieldlist) . ")",
|
||||
]);
|
||||
}
|
||||
|
||||
protected function renderTable() : string
|
||||
{
|
||||
return $this->table . ( $this->alias ? " {$this->alias}" : "" );
|
||||
}
|
||||
}
|
||||
@@ -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