Skip to content

Commit

Permalink
Merge pull request #9 from InitPHP/main-dev
Browse files Browse the repository at this point in the history
2.0.4
  • Loading branch information
muhammetsafak authored Nov 17, 2022
2 parents ac90b0b + 44ba9b4 commit aaca370
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 77 deletions.
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

0 comments on commit aaca370

Please sign in to comment.