From 18aee370577c93bcefd99fa47e91dcb3ccefa07b Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sun, 16 Aug 2020 14:55:40 +0300 Subject: [PATCH] remove sql usage, person name field defs changes --- .../Espo/Core/Utils/Database/Orm/Base.php | 39 ------- .../Utils/Database/Orm/Fields/PersonName.php | 110 ++++++++++++------ .../Espo/ORM/QueryComposer/Functions.php | 2 + 3 files changed, 74 insertions(+), 77 deletions(-) diff --git a/application/Espo/Core/Utils/Database/Orm/Base.php b/application/Espo/Core/Utils/Database/Orm/Base.php index 22c69a86ea..affde0671e 100644 --- a/application/Espo/Core/Utils/Database/Orm/Base.php +++ b/application/Espo/Core/Utils/Database/Orm/Base.php @@ -222,45 +222,6 @@ class Base protected function getForeignField(string $name, string $entityType) { - $foreignField = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $name]); - - if (isset($foreignField['type']) && $foreignField['type'] == 'personName') { - $personNameFormat = $this->config->get('personNameFormat'); - - switch ($personNameFormat) { - case 'lastFirst': - return [ - 'last' . ucfirst($name), - ' ', - 'first' . ucfirst($name), - ]; - - case 'lastFirstMiddle': - return [ - 'last' . ucfirst($name), - ' ', - 'first' . ucfirst($name), - ' ', - 'middle' . ucfirst($name), - ]; - - case 'firstMiddleLast': - return [ - 'first' . ucfirst($name), - ' ', - 'middle' . ucfirst($name), - ' ', - 'last' . ucfirst($name), - ]; - } - - return [ - 'first' . ucfirst($name), - ' ', - 'last' . ucfirst($name), - ]; - } - return $name; } diff --git a/application/Espo/Core/Utils/Database/Orm/Fields/PersonName.php b/application/Espo/Core/Utils/Database/Orm/Fields/PersonName.php index d05495f61b..e5b563740b 100644 --- a/application/Espo/Core/Utils/Database/Orm/Fields/PersonName.php +++ b/application/Espo/Core/Utils/Database/Orm/Fields/PersonName.php @@ -58,8 +58,6 @@ class PersonName extends Base $subList = ['first' . ucfirst($fieldName), ' ', 'last' . ucfirst($fieldName)]; } - $tableName = Util::toUnderScore($entityType); - if ($format === 'lastFirstMiddle' || $format === 'lastFirst') { $orderBy1Field = 'last' . ucfirst($fieldName); $orderBy2Field = 'first' . ucfirst($fieldName); @@ -68,58 +66,88 @@ class PersonName extends Base $orderBy2Field = 'last' . ucfirst($fieldName); } - $uname = ucfirst($fieldName); - $fullList = []; - $fieldList = []; - $parts = []; + $whereItems = []; foreach ($subList as $subFieldName) { $fieldNameTrimmed = trim($subFieldName); - if (!empty($fieldNameTrimmed)) { - $columnName = $tableName . '.' . Util::toUnderScore($fieldNameTrimmed); - $fullList[] = $fieldList[] = $columnName; - $parts[] = $columnName." {operator} {value}"; - } else { + if (empty($fieldNameTrimmed)) { $fullList[] = "'" . $subFieldName . "'"; + continue; } + + $fullList[] = $fieldNameTrimmed; + + $whereItems[] = $fieldNameTrimmed; } - $firstColumn = $tableName . '.' . Util::toUnderScore('first' . $uname); - $lastColumn = $tableName . '.' . Util::toUnderScore('last' . $uname); - $middleColumn = $tableName . '.' . Util::toUnderScore('middle' . $uname); + $uname = ucfirst($fieldName); - $whereString = "".implode(" OR ", $parts); + $firstName = 'first' . $uname; + $middleName = 'middle' . $uname; + $lastName = 'last' . $uname; + + $whereItems[] = "CONCAT:({$firstName}, ' ', {$lastName})"; + $whereItems[] = "CONCAT:({$lastName}, ' ', {$firstName})"; if ($format === 'firstMiddleLast') { - $whereString .= - " OR CONCAT({$firstColumn}, ' ', {$middleColumn}, ' ', {$lastColumn}) {operator} {value}" . - " OR CONCAT({$firstColumn}, ' ', {$lastColumn}) {operator} {value}" . - " OR CONCAT({$lastColumn}, ' ', {$firstColumn}) {operator} {value}"; - } else if ($format === 'lastFirstMiddle') { - $whereString .= - " OR CONCAT({$lastColumn}, ' ', {$firstColumn}, ' ', {$middleColumn}) {operator} {value}" . - " OR CONCAT({$firstColumn}, ' ', {$lastColumn}) {operator} {value}" . - " OR CONCAT({$lastColumn}, ' ', {$firstColumn}) {operator} {value}"; - } else { - $whereString .= " OR CONCAT({$firstColumn}, ' ', {$lastColumn}) {operator} {value}"; - $whereString .= " OR CONCAT({$lastColumn}, ' ', {$firstColumn}) {operator} {value}"; + $whereItems[] = "CONCAT:({$firstName}, ' ', {$middleName}, ' ', {$lastName})"; + } else + if ($format === 'lastFirstMiddle') { + $whereItems[] = "CONCAT:({$lastName}, ' ', {$firstColumn}, ' ', {$middleName})"; } - $selectString = $this->getSelect($fullList); + $selectExpression = $this->getSelect($fullList); + + $selectForeignExpression = $this->getSelect($fullList, '{alias}'); if ($format === 'firstMiddleLast' || $format === 'lastFirstMiddle') { - $selectString = "REPLACE({$selectString}, ' ', ' ')"; + $selectExpression = "REPLACE:({$selectExpression}, ' ', ' ')"; + $selectForeignExpression = "REPLACE:({$selectForeignExpression}, ' ', ' ')"; } $fieldDefs = [ 'type' => 'varchar', - 'select' => $selectString, + 'select' => [ + 'select' => $selectExpression, + ], + 'selectForeign' => [ + 'select' => $selectForeignExpression, + ], 'where' => [ - 'LIKE' => str_replace('{operator}', 'LIKE', $whereString), - '=' => str_replace('{operator}', '=', $whereString), + 'LIKE' => [ + 'whereClause' => [ + 'OR' => array_fill_keys( + array_map( + function ($item) { + return $item . '*'; + }, + $whereItems + ), + '{value}' + ), + ], + ], + 'NOT LIKE' => [ + 'whereClause' => [ + 'AND' => array_fill_keys( + array_map( + function ($item) { + return $item . '!*'; + }, + $whereItems, + ), + '{value}' + ), + ], + ], + '=' => [ + 'whereClause' => [ + 'OR' => array_fill_keys($whereItems, '{value}'), + ], + ], ], 'order' => [ 'order' => [ @@ -141,23 +169,29 @@ class PersonName extends Base $entityType => [ 'fields' => [ $fieldName => $fieldDefs, - ] - ] + ], + ], ]; } - protected function getSelect(array $fullList) + protected function getSelect(array $fullList, ?string $alias = null) : string { foreach ($fullList as &$item) { $rowItem = trim($item, " '"); - if (!empty($rowItem)) { - $item = "IFNULL(".$item.", '')"; + if (empty($rowItem)) { + continue; } + + if ($alias) { + $item = $alias . '.' . $item; + } + + $item = "IFNULL:({$item}, '')"; } - $select = "TRIM(CONCAT(".implode(", ", $fullList)."))"; + $select = "TRIM:(CONCAT:(" . implode(", ", $fullList) . "))"; return $select; } diff --git a/application/Espo/ORM/QueryComposer/Functions.php b/application/Espo/ORM/QueryComposer/Functions.php index 35c670acec..2a505836c1 100644 --- a/application/Espo/ORM/QueryComposer/Functions.php +++ b/application/Espo/ORM/QueryComposer/Functions.php @@ -62,6 +62,7 @@ class Functions 'LOWER', 'UPPER', 'TRIM', + 'REPLACE', 'LENGTH', 'CHAR_LENGTH', 'YEAR_0', @@ -154,6 +155,7 @@ class Functions 'MOD', 'IFNULL', 'NULLIF', + 'REPLACE', 'TIMESTAMPDIFF_DAY', 'TIMESTAMPDIFF_MONTH', 'TIMESTAMPDIFF_YEAR',