Test Setup Failed
Push — master ( c6f3d6...c97842 )
by Php Easy Api
03:48
created

QuerySyntax::change()   A

Complexity

Conditions 6
Paths 13

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 13
nop 1
dl 0
loc 35
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
namespace Migratio\GrammarStructure\Mysql;
4
5
class QuerySyntax extends QuerySyntaxHelper
6
{
7
8
    /**
9
     * @var array $data
10
     */
11
    protected $data = array();
12
    /**
13
     * @var array $syntax
14
     */
15
    protected $syntax = array();
16
17
    /**
18
     * @return array
19
     */
20
    public function syntaxCreate()
21
    {
22
        $this->getWizardObjects($this->object);
23
24
        $this->getCreateTableSyntax();
25
26
        $this->getDefaultSyntaxGroup();
27
28
        $this->syntax[]=')';
29
30
        //get table collation
31
        if(isset($this->data['tableCollation']['table'])){
32
            $this->syntax[]=' DEFAULT CHARACTER SET '.$this->data['tableCollation']['table'];
33
        }
34
        else{
35
            $this->syntax[]=' DEFAULT CHARACTER SET utf8';
36
        }
37
38
        //get engine
39
        if($this->data['engine']!==null)
40
        {
41
            $this->syntax[]=' ENGINE='.$this->data['engine'].' ';
42
        }
43
        else{
44
            $this->syntax[]=' ENGINE=InnoDB ';
45
        }
46
47
        $syntax = implode("",$this->syntax);
48
        
49
        $query=$this->schema->getConnection()->setQueryBasic($syntax);
50
51
        return [
52
            'syntax'=>$syntax,
53
            'type'=>'create',
54
            'result'=>$query['result'],
55
            'message'=>$query['message'],
56
            ];
57
    }
58
59
    /**
60
     * @return mixed|void
61
     */
62
    private function getDefaultSyntaxGroup()
63
    {
64
65
        $this->syntax[]=implode(",",$this->getCreateDefaultList());
66
67
        //get unique values
68
        if(isset($this->data['uniqueValueList']) && count($this->data['uniqueValueList'])){
69
            $this->syntax[]=','.implode(',',$this->data['uniqueValueList']);
70
        }
71
72
        //get index values
73
        if(isset($this->data['indexValueList']) && count($this->data['indexValueList'])){
74
            $this->syntax[]=','.implode(',',$this->data['indexValueList']);
75
        }
76
77
        //get index values for key
78
        if(count($this->getKeyList())){
79
            $this->syntax[]=','.implode(',',$this->getKeyList());
80
        }
81
82
        if(count($this->data['references'])){
83
            $this->syntax[]=$this->getReferenceSyntax($this->data['references']);
84
        }
85
    }
86
87
88
    /**
89
     * @return mixed|void
90
     */
91
    public function syntaxAlter()
92
    {
93
        $this->getWizardObjects($this->object);
94
95
        $alterType = $this->object->getAlterType();
96
97
        $group = $alterType['group'];
98
99
        $this->getDefaultSyntaxGroup();
100
        
101
        return $this->{$group}($alterType);
102
103
    }
104
    
105
    private function change($alterType)
106
    {
107
        if(isset($alterType['place'])){
108
109
            foreach ($alterType['place'] as $placeKey=>$placeValue){
110
                $placeList=$placeKey .' '.$placeValue.'';
111
            }
112
113
            $syntax = implode("",$this->syntax);
114
            
115
            $columns = $this->schema->getConnection()->showColumnsFrom($this->table);
116
            
117
            foreach ($columns as $columnKey=>$columnData){
118
                if($columnData['Field']==$placeValue){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $placeValue seems to be defined by a foreach iteration on line 109. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
119
                    $changeAbleField = $columns[$columnKey+1]['Field'];
120
                }
121
            }
122
            
123
            $syntaxList = explode(' ',$syntax);
124
125
            if(current($syntaxList)!==$changeAbleField){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $changeAbleField does not seem to be defined for all execution paths leading up to this point.
Loading history...
126
                $alterSytanx = 'ALTER TABLE '.$this->table.' change '.$changeAbleField.' '.current($syntaxList).'  '.implode(' ',array_splice($syntaxList,1)).' '.$placeList;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $placeList seems to be defined by a foreach iteration on line 109. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
127
            }
128
            else{
129
                $alterSytanx = 'ALTER TABLE '.$this->table.' modify '.$syntax.' '.$placeList;
130
            }
131
            
132
133
            $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
134
135
            return [
136
                'syntax'=>$syntax,
137
                'type'=>'create',
138
                'result'=>$query['result'],
139
                'message'=>$query['message'],
140
            ];
141
        }
142
    }
143
144
    private function addColumn($alterType)
145
    {
146
        if(isset($alterType['place'])){
147
148
            foreach ($alterType['place'] as $placeKey=>$placeValue){
149
                $placeList=$placeKey .' '.$placeValue.'';
150
            }
151
152
            $syntax = implode("",$this->syntax);
153
154
            $alterSytanx = 'ALTER TABLE '.$this->table.' ADD COLUMN '.$syntax.' '.$placeList;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $placeList seems to be defined by a foreach iteration on line 148. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
155
156
            $query=$this->schema->getConnection()->setQueryBasic($alterSytanx);
157
158
            return [
159
                'syntax'=>$syntax,
160
                'type'=>'create',
161
                'result'=>$query['result'],
162
                'message'=>$query['message'],
163
            ];
164
        }
165
166
    }
167
}
168
169