These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Class SQLite |
||
4 | * |
||
5 | * @filesource SQLite.php |
||
6 | * @created 11.01.2018 |
||
7 | * @package chillerlan\Database\Dialects |
||
8 | * @author Smiley <[email protected]> |
||
9 | * @copyright 2018 Smiley |
||
10 | * @license MIT |
||
11 | */ |
||
12 | |||
13 | namespace chillerlan\Database\Dialects; |
||
14 | |||
15 | class SQLite extends DialectAbstract{ |
||
16 | |||
17 | protected $quotes = ['"', '"']; |
||
18 | |||
19 | /** @inheritdoc */ |
||
20 | public function fieldspec(string $name, string $type, $length = null, string $attribute = null, string $collation = null, bool $isNull = null, string $defaultType = null, $defaultValue = null, string $extra = null):string{ |
||
21 | $type = strtoupper(trim($type)); |
||
22 | $collation = strtoupper($collation); |
||
23 | |||
24 | $field = [$this->quote(trim($name))]; |
||
25 | |||
26 | $type_translation = [ |
||
27 | 'MEDIUMTEXT' => 'TEXT', |
||
28 | 'LONGTEXT' => 'TEXT', |
||
29 | ][$type] ?? $type; |
||
30 | |||
31 | if($length !== null && (in_array($type, ['CHAR', 'NCHAR', 'VARCHAR', 'NVARCHAR', 'CHARACTER'], true) || (is_string($length) && count(explode(',', $length)) === 2 && $type === 'DECIMAL'))){ |
||
32 | $field[] = $type_translation.'('.$length.')'; |
||
33 | } |
||
34 | else{ |
||
35 | $field[] = $type_translation; |
||
36 | } |
||
37 | |||
38 | if(is_bool($isNull)){ |
||
39 | $field[] = $isNull ? 'NULL' : 'NOT NULL'; |
||
40 | } |
||
41 | |||
42 | if(in_array($collation, ['BINARY', 'NOCASE', 'RTRIM'], true)){ |
||
43 | $field[] = 'COLLATE '.$collation; |
||
44 | } |
||
45 | |||
46 | $defaultType = strtoupper($defaultType); |
||
47 | |||
48 | if($defaultType === 'USER_DEFINED'){ |
||
49 | $field[] = 'DEFAULT \''.$defaultValue.'\''; |
||
50 | } |
||
51 | elseif(in_array($defaultType, ['CURRENT_DATE', 'CURRENT_TIME', 'CURRENT_TIMESTAMP'], true)){ |
||
52 | $field[] = 'DEFAULT '.$defaultType; |
||
53 | } |
||
54 | elseif($defaultType === 'NULL' && $isNull === true){ |
||
55 | $field[] = 'DEFAULT NULL'; |
||
56 | } |
||
57 | |||
58 | if($attribute){ |
||
0 ignored issues
–
show
|
|||
59 | $field[] = $attribute; |
||
60 | } |
||
61 | |||
62 | if($extra){ |
||
0 ignored issues
–
show
The expression
$extra of type null|string is loosely compared to true ; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
63 | $field[] = $extra; |
||
64 | } |
||
65 | |||
66 | return implode(' ', $field); |
||
67 | } |
||
68 | |||
69 | /** @inheritdoc */ |
||
70 | public function createTable(string $table, array $cols, string $primaryKey = null, bool $ifNotExists, bool $temp, string $dir = null):array{ |
||
71 | $sql = ['CREATE']; |
||
72 | |||
73 | if($temp){ |
||
74 | $sql[] = 'TEMPORARY'; |
||
75 | } |
||
76 | |||
77 | $sql[] = 'TABLE'; |
||
78 | |||
79 | if($ifNotExists){ |
||
80 | $sql[] = 'IF NOT EXISTS'; |
||
81 | } |
||
82 | |||
83 | $n = explode('.', $table); |
||
84 | |||
85 | $sql[] = $this->quote($n[count($n) - 1]); |
||
86 | |||
87 | if(!empty($cols)){ |
||
88 | $sql[] = '('; |
||
89 | $sql[] = implode(', ', $cols); |
||
90 | |||
91 | if($primaryKey){ |
||
0 ignored issues
–
show
The expression
$primaryKey of type null|string is loosely compared to true ; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
92 | $sql[] = ', PRIMARY KEY'; |
||
93 | $sql[] = '('; |
||
94 | $sql[] = $this->quote($primaryKey); |
||
95 | |||
96 | if(in_array($dir, ['ASC', 'DESC'], true)){ |
||
97 | $sql[] = ($dir); |
||
98 | } |
||
99 | |||
100 | $sql[] = ')'; |
||
101 | } |
||
102 | |||
103 | $sql[] = ')'; |
||
104 | } |
||
105 | |||
106 | return $sql; |
||
107 | } |
||
108 | |||
109 | /** @inheritdoc */ |
||
110 | public function truncate(string $table):array{ |
||
111 | $sql = ['DELETE FROM'];// ??? sqlite |
||
112 | $sql[] = $this->quote($table); |
||
113 | |||
114 | return $sql; |
||
115 | } |
||
116 | |||
117 | /** @inheritdoc */ |
||
118 | public function showTables(string $database = null, string $pattern = null, string $where = null):array{ |
||
119 | /** @noinspection SqlResolve */ |
||
120 | return ['SELECT "name" AS "tablename" FROM sqlite_master']; |
||
121 | } |
||
122 | |||
123 | /** @inheritdoc */ |
||
124 | public function showCreateTable(string $table):array{ |
||
125 | /** @noinspection SqlResolve */ |
||
126 | $sql = ['SELECT "name" AS "Table", "sql" AS "Create Table" FROM sqlite_master WHERE name =']; |
||
127 | $sql[] = $this->quote($table); |
||
128 | |||
129 | return $sql; |
||
130 | } |
||
131 | } |
||
132 |
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: