Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ DB::createImmutable([

#### Create

Single Row :

```php
use \InitPHP\Database\Facade\DB;
$data = [
Expand All @@ -71,7 +69,7 @@ if($isInsert){
}
```

Multi Row:
##### Create Batch

```php
use \InitPHP\Database\Facade\DB;
Expand All @@ -89,7 +87,7 @@ $data = [
];

$isInsert = DB::table('post')
->create($data);
->createBatch($data);

/**
* This executes the following query.
Expand Down
92 changes: 60 additions & 32 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,29 +415,49 @@ public function create(array $set)
if($isCreatedField){
$data[$this->_credentials['createdField']] = $createdFieldParameterName;
}
}else{
$i = 0;
foreach ($set as $row) {
$data[$i] = [];
$this->_validation->setData($row);
foreach ($row as $column => $value) {
if($this->_validation->validation($column, null) === FALSE){
$this->_errors[] = $this->_validation->getError();
return false;
}
$data[$i][$column] = $value;
}
if(empty($data[$i])){
continue;
}
if($isCreatedField){
$data[$i][$this->_credentials['createdField']] = $createdFieldParameterName;
}

$res = $this->query($this->_insertQuery($data));
$this->reset();
return $res->numRows() > 0;
}

/**
* @param array $set
* @return bool
*/
public function createBatch(array $set)
{
if($this->_credentials['writable'] === FALSE){
throw new WritableException('');
}
$isCreatedField = !empty($this->_credentials['createdField']);
if($isCreatedField){
$createdFieldParameterName = Parameters::add($this->_credentials['createdField'], \date($this->_credentials['timestampFormat']));
}
$data = [];
$i = 0;
foreach ($set as $row) {
$data[$i] = [];
$this->_validation->setData($row);
foreach ($row as $column => $value) {
if($this->_validation->validation($column, null) === FALSE){
$this->_errors[] = $this->_validation->getError();
return false;
}
++$i;
$data[$i][$column] = $value;
}
if(empty($data[$i])){
continue;
}
if($isCreatedField){
$data[$i][$this->_credentials['createdField']] = $createdFieldParameterName;
}
++$i;
}
$res = $this->query($this->_insertQuery($data));
$res = $this->query($this->_insertBatchQuery($data));
$this->reset();

return $res->numRows() > 0;
}

Expand Down Expand Up @@ -636,21 +656,28 @@ public function _insertQuery(array $data): string
$columns = [];
$values = [];

if(\count($data) === \count($data, \COUNT_RECURSIVE)){
foreach ($data as $column => $value) {
$column = \trim($column);
if($this->_credentials['allowedFields'] !== null && !\in_array($column, $this->_credentials['allowedFields'])){
continue;
}
$columns[] = $column;
$values[] = Helper::isSQLParameterOrFunction($value) ? $value : Parameters::add($column, $value);
}
if(empty($columns)){
return '';
foreach ($data as $column => $value) {
$column = \trim($column);
if($this->_credentials['allowedFields'] !== null && !\in_array($column, $this->_credentials['allowedFields'])){
continue;
}
return $sql
. ' (' . \implode(', ', $columns) . ') VALUES (' . \implode(', ', $values) . ');';
$columns[] = $column;
$values[] = Helper::isSQLParameterOrFunction($value) ? $value : Parameters::add($column, $value);
}
if(empty($columns)){
return '';
}
return $sql
. ' (' . \implode(', ', $columns) . ') VALUES (' . \implode(', ', $values) . ');';
}

public function _insertBatchQuery($data): string
{
$sql = 'INSERT INTO'
. ' '
. (empty($this->_STRUCTURE['table']) ? $this->getSchema() : end($this->_STRUCTURE['table']));
$columns = [];
$values = [];

foreach ($data as &$row) {
$value = [];
Expand All @@ -676,6 +703,7 @@ public function _insertQuery(array $data): string
}
$multiValues[] = '(' . \implode(', ', $value) . ')';
}

return $sql . ' (' . \implode(', ', $columns) . ') VALUES '
. \implode(', ', $multiValues) . ';';
}
Expand Down
1 change: 1 addition & 0 deletions src/Facade/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
* @method static int insertId()
* @method static Result get(?string $table = null)
* @method static bool create(array $set)
* @method static bool createBatch(array $set)
* @method static Result read(array $selector = [], array $conditions = [], array $parameters = [])
* @method static Result readOne(array $selector = [], array $conditions = [], array $parameters = [])
* @method static bool update(array $set)
Expand Down
Loading