Possibility to check if table supports fulltext index

This commit is contained in:
Taras Machyshyn
2018-06-25 11:08:51 +03:00
parent ed438b1a31
commit ecb3273883
8 changed files with 222 additions and 107 deletions
+1 -1
View File
@@ -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')
);
}
@@ -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()
@@ -0,0 +1,168 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
* Website: http://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Utils\Database;
use Espo\Core\Utils\Util;
use Espo\ORM\Entity;
class Helper
{
private $config;
private $connection;
protected $drivers = array(
'mysqli' => '\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);
}
}
@@ -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
*
@@ -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;
@@ -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
}
}
@@ -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;
+1 -1
View File
@@ -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;
}