Completed
Push — master ( 3deaa5...4c5cb4 )
by Changwan
06:31
created

SelectQuery::toSql()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 0
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Query;
3
4
use Wandu\Database\Contracts\QueryInterface;
5
use Wandu\Database\Query\Expression\HasWhereExpression;
6
7
class SelectQuery extends HasWhereExpression implements QueryInterface
8
{
9
    /** @var string */
10
    protected $table;
11
    
12
    /** @var array */
13
    protected $columns = ['*'];
14
    
15
    /**
16
     * @param string $table
17
     * @param array $columns
18
     */
19 6
    public function __construct($table, array $columns = [])
20
    {
21 6
        $this->table = $table;
22 6
        $this->columns = $columns;
23 6
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 6
    public function toSql()
29
    {
30 6
        $columnSqlParts = [];
31 6
        foreach ($this->columns as $key => $column) {
32 6
            if ($column === '*') {
33 5
                $columnSqlParts[] = '*';
34 5
            } else {
35 1
                $columnSqlParts[] = "`{$column}`";
36
            }
37 6
        }
38 6
        $parts = ["SELECT ". implode(', ', $columnSqlParts) ." FROM `{$this->table}`"];
39 6
        if ($part = parent::toSql()) {
40 6
            $parts[] = $part;
41 6
        }
42 6
        return implode(' ', $parts);
43
    }
44
}
45