orm changes

This commit is contained in:
Yuri Kuznetsov
2020-08-02 23:25:58 +03:00
parent da3e214ecd
commit 4e938d98fc
7 changed files with 34 additions and 32 deletions

View File

@@ -34,7 +34,7 @@ use Espo\ORM\{
Collection,
EntityFactory,
Metadata,
DB\Query\Base as Query,
DB\Query\BaseQuery as Query,
EntityCollection,
Sth2Collection,
};
@@ -1333,5 +1333,8 @@ abstract class BaseMapper implements Mapper
return $sql;
}
abstract protected function toDb(string $attribute);
protected function toDb(string $attribute)
{
return $this->query->toDb($attribute);
}
}

View File

@@ -31,8 +31,5 @@ namespace Espo\ORM\DB;
class MysqlMapper extends BaseMapper
{
protected function toDb(string $attribute)
{
return $this->query->toDb($attribute);
}
}

View File

@@ -29,7 +29,7 @@
namespace Espo\ORM\DB\Query;
use Espo\Core\Exceptions\Error;
use Espo\CoreErrors\Error;
use Espo\ORM\{
Entity,
@@ -42,7 +42,7 @@ use PDO;
/**
* Composes SQL queries.
*/
abstract class Base
abstract class BaseQuery
{
protected static $selectParamList = [
'select',
@@ -372,11 +372,11 @@ abstract class Base
if ($method !== self::SELECT_METHOD) {
if (isset($params['aggregation'])) {
throw new Error("Aggregation is not allowed for '{$method}'.");
throw new Error("ORM Query: Aggregation is not allowed for '{$method}'.");
}
if (isset($params['offset'])) {
throw new Error("Offset is not allowed for '{$method}'.");
throw new Error("ORM Query: Offset is not allowed for '{$method}'.");
}
}
@@ -622,7 +622,7 @@ abstract class Base
protected function getFunctionPart($function, $part, $entityType, $distinct = false, ?array $argumentPartList = null)
{
if (!in_array($function, $this->functionList)) {
throw new \Exception("ORM Query: Not allowed function '{$function}'.");
throw new Error("ORM Query: Not allowed function '{$function}'.");
}
if (strpos($function, 'YEAR_') === 0 && $function !== 'YEAR_NUMBER') {
@@ -658,7 +658,7 @@ abstract class Base
if (in_array($function, $this->comparisonFunctionList)) {
if (count($argumentPartList) < 2) {
throw new \Exception("ORM Query: Not enough arguments for function '{$function}'.");
throw new Error("ORM Query: Not enough arguments for function '{$function}'.");
}
$operator = $this->comparisonFunctionOperatorMap[$function];
return $argumentPartList[0] . ' ' . $operator . ' ' . $argumentPartList[1];
@@ -666,7 +666,7 @@ abstract class Base
if (in_array($function, $this->mathOperationFunctionList)) {
if (count($argumentPartList) < 2) {
throw new \Exception("ORM Query: Not enough arguments for function '{$function}'.");
throw new Error("ORM Query: Not enough arguments for function '{$function}'.");
}
$operator = $this->mathFunctionOperatorMap[$function];
return '(' . implode(' ' . $operator . ' ', $argumentPartList) . ')';
@@ -676,7 +676,7 @@ abstract class Base
$operator = $this->comparisonFunctionOperatorMap[$function];
if (count($argumentPartList) < 2) {
throw new \Exception("ORM Query: Not enough arguments for function '{$function}'.");
throw new Error("ORM Query: Not enough arguments for function '{$function}'.");
}
$operatorArgumentList = $argumentPartList;
array_shift($operatorArgumentList);
@@ -761,7 +761,7 @@ abstract class Base
protected function getFunctionPartTZ($entityType, ?array $argumentPartList = null)
{
if (!$argumentPartList || count($argumentPartList) < 2) {
throw new \Exception("Not enough arguments for function TZ.");
throw new Error("ORM Query: Not enough arguments for function TZ.");
}
$offsetHoursString = $argumentPartList[1];
if (substr($offsetHoursString, 0, 1) === '\'' && substr($offsetHoursString, -1) === '\'') {
@@ -787,14 +787,14 @@ abstract class Base
{
$delimiterPosition = strpos($expression, ':');
if ($delimiterPosition === false) {
throw new \Exception("Bad MATCH usage.");
throw new Error("ORM Query: Bad MATCH usage.");
}
$function = substr($expression, 0, $delimiterPosition);
$rest = substr($expression, $delimiterPosition + 1);
if (empty($rest)) {
throw new \Exception("Empty MATCH parameters.");
throw new Error("ORM Query: Empty MATCH parameters.");
}
if (substr($rest, 0, 1) === '(' && substr($rest, -1) === ')') {
@@ -802,7 +802,7 @@ abstract class Base
$argumentList = self::parseArgumentListFromFunctionContent($rest);
if (count($argumentList) < 2) {
throw new \Exception("Bad MATCH usage.");
throw new Error("ORM Query: Bad MATCH usage.");
}
$columnList = [];
@@ -813,7 +813,7 @@ abstract class Base
} else {
$delimiterPosition = strpos($rest, ':');
if ($delimiterPosition === false) {
throw new \Exception("Bad MATCH usage.");
throw new Error("ORM Query: Bad MATCH usage.");
}
$columns = substr($rest, 0, $delimiterPosition);
@@ -2335,7 +2335,7 @@ abstract class Base
protected function getSetPart(Entity $entity, array $values) : string
{
if (!count($values)) {
throw new Error("No SET values for update query.");
throw new Error("ORM Query: No SET values for update query.");
}
$list = [];

View File

@@ -29,7 +29,7 @@
namespace Espo\ORM\DB\Query;
class Mysql extends Base
class MysqlQuery extends BaseQuery
{
public function limit(string $sql, ?int $offset = null, ?int $limit = null) : string
{

View File

@@ -33,9 +33,11 @@ use Espo\Core\Exceptions\Error;
use Espo\ORM\DB\{
Mapper,
Query\Base as Query,
Query\BaseQuery as Query,
};
use PDO;
/**
* A central access point to ORM functionality.
*/
@@ -100,7 +102,7 @@ class EntityManager
{
if (empty($this->query)) {
$platform = $this->params['platform'];
$className = 'Espo\\ORM\\DB\\Query\\' . ucfirst($platform);
$className = 'Espo\\ORM\\DB\\Query\\' . ucfirst($platform) . 'Query';
$this->query = new $className($this->getPDO(), $this->entityFactory, $this->metadata);
}
return $this->query;
@@ -152,26 +154,26 @@ class EntityManager
$options = [];
if (isset($params['sslCA'])) {
$options[\PDO::MYSQL_ATTR_SSL_CA] = $params['sslCA'];
$options[PDO::MYSQL_ATTR_SSL_CA] = $params['sslCA'];
}
if (isset($params['sslCert'])) {
$options[\PDO::MYSQL_ATTR_SSL_CERT] = $params['sslCert'];
$options[PDO::MYSQL_ATTR_SSL_CERT] = $params['sslCert'];
}
if (isset($params['sslKey'])) {
$options[\PDO::MYSQL_ATTR_SSL_KEY] = $params['sslKey'];
$options[PDO::MYSQL_ATTR_SSL_KEY] = $params['sslKey'];
}
if (isset($params['sslCAPath'])) {
$options[\PDO::MYSQL_ATTR_SSL_CAPATH] = $params['sslCAPath'];
$options[PDO::MYSQL_ATTR_SSL_CAPATH] = $params['sslCAPath'];
}
if (isset($params['sslCipher'])) {
$options[\PDO::MYSQL_ATTR_SSL_CIPHER] = $params['sslCipher'];
$options[PDO::MYSQL_ATTR_SSL_CIPHER] = $params['sslCipher'];
}
$this->pdo = new \PDO(
$this->pdo = new PDO(
$platform . ':host='.$params['host'].';'.$port.'dbname=' . $params['dbname'] . ';charset=' . $params['charset'],
$params['user'], $params['password'], $options
);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
/**

View File

@@ -28,7 +28,7 @@
************************************************************************/
use Espo\ORM\DB\MysqlMapper;
use Espo\ORM\DB\Query\Mysql as Query;
use Espo\ORM\DB\Query\MysqlQuery as Query;
use Espo\ORM\EntityFactory;
use Espo\ORM\EntityCollection;

View File

@@ -27,7 +27,7 @@
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
use Espo\ORM\DB\Query\Mysql as Query;
use Espo\ORM\DB\Query\MysqlQuery as Query;
use Espo\ORM\EntityFactory;
use Espo\ORM\Metadata;