Completed
Push — master ( 47c098...6c95d6 )
by smiley
02:23
created

SelectAbstract::cols()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 1
1
<?php
2
/**
3
 * Class SelectAbstract
4
 *
5
 * @filesource   SelectAbstract.php
6
 * @created      03.06.2017
7
 * @package      chillerlan\Database\Query\Dialects
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query;
14
15
abstract class SelectAbstract extends StatementAbstract implements SelectInterface{
16
17
	protected $limit;
18
	protected $offset;
19
	protected $distinct = false;
20
21
	protected $cols = [];
22
	protected $from = [];
23
	protected $where = [];
24
	protected $orderby = [];
25
	protected $groupby = [];
26
27
	/**
28
	 * @return string
29
	 */
30
	public function sql():string{
31
#		print_r([$this->cols, $this->from, $this->orderby, $this->groupby]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
33
		$glue = ','.PHP_EOL."\t";
34
35
		$sql = 'SELECT ';
36
37
		$sql .= $this->distinct ? 'DISTINCT ' : '';
38
		$sql .= implode($glue , $this->cols).PHP_EOL;
39
		$sql .= !empty($this->from) ? 'FROM '.implode($glue , $this->from) : '';
40
		$sql .= !empty($this->where) ? PHP_EOL.'WHERE ' : '';
41
		$sql .= !empty($this->groupby) ? PHP_EOL.'GROUP BY '.implode($glue, $this->groupby) : '';
42
		$sql .= !empty($this->orderby) ? PHP_EOL.'ORDER BY '.implode($glue, $this->orderby) : '';
43
		$sql .= $this->limit ? PHP_EOL.'LIMIT '.($this->offset ? $this->offset.', '.$this->limit : $this->limit) : '';
44
45
		return $sql;
46
	}
47
48
	/**
49
	 * @return \chillerlan\Database\Query\SelectInterface
50
	 */
51
	public function distinct():SelectInterface{
52
		$this->distinct = true;
53
54
		return $this;
55
	}
56
57
	/**
58
	 * @param      $expr1
59
	 * @param null $expr2
60
	 * @param null $func
61
	 *
62
	 * @return void
63
	 */
64
	protected function addColumn($expr1, $expr2 = null, $func = null){
65
		// @todo: quotes
66
		switch(true){
67
			case  $expr2 && $func:
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
68
				$col = sprintf('%s(%s) AS %s', strtoupper($func), $expr1, $expr2); break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
69
			case  $expr2 && !$func:
0 ignored issues
show
Coding Style introduced by
As per coding-style, case should be followed by a single space.

As per the PSR-2 coding standard, there must be a space after the case keyword, instead of the test immediately following it.

switch (true) {
    case!isset($a):  //wrong
        doSomething();
        break;
    case !isset($b):  //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
70
				$col = sprintf('%s AS %s', $expr1, $expr2); break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71
			case !$expr2 && $func:
72
				$col = sprintf('%s(%s)', strtoupper($func), $expr1); break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73
			case !$expr2 && !$func:
74
			default:
75
				$col = $expr1;
76
		}
77
78
		$this->cols[$expr2 ?? $expr1] = $col;
79
	}
80
81
	/**
82
	 * @param array $expressions
83
	 *
84
	 * @return \chillerlan\Database\Query\SelectInterface
85
	 */
86
	public function cols(array $expressions):SelectInterface{
87
88
		foreach($expressions as $k => $ref){
89
90
			if(is_string($k)){
91
				is_array($ref)
92
					? $this->addColumn($k, $ref[0], $ref[1] ?? null)
93
					: $this->addColumn($k, $ref);
94
			}
95
			else{
96
				is_array($ref)
97
					? $this->addColumn($ref[0], null, $ref[1] ?? null)
98
					: $this->addColumn($ref);
99
			}
100
101
		}
102
103
		return $this;
104
	}
105
106
	/**
107
	 * @param string      $table
108
	 * @param string|null $ref
109
	 */
110
	protected function addFrom(string $table, string $ref = null){
111
		$from = $table;
112
113
		if($ref){
0 ignored issues
show
Bug Best Practice introduced by
The expression $ref 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 ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
114
			$from = sprintf('%s AS %s', $table, $ref);
115
		}
116
117
		$this->from[$ref ?? $table] = $from;
118
	}
119
120
	/**
121
	 * @param array $expressions
122
	 *
123
	 * @return \chillerlan\Database\Query\SelectInterface
124
	 */
125
	public function from(array $expressions):SelectInterface{
126
127
		foreach($expressions as $k => $ref){
128
129
			if(is_string($k)){
130
				$this->addFrom($k, $ref);
131
			}
132
			else{
133
				$x = explode(' ', $ref);
134
135
				if(count($x) === 2){
136
					$this->addFrom($x[0], $x[1]);
137
				}
138
				else{
139
					$this->addFrom($ref);
140
				}
141
			}
142
143
		}
144
145
		return $this;
146
	}
147
148
	protected function addWhere($val1, $val2, $operator = '=', $bind = false, $and_or = 'AND'){
0 ignored issues
show
Unused Code introduced by
The parameter $val1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $val2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $operator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $bind is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $and_or is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
150
	}
151
152
	public function where():SelectInterface{
153
		// TODO: Implement where() method.
154
	}
155
156
	public function join():SelectInterface{
157
		// TODO: Implement join() method.
158
	}
159
160
	public function having():SelectInterface{
161
		// TODO: Implement having() method.
162
	}
163
164
	/**
165
	 * @param int $limit
166
	 *
167
	 * @return \chillerlan\Database\Query\SelectInterface
168
	 */
169
	public function limit(int $limit):SelectInterface{
170
		$this->limit = $limit;
171
172
		return $this;
173
	}
174
175
	/**
176
	 * @param int $offset
177
	 *
178
	 * @return \chillerlan\Database\Query\SelectInterface
179
	 */
180
	public function offset(int $offset):SelectInterface{
181
		$this->offset = $offset;
182
183
		return $this;
184
	}
185
186
	/**
187
	 * @param array $expressions
188
	 *
189
	 * @return \chillerlan\Database\Query\SelectInterface
190
	 */
191
	public function orderby(array $expressions):SelectInterface{
192
193
		foreach($expressions as $k => $expression){
194
195
			if(is_string($k)){
196
				$orderby = $k;
197
				$expression = strtoupper($expression);
198
199
				if(in_array($expression, ['ASC', 'DESC'])){
200
					$orderby .= ' '.$expression;
201
				}
202
203
				$this->orderby[] = $orderby;
204
			}
205
			else{
206
				$this->orderby[] = $expression;
207
			}
208
209
		}
210
211
		return $this;
212
	}
213
214
	public function groupBy(array $expressions):SelectInterface{
215
216
		foreach($expressions as $expression){
217
			$this->groupby[] = $expression;
218
		}
219
220
		return $this;
221
	}
222
223
	public function union():SelectInterface{
224
		// TODO: Implement union() method.
225
	}
226
227
}
228