Completed
Push — master ( 117114...c02abd )
by smiley
04:21
created

src/Dialects/MSSQL.php (1 issue)

Upgrade to new PHP Analysis Engine

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{
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){
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)){
76
			$sql[] = '(';
77
			$sql[] = implode(',', $cols);
78
79
			if($primaryKey){
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'))
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 return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

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){
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