-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from InitPHP/v2.1
2.1
- Loading branch information
Showing
7 changed files
with
95 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 Muhammet ŞAFAK | ||
* @license ./LICENSE MIT | ||
* @version 2.0.7 | ||
* @version 2.1 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
|
@@ -295,29 +295,6 @@ final public function escape_str($value) | |
return false; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param string|int|float|bool|null $value | ||
* @return $this | ||
*/ | ||
final public function setParameter(string $key, $value): self | ||
{ | ||
Parameters::set($key, $value); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array $parameters | ||
* @return $this | ||
*/ | ||
final public function setParameters(array $parameters = []): self | ||
{ | ||
foreach ($parameters as $key => $value) { | ||
Parameters::set($key, $value); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $sqlQuery | ||
* @param array $parameters | ||
|
@@ -358,7 +335,7 @@ final public function query(string $sqlQuery, array $parameters = []): Result | |
case self::ARRAY: | ||
return $this->_last->asArray(); | ||
case self::ENTITY: | ||
return $this->_last->asEntity(); | ||
return $this->_last->asEntity($this->_credentials['entity'] ?? Entity::class); | ||
case self::OBJECT: | ||
return $this->_last->asObject(); | ||
case self::LAZY: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 Muhammet ŞAFAK | ||
* @license ./LICENSE MIT | ||
* @version 2.0.8 | ||
* @version 2.1 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
|
@@ -59,7 +59,7 @@ public static function isSQLParameterOrFunction($value): bool | |
return ((\is_string($value)) && ( | ||
$value === '?' | ||
|| (bool)\preg_match('/^:[\w]+$/', $value) | ||
|| (bool)\preg_match('/^[a-zA-Z_\.]+$/', $value) | ||
|| (bool)\preg_match('/^[a-zA-Z_]+[\.]+[a-zA-Z_]+$/', $value) | ||
|| (bool)\preg_match('/^[a-zA-Z_]+\(\)$/', $value) | ||
)) || ($value instanceof Raw) || \is_int($value); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,10 @@ | |
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 Muhammet ŞAFAK | ||
* @license ./LICENSE MIT | ||
* @version 2.0.8 | ||
* @version 2.1 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace InitPHP\Database; | ||
|
||
|
@@ -226,6 +227,7 @@ public function __construct() | |
$credentials['updatable'] = $this->updatable ?? true; | ||
$credentials['deletable'] = $this->deletable ?? true; | ||
$credentials['writable'] = $this->writable ?? true; | ||
$credentials['return'] = $this->return ?? self::ENTITY; | ||
parent::__construct($credentials); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,15 @@ | |
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 Muhammet ŞAFAK | ||
* @license ./LICENSE MIT | ||
* @version 2.0.8 | ||
* @version 2.1 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace InitPHP\Database; | ||
|
||
use InitPHP\Database\Helpers\{Helper, Parameters, Validation}; | ||
use InitPHP\Database\Exceptions\QueryBuilderException; | ||
use InitPHP\Database\Exceptions\QueryGeneratorException; | ||
use \InitPHP\Database\Exceptions\ValueException; | ||
use \InitPHP\Database\Helpers\{Helper, Parameters}; | ||
use \InitPHP\Database\Exceptions\{QueryBuilderException, QueryGeneratorException, ValueException}; | ||
|
||
class QueryBuilder | ||
{ | ||
|
@@ -65,6 +64,29 @@ public function exportQB(): array | |
return $this->_STRUCTURE; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param string|int|float|bool|null $value | ||
* @return $this | ||
*/ | ||
final public function setParameter(string $key, $value): self | ||
{ | ||
Parameters::set($key, $value); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array $parameters | ||
* @return $this | ||
*/ | ||
final public function setParameters(array $parameters = []): self | ||
{ | ||
foreach ($parameters as $key => $value) { | ||
Parameters::set($key, $value); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param string|Raw ...$columns | ||
* @return $this | ||
|
@@ -368,7 +390,7 @@ final public function join($table, $onStmt = null, string $type = 'INNER'): self | |
$this->_STRUCTURE['join'][$table] = 'NATURAL JOIN ' . $table; | ||
break; | ||
default: | ||
$this->_STRUCTURE['join'][$table] = $type . ' JOIN ' . $table . ' ON ' . $onStmt; | ||
$this->_STRUCTURE['join'][$table] = \trim(($type . ' JOIN ' . $table . ' ON ' . $onStmt)); | ||
} | ||
return $this; | ||
} | ||
|
@@ -1124,6 +1146,23 @@ final public function raw(string $rawQuery): Raw | |
return new Raw($rawQuery); | ||
} | ||
|
||
final public function subQuery(\Closure $closure, ?string $alias = null, bool $isIntervalQuery = true): Raw | ||
{ | ||
$queryBuilder = new self(); | ||
\call_user_func_array($closure, [$queryBuilder]); | ||
|
||
if ($alias !== null && $isIntervalQuery !== TRUE) { | ||
throw new QueryBuilderException('To define alias to a subquery, it must be an inner query.'); | ||
} | ||
|
||
$rawQuery = ($isIntervalQuery === TRUE ? '(' : '') | ||
. $queryBuilder->generateSelectQuery() | ||
. ($isIntervalQuery === TRUE ? ')' : '') | ||
. ($alias !== null ? ' AS ' . $alias : ''); | ||
|
||
return $this->raw($rawQuery); | ||
} | ||
|
||
final public function generateInsertQuery(): string | ||
{ | ||
if (!empty($this->_STRUCTURE['table'])) { | ||
|
@@ -1533,15 +1572,15 @@ private function whereOrHavingStatementPrepare($column, $value, string $mark = ' | |
} | ||
$values[] = Helper::isSQLParameterOrFunction($val) ? $val : Parameters::add($column, $val); | ||
} | ||
$value = \implode(', ', \array_unique($values)); | ||
$value = '(' . \implode(', ', \array_unique($values)) . ')'; | ||
} elseif (Helper::isSQLParameterOrFunction($value)) { | ||
$value = (string)$value; | ||
}else{ | ||
throw new ValueException('An incorrect value was defined.'); | ||
} | ||
return $column | ||
. ($searchMark === 'NOTIN' ? ' NOT ' : ' ') | ||
. 'IN (' . $value . ')'; | ||
. 'IN ' . $value; | ||
case 'FINDINSET': | ||
case 'NOTFINDINSET': | ||
if(\is_array($value)){ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters