Completed
Push — master ( ffbb02...68e5b2 )
by smiley
02:42
created

CreateDatabase::sql()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 8.7624
cc 5
eloc 12
nc 9
nop 0
1
<?php
2
/**
3
 * Class CreateDatabase
4
 *
5
 * @filesource   CreateDatabase.php
6
 * @created      12.06.2017
7
 * @package      chillerlan\Database\Query\Dialects\MySQL
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query\Dialects\MySQL;
14
15
use chillerlan\Database\Query\{CreateDatabaseAbstract, QueryException};
16
17
class CreateDatabase extends CreateDatabaseAbstract{
18
19
	protected $collate = 'utf8mb4_bin';
20
21
	public function sql():string{
22
23
		if(empty($this->name)){
24
			throw new QueryException('no name specified');
25
		}
26
27
		list($charset) = explode('_', $this->collate);
28
29
		$collate = 'CHARACTER SET '.$charset;
30
31
		if($charset !== $this->collate){
32
			$collate .= ' COLLATE '.$this->collate;
33
		}
34
35
		$sql = 'CREATE DATABASE ';
36
		$sql .= $this->ifNotExists ? 'IF NOT EXISTS ' : '';
37
		$sql .= $this->quote($this->name);
38
		$sql .= $this->collate ? ' '.$collate : '';
39
40
		return $sql;
41
	}
42
43
}
44