Permalink
Browse files

Added two public method inside JDatabaseDriver:

- alterDbCharacterSet to change database's character set
- createDatabase to create a new database

AlterDbCharacterSet needs database name as parameter, createDatabase
needs a JObject, similar to that used inside CMS' "initialise" function,
and a bool to set UTF8 database's support.

They use two protected method to obtain correct query in database's
supported syntax:
- getAlterDbCharacterSet
- getCreateDatabaseQuery

Protected method inside JDatabaseDriver return query for MySQL and
MySQLi syntax, this commit contain also overridden version for
PostgreSQL database.

There are tests only for protected methods inside MySQL, MySQLi and
PostgreSQL test classes.
  • Loading branch information...
gpongelli committed Jun 7, 2012
1 parent 49a4e5b commit 0325d8f6d607a66920e6509affd3d4d102c841bb
@@ -377,6 +377,27 @@ public function __construct($options)
$this->options = $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.
*
@@ -396,6 +417,31 @@ public function __construct($options)
*/
abstract public function connected();
+ /**
+ * Create a new database using information from $options object, obtaining query string
+ * from protected member.
+ *
+ * @param JObject $options JObject coming from CMS' "initialise" function to pass user
+ * and database name to database driver.
+ * @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.');
+ }
+
+ $this->setQuery($this->getCreateDatabaseQuery($options, $utf));
+ return $this->execute();
+ }
+
+
/**
* Disconnects the database.
*
@@ -484,6 +530,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 JObject $options JObject coming from CMS' "initialise" function to pass user
+ * and database name to database driver.
+ * @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.
*
@@ -1093,34 +1093,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 JObject $options JObject coming from CMS' "initialise" function to pass user
+ * and database name to database driver.
+ * @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);
@@ -15,6 +15,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.
*
@@ -51,6 +67,18 @@ public function test__destruct()
$this->markTestIncomplete('This test has not been implemented yet.');
}
+ /**
+ * Test alterDbCharacterSet with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testAlterDbCharacterSet()
+ {
+ self::$driver->alterDbCharacterSet(null);
+ }
+
/**
* @todo Implement testConnected().
*/
@@ -60,6 +88,18 @@ public function testConnected()
$this->markTestIncomplete('This test has not been implemented yet.');
}
+ /**
+ * Test createDatabase with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testCreateDatabase()
+ {
+ self::$driver->createDatabase(null);
+ }
+
/**
* Tests the JDatabaseMysql dropTable method.
*
@@ -107,6 +147,45 @@ public function testGetAffectedRows()
$this->assertThat(self::$driver->getAffectedRows(), $this->equalTo(4), __LINE__);
}
+ /**
+ * Tests the JDatabasePostgresql 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 JObject $options JObject 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.
+ *
+ * @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().
*/
@@ -15,6 +15,22 @@
*/
class JDatabaseMysqliTest extends TestCaseDatabaseMysqli
{
+ /**
+ * 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.
*
@@ -51,6 +67,18 @@ public function test__destruct()
$this->markTestIncomplete('This test has not been implemented yet.');
}
+ /**
+ * Test alterDbCharacterSet with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testAlterDbCharacterSet()
+ {
+ self::$driver->alterDbCharacterSet(null);
+ }
+
/**
* @todo Implement testConnected().
*/
@@ -60,6 +88,18 @@ public function testConnected()
$this->markTestIncomplete('This test has not been implemented yet.');
}
+ /**
+ * Test createDatabase with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testCreateDatabase()
+ {
+ self::$driver->createDatabase(null);
+ }
+
/**
* Tests the JDatabaseDriverMysqli dropTable method.
*
@@ -107,6 +147,45 @@ public function testGetAffectedRows()
$this->assertThat(self::$driver->getAffectedRows(), $this->equalTo(4), __LINE__);
}
+ /**
+ * Tests the JDatabasePostgresql 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 JObject $options JObject 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.
+ *
+ * @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__);
+ }
+
/**
* Test getExporter method.
*
Oops, something went wrong.

0 comments on commit 0325d8f

Please sign in to comment.