Added some method inside JDatabaseDriver to create database and alter character set #1261

Closed
wants to merge 7 commits into
from
@@ -378,6 +378,27 @@ public function __construct($options)
}
/**
+ * Alter database's character set, obtaining query string from protected member.
+ *
+ * @param string $dbName The database name that will be altered
+ *
+ * @return string The query that alter the database query string
+ *
+ * @since 12.2
+ * @throws RuntimeException
+ */
+ public function alterDbCharacterSet($dbName)
+ {
+ if (is_null($dbName))
+ {
+ throw new RuntimeException('Database name must not be null.');
+ }
+
+ $this->setQuery($this->getAlterDbCharacterSet($dbName));
+ return $this->execute();
+ }
+
+ /**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
@@ -397,6 +418,38 @@ public function __construct($options)
abstract public function connected();
/**
+ * Create a new database using information from $options object, obtaining query string
+ * from protected member.
+ *
+ * @param stdClass $options Object used to pass user and database name to database driver.
+ * This object must have "db_name" and "db_user" set.
+ * @param boolean $utf True if the database supports the UTF-8 character set.
+ *
+ * @return string The query that creates database
+ *
+ * @since 12.2
+ * @throws RuntimeException
+ */
+ public function createDatabase($options, $utf = true)
+ {
+ if (is_null($options))
+ {
+ throw new RuntimeException('$options object must not be null.');
+ }
+ elseif (empty($options->db_name))
+ {
+ throw new RuntimeException('$options object must have db_name set.');
+ }
+ elseif (empty($options->db_user))
+ {
+ throw new RuntimeException('$options object must have db_user set.');
+ }
+
+ $this->setQuery($this->getCreateDatabaseQuery($options, $utf));
+ return $this->execute();
+ }
+
+ /**
* Disconnects the database.
*
* @return void
@@ -485,6 +538,48 @@ public function __construct($options)
abstract public function getAffectedRows();
/**
+ * Return the query string to alter the database character set.
+ *
+ * @param string $dbName The database name
+ *
+ * @return string The query that alter the database query string
+ *
+ * @since 12.2
+ */
+ protected function getAlterDbCharacterSet($dbName)
+ {
+ $query = 'ALTER DATABASE ' . $this->quoteName($dbName) . ' CHARACTER SET `utf8`';
+
+ return $query;
+ }
+
+ /**
+ * Return the query string to create new Database.
+ * Each database driver, other than MySQL, need to override this member to return correct string.
+ *
+ * @param stdClass $options Object used to pass user and database name to database driver.
+ * This object must have "db_name" and "db_user" set.
+ * @param boolean $utf True if the database supports the UTF-8 character set.
+ *
+ * @return string The query that creates database
+ *
+ * @since 12.2
+ */
+ protected function getCreateDatabaseQuery($options, $utf)
+ {
+ if ($utf)
+ {
+ $query = 'CREATE DATABASE ' . $this->quoteName($options->db_name) . ' CHARACTER SET `utf8`';
+ }
+ else
+ {
+ $query = 'CREATE DATABASE ' . $this->quoteName($options->db_name);
+ }
+
+ return $query;
+ }
+
+ /**
* Method to get the database collation in use by sampling a text field of a table in the database.
*
* @return mixed The collation in use by the database or boolean false if not supported.
@@ -787,15 +787,15 @@ public function setUTF()
/**
* This function return a field value as a prepared string to be used in a SQL statement.
*
- * @param array $columns Array of table's column returned by ::getTableColumns.
+ * @param array &$columns Array of table's column returned by JDatabasePostgreSQL::getTableColumns.
* @param string $field_name The table field's name.
* @param string $field_value The variable value to quote and return.
*
* @return string The quoted string.
*
* @since 11.3
*/
- public function sqlValue($columns, $field_name, $field_value)
+ protected function sqlValue(&$columns, $field_name, $field_value)
{
switch ($columns[$field_name])
{
@@ -814,7 +814,6 @@ public function sqlValue($columns, $field_name, $field_value)
case 'bigserial':
case 'integer':
case 'money':
- case 'numeric':
case 'real':
case 'smallint':
case 'serial':
@@ -1093,34 +1092,33 @@ public function getRandom()
}
/**
- * Get the query string to alter the database character set.
+ * Return the query string to alter the database character set.
*
* @param string $dbName The database name
*
* @return string The query that alter the database query string
*
- * @since 12.1
+ * @since 12.2
*/
- public function getAlterDbCharacterSet( $dbName )
+ protected function getAlterDbCharacterSet($dbName)
{
$query = 'ALTER DATABASE ' . $this->quoteName($dbName) . ' SET CLIENT_ENCODING TO ' . $this->quote('UTF8');
return $query;
}
/**
- * Get the query string to create new Database in correct PostgreSQL syntax.
+ * Return the query string to create new Database using PostgreSQL's syntax
*
- * @param object $options object coming from "initialise" function to pass user
- * and database name to database driver.
- * @param boolean $utf True if the database supports the UTF-8 character set,
- * not used in PostgreSQL "CREATE DATABASE" query.
+ * @param stdClass $options Object used to pass user and database name to database driver.
+ * This object must have "db_name" and "db_user" set.
+ * @param boolean $utf True if the database supports the UTF-8 character set.
*
- * @return string The query that creates database, owned by $options['user']
+ * @return string The query that creates database, owned by $options['user']
*
- * @since 12.1
+ * @since 12.2
*/
- public function getCreateDbQuery($options, $utf)
+ protected function getCreateDatabaseQuery($options, $utf)
{
$query = 'CREATE DATABASE ' . $this->quoteName($options->db_name) . ' OWNER ' . $this->quoteName($options->db_user);
@@ -16,6 +16,22 @@
class JDatabaseMysqlTest extends TestCaseDatabaseMysql
{
/**
+ * Data for the getCreateDbQuery test.
+ *
+ * @return array
+ *
+ * @since 11.3
+ */
+ public function dataGetCreateDbQuery()
+ {
+ $obj = new stdClass;
+ $obj->db_user = 'testName';
+ $obj->db_name = 'testDb';
+
+ return array(array($obj, false), array($obj, true));
+ }
+
+ /**
* Data for the testEscape test.
*
* @return array
@@ -52,6 +68,18 @@ public function test__destruct()
}
/**
+ * Test alterDbCharacterSet with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testAlterDbCharacterSet()
+ {
+ self::$driver->alterDbCharacterSet(null);
+ }
+
+ /**
* @todo Implement testConnected().
*/
public function testConnected()
@@ -61,6 +89,18 @@ public function testConnected()
}
/**
+ * Test createDatabase with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testCreateDatabase()
+ {
+ self::$driver->createDatabase(null);
+ }
+
+ /**
* Tests the JDatabaseMysql dropTable method.
*
* @return void
@@ -108,6 +148,45 @@ public function testGetAffectedRows()
}
/**
+ * Tests the JDatabaseMysql getAlterDbCharacterSet method.
+ *
+ * @return void
+ */
+ public function testGetAlterDbCharacterSet()
+ {
+ $expected = 'ALTER DATABASE ' . self::$driver->quoteName('test') . ' CHARACTER SET ' . self::$driver->quote('utf8');
+
+ $result = TestReflection::invoke(self::$driver, 'getAlterDbCharacterSet', 'test');
+
+ $this->assertThat($result, $this->equalTo($expected), __LINE__);
+ }
+
+ /**
+ * Tests the JDatabaseMysqli getCreateDbQuery method.
+ *
+ * @param stdClass $options Object used to pass user and database name to database driver.
+ * This object must have "db_name" and "db_user" set.
+ * @param boolean $utf True if the database supports the UTF-8 character set.
+ *
+ * @return void
+ *
+ * @dataProvider dataGetCreateDbQuery
+ */
+ public function testGetCreateDatabaseQuery($options, $utf)
+ {
+ $expected = 'CREATE DATABASE ' . self::$driver->quoteName($options->db_name);
+
+ if ($utf)
+ {
+ $expected .= ' CHARACTER SET ' . self::$driver->quote('utf8');
+ }
+
+ $result = TestReflection::invoke(self::$driver, 'getCreateDatabaseQuery', $options, $utf);
+
+ $this->assertThat($result, $this->equalTo($expected), __LINE__);
+ }
+
+ /**
* @todo Implement testGetCollation().
*/
public function testGetCollation()
Oops, something went wrong.