Skip to content

Commit

Permalink
Some adjusts
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Nov 24, 2018
1 parent ce96ba4 commit 918ead8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ $connection->commitTransaction();

## Install

Just type: `composer require "byjg/micro-orm=2.0.*"`
Just type:

```
composer require "byjg/micro-orm=4.0.*"
```

## Running Tests

Expand Down
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": "dev-master",
"byjg/anydataset-db": "4.0.*",
"byjg/serializer": "1.0.*"
},
"require-dev": {
Expand Down
2 changes: 2 additions & 0 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ByJG\MicroOrm;

use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\MicroOrm\Exception\OrmModelInvalidException;

class Mapper
Expand Down Expand Up @@ -69,6 +70,7 @@ public function prepareField($field)
* @param \Closure|null|bool $updateMask
* @param \Closure $selectMask
* @return $this
* @throws InvalidArgumentException
*/
public function addFieldMap($property, $fieldName, \Closure $updateMask = null, \Closure $selectMask = null)
{
Expand Down
22 changes: 11 additions & 11 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,28 +203,28 @@ protected function getFields()

protected function getJoin()
{
$join = $this->table;
$joinStr = $this->table;
foreach ($this->join as $item) {
$join .= ' ' . $item['type'] . ' JOIN ' . $item['table'] . ' ON ' . $item['filter'];
$joinStr .= ' ' . $item['type'] . ' JOIN ' . $item['table'] . ' ON ' . $item['filter'];
}
return $join;
return $joinStr;
}

protected function getWhere()
{
$where = [];
$whereStr = [];
$params = [];

foreach ($this->where as $item) {
$where[] = $item['filter'];
$whereStr[] = $item['filter'];
$params = array_merge($params, $item['params']);
}

if (empty($where)) {
if (empty($whereStr)) {
return null;
}

return [ implode(' AND ', $where), $params ];
return [ implode(' AND ', $whereStr), $params ];
}

/**
Expand All @@ -238,11 +238,11 @@ public function build(DbDriverInterface $dbDriver = null)
$this->getFields() .
"FROM " . $this->getJoin();

$where = $this->getWhere();
$whereStr = $this->getWhere();
$params = null;
if (!is_null($where)) {
$sql .= ' WHERE ' . $where[0];
$params = $where[1];
if (!is_null($whereStr)) {
$sql .= ' WHERE ' . $whereStr[0];
$params = $whereStr[1];
}

$sql .= $this->addGroupBy();
Expand Down
54 changes: 24 additions & 30 deletions src/Updatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ protected function getFields()

protected function getWhere()
{
$where = [];
$whereStr = [];
$params = [];

foreach ($this->where as $item) {
$where[] = $item['filter'];
$whereStr[] = $item['filter'];
$params = array_merge($params, $item['params']);
}

if (empty($where)) {
if (empty($whereStr)) {
return null;
}

return [ implode(' AND ', $where), $params ];
return [ implode(' AND ', $whereStr), $params ];
}


Expand All @@ -98,25 +98,23 @@ public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null)
throw new OrmInvalidFieldsException('You must specifiy the fields for insert');
}

$fields = $this->fields;
$fieldsStr = $this->fields;
if (!is_null($dbHelper)) {
$fields = $dbHelper->delimiterField($fields);
$fieldsStr = $dbHelper->delimiterField($fieldsStr);
}

$table = $this->table;
$tableStr = $this->table;
if (!is_null($dbHelper)) {
$table = $dbHelper->delimiterTable($table);
$tableStr = $dbHelper->delimiterTable($tableStr);
}

$sql = 'INSERT INTO '
. $table
. '( ' . implode(', ', $fields) . ' ) '
. $tableStr
. '( ' . implode(', ', $fieldsStr) . ' ) '
. ' values '
. '( [[' . implode(']], [[', $this->fields) . ']] ) ';

$sql = ORMHelper::processLiteral($sql, $params);

return $sql;
return ORMHelper::processLiteral($sql, $params);
}

/**
Expand All @@ -131,17 +129,17 @@ public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
throw new InvalidArgumentException('You must specifiy the fields for insert');
}

$fields = [];
$fieldsStr = [];
foreach ($this->fields as $field) {
$fieldName = $field;
if (!is_null($dbHelper)) {
$fieldName = $dbHelper->delimiterField($fieldName);
}
$fields[] = "$fieldName = [[$field]] ";
$fieldsStr[] = "$fieldName = [[$field]] ";
}

$where = $this->getWhere();
if (is_null($where)) {
$whereStr = $this->getWhere();
if (is_null($whereStr)) {
throw new InvalidArgumentException('You must specifiy a where clause');
}

Expand All @@ -151,14 +149,12 @@ public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
}

$sql = 'UPDATE ' . $tableName . ' SET '
. implode(', ', $fields)
. ' WHERE ' . $where[0];

$params = array_merge($params, $where[1]);
. implode(', ', $fieldsStr)
. ' WHERE ' . $whereStr[0];

$sql = ORMHelper::processLiteral($sql, $params);
$params = array_merge($params, $whereStr[1]);

return $sql;
return ORMHelper::processLiteral($sql, $params);
}

/**
Expand All @@ -168,18 +164,16 @@ public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
*/
public function buildDelete(&$params)
{
$where = $this->getWhere();
if (is_null($where)) {
$whereStr = $this->getWhere();
if (is_null($whereStr)) {
throw new InvalidArgumentException('You must specifiy a where clause');
}

$sql = 'DELETE FROM ' . $this->table
. ' WHERE ' . $where[0];

$params = array_merge($params, $where[1]);
. ' WHERE ' . $whereStr[0];

$sql = ORMHelper::processLiteral($sql, $params);
$params = array_merge($params, $whereStr[1]);

return $sql;
return ORMHelper::processLiteral($sql, $params);
}
}

0 comments on commit 918ead8

Please sign in to comment.