Database helper improvements

This commit is contained in:
Taras Machyshyn
2018-06-25 13:03:07 +03:00
parent ecb3273883
commit 2dfffe00d0
3 changed files with 157 additions and 13 deletions
+25 -10
View File
@@ -43,7 +43,7 @@ class Helper
'pdo_mysql' => '\Espo\Core\Utils\Database\DBAL\Driver\PDOMySql\Driver',
);
public function __construct(\Espo\Core\Utils\Config $config)
public function __construct(\Espo\Core\Utils\Config $config = null)
{
$this->config = $config;
}
@@ -56,6 +56,10 @@ class Helper
public function getDbalConnection()
{
if (!isset($this->connection)) {
if (!$this->getConfig()) {
return null;
}
$connectionParams = $this->getConfig()->get('database');
$connectionParams['driverClass'] = $this->drivers[ $connectionParams['driver'] ];
unset($connectionParams['driver']);
@@ -74,10 +78,12 @@ class Helper
*
* @return int
*/
public function getMaxIndexLength($tableName = null)
public function getMaxIndexLength($tableName = null, $default = 1000)
{
$connection = $this->getDbalConnection();
$mysqlEngine = $this->getMysqlEngine($tableName);
if (!$mysqlEngine) {
return $default;
}
switch ($mysqlEngine) {
case 'InnoDB':
@@ -98,14 +104,18 @@ class Helper
return 1000; //MyISAM
}
public function getTableMaxIndexLength($tableName)
public function getTableMaxIndexLength($tableName, $default = 1000)
{
return $this->getMaxIndexLength($tableName);
return $this->getMaxIndexLength($tableName, $default);
}
protected function getMysqlVersion()
{
$connection = $this->getDbalConnection();
if (!$connection) {
return null;
}
return $connection->fetchColumn("select version()");
}
@@ -116,9 +126,12 @@ class Helper
*
* @return string
*/
protected function getMysqlEngine($tableName = null)
protected function getMysqlEngine($tableName = null, $default = null)
{
$connection = $this->getDbalConnection();
if (!$connection) {
return $default;
}
$query = "SHOW TABLE STATUS WHERE Engine = 'MyISAM'";
if (!empty($tableName)) {
@@ -141,10 +154,12 @@ class Helper
*
* @return boolean
*/
public function isSupportsFulltext($tableName = null)
public function isSupportsFulltext($tableName = null, $default = false)
{
$connection = $this->getDbalConnection();
$mysqlEngine = $this->getMysqlEngine($tableName);
if (!$mysqlEngine) {
return $default;
}
switch ($mysqlEngine) {
case 'InnoDB':
@@ -161,8 +176,8 @@ class Helper
return true; //MyISAM
}
public function isTableSupportsFulltext($tableName)
public function isTableSupportsFulltext($tableName, $default = false)
{
return $this->isSupportsFulltext($tableName);
return $this->isSupportsFulltext($tableName, $default);
}
}
@@ -45,13 +45,18 @@ class OrmMetadata
protected $useCache;
public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Config $config)
public function __construct(\Espo\Core\Utils\Metadata $metadata, \Espo\Core\Utils\File\Manager $fileManager, $config)
{
$this->metadata = $metadata;
$this->fileManager = $fileManager;
$this->config = $config;
$this->useCache = $this->config->get('useCache', false);
$this->useCache = false;
if ($config instanceof \Espo\Core\Utils\Config) {
$this->config = $config;
$this->useCache = $this->config->get('useCache', false);
} elseif (is_bool($config)) {
$this->useCache = $config;
}
}
protected function getConverter()
@@ -0,0 +1,124 @@
<?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 tests\unit\Espo\Core\Utils;
use tests\unit\ReflectionHelper;
use Espo\Core\Utils\Util;
class HelperTest extends \PHPUnit\Framework\TestCase
{
protected $object;
protected $objects;
protected $reflection;
protected function setUp()
{
$this->objects['config'] = $this->getMockBuilder('\\Espo\\Core\\Utils\\Config')->disableOriginalConstructor()->getMock();
$this->objects['config']->expects($this->any())
->method('get')
->with($this->equalTo('database'))
->will($this->returnValue([
'driver' => 'pdo_mysql',
'dbname' => 'test',
'user' => 'test_database',
'password' => 'test_user',
'host' => 'localhost',
'port' => '',
'charset' => 'utf8mb4'
]));
}
protected function tearDown()
{
$this->object = NULL;
}
protected function initDatabaseHelper($config = null)
{
$this->object = new \Espo\Core\Utils\Database\Helper($config);
$this->reflection = new ReflectionHelper($this->object);
return $this->object;
}
public function testGetDbalConnection()
{
$this->initDatabaseHelper(null);
$this->assertNull($this->object->getDbalConnection());
}
public function testGetDbalConnectionWithConfig()
{
$this->initDatabaseHelper($this->objects['config']);
$this->assertInstanceOf('\\Doctrine\\DBAL\\Connection', $this->object->getDbalConnection());
}
public function testGetMaxIndexLength()
{
$this->initDatabaseHelper(null);
$this->assertEquals(1000, $this->object->getMaxIndexLength());
$this->assertEquals(1000, $this->object->getMaxIndexLength('table_name'));
$this->assertEquals(2000, $this->object->getMaxIndexLength('table_name', 2000));
$this->assertEquals(1000, $this->object->getTableMaxIndexLength('table_name'));
$this->assertEquals(2000, $this->object->getTableMaxIndexLength('table_name', 2000));
}
public function testGetMysqlVersion()
{
$this->initDatabaseHelper(null);
$this->assertNull($this->reflection->invokeMethod('getMysqlVersion'));
}
public function testGetMysqlEngine()
{
$this->initDatabaseHelper(null);
$this->assertNull($this->reflection->invokeMethod('getMysqlEngine'));
$this->assertEquals('InnoDB', $this->reflection->invokeMethod('getMysqlEngine', array(null, 'InnoDB')));
}
public function testIsSupportsFulltext()
{
$this->initDatabaseHelper(null);
$this->assertFalse($this->object->isSupportsFulltext());
$this->assertFalse($this->object->isSupportsFulltext('table_name'));
$this->assertTrue($this->object->isSupportsFulltext('table_name', true));
$this->assertFalse($this->object->isTableSupportsFulltext('table_name'));
$this->assertTrue($this->object->isTableSupportsFulltext('table_name', true));
}
}