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
112 changes: 102 additions & 10 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

namespace InitPHP\Database;

use InitPHP\Database\Utils\{Pagination,
Datatables};
use \InitPHP\Database\Exceptions\{WritableException,
ReadableException,
UpdatableException,
Expand Down Expand Up @@ -83,6 +85,15 @@ public function __construct(array $credentials = [])
$this->_validation = new Validation($this->_credentials['validation']['methods'], $this->_credentials['validation']['messages'], $this->_credentials['validation']['labels'], $this);
}

public function __call($name, $arguments)
{
if(Helper::str_starts_with($name, 'findBy') === FALSE){
throw new \RuntimeException('There is no "' . $name . '" method.');
}
$this->where(Helper::camelCaseToSnakeCase(\substr($name, 6)), \current($arguments));
return $this;
}

final public function newInstance(array $credentials = []): Database
{
return new self(empty($credentials) ? $this->_credentials : \array_merge($this->_credentials, $credentials));
Expand Down Expand Up @@ -625,6 +636,31 @@ public function all(int $limit = 100, int $offset = 0)
->read();
}

public function group(\Closure $group, string $logical = 'AND'): self
{
$logical = \str_replace(['&&', '||'], ['AND', 'OR'], \strtoupper($logical));
if(!\in_array($logical, ['AND', 'OR'], true)){
throw new \InvalidArgumentException('Logical operator OR, AND, && or || it could be.');
}

$clone = clone $this;
$clone->reset();

\call_user_func_array($group, [$clone]);

$where = $clone->_whereQuery();
if($where !== ''){
$this->_STRUCTURE['where'][$logical][] = '(' . $where . ')';
}

$having = $clone->_havingQuery();
if($having !== ''){
$this->_STRUCTURE['having'][$logical][] = '(' . $having . ')';
}
unset($clone);
return $this;
}

public function onlyDeleted(): self
{
$this->_isOnlyDeletes = true;
Expand All @@ -637,17 +673,57 @@ public function onlyUndeleted(): self
return $this;
}

/**
* QueryBuilder resetlemeden SELECT cümlesi kurar ve satır sayısını döndürür.
*
* @return int
*/
public function count(): int
{
$select = $this->_STRUCTURE['select'];
$this->_STRUCTURE['select'][] = 'COUNT(*) AS row_count';
$this->_deleteFieldBuild(false);
$parameters = Parameters::get(false);
$res = $this->query($this->_readQuery());
$count = $res->toArray()['row_count'] ?? 0;
unset($res);
Parameters::merge($parameters);
$this->_STRUCTURE['select'] = $select;
return $count;
}

public function pagination(int $page = 1, int $per_page_limit = 10, string $link = '?page={page}'): Pagination
{
$total_row = $this->count();
$this->offset(($page - 1) * $per_page_limit)
->limit($per_page_limit);
$res = $this->query($this->_readQuery());
$this->reset();

return new Pagination($res, $page, $per_page_limit, $total_row, $link);
}

public function datatables(array $columns, int $method = Datatables::GET_REQUEST): string
{
return (new Datatables($this, $columns, $method))->__toString();
}

public function _readQuery(): string
{
if($this->getSchema() !== null){
$this->table($this->getSchema());
}

$where = $this->_whereQuery();
if($where !== ''){
$where = ' WHERE ' . $where;
}else{
$where = ' WHERE 1';
}
return 'SELECT '
. (empty($this->_STRUCTURE['select']) ? '*' : \implode(', ', $this->_STRUCTURE['select']))
. ' FROM ' . \implode(', ', $this->_STRUCTURE['table'])
. (!empty($this->_STRUCTURE['join']) ? ' ' . \implode(', ', $this->_STRUCTURE['join']) : '')
. $this->_whereQuery()
. $where
. $this->_havingQuery()
. (!empty($this->_STRUCTURE['group_by']) ? ' GROUP BY ' . \implode(', ', $this->_STRUCTURE['group_by']) : '')
. (!empty($this->_STRUCTURE['order_by']) ? ' ORDER BY ' . \implode(', ', $this->_STRUCTURE['order_by']) : '')
Expand Down Expand Up @@ -734,11 +810,17 @@ public function _updateQuery(array $data): string
if($schemaID !== null && isset($data[$schemaID])){
$this->where($schemaID, $data[$schemaID]);
}
$where = $this->_whereQuery();
if($where !== ''){
$where = ' WHERE ' . $where;
}else{
$where = ' WHERE 1';
}
return 'UPDATE '
. (empty($this->_STRUCTURE['table']) ? $this->getSchema() : end($this->_STRUCTURE['table']))
. ' SET '
. \implode(', ', $update)
. $this->_whereQuery()
. $where
. $this->_havingQuery()
. $this->_limitQuery();
}
Expand Down Expand Up @@ -787,22 +869,33 @@ public function _updateBatchQuery(array $data, $referenceColumn): string
$update[] = $syntax;
}
$this->in($referenceColumn, $where);

$where = $this->_whereQuery();
if($where !== ''){
$where = ' WHERE ' . $where;
}else{
$where = ' WHERE 1';
}
return 'UPDATE '
. (empty($this->_STRUCTURE['table']) ? $this->getSchema() : end($this->_STRUCTURE['table']))
. ' SET '
. \implode(', ', $update)
. $this->_whereQuery()
. $where
. $this->_havingQuery()
. $this->_limitQuery();
}

public function _deleteQuery(): string
{
$where = $this->_whereQuery();
if($where !== ''){
$where = ' WHERE ' . $where;
}else{
$where = ' WHERE 1';
}
return 'DELETE FROM'
. ' '
. (empty($this->_STRUCTURE['table']) ? $this->getSchema() : end($this->_STRUCTURE['table']))
. $this->_whereQuery()
. $where
. $this->_havingQuery()
. $this->_limitQuery();
}
Expand Down Expand Up @@ -840,10 +933,9 @@ private function _whereQuery(): string
$isAndEmpty = empty($this->_STRUCTURE['where']['AND']);
$isOrEmpty = empty($this->_STRUCTURE['where']['OR']);
if($isAndEmpty && $isOrEmpty){
return ' WHERE 1';
return '';
}
return ' WHERE '
. (!$isAndEmpty ? \implode(' AND ', $this->_STRUCTURE['where']['AND']) : '')
return (!$isAndEmpty ? \implode(' AND ', $this->_STRUCTURE['where']['AND']) : '')
. (!$isAndEmpty && !$isOrEmpty ? ' AND ' : '')
. (!$isOrEmpty ? \implode(' OR ', $this->_STRUCTURE['where']['OR']) : '');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/DeletableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ModelCallbacksException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ModelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ModelRelationsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ReadableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/SQLQueryExecuteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/UpdatableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/WritableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
8 changes: 4 additions & 4 deletions src/Facade/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

namespace InitPHP\Database\Facade;

use \InitPHP\Database\{Database,
Raw,
Result};
use InitPHP\Database\{Database, Raw, Result, Utils\Pagination};

/**
* @mixin Database
Expand Down Expand Up @@ -45,6 +43,8 @@
* @method static Result get(?string $table = null)
* @method static bool create(array $set)
* @method static bool createBatch(array $set)
* @method static int count()
* @method static Pagination pagination(int $page = 1, int $per_page_limit = 10, string $link = '?page={page}')
* @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
2 changes: 1 addition & 1 deletion src/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
4 changes: 3 additions & 1 deletion src/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand All @@ -33,3 +33,5 @@
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Model.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Raw.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Result.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Utils/Pagination.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Utils/Datatables.php';
2 changes: 1 addition & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down
16 changes: 15 additions & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
* @copyright Copyright © 2022 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 2.0.5
* @version 2.0.6
* @link https://www.muhammetsafak.com.tr
*/

Expand Down Expand Up @@ -43,6 +43,20 @@ public function reset(): void
$this->_STRUCTURE = self::STRUCTURE;
}

public function importQB(array $structure): self
{
$this->_STRUCTURE = $structure;

return $this;
}

public function exportQB(): array
{
return $this->_STRUCTURE;
}



/**
* @param string|Raw ...$columns
* @return $this
Expand Down
Loading