From c9e7f02b23f84f24ff7ff283f6d526df0285f457 Mon Sep 17 00:00:00 2001 From: yuri Date: Tue, 1 Nov 2016 15:07:18 +0200 Subject: [PATCH] cut text columns --- application/Espo/ORM/DB/Query/Base.php | 57 +++++++++++++++----------- application/Espo/Services/Record.php | 6 +++ 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index 363b2838b2..48aba010fa 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -52,7 +52,8 @@ abstract class Base 'aggregation', 'aggregationBy', 'groupBy', - 'skipTextColumns' + 'skipTextColumns', + 'maxTextColumnsLength' ); protected static $sqlOperators = array( @@ -124,7 +125,7 @@ abstract class Base $wherePart = $this->getWhere($entity, $whereClause, 'AND', $params); if (empty($params['aggregation'])) { - $selectPart = $this->getSelect($entity, $params['select'], $params['distinct'], $params['skipTextColumns']); + $selectPart = $this->getSelect($entity, $params['select'], $params['distinct'], $params['skipTextColumns'], $params['maxTextColumnsLength']); $orderPart = $this->getOrder($entity, $params['orderBy'], $params['order']); if (!empty($params['additionalColumns']) && is_array($params['additionalColumns']) && !empty($params['relationName'])) { @@ -261,58 +262,63 @@ abstract class Base return $part; } - protected function getSelect(IEntity $entity, $fields = null, $distinct = false, $skipTextColumns = false) + protected function getSelect(IEntity $entity, $fields = null, $distinct = false, $skipTextColumns = false, $maxTextColumnsLength = null) { $select = ""; $arr = array(); $specifiedList = is_array($fields) ? true : false; if (empty($fields)) { - $fieldList = array_keys($entity->fields); + $attributeList = array_keys($entity->fields); } else { - $fieldList = $fields; - foreach ($fieldList as $i => $field) { - if (!is_array($field)) { - $fieldList[$i] = $this->sanitizeAlias($field); + $attributeList = $fields; + foreach ($attributeList as $i => $attribute) { + if (!is_array($attribute)) { + $attributeList[$i] = $this->sanitizeAlias($attribute); } } } - foreach ($fieldList as $field) { + foreach ($attributeList as $attribute) { + $attributeType = null; + if (is_string($attribute)) { + $attributeType = $entity->getAttributeType($attribute); + } if ($skipTextColumns) { - if ($entity->getAttributeType($field) === $entity::TEXT) { + if ($attributeType === $entity::TEXT) { continue; } } - if (is_array($field) && count($field) == 2) { - if (stripos($field[0], 'VALUE:') === 0) { - $part = substr($field[0], 6); + + if (is_array($attribute) && count($attribute) == 2) { + if (stripos($attribute[0], 'VALUE:') === 0) { + $part = substr($attribute[0], 6); $part = $this->quote($part); } else { - if (!array_key_exists($field[0], $entity->fields)) { - $part = $this->convertComplexExpression($entity, $field[0], $distinct); + if (!array_key_exists($attribute[0], $entity->fields)) { + $part = $this->convertComplexExpression($entity, $attribute[0], $distinct); } else { - $fieldDefs = $entity->fields[$field[0]]; + $fieldDefs = $entity->fields[$attribute[0]]; if (!empty($fieldDefs['select'])) { $part = $fieldDefs['select']; } else { if (!empty($fieldDefs['notStorable'])) { continue; } - $part = $this->getFieldPath($entity, $field[0]); + $part = $this->getFieldPath($entity, $attribute[0]); } } } - $arr[] = $part . ' AS `' . $this->sanitizeAlias($field[1]) . '`'; + $arr[] = $part . ' AS `' . $this->sanitizeAlias($attribute[1]) . '`'; continue; } - if (array_key_exists($field, $entity->fields)) { - $fieldDefs = $entity->fields[$field]; + if (array_key_exists($attribute, $entity->fields)) { + $fieldDefs = $entity->fields[$attribute]; } else { - $part = $this->convertComplexExpression($entity, $field, $distinct); - $arr[] = $part . ' AS `' . $field . '`'; + $part = $this->convertComplexExpression($entity, $attribute, $distinct); + $arr[] = $part . ' AS `' . $attribute . '`'; continue; } @@ -322,10 +328,13 @@ abstract class Base if (!empty($fieldDefs['notStorable'])) { continue; } - $fieldPath = $this->getFieldPath($entity, $field); + $fieldPath = $this->getFieldPath($entity, $attribute); + if ($attributeType === $entity::TEXT && $maxTextColumnsLength !== null) { + $fieldPath = 'LEFT(' . $fieldPath . ', '. intval($maxTextColumnsLength) . ')'; + } } - $arr[] = $fieldPath . ' AS `' . $field . '`'; + $arr[] = $fieldPath . ' AS `' . $attribute . '`'; } $select = implode(', ', $arr); diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 4e5088b7f7..978707fcb2 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -83,6 +83,8 @@ class Record extends \Espo\Core\Services\Base protected $duplicatingLinkList = []; + const MAX_TEXT_COLUMN_LENGTH_FOR_LIST = 5000; + const FOLLOWERS_LIMIT = 4; public function __construct() @@ -685,6 +687,8 @@ class Record extends \Espo\Core\Services\Base $selectParams = $this->getSelectParams($params); + $selectParams['maxTextColumnsLength'] = $this->getConfig()->get('maxTextColumnLengthForList', self::MAX_TEXT_COLUMN_LENGTH_FOR_LIST); + $collection = $this->getRepository()->find($selectParams); foreach ($collection as $e) { @@ -749,6 +753,8 @@ class Record extends \Espo\Core\Services\Base $selectParams = array_merge($selectParams, $this->linkSelectParams[$link]); } + $selectParams['maxTextColumnsLength'] = $this->getConfig()->get('maxTextColumnLengthForList', self::MAX_TEXT_COLUMN_LENGTH_FOR_LIST); + $collection = $this->getRepository()->findRelated($entity, $link, $selectParams); $recordService = $this->getRecordService($foreignEntityName);