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

CreateDatabase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 34
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B sql() 0 30 6
1
<?php
2
/**
3
 * Class CreateDatabase
4
 *
5
 * @filesource   CreateDatabase.php
6
 * @created      12.06.2017
7
 * @package      chillerlan\Database\Query\Dialects\Postgres
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query\Dialects\Postgres;
14
15
use chillerlan\Database\Query\{CreateDatabaseAbstract, QueryException};
16
17
class CreateDatabase extends CreateDatabaseAbstract{
18
19
	public function sql():string{
20
21
		if(empty($this->name)){
22
			throw new QueryException('no name specified');
23
		}
24
25
		$sql = 'CREATE DATABASE ';
26
		$sql .= $this->quote($this->name);
27
28
		if($this->collate){
29
			$charset = explode(',', $this->collate, 3);
30
31
			$count = count($charset);
32
33
			if($count > 0){
34
				$sql .= ' ENCODING \''.strtoupper($charset[0]).'\'';
35
			}
36
37
			if($count > 1){
38
				$sql .= ' LC_COLLATE=\''.$charset[1].'\'';
39
			}
40
41
			if($count > 2){
42
				$sql .= ' LC_CTYPE=\''.$charset[2].'\'';
43
			}
44
45
		}
46
47
		return $sql;
48
	}
49
50
}
51