Skip to content

Commit

Permalink
Merge pull request #10 from byjg/4.1.0
Browse files Browse the repository at this point in the history
4.1.0
  • Loading branch information
byjg committed Jul 11, 2020
2 parents a2f2b52 + 2528a80 commit 723dbc9
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": ">=5.6.0",
"ext-json": "*",
"byjg/anydataset-db": "4.0.*",
"byjg/anydataset-db": "4.1.*",
"byjg/serializer": "1.0.*"
},
"require-dev": {
Expand Down
6 changes: 5 additions & 1 deletion src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function addFieldAlias($fieldName, $alias)
}

/**
* @return string
* @return object
*/
public function getEntity()
{
Expand Down Expand Up @@ -163,6 +163,10 @@ public function getFieldMap($property = null, $key = null)
return $fieldMap[$key];
}

/**
* @param null $fieldName
* @return array|mixed|null
*/
public function getFieldAlias($fieldName = null)
{
if (empty($fieldName)) {
Expand Down
5 changes: 5 additions & 0 deletions src/ORMHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class ORMHelper
{
/**
* @param string $sql
* @param array $params
* @return string
*/
public static function processLiteral($sql, &$params)
{
if (!is_array($params)) {
Expand Down
61 changes: 50 additions & 11 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ class Query
{
protected $fields = [];
protected $table = "";
protected $alias = "";
protected $where = [];
protected $groupBy = [];
protected $orderBy = [];
protected $join = [];
protected $limitStart = null;
protected $limitEnd = null;
protected $top = null;
protected $dbDriver = null;

protected $forUpdate = false;

Expand Down Expand Up @@ -76,11 +78,13 @@ private function addFieldFromMapper(Mapper $mapper)
* $query->table('product');
*
* @param string $table
* @param string $alias
* @return $this
*/
public function table($table)
public function table($table, $alias = null)
{
$this->table = $table;
$this->alias = $alias;

return $this;
}
Expand All @@ -89,27 +93,44 @@ public function table($table)
* Example:
* $query->join('sales', 'product.id = sales.id');
*
* @param string $table
* @param Query|string $table
* @param string $filter
* @param string $alias
* @return $this
*/
public function join($table, $filter)
public function join($table, $filter, $alias = null)
{
$this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'INNER'];
$this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'INNER', 'alias' => empty($alias) ? $table : $alias];
return $this;
}

/**
* Example:
* $query->join('sales', 'product.id = sales.id');
* $query->leftJoin('sales', 'product.id = sales.id');
*
* @param string $table
* @param Query|string $table
* @param string $filter
* @param string $alias
* @return $this
*/
public function leftJoin($table, $filter, $alias = null)
{
$this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'LEFT', 'alias' => empty($alias) ? $table : $alias];
return $this;
}

/**
* Example:
* $query->rightJoin('sales', 'product.id = sales.id');
*
* @param Query|string $table
* @param string $filter
* @param string $alias
* @return $this
*/
public function leftJoin($table, $filter)
public function rightJoin($table, $filter, $alias = null)
{
$this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'LEFT'];
$this->join[] = [ 'table'=>$table, 'filter'=>$filter, 'type' => 'RIGHT', 'alias' => empty($alias) ? $table : $alias];
return $this;
}

Expand Down Expand Up @@ -200,12 +221,28 @@ protected function getFields()

return ' ' . implode(', ', $this->fields) . ' ';
}


/**
* @return string
* @throws InvalidArgumentException
*/
protected function getJoin()
{
$joinStr = $this->table;
$joinStr = $this->table . (!empty($this->alias) ? " as " . $this->alias : "");
foreach ($this->join as $item) {
$joinStr .= ' ' . $item['type'] . ' JOIN ' . $item['table'] . ' ON ' . $item['filter'];
$table = $item['table'];
if ($table instanceof Query) {
$subQuery = $table->build($this->dbDriver);
if (!empty($subQuery["params"])) {
throw new InvalidArgumentException("SubQuery does not support filters");
}
if ($item["alias"] instanceof Query) {
throw new InvalidArgumentException("SubQuery requires you define an alias");
}
$table = "(${subQuery["sql"]})";
}
$alias = $item['table'] == $item['alias'] ? "" : " as ". $item['alias'];
$joinStr .= ' ' . $item['type'] . " JOIN $table$alias ON " . $item['filter'];
}
return $joinStr;
}
Expand Down Expand Up @@ -234,6 +271,8 @@ protected function getWhere()
*/
public function build(DbDriverInterface $dbDriver = null)
{
$this->dbDriver = $dbDriver;

$sql = "SELECT " .
$this->getFields() .
"FROM " . $this->getJoin();
Expand Down
15 changes: 15 additions & 0 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ public function getByQuery(Query $query, array $mapper = [])
return $result;
}

/**
* Get by Query without map to an instance.
*
* @param Query $query
* @return array
* @throws Exception\InvalidArgumentException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function getByQueryRaw(Query $query)
{
$query = $query->build($this->getDbDriver());
$iterator = $this->getDbDriver()->getIterator($query['sql'], $query['params']);
return $iterator->toArray();
}

/**
* @param mixed $instance
* @return mixed
Expand Down
16 changes: 8 additions & 8 deletions src/Updatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function getWhere()

/**
* @param $params
* @param \ByJG\AnyDataset\Db\DbFunctionsInterface|null $dbHelper
* @param DbFunctionsInterface|null $dbHelper
* @return null|string|string[]
* @throws \ByJG\MicroOrm\Exception\OrmInvalidFieldsException
*/
Expand Down Expand Up @@ -118,10 +118,10 @@ public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null)
}

/**
* @param $params
* @param \ByJG\AnyDataset\Db\DbFunctionsInterface|null $dbHelper
* @return array
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @param array $params
* @param DbFunctionsInterface|null $dbHelper
* @return string
* @throws InvalidArgumentException
*/
public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
{
Expand Down Expand Up @@ -158,9 +158,9 @@ public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
}

/**
* @param $params
* @return array
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @param array $params
* @return string
* @throws InvalidArgumentException
*/
public function buildDelete(&$params)
{
Expand Down

0 comments on commit 723dbc9

Please sign in to comment.