Skip to content

Commit

Permalink
add join, order by, group by
Browse files Browse the repository at this point in the history
  • Loading branch information
fudyartanto committed Jun 29, 2017
1 parent a7a1f0b commit 768b0fa
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 5 deletions.
6 changes: 4 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ <h4 id="installation">Installation</h4>
</ul>
</div>
<p>
Then you need include composer autoload file to load C5orm class. In your controller.php add this code below.
Register C5orm class to your package controller.
</p>
<pre class="line-numbers"><code class="language-php">
require __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
protected $pkgAutoloaderRegistries = [
'vendor/fudyartanto/c5orm/src' => '\Fudyartanto\C5orm'
];
</code></pre>
</section>

Expand Down
187 changes: 184 additions & 3 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class Builder
{
const WHERE_TYPE_IN = 'IN';

/**
* Table name
Expand Down Expand Up @@ -49,6 +50,41 @@ class Builder
*/
protected $values = [];

/**
* Join
*
* @var array
*/
protected $join = [];

/**
* Limit
*
* @var integer
*/
protected $limit;

/**
* Offset
*
* @var integer
*/
protected $offset;

/**
* Group By
*
* @var array
*/
protected $groupBy = [];

/**
* Order by
*
* @var array
*/
protected $orderBy = [];

/**
* Get database
*/
Expand All @@ -68,18 +104,51 @@ public function __construct($table, $modelClass)
$this->modelClass = $modelClass;
}

/**
* Add select query
*
* @param string $column
* @return Fudyartanto\C5orm\Builder
*/
public function select($column)
{
$this->columns[] = $column;
return $this;
}

/**
* Add join query
*
* @param string $table
* @param string $colLeft
* @param string $operator
* @param string $colRight
* @return Fudyartanto\C5orm\Builder
*/
public function join($table, $colLeft, $operator, $colRight)
{
$this->join[] = [
'table' => $table,
'colLeft' => $colLeft,
'operator' => $operator,
'colRight' => $colRight
];
return $this;
}

/**
* Add query condition
*
* @param string $column
* @param string $operator
* @param string|int $value
* @return object Fudyartanto\C5orm\Builder
* @return Fudyartanto\C5orm\Builder
*/
public function where($column, $operator, $value)
{
// add value to stack
$this->values[] = $value;
// add where condition to stack
$this->where[] = [
'column' => $column,
'operator' => $operator,
Expand All @@ -88,20 +157,114 @@ public function where($column, $operator, $value)
return $this;
}

/**
* Add where in condition
*
* @param string $column
* @param array $values
* @return Fudyartanto\C5orm\Builder
*/
public function whereIn($column, $values) {
// add value to stack
$this->values = array_merge($this->values, $values);

$values = array_map(function($v) {
return '?';
}, $values);
$this->where[] = [
'column' => $column,
'value' => implode(',', $values),
'type' => self::WHERE_TYPE_IN
];
return $this;
}

/**
* Add limit query
*
* @param integer $limit
* @return Fudyartanto\C5orm\Builder
*/
public function limit($limit)
{
$this->limit = $limit;
return $this;
}

/**
* Add offset query
*
* @param integer $offset
* @return Fudyartanto\C5orm\Builder
*/
public function offset($offset)
{
$this->offset = $offset;
return $this;
}

/**
* Add group by query
*
* @param integer $column
* @return Fudyartanto\C5orm\Builder
*/
public function groupBy($column)
{
$this->groupBy[] = $column;
return $this;
}

/**
* Add order by query
*
* @param string $column
* @param string $direction ASC|DESC
* @return Fudyartanto\C5orm\Builder
*/
public function orderBy($column, $direction = 'ASC')
{
$this->orderBy[] = $column . ' ' . $direction;
return $this;
}

/**
* Build SQL query
*
* @return string
*/
public function getRawQuery()
{
$q = "SELECT * FROM {$this->table}";
$select = $this->columns ? implode(',', $this->columns) : '*';
$q = "SELECT {$select} FROM {$this->table}";
if ($this->join) {
$join = array_map(function ($v) {
return "{$v['table']} ON ({$v['colLeft']} {$v[operator]} {$v['colRight']})";
}, $this->join);
$q .= " JOIN " . implode(" JOIN ", $join);
}
if ($this->where) {
$where = array_map(function ($v) {
return "`{$v['column']}` {$v['operator']} {$v['value']}";
if (isset($v['type']) && $v['type'] == self::WHERE_TYPE_IN) {
return "{$v['column']} IN ({$v['value']})";
} else {
return "{$v['column']} {$v['operator']} {$v['value']}";
}
}, $this->where);
$q .= " WHERE " . implode(" AND ", $where);
}
if ($this->groupBy) {
$q .= " GROUP BY " . implode(',', $this->groupBy);
}
if ($this->orderBy) {
$q .= " ORDER BY " . implode(',', $this->orderBy);
}
if ($this->limit) {
$q .= " LIMIT {$this->limit}" ;
}
if ($this->offset) {
$q .= " OFFSET {$this->offset}" ;
}
return $q;
}

Expand Down Expand Up @@ -152,4 +315,22 @@ public function get()
return new Collections([]);
}
}

/**
* Pluck result by column name
*
* @param string $column
* @return array
*/
public function pluck($column)
{
if ($result = $this->get()->data()) {
return array_map(function($obj) use ($column) {
return $obj->{$column};
}, $result);
return $result;
} else {
return [];
}
}
}
37 changes: 37 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,41 @@ public function hasOne($className, $foreignkey, $localKey)
{
return call_user_func_array([$className, "where"], [$foreignkey, "=", $this->$localKey])->first();
}

/**
* Add select query
*
* @param string $column
* @return Fudyartanto\C5orm\Builder
*/
public static function select($column)
{
return self::getBuilder()->select($column);
}

/**
* Add join query
*
* @param string $table
* @param string $colLeft
* @param string $operator
* @param string $colRight
* @return Fudyartanto\C5orm\Builder
*/
public static function join($table, $colLeft, $operator, $colRight)
{
return self::getBuilder()->join($table, $colLeft, $operator, $colRight);
}

/**
* Add order by query
*
* @param string $column
* @param string $direction ASC|DESC
* @return Fudyartanto\C5orm\Builder
*/
public static function orderBy($column, $direction)
{
return self::getBuilder()->orderBy($column, $direction);
}
}

0 comments on commit 768b0fa

Please sign in to comment.