Completed
Push — master ( 8a3ba3...5e5262 )
by James Ekow Abaka
12s
created

MysqlDescriptor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 73
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getForeignKeys() 0 24 1
A getIndices() 0 8 1
A getSchemata() 0 18 2
A hasAutoIncrementingKey() 0 17 2
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2014-2018 James Ekow Abaka Ainooson
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace ntentan\atiaa\descriptors;
28
29
class MysqlDescriptor extends InformationSchemaDescriptor
30
{
31
    protected function getForeignKeys(&$table)
32
    {
33
        return $this->driver->query(
34
            sprintf("SELECT
35
                    kcu.constraint_name as name,
36
                    kcu.table_schema as `schema`,
37
                    kcu.table_name as `table`,
38
                    kcu.column_name as `column`,
39
                    kcu.referenced_table_name AS foreign_table,
40
                    kcu.referenced_table_schema AS foreign_schema,
41
                    kcu.referenced_column_name AS foreign_column,
42
                    rc.update_rule as on_update,
43
                    rc.delete_rule as on_delete
44
                FROM
45
                    information_schema.table_constraints AS tc
46
                    JOIN information_schema.key_column_usage AS kcu
47
                      ON tc.constraint_name = kcu.constraint_name and tc.table_schema = kcu.table_schema
48
                    JOIN information_schema.referential_constraints AS rc
49
                      ON rc.constraint_name = tc.constraint_name and rc.constraint_schema = tc.table_schema
50
                WHERE constraint_type = 'FOREIGN KEY'
51
                    AND tc.table_name='%s' AND tc.table_schema='%s' order by kcu.constraint_name, kcu.column_name", $table['name'], $table['schema']
52
            )
53
        );
54
    }
55
56
    protected function getIndices(&$table)
57
    {
58
        return $this->driver->query(
59
            sprintf("SELECT table_name, column_name as `column`,index_name as `name` FROM information_schema.STATISTICS 
60
            WHERE INDEX_NAME not in (SELECT CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE)
61
            AND table_name = '%s' and table_schema = '%s' order by index_name, column_name", $table['name'], $table['schema'])
62
        );
63
    }
64
65
    protected function getSchemata()
66
    {
67
        $defaultSchema = $this->driver->getDefaultSchema();
68
        if ($defaultSchema == '') {
69
            $schemata = $this->driver->query(
70
                "select schema_name as name from information_schema.schemata
71
                where schema_name <> 'information_schema' order by schema_name"
72
            );
73
        } else {
74
            $schemata = [
75
                [
76
                    'name' => $defaultSchema,
77
                ],
78
            ];
79
        }
80
81
        return $schemata;
82
    }
83
84
    protected function hasAutoIncrementingKey(&$table)
85
    {
86
        $auto = false;
87
        $found = $this->driver->query(
88
            sprintf(
89
                "select column_name as name
90
                from information_schema.columns
91
                where table_name = '%s' and table_schema='%s' and extra = 'auto_increment'", $table['name'], $table['schema']
92
            )
93
        );
94
95
        if (count($found) > 0) {
96
            $auto = true;
97
        }
98
99
        return $auto;
100
    }
101
}
102