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 MSSQL |
||
4 | * |
||
5 | * @filesource MSSQL.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 MSSQL extends DialectAbstract{ |
||
16 | |||
17 | protected $quotes = ['[', ']']; |
||
18 | |||
19 | /** @inheritdoc */ |
||
20 | View Code Duplication | public function select(array $cols, array $from, string $where = null, $limit = null, $offset = null, bool $distinct = null, array $groupby, array $orderby):array{ |
|
0 ignored issues
–
show
|
|||
21 | $sql = ['SELECT']; |
||
22 | |||
23 | if($distinct){ |
||
24 | $sql[] = 'DISTINCT'; |
||
25 | } |
||
26 | |||
27 | !empty($cols) |
||
28 | ? $sql[] = implode(', ', $cols) |
||
29 | : $sql[] = '*'; |
||
30 | |||
31 | $sql[] = 'FROM'; |
||
32 | $sql[] = implode(', ', $from); |
||
33 | $sql[] = $where; |
||
34 | |||
35 | if(!empty($groupby)){ |
||
36 | $sql[] = 'GROUP BY'; |
||
37 | $sql[] = implode(', ', $groupby); |
||
38 | } |
||
39 | |||
40 | if(!empty($orderby)){ |
||
41 | $sql[] = 'ORDER BY'; |
||
42 | $sql[] = implode(', ', $orderby); |
||
43 | } |
||
44 | |||
45 | if($limit !== null){ |
||
46 | |||
47 | if(empty($orderby)){ |
||
48 | $sql[] = 'ORDER BY 1'; |
||
49 | } |
||
50 | |||
51 | $sql[] = 'OFFSET ? ROWS FETCH NEXT ? ROWS ONLY'; |
||
52 | } |
||
53 | |||
54 | return $sql; |
||
55 | } |
||
56 | |||
57 | /** @inheritdoc */ |
||
58 | public function createDatabase(string $dbname, bool $ifNotExists = null, string $collate = null):array{ |
||
59 | $sql = [ 'CREATE DATABASE']; |
||
60 | $sql[] = $this->quote($dbname); |
||
61 | |||
62 | if($collate){ |
||
0 ignored issues
–
show
The expression
$collate 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 | $sql[] = 'COLLATE'; |
||
64 | $sql[] = $collate; |
||
65 | } |
||
66 | |||
67 | return $sql; |
||
68 | } |
||
69 | |||
70 | /** @inheritdoc */ |
||
71 | public function createTable(string $table, array $cols, string $primaryKey = null, bool $ifNotExists, bool $temp, string $dir = null):array{ |
||
72 | $sql = ['CREATE TABLE']; |
||
73 | $sql[] = $this->quote($table); |
||
74 | |||
75 | View Code Duplication | if(!empty($this->cols)){ |
|
0 ignored issues
–
show
The property
cols does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
76 | $sql[] = '('; |
||
77 | $sql[] = implode(',', $cols); |
||
78 | |||
79 | 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...
|
|||
80 | $sql[] = ', PRIMARY KEY ('.$this->quote($primaryKey).')'; |
||
81 | } |
||
82 | |||
83 | $sql[] = ')'; |
||
84 | } |
||
85 | |||
86 | return $sql; |
||
87 | } |
||
88 | |||
89 | /** @inheritdoc */ |
||
90 | 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{ |
||
91 | $type = strtolower(trim($type)); |
||
92 | |||
93 | $field = [$this->quote(trim($name))]; |
||
94 | |||
95 | $type_translation = [ |
||
96 | 'boolean' => 'tinyint', |
||
97 | 'bool ' => 'tinyint', |
||
98 | 'mediumint' => 'int', |
||
99 | 'double' => 'float', |
||
100 | 'tinytext' => 'text', |
||
101 | 'mediumtext' => 'text', |
||
102 | 'longtext' => 'text', |
||
103 | 'timestamp' => 'datetime2', |
||
104 | ][$type] ?? $type; |
||
105 | |||
106 | View Code Duplication | if((is_int($length) || is_string($length) && (count(explode(',', $length)) === 2 || $length === 'max')) |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
107 | && in_array($type, ['char', 'varchar', 'nchar', 'nvarchar', 'decimal', 'numeric', 'datetime2', 'time'], true)){ |
||
108 | $field[] = $type_translation.'('.$length.')'; |
||
109 | } |
||
110 | else{ |
||
111 | $field[] = $type_translation; |
||
112 | } |
||
113 | |||
114 | if($isNull !== null){ |
||
115 | $field[] = $isNull ? 'NULL' : 'NOT NULL'; |
||
116 | } |
||
117 | |||
118 | $defaultType = strtoupper($defaultType); |
||
119 | |||
120 | if($defaultType === 'USER_DEFINED'){ |
||
121 | |||
122 | // @todo |
||
123 | switch(true){ |
||
124 | default: |
||
0 ignored issues
–
show
default: $field[] = .... $defaultValue . '\''; does not seem to be reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last
Loading history...
|
|||
125 | $field[] = 'DEFAULT \''.$defaultValue.'\''; |
||
126 | } |
||
127 | |||
128 | } |
||
129 | elseif($defaultType === 'CURRENT_TIMESTAMP'){ |
||
130 | $field[] = 'DEFAULT CURRENT_TIMESTAMP'; |
||
131 | } |
||
132 | elseif($defaultType === 'NULL' && $isNull === true){ |
||
133 | $field[] = 'DEFAULT NULL'; |
||
134 | } |
||
135 | |||
136 | 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...
|
|||
137 | $field[] = $extra; |
||
138 | } |
||
139 | |||
140 | return implode(' ', $field); |
||
141 | } |
||
142 | |||
143 | /** @inheritdoc */ |
||
144 | public function showDatabases():array{ |
||
145 | // https://stackoverflow.com/questions/147659/get-list-of-databases-from-sql-server |
||
146 | // EXEC sp_databases |
||
147 | return ['SELECT name AS [Database] FROM master.dbo.sysdatabases WHERE name NOT IN (\'master\', \'tempdb\', \'model\', \'msdb\')']; |
||
148 | } |
||
149 | |||
150 | } |
||
151 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.