ConstraintExpression::reference()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database\Query\Expression;
3
4
use Wandu\Database\Contracts\ExpressionInterface;
5
use Wandu\Database\Support\Attributes;
6
use Wandu\Database\Support\Helper;
7
8
/**
9
 * @see http://dev.mysql.com/doc/refman/5.7/en/create-table.html
10
 *
11
 * ConstraintExpression:
12
 *     KEY [index_name] [index_type] (index_col_name,...)
13
 *   | PRIMARY KEY [index_type] (index_col_name,...)
14
 *   | UNIQUE KEY [index_name] [index_type] (index_col_name,...)
15
 *   | FOREIGN KEY [index_name] (index_col_name,...) ReferenceExpression
16
 * index_type:
17
 *     USING {BTREE | HASH}
18
 *
19
 * @method \Wandu\Database\Query\Expression\ConstraintExpression using(int $indexType)
20
 */
21
class ConstraintExpression implements ExpressionInterface
22
{
23
    use Attributes;
24
    
25
    const KEY_TYPE_NONE = 0;
26
    const KEY_TYPE_PRIMARY = 1;
27
    const KEY_TYPE_UNIQUE = 2;
28
    const KEY_TYPE_FOREIGN = 3;
29
    
30
    const INDEX_TYPE_NONE = 0; // but maybe use B-Tree
31
    const INDEX_TYPE_BTREE = 1;
32
    const INDEX_TYPE_HASH = 2;
33
    
34
    /** @var array */
35
    protected $columns;
36
    
37
    /** @var string */
38
    protected $name;
39
40
    /** @var \Wandu\Database\Query\Expression\ReferenceExpression */
41
    protected $reference;
42
    
43
    /**
44
     * @param array $columns
45
     * @param string $name
46
     * @param array $attributes
47
     */
48 1
    public function __construct(array  $columns, $name = null, array $attributes = [])
49
    {
50 1
        $this->columns = $columns;
51 1
        $this->name = $name;
52 1
        $this->attributes = $attributes;
53 1
    }
54
55
    /**
56
     * @param string $table
57
     * @param string|array $column
58
     * @return \Wandu\Database\Query\Expression\ReferenceExpression
59
     */
60 1
    public function reference($table, $column)
61
    {
62 1
        return $this->reference = new ReferenceExpression($table, is_array($column) ? $column : [$column]);
63
    }
64
65
    /**
66
     * @example
67
     *          KEY [index_name] [index_type] (index_col_name,...)
68
     *  PRIMARY KEY              [index_type] (index_col_name,...)
69
     *  UNIQUE  KEY [index_name] [index_type] (index_col_name,...)
70
     *  FOREIGN KEY [index_name]              (index_col_name,...) ReferenceExpression
71
     * 
72
     * {@inheritdoc}
73
     */
74 1
    public function toSql()
75
    {
76 1
        $stringToReturn = '';
77 1
        $keyType = isset($this->attributes['key_type']) ? $this->attributes['key_type'] : static::KEY_TYPE_NONE;
78
        switch ($keyType) {
79 1
            case static::KEY_TYPE_PRIMARY:
80 1
                $stringToReturn .= 'PRIMARY '; 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...
81 1
            case static::KEY_TYPE_UNIQUE:
82 1
                $stringToReturn .= 'UNIQUE '; 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...
83 1
            case static::KEY_TYPE_FOREIGN:
84 1
                $stringToReturn .= 'FOREIGN '; 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...
85
            default:
86 1
                $keyType = static::KEY_TYPE_NONE; // normalize
87
        }
88 1
        $stringToReturn .= 'KEY';
89 1
        if ($keyType !== static::KEY_TYPE_PRIMARY && $this->name) {
90 1
            $stringToReturn .= " `{$this->name}`";
91
        }
92 1
        if ($keyType !== static::KEY_TYPE_FOREIGN && isset($this->attributes['using'])) {
93 1
            switch ($this->attributes['using']) {
94 1
                case static::INDEX_TYPE_BTREE:
95
                    $stringToReturn .= ' USING BTREE'; 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...
96 1
                case static::INDEX_TYPE_HASH:
97 1
                    $stringToReturn .= ' USING HASH'; 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...
98
            }
99
        }
100 1
        $stringToReturn .= ' (' . Helper::arrayImplode(', ', $this->columns, '`', '`') .')';
101 1
        if ($keyType === static::KEY_TYPE_FOREIGN && isset($this->reference)) {
102 1
            $referenceString = $this->reference->toSql();
103 1
            if ($referenceString) {
104 1
                $stringToReturn .= " {$referenceString}";
105
            }
106
        }
107 1
        return $stringToReturn;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getBindings()
114
    {
115
        return [];
116
    }
117
}
118