1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CreateTable |
4
|
|
|
* |
5
|
|
|
* @filesource CreateTable.php |
6
|
|
|
* @created 03.06.2017 |
7
|
|
|
* @package chillerlan\Database\Query\Dialects\Firebird |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query\Dialects\Firebird; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Query\CreateTableAbstract; |
16
|
|
|
use chillerlan\Database\Query\QueryException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @link https://www.firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-ddl-tbl.html#fblangref25-ddl-tbl-create |
20
|
|
|
*/ |
21
|
|
|
class CreateTable extends CreateTableAbstract{ |
22
|
|
|
|
23
|
|
|
public function sql():string{ |
24
|
|
|
|
25
|
|
|
if(empty($this->name)){ |
26
|
|
|
throw new QueryException('no name specified'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$sql = $this->ifNotExists ? 'RECREATE ' : 'CREATE '; // nasty |
30
|
|
|
$sql .= $this->temp ? 'GLOBAL TEMPORARY ' : ''; |
31
|
|
|
$sql .= 'TABLE '; |
32
|
|
|
$sql .= strtoupper($this->name); |
33
|
|
|
|
34
|
|
|
$cols = []; |
35
|
|
|
|
36
|
|
|
if(!empty($this->cols)){ |
37
|
|
|
|
38
|
|
|
foreach($this->cols as $name => $col){ |
39
|
|
|
|
40
|
|
|
if(strtoupper($name) === strtoupper($this->primaryKey)){ |
41
|
|
|
$x = explode(' NOT NULL', $col, 2); |
42
|
|
|
|
43
|
|
|
if(count($x) > 0){ |
44
|
|
|
$col = $x[0].' NOT NULL PRIMARY KEY'; |
45
|
|
|
$col .= $x[1] ?? ''; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$cols[] = $col; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$sql .= ' ('.PHP_EOL."\t".implode(','.PHP_EOL."\t", $cols).PHP_EOL.')'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$sql .= ''; |
57
|
|
|
|
58
|
|
|
return $sql; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function fieldspec( |
62
|
|
|
string $name, |
63
|
|
|
string $type, |
64
|
|
|
$length = null, |
65
|
|
|
string $constraint = null, |
66
|
|
|
string $collation = null, |
67
|
|
|
bool $isNull = false, |
68
|
|
|
string $defaultType = null, |
69
|
|
|
$defaultValue = null, |
70
|
|
|
string $extra = null |
71
|
|
|
){ |
72
|
|
|
$name = strtoupper(trim($name)); |
73
|
|
|
$type = strtoupper(trim($type)); |
74
|
|
|
|
75
|
|
|
$field = [$name]; |
76
|
|
|
|
77
|
|
|
// @todo: whitelist types? |
78
|
|
|
$nolengthtypes = ['BLOB', 'SMALLINT', 'INTEGER', 'BIGINT', 'FLOAT', |
79
|
|
|
'DOUBLE PRECISION', 'DATE', 'TIME', 'TINYTEXT']; |
80
|
|
|
|
81
|
|
|
$field[] = (is_int($length) || is_string($length) && count(explode(',', $length)) === 2) && !in_array($type, $nolengthtypes) |
82
|
|
|
? $type.'('. $length . ')' |
83
|
|
|
: $type; |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
if(!$isNull && !in_array($type, ['DATE', 'TIME', 'TIMESTAMP', 'CHAR', 'NCHAR', 'VARCHAR'])){ |
87
|
|
|
$field[] = 'NOT NULL'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$defaultType = strtoupper($defaultType); |
91
|
|
|
|
92
|
|
|
if($defaultType === 'USER_DEFINED'){ |
93
|
|
|
|
94
|
|
|
switch(true){ |
95
|
|
|
case $type === 'TIMESTAMP' && intval($defaultValue) === 0: |
|
|
|
|
96
|
|
|
$field[] = 'DEFAULT 0'; |
97
|
|
|
break; |
98
|
|
|
case strtoupper($defaultValue) === 'NULL' && $isNull: |
|
|
|
|
99
|
|
|
$field[] = 'DEFAULT NULL'; |
100
|
|
|
break; |
101
|
|
|
default: |
102
|
|
|
$field[] = 'DEFAULT \''.$defaultValue.'\''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
View Code Duplication |
else if($defaultType === 'CURRENT_TIMESTAMP'){ |
|
|
|
|
107
|
|
|
$field[] = 'DEFAULT CURRENT_TIMESTAMP'; |
108
|
|
|
} |
109
|
|
|
else if($defaultType === 'NULL' && $isNull){ |
110
|
|
|
$field[] = 'DEFAULT NULL'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if($constraint){ |
|
|
|
|
114
|
|
|
$field[] = strtoupper($constraint); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if($extra){ |
|
|
|
|
118
|
|
|
$field[] = $extra; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return implode(' ', $field); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.