Skip to content

Commit

Permalink
Merge branch 'developer'
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Jun 15, 2016
2 parents 3db1f9f + 46d9e3a commit 34db1ee
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 56 deletions.
112 changes: 56 additions & 56 deletions src/Query/Sql/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function fields($fields)
// when a string is given
if (is_string($fields))
{
$fields = $this->stringArgumentToArray($fields);
$fields = $this->stringArgumentToArray($fields);
}
// it also could be an object
elseif (is_object($fields))
Expand All @@ -107,13 +107,13 @@ public function fields($fields)
// add the fields
foreach($fields as $key => $field)
{
// when we have a string as key we have an alias definition
if (is_string($key))
{
// when we have a string as key we have an alias definition
if (is_string($key))
{
$this->addField($key, $field);
} else {
} else {
$this->addField($field);
}
}
}

return $this;
Expand All @@ -124,13 +124,13 @@ public function fields($fields)
*
* ->addField('title')
*
* @param string $field
* @param string $alias
* @param string $field
* @param string $alias
* @return self
*/
public function addField($field, $alias = null)
{
$this->fields[] = array($field, $alias); return $this;
$this->fields[] = array($field, $alias); return $this;
}

/**
Expand Down Expand Up @@ -222,23 +222,23 @@ public function addFieldRound($field, $decimals = 0, $alias = null)
*
* ->orderBy('created_at')
* ->orderBy('modified_at', 'desc')
*
*
* // multiple order statements
* ->orderBy(['firstname', 'lastname'], 'desc')
*
* // muliple order statements with diffrent directions
* ->orderBy(['firstname' => 'asc', 'lastname' => 'desc'])
*
* @param array|string $cols
* @param string $order
* @param array|string $cols
* @param string $order
* @return self
*/
public function orderBy($columns, $direction = 'asc')
{
if (is_string($columns))
{
$columns = $this->stringArgumentToArray($columns);
}
if (is_string($columns))
{
$columns = $this->stringArgumentToArray($columns);
}

foreach ($columns as $key => $column)
{
Expand All @@ -259,15 +259,15 @@ public function orderBy($columns, $direction = 'asc')
* ->groupBy('id')
* ->gorupBy(['id', 'category'])
*
* @param array|string $keys
* @param array|string $keys
* @return self
*/
public function groupBy($groupKeys)
{
if (is_string($groupKeys))
{
$groupKeys = $this->stringArgumentToArray($groupKeys);
}
if (is_string($groupKeys))
{
$groupKeys = $this->stringArgumentToArray($groupKeys);
}

foreach ($groupKeys as $groupKey)
{
Expand All @@ -282,21 +282,21 @@ public function groupBy($groupKeys)
*
* ->join('avatars', 'users.id', '=', 'avatars.user_id')
*
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
* @param string $type
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
* @param string $type
*
* @return self
*/
public function join($table, $localKey, $operator = null, $referenceKey = null, $type = 'left')
{
// validate the join type
if (!in_array($type, array('inner', 'left', 'right', 'outer')))
{
throw new Exception('Invalid join type "'.$type.'" given. Available type: inner, left, right, outer');
}
// validate the join type
if (!in_array($type, array('inner', 'left', 'right', 'outer')))
{
throw new Exception('Invalid join type "'.$type.'" given. Available type: inner, left, right, outer');
}

// to make nested joins possible you can pass an closure
// wich will create a new query where you can add your nested wheres
Expand All @@ -312,67 +312,67 @@ public function join($table, $localKey, $operator = null, $referenceKey = null,
$this->joins[] = array($type, $table, $subquery); return $this;
}

$this->joins[] = array($type, $table, $localKey, $operator, $referenceKey); return $this;
$this->joins[] = array($type, $table, $localKey, $operator, $referenceKey); return $this;
}

/**
* Left join same as join with special type
*
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
*
* @return self
*/
public function leftJoin($table, $localKey, $operator = null, $referenceKey = null)
{
return $this->join($table, $localKey, $operator, $referenceKey, 'left');
return $this->join($table, $localKey, $operator, $referenceKey, 'left');
}

/**
* Left join same as join with special type
*
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
*
* @return self
*/
public function rightJoin($table, $localKey, $operator = null, $referenceKey = null)
{
return $this->join($table, $localKey, $operator, $referenceKey, 'right');
return $this->join($table, $localKey, $operator, $referenceKey, 'right');
}

/**
* Left join same as join with special type
*
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
*
* @return self
*/
public function innerJoin($table, $localKey, $operator = null, $referenceKey = null)
{
return $this->join($table, $localKey, $operator, $referenceKey, 'inner');
return $this->join($table, $localKey, $operator, $referenceKey, 'inner');
}

/**
* Left join same as join with special type
*
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
* @param array|string $table
* @param string $localKey
* @param string $operator
* @param string $referenceKey
*
* @return self
*/
public function outerJoin($table, $localKey, $operator = null, $referenceKey = null)
{
return $this->join($table, $localKey, $operator, $referenceKey, 'outer');
return $this->join($table, $localKey, $operator, $referenceKey, 'outer');
}

/**
Expand Down Expand Up @@ -433,14 +433,14 @@ public function get()
$results = $this->executeResultFetcher();

// we always exprect an array here!
if (!is_array($results))
if (!is_array($results) || empty($results))
{
$results = array();
}

// In case we should forward a key means using a value
// from every result as array key.
if ($this->forwardKey !== false && is_string($this->forwardKey))
if ((!empty($results)) && $this->forwardKey !== false && is_string($this->forwardKey))
{
$rawResults = $results;
$results = array();
Expand All @@ -459,7 +459,7 @@ public function get()
}

// Group the resuls by a items value
if ($this->groupResults !== false && is_string($this->groupResults))
if ((!empty($results)) && $this->groupResults !== false && is_string($this->groupResults))
{
$rawResults = $results;
$results = array();
Expand Down Expand Up @@ -516,7 +516,7 @@ public function one()
/**
* Find something, means select one record by key
*
* @param int $id
* @param int $id
* @param string $key
* @return mixed
*/
Expand Down
30 changes: 30 additions & 0 deletions src/Query/Sql/SelectBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,36 @@ protected function stringArgumentToArray($argument)
return array($argument);
}

/**
* Will reset the current selects where conditions
*
* @return self
*/
public function resetWheres()
{
$this->wheres = array(); return $this;
}

/**
* Will reset the current selects limit
*
* @return self
*/
public function resetLimit()
{
$this->limit = null; return $this;
}

/**
* Will reset the current selects offset
*
* @return self
*/
public function resetOffset()
{
$this->offset = null; return $this;
}

/**
* Create a where statement
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Query/Sql/BaseSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function testWhere()
));
}

/**
* BaseSql::whereReset
*/
public function testWhereReset()
{
$this->assertAttributes($this->createQuery()->where('id', 42)->resetWheres(), array());
}

/**
* BaseSql::orWhere
*/
Expand Down Expand Up @@ -211,6 +219,14 @@ public function testLimit()
$this->assertAttributes($this->createQuery()->limit(10)->limit(null));
}

/**
* BaseSql::limitReset
*/
public function testLimitReset()
{
$this->assertAttributes($this->createQuery()->limit(10)->resetLimit(), array());
}

/**
* BaseSql::offset
*/
Expand All @@ -223,6 +239,14 @@ public function testOffset()
$this->assertAttributes($this->createQuery()->limit(2, 5)->offset(3), array('limit' => 5, 'offset' => 3));
}

/**
* BaseSql::offsetReset
*/
public function testOffsetReset()
{
$this->assertAttributes($this->createQuery()->offset(10)->resetOffset(), array());
}

/**
* BaseSql::page
*/
Expand Down

0 comments on commit 34db1ee

Please sign in to comment.