Skip to content
This repository

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

Closed
wants to merge 7 commits into from

5 participants

gpongelli ianmacl elinw Rouven Weßling Stefan Neculai
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.

added some commits June 07, 2012
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 Comments correction. 5a75e70
libraries/joomla/database/driver.php
((12 lines not shown))
  429
+	 *
  430
+	 * @since   12.2
  431
+	 * @throws  RuntimeException
  432
+	 */
  433
+	public function createDatabase($options, $utf = true)
  434
+	{
  435
+		if (is_null($options))
  436
+		{
  437
+			throw new RuntimeException('$options object must not be null.');
  438
+		}
  439
+
  440
+		$this->setQuery($this->getCreateDatabaseQuery($options, $utf));
  441
+		return $this->execute();
  442
+	}
  443
+	
  444
+	
1
elinw
elinw added a note June 10, 2012

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))
  559
+	 * @since   12.2
  560
+	 */
  561
+	protected function getCreateDatabaseQuery($options, $utf)
  562
+	{
  563
+		if ($utf)
  564
+		{
  565
+			$query = 'CREATE DATABASE ' . $this->quoteName($options->db_name) . ' CHARACTER SET `utf8`';
  566
+		}
  567
+		else
  568
+		{
  569
+			$query = 'CREATE DATABASE ' . $this->quoteName($options->db_name);
  570
+		}
  571
+
  572
+		return $query;
  573
+	}
  574
+	
1
elinw
elinw added a note June 10, 2012

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.

Stefan Neculai

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))
  424
+	 * @param   JObject  $options  JObject coming from CMS' "initialise" function to pass user
  425
+	 *								and database name to database driver.
  426
+	 * @param   boolean  $utf      True if the database supports the UTF-8 character set.
  427
+	 *
  428
+	 * @return  string  The query that creates database
  429
+	 *
  430
+	 * @since   12.2
  431
+	 * @throws  RuntimeException
  432
+	 */
  433
+	public function createDatabase($options, $utf = true)
  434
+	{
  435
+		if (is_null($options))
  436
+		{
  437
+			throw new RuntimeException('$options object must not be null.');
  438
+		}
  439
+		elseif (!isset($options->db_name) || empty($options->db_name))
2
Rouven Weßling Collaborator
realityking added a note July 18, 2012

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

gpongelli
gpongelli added a note July 19, 2012

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))
  428
+	 * @return  string  The query that creates database
  429
+	 *
  430
+	 * @since   12.2
  431
+	 * @throws  RuntimeException
  432
+	 */
  433
+	public function createDatabase($options, $utf = true)
  434
+	{
  435
+		if (is_null($options))
  436
+		{
  437
+			throw new RuntimeException('$options object must not be null.');
  438
+		}
  439
+		elseif (!isset($options->db_name) || empty($options->db_name))
  440
+		{
  441
+			throw new RuntimeException('$options object must have db_name set.');
  442
+		}
  443
+		elseif (!isset($options->db_user) || empty($options->db_user))
1
Rouven Weßling Collaborator
realityking added a note July 18, 2012

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)
397 418
 	abstract public function connected();
398 419
 
399 420
 	/**
  421
+	 * Create a new database using information from $options object, obtaining query string
  422
+	 * from protected member.
  423
+	 *
  424
+	 * @param   JObject  $options  JObject coming from CMS' "initialise" function to pass user
1
Rouven Weßling Collaborator
realityking added a note July 18, 2012

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.

added some commits July 21, 2012
ianmacl
Owner

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 October 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 7 unique commits by 1 author.

Jun 07, 2012
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 Comments correction. 5a75e70
Jun 11, 2012
gpongelli Style fix.
Improved check for createDatabase.
60352cc
Jun 13, 2012
gpongelli Improved sqlValue and changed its test. 595906e
Jun 22, 2012
gpongelli Fixed a comment. f37c04e
Jul 21, 2012
gpongelli Simplified checks.
Changed input parameter comment to generic object.
a24d16d
gpongelli Changed JObject with generic object. d0e9b5b
This page is out of date. Refresh to see the latest.
95  libraries/joomla/database/driver.php
@@ -378,6 +378,27 @@ public function __construct($options)
378 378
 	}
379 379
 
380 380
 	/**
  381
+	 * Alter database's character set, obtaining query string from protected member.
  382
+	 *
  383
+	 * @param   string  $dbName  The database name that will be altered
  384
+	 *
  385
+	 * @return  string  The query that alter the database query string
  386
+	 *
  387
+	 * @since   12.2
  388
+	 * @throws  RuntimeException
  389
+	 */
  390
+	public function alterDbCharacterSet($dbName)
  391
+	{
  392
+		if (is_null($dbName))
  393
+		{
  394
+			throw new RuntimeException('Database name must not be null.');
  395
+		}
  396
+
  397
+		$this->setQuery($this->getAlterDbCharacterSet($dbName));
  398
+		return $this->execute();
  399
+	}
  400
+
  401
+	/**
381 402
 	 * Connects to the database if needed.
382 403
 	 *
383 404
 	 * @return  void  Returns void if the database connected successfully.
@@ -397,6 +418,38 @@ public function __construct($options)
397 418
 	abstract public function connected();
398 419
 
399 420
 	/**
  421
+	 * Create a new database using information from $options object, obtaining query string
  422
+	 * from protected member.
  423
+	 *
  424
+	 * @param   stdClass  $options  Object used to pass user and database name to database driver.
  425
+	 * 									This object must have "db_name" and "db_user" set.
  426
+	 * @param   boolean   $utf      True if the database supports the UTF-8 character set.
  427
+	 *
  428
+	 * @return  string  The query that creates database
  429
+	 *
  430
+	 * @since   12.2
  431
+	 * @throws  RuntimeException
  432
+	 */
  433
+	public function createDatabase($options, $utf = true)
  434
+	{
  435
+		if (is_null($options))
  436
+		{
  437
+			throw new RuntimeException('$options object must not be null.');
  438
+		}
  439
+		elseif (empty($options->db_name))
  440
+		{
  441
+			throw new RuntimeException('$options object must have db_name set.');
  442
+		}
  443
+		elseif (empty($options->db_user))
  444
+		{
  445
+			throw new RuntimeException('$options object must have db_user set.');
  446
+		}
  447
+
  448
+		$this->setQuery($this->getCreateDatabaseQuery($options, $utf));
  449
+		return $this->execute();
  450
+	}
  451
+
  452
+	/**
400 453
 	 * Disconnects the database.
401 454
 	 *
402 455
 	 * @return  void
@@ -485,6 +538,48 @@ public function __construct($options)
485 538
 	abstract public function getAffectedRows();
486 539
 
487 540
 	/**
  541
+	 * Return the query string to alter the database character set.
  542
+	 *
  543
+	 * @param   string  $dbName  The database name
  544
+	 *
  545
+	 * @return  string  The query that alter the database query string
  546
+	 *
  547
+	 * @since   12.2
  548
+	 */
  549
+	protected function getAlterDbCharacterSet($dbName)
  550
+	{
  551
+		$query = 'ALTER DATABASE ' . $this->quoteName($dbName) . ' CHARACTER SET `utf8`';
  552
+
  553
+		return $query;
  554
+	}
  555
+
  556
+	/**
  557
+	 * Return the query string to create new Database.
  558
+	 * Each database driver, other than MySQL, need to override this member to return correct string.
  559
+	 *
  560
+	 * @param   stdClass  $options  Object used to pass user and database name to database driver.
  561
+	 * 									This object must have "db_name" and "db_user" set.
  562
+	 * @param   boolean   $utf      True if the database supports the UTF-8 character set.
  563
+	 *
  564
+	 * @return  string  The query that creates database
  565
+	 *
  566
+	 * @since   12.2
  567
+	 */
  568
+	protected function getCreateDatabaseQuery($options, $utf)
  569
+	{
  570
+		if ($utf)
  571
+		{
  572
+			$query = 'CREATE DATABASE ' . $this->quoteName($options->db_name) . ' CHARACTER SET `utf8`';
  573
+		}
  574
+		else
  575
+		{
  576
+			$query = 'CREATE DATABASE ' . $this->quoteName($options->db_name);
  577
+		}
  578
+
  579
+		return $query;
  580
+	}
  581
+
  582
+	/**
488 583
 	 * Method to get the database collation in use by sampling a text field of a table in the database.
489 584
 	 *
490 585
 	 * @return  mixed  The collation in use by the database or boolean false if not supported.
26  libraries/joomla/database/driver/postgresql.php
@@ -787,7 +787,7 @@ public function setUTF()
787 787
 	/**
788 788
 	 * This function return a field value as a prepared string to be used in a SQL statement.
789 789
 	 *
790  
-	 * @param   array   $columns      Array of table's column returned by ::getTableColumns.
  790
+	 * @param   array   &$columns     Array of table's column returned by JDatabasePostgreSQL::getTableColumns.
791 791
 	 * @param   string  $field_name   The table field's name.
792 792
 	 * @param   string  $field_value  The variable value to quote and return.
793 793
 	 * 
@@ -795,7 +795,7 @@ public function setUTF()
795 795
 	 *
796 796
 	 * @since   11.3
797 797
 	 */
798  
-	public function sqlValue($columns, $field_name, $field_value)
  798
+	protected function sqlValue(&$columns, $field_name, $field_value)
799 799
 	{
800 800
 		switch ($columns[$field_name])
801 801
 		{
@@ -814,7 +814,6 @@ public function sqlValue($columns, $field_name, $field_value)
814 814
 			case 'bigserial':
815 815
 			case 'integer':
816 816
 			case 'money':
817  
-			case 'numeric':
818 817
 			case 'real':
819 818
 			case 'smallint':
820 819
 			case 'serial':
@@ -1093,15 +1092,15 @@ public function getRandom()
1093 1092
 	}
1094 1093
 
1095 1094
 	/**
1096  
-	 * Get the query string to alter the database character set.
  1095
+	 * Return the query string to alter the database character set.
1097 1096
 	 *
1098 1097
 	 * @param   string  $dbName  The database name
1099 1098
 	 *
1100 1099
 	 * @return  string  The query that alter the database query string
1101 1100
 	 *
1102  
-	 * @since   12.1
  1101
+	 * @since   12.2
1103 1102
 	 */
1104  
-	public function getAlterDbCharacterSet( $dbName )
  1103
+	protected function getAlterDbCharacterSet($dbName)
1105 1104
 	{
1106 1105
 		$query = 'ALTER DATABASE ' . $this->quoteName($dbName) . ' SET CLIENT_ENCODING TO ' . $this->quote('UTF8');
1107 1106
 
@@ -1109,18 +1108,17 @@ public function getAlterDbCharacterSet( $dbName )
1109 1108
 	}
1110 1109
 
1111 1110
 	/**
1112  
-	 * Get the query string to create new Database in correct PostgreSQL syntax.
  1111
+	 * Return the query string to create new Database using PostgreSQL's syntax
1113 1112
 	 *
1114  
-	 * @param   object   $options  object coming from "initialise" function to pass user
1115  
-	 * 									and database name to database driver.
1116  
-	 * @param   boolean  $utf      True if the database supports the UTF-8 character set,
1117  
-	 * 									not used in PostgreSQL "CREATE DATABASE" query.
  1113
+	 * @param   stdClass  $options  Object used to pass user and database name to database driver.
  1114
+	 * 									This object must have "db_name" and "db_user" set.
  1115
+	 * @param   boolean   $utf      True if the database supports the UTF-8 character set.
1118 1116
 	 *
1119  
-	 * @return  string	The query that creates database, owned by $options['user']
  1117
+	 * @return  string  The query that creates database, owned by $options['user']
1120 1118
 	 *
1121  
-	 * @since   12.1
  1119
+	 * @since   12.2
1122 1120
 	 */
1123  
-	public function getCreateDbQuery($options, $utf)
  1121
+	protected function getCreateDatabaseQuery($options, $utf)
1124 1122
 	{
1125 1123
 		$query = 'CREATE DATABASE ' . $this->quoteName($options->db_name) . ' OWNER ' . $this->quoteName($options->db_user);
1126 1124
 
79  tests/suites/database/driver/mysql/JDatabaseMySQLTest.php
@@ -16,6 +16,22 @@
16 16
 class JDatabaseMysqlTest extends TestCaseDatabaseMysql
17 17
 {
18 18
 	/**
  19
+	 * Data for the getCreateDbQuery test.
  20
+	 *
  21
+	 * @return  array
  22
+	 *
  23
+	 * @since   11.3
  24
+	 */
  25
+	public function dataGetCreateDbQuery()
  26
+	{
  27
+		$obj = new stdClass;
  28
+		$obj->db_user = 'testName';
  29
+		$obj->db_name = 'testDb';
  30
+
  31
+		return array(array($obj, false), array($obj, true));
  32
+	}
  33
+
  34
+	/**
19 35
 	 * Data for the testEscape test.
20 36
 	 *
21 37
 	 * @return  array
@@ -52,6 +68,18 @@ public function test__destruct()
52 68
 	}
53 69
 
54 70
 	/**
  71
+	 * Test alterDbCharacterSet with null param.
  72
+	 *
  73
+	 * @return   void
  74
+	 *
  75
+	 * @expectedException RuntimeException
  76
+	 */
  77
+	public function testAlterDbCharacterSet()
  78
+	{
  79
+		self::$driver->alterDbCharacterSet(null);
  80
+	}
  81
+
  82
+	/**
55 83
 	 * @todo Implement testConnected().
56 84
 	 */
57 85
 	public function testConnected()
@@ -61,6 +89,18 @@ public function testConnected()
61 89
 	}
62 90
 
63 91
 	/**
  92
+	 * Test createDatabase with null param.
  93
+	 *
  94
+	 * @return   void
  95
+	 *
  96
+	 * @expectedException RuntimeException
  97
+	 */
  98
+	public function testCreateDatabase()
  99
+	{
  100
+		self::$driver->createDatabase(null);
  101
+	}
  102
+
  103
+	/**
64 104
 	 * Tests the JDatabaseMysql dropTable method.
65 105
 	 *
66 106
 	 * @return  void
@@ -108,6 +148,45 @@ public function testGetAffectedRows()
108 148
 	}
109 149
 
110 150
 	/**
  151
+	 * Tests the JDatabaseMysql getAlterDbCharacterSet method.
  152
+	 *
  153
+	 * @return  void
  154
+	 */
  155
+	public function testGetAlterDbCharacterSet()
  156
+	{
  157
+		$expected = 'ALTER DATABASE ' . self::$driver->quoteName('test') . ' CHARACTER SET ' . self::$driver->quote('utf8');
  158
+
  159
+		$result = TestReflection::invoke(self::$driver, 'getAlterDbCharacterSet', 'test');
  160
+
  161
+		$this->assertThat($result, $this->equalTo($expected), __LINE__);
  162
+	}
  163
+
  164
+	/**
  165
+	 * Tests the JDatabaseMysqli getCreateDbQuery method.
  166
+	 *
  167
+	 * @param   stdClass  $options  Object used to pass user and database name to database driver.
  168
+	 * 									This object must have "db_name" and "db_user" set.
  169
+	 * @param   boolean   $utf      True if the database supports the UTF-8 character set.
  170
+	 *
  171
+	 * @return  void
  172
+	 *
  173
+	 * @dataProvider dataGetCreateDbQuery
  174
+	 */
  175
+	public function testGetCreateDatabaseQuery($options, $utf)
  176
+	{
  177
+		$expected = 'CREATE DATABASE ' . self::$driver->quoteName($options->db_name);
  178
+
  179
+		if ($utf)
  180
+		{
  181
+			$expected .= ' CHARACTER SET ' . self::$driver->quote('utf8');
  182
+		}
  183
+
  184
+		$result = TestReflection::invoke(self::$driver, 'getCreateDatabaseQuery', $options, $utf);
  185
+
  186
+		$this->assertThat($result, $this->equalTo($expected), __LINE__);
  187
+	}
  188
+
  189
+	/**
111 190
 	 * @todo Implement testGetCollation().
112 191
 	 */
113 192
 	public function testGetCollation()
79  tests/suites/database/driver/mysqli/JDatabaseMySQLiTest.php
@@ -16,6 +16,22 @@
16 16
 class JDatabaseMysqliTest extends TestCaseDatabaseMysqli
17 17
 {
18 18
 	/**
  19
+	 * Data for the getCreateDbQuery test.
  20
+	 *
  21
+	 * @return  array
  22
+	 *
  23
+	 * @since   11.3
  24
+	 */
  25
+	public function dataGetCreateDbQuery()
  26
+	{
  27
+		$obj = new stdClass;
  28
+		$obj->db_user = 'testName';
  29
+		$obj->db_name = 'testDb';
  30
+
  31
+		return array(array($obj, false), array($obj, true));
  32
+	}
  33
+
  34
+	/**
19 35
 	 * Data for the testEscape test.
20 36
 	 *
21 37
 	 * @return  array
@@ -52,6 +68,18 @@ public function test__destruct()
52 68
 	}
53 69
 
54 70
 	/**
  71
+	 * Test alterDbCharacterSet with null param.
  72
+	 *
  73
+	 * @return   void
  74
+	 *
  75
+	 * @expectedException RuntimeException
  76
+	 */
  77
+	public function testAlterDbCharacterSet()
  78
+	{
  79
+		self::$driver->alterDbCharacterSet(null);
  80
+	}
  81
+
  82
+	/**
55 83
 	 * @todo Implement testConnected().
56 84
 	 */
57 85
 	public function testConnected()
@@ -61,6 +89,18 @@ public function testConnected()
61 89
 	}
62 90
 
63 91
 	/**
  92
+	 * Test createDatabase with null param.
  93
+	 *
  94
+	 * @return   void
  95
+	 *
  96
+	 * @expectedException RuntimeException
  97
+	 */
  98
+	public function testCreateDatabase()
  99
+	{
  100
+		self::$driver->createDatabase(null);
  101
+	}
  102
+
  103
+	/**
64 104
 	 * Tests the JDatabaseDriverMysqli dropTable method.
65 105
 	 *
66 106
 	 * @return  void
@@ -108,6 +148,45 @@ public function testGetAffectedRows()
108 148
 	}
109 149
 
110 150
 	/**
  151
+	 * Tests the JDatabaseMysqli getAlterDbCharacterSet method.
  152
+	 *
  153
+	 * @return  void
  154
+	 */
  155
+	public function testGetAlterDbCharacterSet()
  156
+	{
  157
+		$expected = 'ALTER DATABASE ' . self::$driver->quoteName('test') . ' CHARACTER SET ' . self::$driver->quote('utf8');
  158
+
  159
+		$result = TestReflection::invoke(self::$driver, 'getAlterDbCharacterSet', 'test');
  160
+
  161
+		$this->assertThat($result, $this->equalTo($expected), __LINE__);
  162
+	}
  163
+
  164
+	/**
  165
+	 * Tests the JDatabaseMysqli getCreateDbQuery method.
  166
+	 *
  167
+	 * @param   stdClass  $options  Object used to pass user and database name to database driver.
  168
+	 * 									This object must have "db_name" and "db_user" set.
  169
+	 * @param   boolean   $utf      True if the database supports the UTF-8 character set.
  170
+	 *
  171
+	 * @return  void
  172
+	 *
  173
+	 * @dataProvider dataGetCreateDbQuery
  174
+	 */
  175
+	public function testGetCreateDatabaseQuery($options, $utf)
  176
+	{
  177
+		$expected = 'CREATE DATABASE ' . self::$driver->quoteName($options->db_name);
  178
+
  179
+		if ($utf)
  180
+		{
  181
+			$expected .= ' CHARACTER SET ' . self::$driver->quote('utf8');
  182
+		}
  183
+
  184
+		$result = TestReflection::invoke(self::$driver, 'getCreateDatabaseQuery', $options, $utf);
  185
+
  186
+		$this->assertThat($result, $this->equalTo($expected), __LINE__);
  187
+	}
  188
+
  189
+	/**
111 190
 	 * Test getExporter method.
112 191
 	 *
113 192
 	 * @return  void
149  tests/suites/database/driver/postgresql/JDatabasePostgresqlTest.php
@@ -225,6 +225,18 @@ public function test__destruct()
225 225
 	}
226 226
 
227 227
 	/**
  228
+	 * Test alterDbCharacterSet with null param.
  229
+	 *
  230
+	 * @return   void
  231
+	 *
  232
+	 * @expectedException RuntimeException
  233
+	 */
  234
+	public function testAlterDbCharacterSet()
  235
+	{
  236
+		self::$driver->alterDbCharacterSet(null);
  237
+	}
  238
+
  239
+	/**
228 240
 	 * Check if connected() method returns true.
229 241
 	 *
230 242
 	 * @return   void
@@ -235,6 +247,63 @@ public function testConnected()
235 247
 	}
236 248
 
237 249
 	/**
  250
+	 * Test createDatabase with null param.
  251
+	 *
  252
+	 * @return   void
  253
+	 *
  254
+	 * @expectedException RuntimeException
  255
+	 */
  256
+	public function testCreateDatabase()
  257
+	{
  258
+		self::$driver->createDatabase(null);
  259
+	}
  260
+
  261
+	/**
  262
+	 * Test createDatabase with null database's name.
  263
+	 *
  264
+	 * @return   void
  265
+	 *
  266
+	 * @expectedException RuntimeException
  267
+	 */
  268
+	public function testCreateDatabase_nullDbName()
  269
+	{
  270
+		$obj = new stdClass;
  271
+		$obj->db_user = 'test';
  272
+
  273
+		self::$driver->createDatabase($obj);
  274
+	}
  275
+
  276
+	/**
  277
+	 * Test createDatabase with null user's name.
  278
+	 *
  279
+	 * @return   void
  280
+	 *
  281
+	 * @expectedException RuntimeException
  282
+	 */
  283
+	public function testCreateDatabase_nullUserName()
  284
+	{
  285
+		$obj = new stdClass;
  286
+		$obj->db_name = 'test';
  287
+
  288
+		self::$driver->createDatabase($obj);
  289
+	}
  290
+
  291
+	/**
  292
+	 * Test createDatabase with empty user's name.
  293
+	 *
  294
+	 * @return   void
  295
+	 *
  296
+	 * @expectedException RuntimeException
  297
+	 */
  298
+	public function testCreateDatabase_emptyUserName()
  299
+	{
  300
+		$obj = new stdClass;
  301
+		$obj->db_name = '';
  302
+
  303
+		self::$driver->createDatabase($obj);
  304
+	}
  305
+
  306
+	/**
238 307
 	 * Tests the JDatabasePostgresql escape method.
239 308
 	 *
240 309
 	 * @param   string  $text    The string to be escaped.
@@ -271,6 +340,45 @@ public function testGetAffectedRows()
271 340
 	}
272 341
 
273 342
 	/**
  343
+	 * Tests the JDatabasePostgresql getCreateDbQuery method.
  344
+	 *
  345
+	 * @param   stdClass  $options  Object used to pass user and database name to database driver.
  346
+	 * 									This object must have "db_name" and "db_user" set.
  347
+	 * @param   boolean   $utf      True if the database supports the UTF-8 character set.
  348
+	 *
  349
+	 * @return  void
  350
+	 *
  351
+	 * @dataProvider dataGetCreateDbQuery
  352
+	 */
  353
+	public function testGetCreateDatabaseQuery($options, $utf)
  354
+	{
  355
+		$expected = 'CREATE DATABASE ' . self::$driver->quoteName($options->db_name) . ' OWNER ' . self::$driver->quoteName($options->db_user);
  356
+
  357
+		if ($utf)
  358
+		{
  359
+			$expected .= ' ENCODING ' . self::$driver->quote('UTF-8');
  360
+		}
  361
+
  362
+		$result = TestReflection::invoke(self::$driver, 'getCreateDatabaseQuery', $options, $utf);
  363
+
  364
+		$this->assertThat($result, $this->equalTo($expected), __LINE__);
  365
+	}
  366
+
  367
+	/**
  368
+	 * Tests the JDatabasePostgresql getAlterDbCharacterSet method.
  369
+	 *
  370
+	 * @return  void
  371
+	 */
  372
+	public function testGetAlterDbCharacterSet()
  373
+	{
  374
+		$expected = 'ALTER DATABASE ' . self::$driver->quoteName('test') . ' SET CLIENT_ENCODING TO ' . self::$driver->quote('UTF8');
  375
+
  376
+		$result = TestReflection::invoke(self::$driver, 'getAlterDbCharacterSet', 'test');
  377
+
  378
+		$this->assertThat($result, $this->equalTo($expected), __LINE__);
  379
+	}
  380
+
  381
+	/**
274 382
 	 * Tests the JDatabasePostgresql getCollation method.
275 383
 	 *
276 384
 	 * @return  void
@@ -1071,7 +1179,7 @@ public function testSqlValue()
1071 1179
 
1072 1180
 		foreach (get_object_vars($tst) as $key => $val)
1073 1181
 		{
1074  
-			$values[] = self::$driver->sqlValue($tablCol, $key, $val);
  1182
+			$values[] = TestReflection::invoke(self::$driver, 'sqlValue', $tablCol, $key, $val);
1075 1183
 		}
1076 1184
 
1077 1185
 		$this->assertThat(
@@ -1346,43 +1454,4 @@ public function testTransactionSavepoint( /*$savepointName*/ )
1346 1454
 	{
1347 1455
 		$this->markTestSkipped('This command is tested inside testTransactionRollback.');
1348 1456
 	}
1349  
-
1350  
-	/**
1351  
-	 * Tests the JDatabasePostgresql getCreateDbQuery method.
1352  
-	 *
1353  
-	 * @param   JObject  $options  JObject coming from "initialise" function to pass user
1354  
-	 * 									and database name to database driver.
1355  
-	 * @param   boolean  $utf      True if the database supports the UTF-8 character set.
1356  
-	 *
1357  
-	 * @return  void
1358  
-	 *
1359  
-	 * @dataProvider dataGetCreateDbQuery
1360  
-	 */
1361  
-	public function testGetCreateDbQuery($options, $utf)
1362  
-	{
1363  
-		$expected = 'CREATE DATABASE ' . self::$driver->quoteName($options->db_name) . ' OWNER ' . self::$driver->quoteName($options->db_user);
1364  
-
1365  
-		if ($utf)
1366  
-		{
1367  
-			$expected .= ' ENCODING ' . self::$driver->quote('UTF-8');
1368  
-		}
1369  
-
1370  
-		$result = self::$driver->getCreateDbQuery($options, $utf);
1371  
-
1372  
-		$this->assertThat($result, $this->equalTo($expected), __LINE__);
1373  
-	}
1374  
-
1375  
-	/**
1376  
-	 * Tests the JDatabasePostgresql getAlterDbCharacterSet method.
1377  
-	 *
1378  
-	 * @return  void
1379  
-	 */
1380  
-	public function testGetAlterDbCharacterSet()
1381  
-	{
1382  
-		$expected = 'ALTER DATABASE ' . self::$driver->quoteName('test') . ' SET CLIENT_ENCODING TO ' . self::$driver->quote('UTF8');
1383  
-
1384  
-		$result = self::$driver->getAlterDbCharacterSet('test');
1385  
-
1386  
-		$this->assertThat($result, $this->equalTo($expected), __LINE__);
1387  
-	}
1388 1457
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.