Test Setup Failed
Push — master ( 40bc14...dc4600 )
by Php Easy Api
04:00
created

QueryStack::showColumnsFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Migratio\GrammarStructure\Mysql\Traits;
4
5
use Resta\Support\Generator\Generator;
6
use Resta\Support\Utils;
7
8
trait QueryStack
9
{
10
    /**
11
     * @return mixed
12
     */
13
    public function connection()
14
    {
15
        return $this->schema->getConnection();
16
    }
17
18
    public function registerMigration($table,$name)
19
    {
20
        return $this->connection()->query(
21
            "INSERT INTO migrations SET table_name ='".$table."',name='".$name."'
22
            ");
23
    }
24
25
    public function checkMigration($table,$name)
26
    {
27
        $query = $this->connection()->query(
28
            "SELECT * FROM migrations WHERE table_name='".$table."' and name='".$name."'
29
            ")->fetchAll();
30
        
31
        return (isset($query[0])) ? true : false;
32
    }
33
34
    public function checkMigrationMain()
35
    {
36
        return (in_array('migrations',$this->showTables())) ? true : false;
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function showTables()
43
    {
44
        $tables = $this->connection()->query('SHOW TABLES')->fetchAll();
45
46
        $list = [];
47
48
        foreach ($tables as $key=>$table) {
49
            $list[] = $table[0];
50
        }
51
52
        return $list;
53
    }
54
55
    /**
56
     * @param $table
57
     * @return mixed
58
     */
59
    public function showColumnsFrom($table)
60
    {
61
        return $this->connection()->query('SHOW FULL COLUMNS FROM '.$table)->fetchAll();
62
    }
63
64
    /**
65
     * get charset for collation
66
     *
67
     * @param $collation
68
     * @return mixed
69
     */
70
    public function getCharsetForCollation($collation)
71
    {
72
        $collation = $this->connection()->query('SHOW COLLATION LIKE \''.$collation.'%\'')->fetchAll();
73
74
        if(isset($collation[0])){
75
            return $collation[0]['Charset'];
76
        }
77
78
        return 'utf8';
79
    }
80
81
    /**
82
     * get table status
83
     *
84
     * @param $table
85
     * @return array
86
     */
87
    public function getTableStatus($table)
88
    {
89
        $status = $this->connection()->query('show table status like \''.$table.'\'')->fetchAll();
90
91
        if(isset($status[0])){
92
            return $status[0];
93
        }
94
95
        return [];
96
    }
97
98
    /**
99
     * get show indexes
100
     *
101
     * @param $table
102
     * @return mixed
103
     */
104
    public function showIndexes($table)
105
    {
106
        return $this->connection()->query('SHOW INDEXES FROM '.$table)->fetchAll();
107
    }
108
109
    /**
110
     * get foreign keys from table
111
     *
112
     * @param $table
113
     * @return array
114
     */
115
    public function getForeignKeys($table)
116
    {
117
        $config = $this->getConfig();
0 ignored issues
show
Bug introduced by
It seems like getConfig() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        /** @scrutinizer ignore-call */ 
118
        $config = $this->getConfig();
Loading history...
118
119
        $database = (isset($config['database'])) ? $config['database'] : null;
120
121
        $query = $this->connection()->query('SELECT rc.MATCH_OPTION,rc.UPDATE_RULE,rc.DELETE_RULE,r.*
122
        FROM information_schema.REFERENTIAL_CONSTRAINTS as rc
123
        LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE as r ON r.TABLE_NAME=rc.TABLE_NAME
124
        WHERE rc.CONSTRAINT_SCHEMA = \''.$database.'\'
125
        AND rc.TABLE_NAME = \''.$table.'\' AND r.REFERENCED_TABLE_NAME is not null')->fetchAll();
126
127
        if(isset($query[0])){
128
            return $query[0];
129
        }
130
131
        return [];
132
    }
133
134
    /**
135
     * set query basic
136
     *
137
     * @param $query
138
     * @return mixed
139
     */
140
    public function setQueryBasic($query)
141
    {
142
        try {
143
144
            $query =$this->connection()->query($query);
145
146
            return [
147
                'result'=>true,
148
                'query'=>$query,
149
                'message'=>null,
150
            ];
151
        }
152
        catch (\PDOException $exception){
153
154
            return [
155
                'result'=>false,
156
                'query'=>$query,
157
                'message'=>$exception->getMessage(),
158
            ];
159
        }
160
161
    }
162
163
    /**
164
     * @param $table
165
     */
166
    public function generateEntity($table)
167
    {
168
        if(in_array($table,$this->showTables()) || in_array($table = strtolower($table),$this->showTables())){
169
170
            $entityDirectory = path()->model().''.DIRECTORY_SEPARATOR.'Entity';
171
172
            if(!file_exists(app()->path()->model())){
173
                files()->makeDirectory(app()->path()->model());
174
            }
175
176
            if(!file_exists($entityDirectory)){
177
                files()->makeDirectory($entityDirectory);
178
            }
179
180
            $columns = $this->showColumnsFrom($table);
181
182
            $list = [];
183
184
            foreach ($columns as $column) {
185
                $list[] = $column['Field'];
186
            }
187
188
            if(!file_exists($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table))){
189
                $generator = new Generator($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table),$table.'');
190
                $generator->createClass();
191
            }
192
            else{
193
                $generator = new Generator($entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table),$table.'');
194
            }
195
196
197
            $abstractClassPath = $entityDirectory.''.DIRECTORY_SEPARATOR.''.ucfirst($table).''.DIRECTORY_SEPARATOR.'Entity';
198
            $abstractNamespace = Utils::getNamespace($abstractClassPath.''.DIRECTORY_SEPARATOR.''.ucfirst($table).'Abstract');
199
200
            $generator->createClassExtend($abstractNamespace,ucfirst($table).'Abstract');
201
202
            $generator = new Generator($abstractClassPath,$table.'Abstract');
203
204
            $generator->createClass();
205
206
            $method =array_merge([
207
                '__construct'
208
            ],array_merge($list,['__get']));
209
210
            $generator->createMethod($method);
211
212
            $generator->createMethodParameters([
213
                '__construct' => '$query',
214
                '__get' => '$name'
215
            ]);
216
217
            $methodBodyList = [];
218
            $createMethodAccessibleProperty = [];
219
            $createMethodDocument = [];
220
            $createClassDocument = [];
221
222
            foreach ($list as $item) {
223
                $methodBodyList[$item] = 'return self::$query->'.$item.';';
224
                $createClassDocument[] = '@property $this '.$item;
225
                $createMethodAccessibleProperty[$item] = 'protected static';
226
                $createMethodDocument[$item] = [
227
                    '@return mixed'
228
                ];
229
            }
230
231
            $generator->createClassDocument($createClassDocument);
232
233
            $generator->createMethodDocument(array_merge($createMethodDocument,[
234
                '__construct' => [
235
                    ''.$table.' constructor.',
236
                    '@param null|object $query'
237
                ],
238
                '__get' =>[
239
                    'access entity object with magic method',
240
                    '',
241
                    '@param $name',
242
                    '@return mixed'
243
                ]
244
            ]));
245
246
            $createMethodBody = array_merge([
247
                '__construct' => 'self::$query = $query;',
248
                '__get' => 'return static::{$name}();'
249
            ],$methodBodyList);
250
251
            $generator->createMethodBody($createMethodBody);
252
253
            $generator->createMethodAccessibleProperty($createMethodAccessibleProperty);
254
255
256
            $generator->createClassProperty([
257
                'protected static $query;'
258
            ]);
259
260
            $generator->createClassPropertyDocument([
261
                'protected static $query' => [
262
                    '@var object|null'
263
                ]
264
            ]);
265
        }
266
267
    }
268
269
}
270
271