From ecb32738839de8ec3bd9f0ca21acc673fa174eb2 Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Mon, 25 Jun 2018 11:08:51 +0300 Subject: [PATCH] Possibility to check if table supports fulltext index --- application/Espo/Core/Container.php | 2 +- .../Espo/Core/Utils/Database/Converter.php | 11 +- .../Espo/Core/Utils/Database/Helper.php | 168 ++++++++++++++++++ .../Core/Utils/Database/Orm/Converter.php | 26 ++- .../Core/Utils/Database/Schema/Converter.php | 2 +- .../Core/Utils/Database/Schema/Schema.php | 103 +---------- .../Espo/Core/Utils/Metadata/OrmMetadata.php | 15 +- application/Espo/Services/MysqlCharacter.php | 2 +- 8 files changed, 222 insertions(+), 107 deletions(-) create mode 100644 application/Espo/Core/Utils/Database/Helper.php diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index b8ad931511..b2dd4ffcb3 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -273,7 +273,7 @@ class Container return new \Espo\Core\Utils\Metadata\OrmMetadata( $this->get('metadata'), $this->get('fileManager'), - $this->get('config')->get('useCache') + $this->get('config') ); } diff --git a/application/Espo/Core/Utils/Database/Converter.php b/application/Espo/Core/Utils/Database/Converter.php index 91029d585e..f046112026 100644 --- a/application/Espo/Core/Utils/Database/Converter.php +++ b/application/Espo/Core/Utils/Database/Converter.php @@ -29,8 +29,8 @@ namespace Espo\Core\Utils\Database; -use Espo\Core\Utils\Util, - Espo\ORM\Entity; +use Espo\Core\Utils\Util; +use Espo\ORM\Entity; class Converter { @@ -38,15 +38,18 @@ class Converter private $fileManager; + private $config; + private $schemaConverter; private $schemaFromMetadata = null; - public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager) + public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Config $config = null) { $this->metadata = $metadata; $this->fileManager = $fileManager; - $this->ormConverter = new Orm\Converter($this->metadata, $this->fileManager); + $this->config = $config; + $this->ormConverter = new Orm\Converter($this->metadata, $this->fileManager, $this->config); } protected function getMetadata() diff --git a/application/Espo/Core/Utils/Database/Helper.php b/application/Espo/Core/Utils/Database/Helper.php new file mode 100644 index 0000000000..706ab4d873 --- /dev/null +++ b/application/Espo/Core/Utils/Database/Helper.php @@ -0,0 +1,168 @@ + '\Espo\Core\Utils\Database\DBAL\Driver\Mysqli\Driver', + 'pdo_mysql' => '\Espo\Core\Utils\Database\DBAL\Driver\PDOMySql\Driver', + ); + + public function __construct(\Espo\Core\Utils\Config $config) + { + $this->config = $config; + } + + protected function getConfig() + { + return $this->config; + } + + public function getDbalConnection() + { + if (!isset($this->connection)) { + $connectionParams = $this->getConfig()->get('database'); + $connectionParams['driverClass'] = $this->drivers[ $connectionParams['driver'] ]; + unset($connectionParams['driver']); + + $dbalConfig = new \Doctrine\DBAL\Configuration(); + $this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $dbalConfig); + } + + return $this->connection; + } + + /** + * Get maximum index length. If $tableName is empty get a value for all database tables + * + * @param string|null $tableName + * + * @return int + */ + public function getMaxIndexLength($tableName = null) + { + $connection = $this->getDbalConnection(); + $mysqlEngine = $this->getMysqlEngine($tableName); + + switch ($mysqlEngine) { + case 'InnoDB': + $mysqlVersion = $this->getMysqlVersion(); + + if (version_compare($mysqlVersion, '10.0.0') >= 0) { + return 767; //InnoDB, MariaDB + } + + if (version_compare($mysqlVersion, '5.7.0') >= 0) { + return 3072; //InnoDB, MySQL 5.7+ + } + + return 767; //InnoDB + break; + } + + return 1000; //MyISAM + } + + public function getTableMaxIndexLength($tableName) + { + return $this->getMaxIndexLength($tableName); + } + + protected function getMysqlVersion() + { + $connection = $this->getDbalConnection(); + return $connection->fetchColumn("select version()"); + } + + /** + * Get table/database tables engine. If $tableName is empty get a value for all database tables + * + * @param string|null $tableName + * + * @return string + */ + protected function getMysqlEngine($tableName = null) + { + $connection = $this->getDbalConnection(); + + $query = "SHOW TABLE STATUS WHERE Engine = 'MyISAM'"; + if (!empty($tableName)) { + $query = "SHOW TABLE STATUS WHERE Engine = 'MyISAM' AND Name = '" . $tableName . "'"; + } + + $result = $connection->fetchColumn($query); + + if (!empty($result)) { + return 'MyISAM'; + } + + return 'InnoDB'; + } + + /** + * Check if full text supports. If $tableName is empty get a value for all database tables + * + * @param string $tableName + * + * @return boolean + */ + public function isSupportsFulltext($tableName = null) + { + $connection = $this->getDbalConnection(); + $mysqlEngine = $this->getMysqlEngine($tableName); + + switch ($mysqlEngine) { + case 'InnoDB': + $mysqlVersion = $this->getMysqlVersion(); + + if (version_compare($mysqlVersion, '5.6.0') >= 0) { + return true; //InnoDB, MySQL 5.6+ + } + + return false; //InnoDB + break; + } + + return true; //MyISAM + } + + public function isTableSupportsFulltext($tableName) + { + return $this->isSupportsFulltext($tableName); + } +} \ No newline at end of file diff --git a/application/Espo/Core/Utils/Database/Orm/Converter.php b/application/Espo/Core/Utils/Database/Orm/Converter.php index 20dc18a086..7be1c9cbef 100644 --- a/application/Espo/Core/Utils/Database/Orm/Converter.php +++ b/application/Espo/Core/Utils/Database/Orm/Converter.php @@ -28,20 +28,28 @@ ************************************************************************/ namespace Espo\Core\Utils\Database\Orm; -use Espo\Core\Utils\Util, - Espo\ORM\Entity; + +use Espo\Core\Utils\Util; +use Espo\ORM\Entity; class Converter { private $metadata; + private $fileManager; + + private $config; + private $metadataHelper; + private $databaseHelper; + private $relationManager; private $entityDefs; protected $defaultFieldType = 'varchar'; + protected $defaultNaming = 'postfix'; protected $defaultLength = array( @@ -99,14 +107,16 @@ class Converter 'additionalTables', ); - public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager) + public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Config $config = null) { $this->metadata = $metadata; $this->fileManager = $fileManager; //need to featue with ormHooks. Ex. isFollowed field + $this->config = $config; $this->relationManager = new RelationManager($this->metadata); $this->metadataHelper = new \Espo\Core\Utils\Metadata\Helper($this->metadata); + $this->databaseHelper = new \Espo\Core\Utils\Database\Helper($this->config); } protected function getMetadata() @@ -114,6 +124,11 @@ class Converter return $this->metadata; } + protected function getConfig() + { + return $this->config; + } + protected function getEntityDefs($reload = false) { if (empty($this->entityDefs) || $reload) { @@ -138,6 +153,11 @@ class Converter return $this->metadataHelper; } + protected function getDatabaseHelper() + { + return $this->databaseHelper; + } + /** * Orm metadata convertation process * diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index d82611ed04..d6dd51297e 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -130,7 +130,7 @@ class Converter protected function getMaxIndexLength() { if (!isset($this->maxIndexLength)) { - $this->maxIndexLength = $this->getDatabaseSchema()->getMaxIndexLength(); + $this->maxIndexLength = $this->getDatabaseSchema()->getDatabaseHelper()->getMaxIndexLength(); } return $this->maxIndexLength; diff --git a/application/Espo/Core/Utils/Database/Schema/Schema.php b/application/Espo/Core/Utils/Database/Schema/Schema.php index 7a40e057bb..ae3bc812ed 100644 --- a/application/Espo/Core/Utils/Database/Schema/Schema.php +++ b/application/Espo/Core/Utils/Database/Schema/Schema.php @@ -48,12 +48,7 @@ class Schema private $converter; - private $connection; - - protected $drivers = array( - 'mysqli' => '\Espo\Core\Utils\Database\DBAL\Driver\Mysqli\Driver', - 'pdo_mysql' => '\Espo\Core\Utils\Database\DBAL\Driver\PDOMySql\Driver', - ); + private $databaseHelper; protected $fieldTypePaths = array( 'application/Espo/Core/Utils/Database/DBAL/FieldTypes', @@ -79,8 +74,6 @@ class Schema */ protected $rebuildActionClasses = null; - - public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\ORM\EntityManager $entityManager, \Espo\Core\Utils\File\ClassParser $classParser, \Espo\Core\Utils\Metadata\OrmMetadata $ormMetadata) { $this->config = $config; @@ -89,17 +82,17 @@ class Schema $this->entityManager = $entityManager; $this->classParser = $classParser; + $this->databaseHelper = new \Espo\Core\Utils\Database\Helper($this->config); + $this->comparator = new \Espo\Core\Utils\Database\DBAL\Schema\Comparator(); $this->initFieldTypes(); - $this->converter = new \Espo\Core\Utils\Database\Converter($this->metadata, $this->fileManager); - + $this->converter = new \Espo\Core\Utils\Database\Converter($this->metadata, $this->fileManager, $this->config); $this->schemaConverter = new Converter($this->metadata, $this->fileManager, $this, $this->config); $this->ormMetadata = $ormMetadata; } - protected function getConfig() { return $this->config; @@ -140,26 +133,16 @@ class Schema return $this->getConnection()->getDatabasePlatform(); } + public function getDatabaseHelper() + { + return $this->databaseHelper; + } public function getConnection() { - if (isset($this->connection)) { - return $this->connection; - } - - $dbalConfig = new \Doctrine\DBAL\Configuration(); - - $connectionParams = $this->getConfig()->get('database'); - - $connectionParams['driverClass'] = $this->drivers[ $connectionParams['driver'] ]; - unset($connectionParams['driver']); - - $this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $dbalConfig); - - return $this->connection; + return $this->getDatabaseHelper()->getDbalConnection(); } - protected function initFieldTypes() { foreach($this->fieldTypePaths as $path) { @@ -187,8 +170,6 @@ class Schema } } - - /* * Rebuild database schema */ @@ -224,7 +205,6 @@ class Schema return (bool) $result; } - /* * Get current database schema * @@ -248,7 +228,6 @@ class Schema //return $schema->toSql($this->getPlatform()); //it can return with DROP TABLE } - /* * Get SQL queries to get from one to another schema * @@ -261,8 +240,6 @@ class Schema return $this->toSql($schemaDiff); //$schemaDiff->toSql($this->getPlatform()); } - - /** * Init Rebuild Actions, get all classes and create them * @return void @@ -311,66 +288,4 @@ class Schema } } } - - public function getMaxIndexLength() - { - $connection = $this->getConnection(); - $mysqlEngine = $this->getMysqlEngine(); - - switch ($mysqlEngine) { - case 'InnoDB': - $mysqlVersion = $this->getMysqlVersion(); - - if (version_compare($mysqlVersion, '10.0.0') >= 0) { - return 767; //InnoDB, MariaDB - } - - if (version_compare($mysqlVersion, '5.7.0') >= 0) { - return 3072; //InnoDB, MySQL 5.7+ - } - - return 767; //InnoDB - break; - } - - return 1000; //MyISAM - } - - protected function getMysqlVersion() - { - $connection = $this->getConnection(); - return $connection->fetchColumn("select version()"); - } - - protected function getMysqlEngine() - { - $connection = $this->getConnection(); - $result = $connection->fetchColumn("SHOW TABLE STATUS WHERE Engine = 'MyISAM'"); - - if (!empty($result)) { - return 'MyISAM'; - } - - return 'InnoDB'; - } - - public function isFulltextSupports() - { - $connection = $this->getConnection(); - $mysqlEngine = $this->getMysqlEngine(); - - switch ($mysqlEngine) { - case 'InnoDB': - $mysqlVersion = $this->getMysqlVersion(); - - if (version_compare($mysqlVersion, '5.6.0') >= 0) { - return true; //InnoDB, MySQL 5.6+ - } - - return false; //InnoDB - break; - } - - return true; //MyISAM - } } diff --git a/application/Espo/Core/Utils/Metadata/OrmMetadata.php b/application/Espo/Core/Utils/Metadata/OrmMetadata.php index 219d6032cb..d44d187969 100644 --- a/application/Espo/Core/Utils/Metadata/OrmMetadata.php +++ b/application/Espo/Core/Utils/Metadata/OrmMetadata.php @@ -41,19 +41,23 @@ class OrmMetadata protected $fileManager; + protected $config; + protected $useCache; - public function __construct($metadata, $fileManager, $useCache = false) + public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Config $config) { $this->metadata = $metadata; $this->fileManager = $fileManager; - $this->useCache = $useCache; + $this->config = $config; + + $this->useCache = $this->config->get('useCache', false); } protected function getConverter() { if (!isset($this->converter)) { - $this->converter = new \Espo\Core\Utils\Database\Converter($this->metadata, $this->fileManager); + $this->converter = new \Espo\Core\Utils\Database\Converter($this->metadata, $this->fileManager, $this->config); } return $this->converter; @@ -64,6 +68,11 @@ class OrmMetadata return $this->fileManager; } + protected function getConfig() + { + return $this->config; + } + public function clearData() { $this->ormData = null; diff --git a/application/Espo/Services/MysqlCharacter.php b/application/Espo/Services/MysqlCharacter.php index 877a7b9e80..bfb99ba769 100644 --- a/application/Espo/Services/MysqlCharacter.php +++ b/application/Espo/Services/MysqlCharacter.php @@ -49,7 +49,7 @@ class MysqlCharacter extends \Espo\Core\Services\Base $ormMeta = $container->get('ormMetadata')->getData(true); $databaseSchema = $container->get('schema'); - $maxIndexLength = $databaseSchema->getMaxIndexLength(); + $maxIndexLength = $databaseSchema->getDatabaseHelper()->getMaxIndexLength(); if ($maxIndexLength > 1000) { $maxIndexLength = 1000; }