New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add or support #20

Merged
merged 3 commits into from Mar 13, 2016
Jump to file or symbol
Failed to load files and symbols.
+82 −21
Diff settings

Always

Just for now

Copy path View file
@@ -40,7 +40,13 @@ class Expression implements \ArrayAccess {
public $params=[];
function __construct($template = null, $arguments = null)
/**
* Specifying options to constructors will override default
* attribute values of this class
*
* @param array $options will initialize class properties

This comment has been minimized.

@DarkSide666

DarkSide666 Mar 12, 2016

Member

Incorrect description. Should be @param array $template and @param array $arguments

*/
function __construct($template = [], $arguments = null)
{
;
if(is_string($template)){
Copy path View file
@@ -309,12 +309,16 @@ public function where($field, $cond = null, $value = null, $kind = 'where', $num
// or conditions
$or = $this->orExpr();
foreach ($field as $row) {
call_user_func_array([$or,'where'],$row);
if (is_array($row)) {
call_user_func_array([$or,'where'],$row);
}else{
$or->where($row);
}
}
$field = $or;
}
if ($num_args === 1) {
if ($num_args === 1 && is_string($field)) {
$this->args[$kind][] = [$this->expr($field)];
return $this;
}
@@ -353,6 +357,7 @@ public function where($field, $cond = null, $value = null, $kind = 'where', $num
switch($num_args){
case 1: $this->args[$kind][] = [$field]; break;
case 2: $this->args[$kind][] = [$field, $cond]; break;
case 3: $this->args[$kind][] = [$field, $cond, $value]; break;
}
@@ -382,7 +387,7 @@ public function having($field, $cond = null, $value = null)
*
* @return array Parsed chunks of query
*/
public function __render_where($kind)
protected function __render_where($kind)
{
$ret = [];
@@ -467,7 +472,7 @@ public function __render_where($kind)
*
* @return string rendered SQL chunk
*/
public function _render_where()
protected function _render_where()
{
if (!isset($this->args['where'])) {
return;
@@ -481,7 +486,7 @@ public function _render_where()
*
* @return string rendered SQL chunk
*/
public function _render_orwhere()
protected function _render_orwhere()
{
if (!isset($this->args['where'])) {
return;
@@ -495,7 +500,7 @@ public function _render_orwhere()
*
* @return string rendered SQL chunk
*/
public function render_andwhere()
protected function _render_andwhere()
{
if (!isset($this->args['where'])) {
return;
@@ -509,7 +514,7 @@ public function render_andwhere()
*
* @return string rendered SQL chunk
*/
public function _render_having()
protected function _render_having()
{
if (!isset($this->args['having'])) {
return;
@@ -520,19 +525,6 @@ public function _render_having()
// }}}
// {{{ Miscelanious
/**
* Specifying options to constructors will override default
* attribute values of this class
*
* @param array $options will initialize class properties
*/
public function __construct($options = array())
{
foreach ($options as $key => $val) {
$this->$key = $val;
}
}
/**
* When rendering a query, if the template is not set explicitly will use "select" mode
* @return [type] [description]
@@ -568,5 +560,15 @@ public function expr($expr, $options = [])
{
return new Expression($expr, $options);
}
public function orExpr()
{
return new Query(['template'=>'[orwhere]']);
}
public function andExpr()
{
return new Query(['template'=>'[andwhere]']);
}
/// }}}
}
Copy path View file
@@ -256,4 +256,57 @@ public function testCombinedWhere()
->render()
);
}
public function testOrWhere()
{
$this->assertEquals(
'select `name` from `employee` where (`a` = :a or `b` = :b)',
(new Query())
->field('name')->table('employee')->where([['a',1],['b',1]])
->render()
);
$this->assertEquals(
'select `name` from `employee` where (`a` = :a or a=b)',
(new Query())
->field('name')->table('employee')->where([['a',1],'a=b'])
->render()
);
}
public function testNestedOrAnd()
{
$q = new Query();
$q->table('employee')->field('name');
$q->where(
$q
->orExpr()
->where('a',1)
->where('b',1)
);
$this->assertEquals(
'select `name` from `employee` where (`a` = :a or `b` = :b)',
$q->render()
);
$q = new Query();
$q->table('employee')->field('name');
$q->where(
$q
->orExpr()
->where('a',1)
->where('b',1)
->where($q->andExpr()
->where('true')
->where('false')
)
);
$this->assertEquals(
'select `name` from `employee` where (`a` = :a or `b` = :b or (true and false))',
$q->render()
);
}
}
ProTip! Use n and p to navigate between commits in a pull request.