Skip to content

Commit

Permalink
some bugfixes for stricter handling than before
Browse files Browse the repository at this point in the history
  • Loading branch information
zegenie committed Dec 9, 2018
1 parent 61ce6cd commit aeebb84
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 65 deletions.
5 changes: 4 additions & 1 deletion src/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getSQL($strip = false)
}

if (count($sql_parts) > 1) {
return '(' . join(" {$this->mode} ", $sql_parts) . ')';
return '(' . join(" {$this->getMode()} ", $sql_parts) . ')';
} else {
return $sql_parts[0];
}
Expand All @@ -177,6 +177,9 @@ public function isDistinct()
*/
public function getMode()
{
if (!$this->mode) {
$this->mode = Query::MODE_AND;
}
return $this->mode;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Criterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public function __construct($column, $value = '', $operator = self::EQUALS, $var
if ($operator !== null) {
$this->operator = $operator;
}
if ($variable !== null) {
if ($variable) {
$this->variable = $variable;
}
if ($additional !== null) {
if ($additional) {
$this->additional = $additional;
}
if ($special !== null) {
if ($special) {
$this->special = $special;
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ public function getSql($strip = false)
$this->operator = self::IN;
}

$sql .= ' ' . $this->operator;
$sql .= " {$this->operator} ";

if (!$this->isNullTypeOperator()) {
if (is_array($this->value)) {
Expand Down
23 changes: 17 additions & 6 deletions src/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,31 @@ class Join
/**
* @var Criteria[]
*/
protected $additional_criteria;
protected $additional_criteria = [];

protected $join_type;

/**
*
*/
public function __construct(Table $table, $left_column, $right_column, $original_column, $additional_criteria, $join_type = self::LEFT)
/**
* @param Table $table
* @param $left_column
* @param $right_column
* @param $original_column
* @param $additional_criteria
* @param string $join_type
*/
public function __construct(Table $table, $left_column, $right_column, $original_column, $additional_criteria = null, $join_type = self::LEFT)
{
$this->table = $table;
$this->left_column = $left_column;
$this->right_column = $right_column;
$this->original_column = $original_column;
$this->additional_criteria = $additional_criteria;
if ($additional_criteria) {
foreach ($additional_criteria as $additional_criterion) {
$criteria = new Criteria();
$criteria->where(...$additional_criterion);
$this->additional_criteria[] = $criteria;
}
}
$this->join_type = $join_type;
}

Expand Down
67 changes: 56 additions & 11 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,27 @@ public function getAction()

public function isCount()
{
return (bool) $this->action == QueryInterface::ACTION_COUNT;
return (bool) ($this->action == QueryInterface::ACTION_COUNT);
}

public function isSelect()
{
return (bool) $this->action == QueryInterface::ACTION_SELECT;
return (bool) ($this->action == QueryInterface::ACTION_SELECT);
}

public function isDelete()
{
return (bool) $this->action == QueryInterface::ACTION_DELETE;
return (bool) ($this->action == QueryInterface::ACTION_DELETE);
}

public function isInsert()
{
return (bool) $this->action == QueryInterface::ACTION_INSERT;
return (bool) ($this->action == QueryInterface::ACTION_INSERT);
}

public function isUpdate()
{
return (bool) $this->action == QueryInterface::ACTION_UPDATE;
return (bool) ($this->action == QueryInterface::ACTION_UPDATE);
}

/**
Expand All @@ -161,17 +161,57 @@ public function isUpdate()
*/
public function getValues()
{
$values = [];
$values = $this->values;

foreach ($this->criteria as $criteria) {
foreach ($criteria->getValues() as $value) {
$values[] = $value;
if (is_array($value)) {
foreach ($value as $single_value) {
$values[] = $single_value;
}
} else {
$values[] = $value;
}
}
}

return $values;
}

/**
* Get the quoted / converted value depending on database type
*
* @param mixed $value
* @return int|mixed|string
*/
public function getDatabaseValue($value)
{
if (is_bool($value)) {
if (Core::getDriver() == Core::DRIVER_MYSQL) {
return (int) $value;
} elseif (Core::getDriver() == Core::DRIVER_POSTGRES) {
return ($value) ? 'true' : 'false';
}
}
return $value;
}

/**
* Add a value to the value container
*
* @param mixed $value
*/
public function addValue($value)
{
if (is_array($value)) {
foreach ($value as $single_value) {
$this->addValue($single_value);
}
} else {
$this->values[] = $this->getDatabaseValue($value);
}
}

/**
* Add a column to select
*
Expand Down Expand Up @@ -238,7 +278,7 @@ public function update($column, $value): self
*
* @return Criteria
*/
public function where($column, $value = '', $operator = Criterion::EQUALS, $variable = null, $additional = null, $special = null): self
public function where($column, $value = '', $operator = Criterion::EQUALS, $variable = null, $additional = null, $special = null): Criteria
{
if (!$column instanceof Criteria) {
$criteria = new Criteria();
Expand All @@ -264,7 +304,7 @@ public function where($column, $value = '', $operator = Criterion::EQUALS, $vari
*
* @return Criteria
*/
public function and($column, $value = '', $operator = Criterion::EQUALS, $variable = null, $additional = null, $special = null): self
public function and($column, $value = '', $operator = Criterion::EQUALS, $variable = null, $additional = null, $special = null): Criteria
{
if ($this->mode == query::MODE_OR) {
throw new Exception('Cannot combine two selection types (AND/OR) in the same Query. Use sub-criteria instead');
Expand All @@ -289,7 +329,7 @@ public function and($column, $value = '', $operator = Criterion::EQUALS, $variab
*
* @return Criteria
*/
public function or($column, $value = '', $operator = Criterion::EQUALS, $variable = null, $additional = null, $special = null): self
public function or($column, $value = '', $operator = Criterion::EQUALS, $variable = null, $additional = null, $special = null): Criteria
{
if ($this->mode == query::MODE_AND) {
throw new Exception('Cannot combine two selection types (AND/OR) in the same Query. Use sub-criteria instead');
Expand Down Expand Up @@ -532,7 +572,7 @@ public function getLimit()

public function hasLimit()
{
return (bool) $this->limit !== null;
return (bool) ($this->limit !== null);
}

/**
Expand Down Expand Up @@ -630,6 +670,7 @@ public function generateUpdateSQL(Update $update)
$sql_generator = new SqlGenerator($this);

$this->sql = $sql_generator->getUpdateSQL($update);
$this->values = $sql_generator->getValues();
}

return $this->sql;
Expand All @@ -649,6 +690,7 @@ public function generateInsertSQL(Insertion $insertion)
$sql_generator = new SqlGenerator($this);

$this->sql = $sql_generator->getInsertSQL($insertion);
$this->values = $sql_generator->getValues();
}

return $this->sql;
Expand Down Expand Up @@ -749,6 +791,9 @@ public function isDistinct()

public function getMode()
{
if (!$this->mode) {
$this->mode = self::MODE_AND;
}
return $this->mode;
}

Expand Down
10 changes: 5 additions & 5 deletions src/QueryColumnSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function __construct(Query $query, $column, $alias = '', $special = null,
$this->query = $query;
$this->column = $column;
$this->alias = $alias;
if ($variable !== null) {
if ($variable) {
$this->variable = $variable;
}
if ($additional !== null) {
if ($additional) {
$this->additional = $additional;
}
if ($special !== null) {
if ($special) {
$this->special = $special;
}
}
Expand Down Expand Up @@ -94,7 +94,7 @@ public function getAdditional()

public function hasAdditional()
{
return (bool) $this->additional != '';
return (bool) ($this->additional != '');
}

/**
Expand All @@ -107,7 +107,7 @@ public function getSpecial()

public function isSpecial()
{
return (bool) $this->special != '';
return (bool) ($this->special != '');
}

}
10 changes: 5 additions & 5 deletions src/RawQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,27 @@ public function getAction()

public function isCount()
{
return (bool) $this->action == QueryInterface::ACTION_COUNT;
return (bool) ($this->action == QueryInterface::ACTION_COUNT);
}

public function isSelect()
{
return (bool) $this->action == QueryInterface::ACTION_SELECT;
return (bool) ($this->action == QueryInterface::ACTION_SELECT);
}

public function isDelete()
{
return (bool) $this->action == QueryInterface::ACTION_DELETE;
return (bool) ($this->action == QueryInterface::ACTION_DELETE);
}

public function isInsert()
{
return (bool) $this->action == QueryInterface::ACTION_INSERT;
return (bool) ($this->action == QueryInterface::ACTION_INSERT);
}

public function isUpdate()
{
return (bool) $this->action == QueryInterface::ACTION_UPDATE;
return (bool) ($this->action == QueryInterface::ACTION_UPDATE);
}

}
Loading

0 comments on commit aeebb84

Please sign in to comment.