Skip to content
This repository was archived by the owner on Nov 26, 2022. It is now read-only.

Commit 1153512

Browse files
committed
Added createDatabase and useDatabase
1 parent 64f53af commit 1153512

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/Database/Schema.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,37 @@ public function getDatabases($like = null)
7777
return $this->db->queryValues($sql, 'Database');
7878
}
7979

80+
/**
81+
* Create a database.
82+
*
83+
* @param string $dbName The database name
84+
* @param string $characterSet
85+
* @param string $collate
86+
* @return bool Success
87+
*/
88+
public function createDatabase(string $dbName, $characterSet = 'utf8', $collate = 'utf8_unicode_ci'): bool
89+
{
90+
$sql = "CREATE DATABASE %s CHARACTER SET %s COLLATE %s;";
91+
$sql = vsprintf($sql, [
92+
$this->quoter->quoteName($dbName),
93+
$this->quoter->quoteValue($characterSet),
94+
$this->quoter->quoteValue($collate)
95+
]);
96+
return $this->db->exec($sql);
97+
}
98+
99+
/**
100+
* Create a database.
101+
*
102+
* @param string $dbName The database name
103+
* @return bool Success
104+
*/
105+
public function useDatabase(string $dbName): bool
106+
{
107+
$sql = sprintf("USE %s;", $this->quoter->quoteName($dbName));
108+
return $this->db->exec($sql);
109+
}
110+
80111
/**
81112
* Return all Tables from Database
82113
*

tests/BaseTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Odan\Database\Connection;
66
use Odan\Database\Schema;
77
use Odan\Database\SelectQuery;
8-
use PHPUnit\Framework\TestCase;
98
use PDO;
9+
use PHPUnit\Framework\TestCase;
1010

1111
/**
1212
* ConnectionTest
@@ -40,7 +40,7 @@ protected function getConnection()
4040
$password = '';
4141
$charset = 'utf8';
4242
$collate = 'utf8_unicode_ci';
43-
$this->connection = new Connection("mysql:host=$host;dbname=$dbname;charset=$charset", $username, $password, array(
43+
$this->connection = new Connection("mysql:host=$host;charset=$charset", $username, $password, array(
4444
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
4545
PDO::ATTR_PERSISTENT => false,
4646
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
@@ -70,15 +70,22 @@ protected function createTestTable()
7070
{
7171
$db = $this->getConnection();
7272
$schema = $this->getSchema();
73+
74+
if (!$schema->existDatabase('database_test')) {
75+
$schema->createDatabase('database_test');
76+
}
77+
78+
$schema->useDatabase('database_test');
79+
7380
foreach ($schema->getTables() as $table) {
7481
$schema->dropTable($table);
7582
}
7683

7784
$result = $db->exec("CREATE TABLE `test` (
78-
`id` int(11) NOT NULL AUTO_INCREMENT,
79-
`keyname` varchar(255) COLLATE utf8_unicode_ci,
80-
`keyvalue` varchar(255) COLLATE utf8_unicode_ci,
81-
`boolvalue` tinyint(1) NOT NULL DEFAULT 0,
85+
`id` INT(11) NOT NULL AUTO_INCREMENT,
86+
`keyname` VARCHAR(255) COLLATE utf8_unicode_ci,
87+
`keyvalue` VARCHAR(255) COLLATE utf8_unicode_ci,
88+
`boolvalue` TINYINT(1) NOT NULL DEFAULT 0,
8289
`created` DATETIME DEFAULT NULL,
8390
`created_user_id` INT(11) DEFAULT NULL,
8491
`updated` DATETIME DEFAULT NULL,

0 commit comments

Comments
 (0)