1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CreateTable |
4
|
|
|
* |
5
|
|
|
* @filesource CreateTable.php |
6
|
|
|
* @created 03.06.2017 |
7
|
|
|
* @package chillerlan\Database\Query\Dialects\SQLite |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query\Dialects\SQLite; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Query\CreateTableInterface; |
16
|
|
|
use chillerlan\Database\Query\CreateTableAbstract; |
17
|
|
|
use chillerlan\Database\Query\QueryException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @link https://www.sqlite.org/lang_createtable.html |
21
|
|
|
*/ |
22
|
|
|
class CreateTable extends CreateTableAbstract{ |
23
|
|
|
|
24
|
|
|
protected $dir; |
25
|
|
|
|
26
|
|
|
public function primaryKey(string $field, string $dir = null):CreateTableInterface{ |
27
|
|
|
$this->primaryKey = $field; |
28
|
|
|
$this->dir = strtoupper($this->dir); |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function sql():string{ |
35
|
|
|
|
36
|
|
|
if(empty($this->name)){ |
37
|
|
|
throw new QueryException('no name specified'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$sql = 'CREATE '; |
41
|
|
|
$sql .= $this->temp ? ' TEMPORARY ' : ''; |
42
|
|
|
$sql .= 'TABLE '; |
43
|
|
|
$sql .= $this->ifNotExists ? 'IF NOT EXISTS ' : ''; |
44
|
|
|
|
45
|
|
|
$n = explode('.', $this->name); |
46
|
|
|
|
47
|
|
|
$sql .= $this->quote($n[count($n)-1]); |
48
|
|
|
|
49
|
|
|
if(!empty($this->cols)){ |
50
|
|
|
$sql .= ' ('.PHP_EOL."\t".implode(','.PHP_EOL."\t", $this->cols); |
51
|
|
|
|
52
|
|
|
if($this->primaryKey){ |
53
|
|
|
$sql .=','.PHP_EOL."\t".'PRIMARY KEY ('.$this->quote($this->primaryKey) |
54
|
|
|
.(in_array($this->dir, ['ASC', 'DESC']) ? $this->dir : '').')'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$sql .= PHP_EOL.')'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$sql .= ''; |
61
|
|
|
|
62
|
|
|
return $sql; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function fieldspec( |
66
|
|
|
string $name, |
67
|
|
|
string $type, |
68
|
|
|
$length = null, |
69
|
|
|
string $attribute = null, |
70
|
|
|
string $collation = null, |
71
|
|
|
bool $isNull = null, |
72
|
|
|
string $defaultType = null, |
73
|
|
|
$defaultValue = null, |
74
|
|
|
string $extra = null |
75
|
|
|
){ |
76
|
|
|
$name = trim($name); |
77
|
|
|
$type = strtoupper(trim($type)); |
78
|
|
|
$collation = strtoupper($collation); |
79
|
|
|
|
80
|
|
|
$field = ['"'.$name.'"']; |
81
|
|
|
|
82
|
|
|
$type_translation = [ |
83
|
|
|
'MEDIUMTEXT' => 'TEXT', |
84
|
|
|
'LONGTEXT' => 'TEXT', |
85
|
|
|
][$type] ?? $type; |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
if(is_int($length)&& in_array($type, ['CHAR', 'NCHAR','VARCHAR', 'NVARCHAR', 'CHARACTER']) |
90
|
|
|
|| is_string($length) && count(explode(',', $length)) === 2 && $type === 'DECIMAL'){ |
91
|
|
|
$field[] = $type_translation.'('. $length . ')'; |
92
|
|
|
} |
93
|
|
|
else{ |
94
|
|
|
$field[] = $type_translation; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if(is_bool($isNull)){ |
98
|
|
|
$field[] = $isNull ? 'NULL' : 'NOT NULL'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if($collation && in_array($collation, ['BINARY', 'NOCASE', 'RTRIM'])){ |
102
|
|
|
$field[] = 'COLLATE '.$collation; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
$defaultType = strtoupper($defaultType); |
107
|
|
|
|
108
|
|
|
if($defaultType === 'USER_DEFINED'){ |
109
|
|
|
$field[] = 'DEFAULT \''.$defaultValue.'\''; |
110
|
|
|
} |
111
|
|
|
else if(in_array($defaultType, ['CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP'])){ |
112
|
|
|
$field[] = 'DEFAULT '.$defaultType; |
113
|
|
|
} |
114
|
|
|
else if($defaultType === 'NULL' && $isNull === true){ |
115
|
|
|
$field[] = 'DEFAULT NULL'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
if($attribute){ |
|
|
|
|
121
|
|
|
$field[] = $attribute; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if($extra){ |
|
|
|
|
125
|
|
|
$field[] = $extra; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return implode(' ', $field); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: