Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 7 commits into from

5 participants

@gpongelli

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.

gpongelli added some commits
@gpongelli gpongelli 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.
0325d8f
@gpongelli gpongelli Comments correction. 5a75e70
libraries/joomla/database/driver.php
((12 lines not shown))
+ *
+ * @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();
+ }
+
+
@elinw
elinw added a note

Extra blank line here. 443 and 444 also have white space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
libraries/joomla/database/driver.php
((29 lines not shown))
+ * @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;
+ }
+
@elinw
elinw added a note

This line 574 has white space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gpongelli

Added style fixes and checks for createDatabase method.

@stefanneculai

I think it should be &$columns - there is a coding style error.

Doc comment for var $columns does not match actual variable name &$columns at position 1

@gpongelli

Fixed.

Eng. Gabriele Pongelli

libraries/joomla/database/driver.php
((7 lines not shown))
+ * @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.');
+ }
+ elseif (!isset($options->db_name) || empty($options->db_name))
@realityking Collaborator

I think this can be simplified to just the empty() check.

Changed, I'll commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
libraries/joomla/database/driver.php
((11 lines not shown))
+ * @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 (!isset($options->db_name) || empty($options->db_name))
+ {
+ throw new RuntimeException('$options object must have db_name set.');
+ }
+ elseif (!isset($options->db_user) || empty($options->db_user))
@realityking Collaborator

Same as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
libraries/joomla/database/driver.php
@@ -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 JObject $options JObject coming from CMS' "initialise" function to pass user
@realityking Collaborator

We don't actually need a JObject here. A generic object will do. Also please don't base you comment based on CMS use but be generic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gpongelli

Committed changes.

Gabriele.

gpongelli added some commits
@ianmacl

So what's the point of public function alterDbCharacterSet($dbName) ?

@gpongelli

Hi Ian,
I added that method because it's used during CMS installation, as createDatabase one, to complete installation on Postgresql.
These methods were born after a private discussion with Andrew, who point me adding these methods here.

Gabriele.

@elinw

Can you update your branch please?

@gpongelli

updated HERE, I'll close this pull.

@gpongelli gpongelli closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 7, 2012
  1. @gpongelli

    Added two public method inside JDatabaseDriver:

    gpongelli authored
    - 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.
  2. @gpongelli

    Comments correction.

    gpongelli authored
Commits on Jun 11, 2012
  1. @gpongelli

    Style fix.

    gpongelli authored
    Improved check for createDatabase.
Commits on Jun 13, 2012
  1. @gpongelli
Commits on Jun 22, 2012
  1. @gpongelli

    Fixed a comment.

    gpongelli authored
Commits on Jul 21, 2012
  1. @gpongelli

    Simplified checks.

    gpongelli authored
    Changed input parameter comment to generic object.
  2. @gpongelli
This page is out of date. Refresh to see the latest.
View
95 libraries/joomla/database/driver.php
@@ -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.
View
26 libraries/joomla/database/driver/postgresql.php
@@ -787,7 +787,7 @@ 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.
*
@@ -795,7 +795,7 @@ public function setUTF()
*
* @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,15 +1092,15 @@ 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');
@@ -1109,18 +1108,17 @@ public function getAlterDbCharacterSet( $dbName )
}
/**
- * 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);
View
79 tests/suites/database/driver/mysql/JDatabaseMySQLTest.php
@@ -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()
View
79 tests/suites/database/driver/mysqli/JDatabaseMySQLiTest.php
@@ -16,6 +16,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.
*
* @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 JDatabaseDriverMysqli dropTable method.
*
* @return void
@@ -108,6 +148,45 @@ public function testGetAffectedRows()
}
/**
+ * Tests the JDatabaseMysqli 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__);
+ }
+
+ /**
* Test getExporter method.
*
* @return void
View
149 tests/suites/database/driver/postgresql/JDatabasePostgresqlTest.php
@@ -225,6 +225,18 @@ public function test__destruct()
}
/**
+ * Test alterDbCharacterSet with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testAlterDbCharacterSet()
+ {
+ self::$driver->alterDbCharacterSet(null);
+ }
+
+ /**
* Check if connected() method returns true.
*
* @return void
@@ -235,6 +247,63 @@ public function testConnected()
}
/**
+ * Test createDatabase with null param.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testCreateDatabase()
+ {
+ self::$driver->createDatabase(null);
+ }
+
+ /**
+ * Test createDatabase with null database's name.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testCreateDatabase_nullDbName()
+ {
+ $obj = new stdClass;
+ $obj->db_user = 'test';
+
+ self::$driver->createDatabase($obj);
+ }
+
+ /**
+ * Test createDatabase with null user's name.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testCreateDatabase_nullUserName()
+ {
+ $obj = new stdClass;
+ $obj->db_name = 'test';
+
+ self::$driver->createDatabase($obj);
+ }
+
+ /**
+ * Test createDatabase with empty user's name.
+ *
+ * @return void
+ *
+ * @expectedException RuntimeException
+ */
+ public function testCreateDatabase_emptyUserName()
+ {
+ $obj = new stdClass;
+ $obj->db_name = '';
+
+ self::$driver->createDatabase($obj);
+ }
+
+ /**
* Tests the JDatabasePostgresql escape method.
*
* @param string $text The string to be escaped.
@@ -271,6 +340,45 @@ public function testGetAffectedRows()
}
/**
+ * Tests the JDatabasePostgresql 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) . ' OWNER ' . self::$driver->quoteName($options->db_user);
+
+ if ($utf)
+ {
+ $expected .= ' ENCODING ' . self::$driver->quote('UTF-8');
+ }
+
+ $result = TestReflection::invoke(self::$driver, 'getCreateDatabaseQuery', $options, $utf);
+
+ $this->assertThat($result, $this->equalTo($expected), __LINE__);
+ }
+
+ /**
+ * Tests the JDatabasePostgresql getAlterDbCharacterSet method.
+ *
+ * @return void
+ */
+ public function testGetAlterDbCharacterSet()
+ {
+ $expected = 'ALTER DATABASE ' . self::$driver->quoteName('test') . ' SET CLIENT_ENCODING TO ' . self::$driver->quote('UTF8');
+
+ $result = TestReflection::invoke(self::$driver, 'getAlterDbCharacterSet', 'test');
+
+ $this->assertThat($result, $this->equalTo($expected), __LINE__);
+ }
+
+ /**
* Tests the JDatabasePostgresql getCollation method.
*
* @return void
@@ -1071,7 +1179,7 @@ public function testSqlValue()
foreach (get_object_vars($tst) as $key => $val)
{
- $values[] = self::$driver->sqlValue($tablCol, $key, $val);
+ $values[] = TestReflection::invoke(self::$driver, 'sqlValue', $tablCol, $key, $val);
}
$this->assertThat(
@@ -1346,43 +1454,4 @@ public function testTransactionSavepoint( /*$savepointName*/ )
{
$this->markTestSkipped('This command is tested inside testTransactionRollback.');
}
-
- /**
- * Tests the JDatabasePostgresql 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 testGetCreateDbQuery($options, $utf)
- {
- $expected = 'CREATE DATABASE ' . self::$driver->quoteName($options->db_name) . ' OWNER ' . self::$driver->quoteName($options->db_user);
-
- if ($utf)
- {
- $expected .= ' ENCODING ' . self::$driver->quote('UTF-8');
- }
-
- $result = self::$driver->getCreateDbQuery($options, $utf);
-
- $this->assertThat($result, $this->equalTo($expected), __LINE__);
- }
-
- /**
- * Tests the JDatabasePostgresql getAlterDbCharacterSet method.
- *
- * @return void
- */
- public function testGetAlterDbCharacterSet()
- {
- $expected = 'ALTER DATABASE ' . self::$driver->quoteName('test') . ' SET CLIENT_ENCODING TO ' . self::$driver->quote('UTF8');
-
- $result = self::$driver->getAlterDbCharacterSet('test');
-
- $this->assertThat($result, $this->equalTo($expected), __LINE__);
- }
}
Something went wrong with that request. Please try again.