Skip to content

Commit

Permalink
Spacing, indentation and brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 20, 2013
1 parent 0057721 commit e18f65e
Showing 1 changed file with 44 additions and 42 deletions.
86 changes: 44 additions & 42 deletions src/SQLBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function update($args)
public function addSelect($columns) {
$args = func_get_args();
$columns = $args[0];
if( is_array($columns) ) {
if ( is_array($columns) ) {
$this->selected = array_merge( $this->selected , $columns );
} else {
$this->selected = array_merge( $this->selected , $args );
Expand All @@ -179,7 +179,7 @@ public function select($columns)
{
$args = func_get_args();
$columns = $args[0];
if( is_array($columns) ) {
if ( is_array($columns) ) {
$this->selected = $columns;
} else {
$this->selected = $args;
Expand Down Expand Up @@ -213,7 +213,7 @@ public function delete()
/*** limit , offset methods ***/
public function limit($limit)
{
if( $this->driver->type == 'sqlite' ) {
if ( $this->driver->type == 'sqlite' ) {
// throw new Exception('sqlite does not support limit syntax');
}
$this->limit = $limit;
Expand All @@ -228,7 +228,7 @@ public function limit($limit)
*/
public function offset($offset)
{
if( $this->driver->type == 'sqlite' ) {
if ( $this->driver->type == 'sqlite' ) {
// throw new Exception('sqlite does not support offset syntax');
}
$this->offset = $offset;
Expand Down Expand Up @@ -276,11 +276,11 @@ public function join($table,$type = 'LEFT')
*/
public function where( $args = null )
{
if( $args && is_array($args) ) {
if ( $args && is_array($args) ) {
return $this->whereFromArgs( $args );
}

if( $this->where ) {
if ( $this->where ) {
return $this->where;
}

Expand All @@ -301,7 +301,7 @@ public function where( $args = null )
*/
public function whereFromArgs($args)
{
if( null === $args || empty($args) )
if ( null === $args || empty($args) )
return $this;

$expr = $this->where();
Expand Down Expand Up @@ -355,7 +355,7 @@ public function orderBy($column,$order = 'desc')
public function groupBy($column)
{
$args = func_get_args();
if( count($args) > 1 ) {
if ( count($args) > 1 ) {
$this->groupBys = $args;
} else {
$this->groupBys[] = $column;
Expand Down Expand Up @@ -387,7 +387,7 @@ public function build()
{
// reset sql vars (for applying SQL statement)
$this->vars = array();
if( ! $this->behavior )
if ( ! $this->behavior )
throw new Exception('behavior is not defined.');

switch( $this->behavior )
Expand Down Expand Up @@ -423,7 +423,7 @@ protected function getTableSql()
{
$sql = '';
$sql .= $this->driver->getQuoteTableName($this->table);
if( $this->alias )
if ( $this->alias )
$sql .= ' ' . $this->alias;
return $sql;
}
Expand All @@ -439,10 +439,10 @@ protected function buildSelectColumns()
foreach( $this->selected as $k => $v ) {

/* column => alias */
if( is_string($k) ) {
if ( is_string($k) ) {
$cols[] = $this->driver->getQuoteColumn($k) . ' AS ' . $v;
}
elseif( is_integer($k) ) {
elseif ( is_integer($k) ) {
$cols[] = $this->driver->getQuoteColumn($v);
}
}
Expand Down Expand Up @@ -478,7 +478,7 @@ protected function buildUpdate()
$sql .= $this->buildConditionSql();

/* only supported in mysql, sqlite */
if( $this->driver->type == 'mysql' || $this->driver->type == 'sqlite' )
if ( $this->driver->type == 'mysql' || $this->driver->type == 'sqlite' )
$sql .= $this->buildLimitSql();

if ( $this->driver->trim ) {
Expand Down Expand Up @@ -510,7 +510,7 @@ protected function buildSelect()

$sql .= $this->buildLimitSql();

if( $this->driver->trim )
if ( $this->driver->trim )
return trim($sql);
return $sql;
}
Expand All @@ -526,17 +526,16 @@ protected function buildInsert()

/* build sql arguments */

if( $this->driver->placeholder ) {
if ( $this->driver->placeholder ) {
foreach( $this->insert as $k => $v ) {
if( is_integer($k) )
if ( is_integer($k) )
$k = $v;

if( is_array($v) ) {
if ( is_array($v) ) {
// just interpolate the raw value
$columns[] = $this->driver->getQuoteColumn($k);
$values[] = $v[0];
}
else {
} else {
$columns[] = $this->driver->getQuoteColumn($k);
$newK = $this->setPlaceHolderVar( $k , $v );
$values[] = $this->driver->getPlaceHolder($newK);
Expand All @@ -545,8 +544,9 @@ protected function buildInsert()

} else {
foreach( $this->insert as $k => $v ) {
if( is_integer($k) )
if ( is_integer($k) ) {
$k = $v;
}
$columns[] = $this->driver->getQuoteColumn( $k );
$values[] = $this->driver->inflate($v);
}
Expand All @@ -555,12 +555,13 @@ protected function buildInsert()
$sql = 'INSERT INTO ' . $this->getTableSql() . ' ( ';
$sql .= join(',',$columns) . ') VALUES ('. join(',', $values ) .')';

if( $this->returning && ( 'pgsql' == $this->driver->type ) ) {
if ( $this->returning && ( 'pgsql' == $this->driver->type ) ) {
$sql .= ' RETURNING ' . $this->driver->getQuoteColumn($this->returning);
}

if ( $this->driver->trim )
if ( $this->driver->trim ) {
return trim($sql);
}
return $sql;
}

Expand All @@ -576,7 +577,7 @@ protected function buildJoinSql()
protected function buildOrderSql()
{
$sql = '';
if( !empty($this->orders) ) {
if ( !empty($this->orders) ) {
$sql .= ' ORDER BY ';
$parts = array();
foreach( $this->orders as $order ) {
Expand All @@ -591,21 +592,21 @@ protected function buildOrderSql()
protected function buildLimitSql()
{
$sql = '';
if( 'pgsql' === $this->driver->type ) {
if( $this->limit && $this->offset ) {
if ( 'pgsql' === $this->driver->type ) {
if ( $this->limit && $this->offset ) {
$sql .= ' LIMIT ' . $this->limit . ' OFFSET ' . $this->offset;
} else if ( $this->limit ) {
} elseif ( $this->limit ) {
$sql .= ' LIMIT ' . $this->limit;
}
}
else if( 'mysql' === $this->driver->type ) {
if( $this->limit && $this->offset ) {
elseif ( 'mysql' === $this->driver->type ) {
if ( $this->limit && $this->offset ) {
$sql .= ' LIMIT ' . $this->offset . ' , ' . $this->limit;
} else if ( $this->limit ) {
} elseif ( $this->limit ) {
$sql .= ' LIMIT ' . $this->limit;
}
}
else if( $this->driver->type == 'sqlite' ) {
elseif ( $this->driver->type == 'sqlite' ) {
// just ignore
}
return $sql;
Expand All @@ -614,7 +615,7 @@ protected function buildLimitSql()
protected function buildGroupBySql()
{
$self = $this;
if( ! empty($this->groupBys) ) {
if ( ! empty($this->groupBys) ) {
return ' GROUP BY ' . join( ',' ,
array_map( function($val) use ($self) {
return $self->driver->getQuoteColumn( $val );
Expand All @@ -626,12 +627,12 @@ protected function buildGroupBySql()
protected function buildSetterSql()
{
$conds = array();
if( $this->driver->placeholder ) {
if ( $this->driver->placeholder ) {
foreach( $this->update as $k => $v ) {
if( is_array($v) ) {
if ( is_array($v) ) {
$conds[] = $this->driver->getQuoteColumn( $k ) . ' = '. $v[0];
} else {
if( is_integer($k) )
if ( is_integer($k) )
$k = $v;

$newK = $this->setPlaceHolderVar( $k , $v );
Expand All @@ -642,7 +643,7 @@ protected function buildSetterSql()
}
else {
foreach( $this->update as $k => $v ) {
if( is_array($v) ) {
if ( is_array($v) ) {
$conds[] = $this->driver->getQuoteColumn($k) . ' = ' . $v ;
} else {
$conds[] = $this->driver->getQuoteColumn($k) . ' = '
Expand All @@ -655,15 +656,17 @@ protected function buildSetterSql()

protected function buildConditionSql()
{
if( $this->where && $this->where->isComplete() )
if ( $this->where && $this->where->isComplete() ) {
return ' WHERE ' . $this->where->toSql();
}
return '';
}

protected function buildHavingSql()
{
if ($this->having )
if ($this->having ) {
return ' HAVING ' . $this->having->toSql();
}
return '';
}

Expand All @@ -682,7 +685,7 @@ public function getVars()
*/
public function setPlaceHolderVar($key,$value)
{
if( $this->driver->placeholder && $this->driver->placeholder === 'named' ) {
if ( $this->driver->placeholder && $this->driver->placeholder === 'named' ) {
$key = preg_replace('#\W+#','_', $key );
// a basic counter to avoid key confliction.
$i = 1;
Expand All @@ -691,21 +694,20 @@ public function setPlaceHolderVar($key,$value)
}
$this->vars[ ':' . $key ] = PDOParameter::cast($value);
return $key;
}
else {
} else {
$this->vars[] = $value;
return $key;
}
}

public function __clone()
{
if( $this->where ) {
if ( $this->where ) {
// after clone, set new builder object to self.
$this->where = clone $this->where;
$this->where->setBuilder($this);
}
if( $this->joinExpr ) {
if ( $this->joinExpr ) {
$nodes = array();
foreach( $this->joinExpr as $expr ) {
$n = clone $expr;
Expand Down

0 comments on commit e18f65e

Please sign in to comment.