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

SelectQuery   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A toSql() 0 16 4
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