Skip to content

Commit

Permalink
Use DI for DatabaseInterface instance
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
  • Loading branch information
MauricioFauth committed Feb 15, 2018
1 parent a1e088d commit 79ab4f4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
Expand Up @@ -120,7 +120,7 @@ public function __construct(
$this->_showtable = $showtable;
$this->table_obj = $this->dbi->getTable($this->db, $this->table);

$this->createAddField = new CreateAddField();
$this->createAddField = new CreateAddField($dbi);
}

/**
Expand Down
32 changes: 24 additions & 8 deletions libraries/classes/CreateAddField.php
Expand Up @@ -8,6 +8,7 @@
namespace PhpMyAdmin;

use PhpMyAdmin\Core;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Index;
use PhpMyAdmin\Table;
use PhpMyAdmin\Util;
Expand All @@ -19,6 +20,21 @@
*/
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
*
Expand Down Expand Up @@ -188,7 +204,7 @@ private function buildIndexStatements(
$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
Expand All @@ -202,12 +218,12 @@ private function buildIndexStatements(

$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)
. "'";
}

Expand Down Expand Up @@ -468,11 +484,11 @@ public function getTableCreationQuery($db, $table)
&& $_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 .= ';';
Expand Down Expand Up @@ -521,9 +537,9 @@ public function tryColumnCreationQuery($db, $table, $errorUrl)

// 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
Expand All @@ -535,6 +551,6 @@ public function tryColumnCreationQuery($db, $table, $errorUrl)
if (isset($_REQUEST['preview_sql'])) {
Core::previewSQL($sqlQuery);
}
return [$GLOBALS['dbi']->tryQuery($sqlQuery), $sqlQuery];
return [$this->dbi->tryQuery($sqlQuery), $sqlQuery];
}
}
2 changes: 1 addition & 1 deletion tbl_addfield.php
Expand Up @@ -64,7 +64,7 @@
//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);

Expand Down
2 changes: 1 addition & 1 deletion tbl_create.php
Expand Up @@ -50,7 +50,7 @@
);
}

$createAddField = new CreateAddField();
$createAddField = new CreateAddField($GLOBALS['dbi']);

// for libraries/tbl_columns_definition_form.inc.php
// check number of fields to be created
Expand Down
5 changes: 4 additions & 1 deletion test/classes/CreateAddFieldTest.php
Expand Up @@ -17,6 +17,9 @@
*/
class CreateAddFieldTest extends TestCase
{
/**
* @var CreateAddField
*/
private $createAddField;

/**
Expand All @@ -26,7 +29,7 @@ class CreateAddFieldTest extends TestCase
*/
protected function setUp()
{
$this->createAddField = new CreateAddField();
$this->createAddField = new CreateAddField($GLOBALS['dbi']);
}

/**
Expand Down

0 comments on commit 79ab4f4

Please sign in to comment.