This commit is contained in:
Dennis Eichhorn 2017-02-19 20:25:12 +01:00
parent 487e43d637
commit 8906406ab7

View File

@ -33,6 +33,13 @@ use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
*/ */
class Builder extends BuilderAbstract class Builder extends BuilderAbstract
{ {
/**
* Is read only.
*
* @var bool
* @since 1.0.0
*/
private $isReadOnly = false;
/** /**
* Columns. * Columns.
@ -212,8 +219,9 @@ class Builder extends BuilderAbstract
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __construct(ConnectionAbstract $connection) public function __construct(ConnectionAbstract $connection, bool $readOnly = false)
{ {
$this->isReadOnly = $readOnly;
$this->setConnection($connection); $this->setConnection($connection);
} }
@ -342,6 +350,19 @@ class Builder extends BuilderAbstract
*/ */
public function raw(string $raw) : Builder public function raw(string $raw) : Builder
{ {
if($this->isReadOnly) {
$test = strtolower($raw);
if(strpos($test, 'insert') !== false
|| strpos($test, 'update') !== false
|| strpos($test, 'drop') !== false
|| strpos($test, 'delete') !== false
|| strpos($test, 'create') !== false
|| strpos($test, 'alter') !== false) {
throw new \Exception();
}
}
$this->type = QueryType::RAW; $this->type = QueryType::RAW;
$this->raw = $raw; $this->raw = $raw;
@ -449,7 +470,7 @@ class Builder extends BuilderAbstract
throw new \InvalidArgumentException('Unknown operator.'); throw new \InvalidArgumentException('Unknown operator.');
} }
$this->wheres[$this->getPublicColumnName($column)][] = [ $this->wheres[self::getPublicColumnName($column)][] = [
'column' => $column, 'column' => $column,
'operator' => $operator[$i], 'operator' => $operator[$i],
'value' => $values[$i], 'value' => $values[$i],
@ -463,7 +484,7 @@ class Builder extends BuilderAbstract
throw new \InvalidArgumentException('Unknown operator.'); throw new \InvalidArgumentException('Unknown operator.');
} }
$this->wheres[$this->getPublicColumnName($columns)][] = ['column' => $columns, 'operator' => $operator, 'value' => $values, $this->wheres[self::getPublicColumnName($columns)][] = ['column' => $columns, 'operator' => $operator, 'value' => $values,
'boolean' => $boolean,]; 'boolean' => $boolean,];
} else { } else {
throw new \InvalidArgumentException(); throw new \InvalidArgumentException();
@ -474,7 +495,7 @@ class Builder extends BuilderAbstract
public function getWhereByColumn($column) public function getWhereByColumn($column)
{ {
return $this->wheres[$this->getPublicColumnName($column)] ?? null; return $this->wheres[self::getPublicColumnName($column)] ?? null;
} }
public function getTableOfSystem($expression, $systemIdentifier) public function getTableOfSystem($expression, $systemIdentifier)
@ -832,6 +853,10 @@ class Builder extends BuilderAbstract
*/ */
public function insert(...$columns) : Builder public function insert(...$columns) : Builder
{ {
if($this->isReadOnly) {
throw new \Exception();
}
$this->type = QueryType::INSERT; $this->type = QueryType::INSERT;
foreach ($columns as $key => $column) { foreach ($columns as $key => $column) {
@ -908,6 +933,10 @@ class Builder extends BuilderAbstract
*/ */
public function update(...$columns) : Builder public function update(...$columns) : Builder
{ {
if($this->isReadOnly) {
throw new \Exception();
}
$this->type = QueryType::UPDATE; $this->type = QueryType::UPDATE;
foreach ($columns as $key => $column) { foreach ($columns as $key => $column) {
@ -1052,7 +1081,7 @@ class Builder extends BuilderAbstract
$sth = $this->connection->con->prepare($this->toSql()); $sth = $this->connection->con->prepare($this->toSql());
foreach($this->binds as $key => $bind) { foreach($this->binds as $key => $bind) {
$type = $this->getBindParamType($bind); $type = self::getBindParamType($bind);
$sth->bindParam($key, $bind, $type); $sth->bindParam($key, $bind, $type);
} }
@ -1070,7 +1099,7 @@ class Builder extends BuilderAbstract
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
private function getBindParamType($value) public static function getBindParamType($value)
{ {
if(is_int($value)) { if(is_int($value)) {
return PDO::PARAM_INT; return PDO::PARAM_INT;
@ -1081,7 +1110,7 @@ class Builder extends BuilderAbstract
throw new \Exception(); throw new \Exception();
} }
public function getPublicColumnName($column) : string public static function getPublicColumnName($column) : string
{ {
if(is_string($column)) { if(is_string($column)) {
return $column; return $column;