Use DI for DatabaseInterface instance

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
This commit is contained in:
Maurício Meneghini Fauth 2018-02-15 14:24:55 -02:00
parent a1e088d5f8
commit 79ab4f4a50
5 changed files with 31 additions and 12 deletions

View File

@ -120,7 +120,7 @@ class TableStructureController extends TableController
$this->_showtable = $showtable;
$this->table_obj = $this->dbi->getTable($this->db, $this->table);
$this->createAddField = new CreateAddField();
$this->createAddField = new CreateAddField($dbi);
}
/**

View File

@ -8,6 +8,7 @@
namespace PhpMyAdmin;
use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Index;
use PhpMyAdmin\Table;
use PhpMyAdmin\Util;
@ -19,6 +20,21 @@ use PhpMyAdmin\Util;
*/
class CreateAddField
{
/**
* @var DatabaseInterface
*/
private $dbi;
/**
* Constructor
*
* @param DatabaseInterface $dbi DatabaseInterface interface
*/
public function __construct(DatabaseInterface $dbi)
{
$this->dbi = $dbi;
}
/**
* Transforms the radio button field_key into 4 arrays
*
@ -188,7 +204,7 @@ class CreateAddField
$keyBlockSizes = $index['Key_block_size'];
if (! empty($keyBlockSizes)) {
$sqlQuery .= " KEY_BLOCK_SIZE = "
. $GLOBALS['dbi']->escapeString($keyBlockSizes);
. $this->dbi->escapeString($keyBlockSizes);
}
// specifying index type is allowed only for primary, unique and index only
@ -202,12 +218,12 @@ class CreateAddField
$parser = $index['Parser'];
if ($index['Index_choice'] == 'FULLTEXT' && ! empty($parser)) {
$sqlQuery .= " WITH PARSER " . $GLOBALS['dbi']->escapeString($parser);
$sqlQuery .= " WITH PARSER " . $this->dbi->escapeString($parser);
}
$comment = $index['Index_comment'];
if (! empty($comment)) {
$sqlQuery .= " COMMENT '" . $GLOBALS['dbi']->escapeString($comment)
$sqlQuery .= " COMMENT '" . $this->dbi->escapeString($comment)
. "'";
}
@ -468,11 +484,11 @@ class CreateAddField
&& $_REQUEST['tbl_storage_engine'] == 'FEDERATED'
) {
$sqlQuery .= " CONNECTION = '"
. $GLOBALS['dbi']->escapeString($_REQUEST['connection']) . "'";
. $this->dbi->escapeString($_REQUEST['connection']) . "'";
}
if (!empty($_REQUEST['comment'])) {
$sqlQuery .= ' COMMENT = \''
. $GLOBALS['dbi']->escapeString($_REQUEST['comment']) . '\'';
. $this->dbi->escapeString($_REQUEST['comment']) . '\'';
}
$sqlQuery .= $this->getPartitionsDefinition();
$sqlQuery .= ';';
@ -521,9 +537,9 @@ class CreateAddField
// To allow replication, we first select the db to use and then run queries
// on this db.
if (!($GLOBALS['dbi']->selectDb($db))) {
if (!($this->dbi->selectDb($db))) {
Util::mysqlDie(
$GLOBALS['dbi']->getError(),
$this->dbi->getError(),
'USE ' . Util::backquote($db),
false,
$errorUrl
@ -535,6 +551,6 @@ class CreateAddField
if (isset($_REQUEST['preview_sql'])) {
Core::previewSQL($sqlQuery);
}
return [$GLOBALS['dbi']->tryQuery($sqlQuery), $sqlQuery];
return [$this->dbi->tryQuery($sqlQuery), $sqlQuery];
}
}

View File

@ -64,7 +64,7 @@ if (isset($_REQUEST['do_save_data'])) {
//tbl_structure.php below
unset($_REQUEST['do_save_data']);
$createAddField = new CreateAddField();
$createAddField = new CreateAddField($GLOBALS['dbi']);
list($result, $sql_query) = $createAddField->tryColumnCreationQuery($db, $table, $err_url);

View File

@ -50,7 +50,7 @@ if ($GLOBALS['dbi']->getColumns($db, $table)) {
);
}
$createAddField = new CreateAddField();
$createAddField = new CreateAddField($GLOBALS['dbi']);
// for libraries/tbl_columns_definition_form.inc.php
// check number of fields to be created

View File

@ -17,6 +17,9 @@ use PHPUnit\Framework\TestCase;
*/
class CreateAddFieldTest extends TestCase
{
/**
* @var CreateAddField
*/
private $createAddField;
/**
@ -26,7 +29,7 @@ class CreateAddFieldTest extends TestCase
*/
protected function setUp()
{
$this->createAddField = new CreateAddField();
$this->createAddField = new CreateAddField($GLOBALS['dbi']);
}
/**