From 4e938d98fc4a6fee851b00a0ca63b94661cf14df Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sun, 2 Aug 2020 23:25:58 +0300 Subject: [PATCH] orm changes --- application/Espo/ORM/DB/BaseMapper.php | 7 +++-- application/Espo/ORM/DB/MysqlMapper.php | 5 +--- .../ORM/DB/Query/{Base.php => BaseQuery.php} | 28 +++++++++---------- .../DB/Query/{Mysql.php => MysqlQuery.php} | 2 +- application/Espo/ORM/EntityManager.php | 20 +++++++------ tests/unit/Espo/ORM/DB/MapperTest.php | 2 +- tests/unit/Espo/ORM/DB/QueryTest.php | 2 +- 7 files changed, 34 insertions(+), 32 deletions(-) rename application/Espo/ORM/DB/Query/{Base.php => BaseQuery.php} (98%) rename application/Espo/ORM/DB/Query/{Mysql.php => MysqlQuery.php} (98%) diff --git a/application/Espo/ORM/DB/BaseMapper.php b/application/Espo/ORM/DB/BaseMapper.php index c46a2be59f..9fd60377c5 100644 --- a/application/Espo/ORM/DB/BaseMapper.php +++ b/application/Espo/ORM/DB/BaseMapper.php @@ -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); + } } diff --git a/application/Espo/ORM/DB/MysqlMapper.php b/application/Espo/ORM/DB/MysqlMapper.php index fc2f9336d3..64067774c5 100644 --- a/application/Espo/ORM/DB/MysqlMapper.php +++ b/application/Espo/ORM/DB/MysqlMapper.php @@ -31,8 +31,5 @@ namespace Espo\ORM\DB; class MysqlMapper extends BaseMapper { - protected function toDb(string $attribute) - { - return $this->query->toDb($attribute); - } + } diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/BaseQuery.php similarity index 98% rename from application/Espo/ORM/DB/Query/Base.php rename to application/Espo/ORM/DB/Query/BaseQuery.php index 78e2d293f0..3d72152919 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/BaseQuery.php @@ -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 = []; diff --git a/application/Espo/ORM/DB/Query/Mysql.php b/application/Espo/ORM/DB/Query/MysqlQuery.php similarity index 98% rename from application/Espo/ORM/DB/Query/Mysql.php rename to application/Espo/ORM/DB/Query/MysqlQuery.php index 1946052250..3395ce7d3d 100644 --- a/application/Espo/ORM/DB/Query/Mysql.php +++ b/application/Espo/ORM/DB/Query/MysqlQuery.php @@ -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 { diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index 2294524191..e7a78eee1c 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -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); } /** diff --git a/tests/unit/Espo/ORM/DB/MapperTest.php b/tests/unit/Espo/ORM/DB/MapperTest.php index 4bcb417c07..ce63857dac 100644 --- a/tests/unit/Espo/ORM/DB/MapperTest.php +++ b/tests/unit/Espo/ORM/DB/MapperTest.php @@ -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; diff --git a/tests/unit/Espo/ORM/DB/QueryTest.php b/tests/unit/Espo/ORM/DB/QueryTest.php index 9dd2c70588..a30ed5837f 100644 --- a/tests/unit/Espo/ORM/DB/QueryTest.php +++ b/tests/unit/Espo/ORM/DB/QueryTest.php @@ -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;