Completed
Push — dev ( 19c9e2...7c72bd )
by James Ekow Abaka
22:29 queued 21:18
created

MysqlDescriptor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.59%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 78
ccs 25
cts 27
cp 0.9259
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getForeignKeys() 0 27 1
A getIndices() 0 8 1
A getSchemata() 0 18 2
A hasAutoIncrementingKey() 0 19 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 3
    protected function getForeignKeys(&$table)
32
    {
33 3
        return $this->driver->query(
34 3
            sprintf(
35 3
                "SELECT
36
                    kcu.constraint_name as name,
37
                    kcu.table_schema as `schema`,
38
                    kcu.table_name as `table`,
39
                    kcu.column_name as `column`,
40
                    kcu.referenced_table_name AS foreign_table,
41
                    kcu.referenced_table_schema AS foreign_schema,
42
                    kcu.referenced_column_name AS foreign_column,
43
                    rc.update_rule as on_update,
44
                    rc.delete_rule as on_delete
45
                FROM
46
                    information_schema.table_constraints AS tc
47
                    JOIN information_schema.key_column_usage AS kcu
48
                      ON tc.constraint_name = kcu.constraint_name and tc.table_schema = kcu.table_schema
49
                    JOIN information_schema.referential_constraints AS rc
50
                      ON rc.constraint_name = tc.constraint_name and rc.constraint_schema = tc.table_schema
51
                WHERE constraint_type = 'FOREIGN KEY'
52
                    AND tc.table_name='%s' AND tc.table_schema='%s' order by kcu.constraint_name, kcu.column_name",
53 3
                $table['name'],
54 3
                $table['schema']
55
            )
56
        );
57
    }
58
59 3
    protected function getIndices(&$table)
60
    {
61 3
        return $this->driver->query(
62 3
            sprintf("SELECT table_name, column_name as `column`,index_name as `name` FROM information_schema.STATISTICS 
63
            WHERE INDEX_NAME not in (SELECT CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE)
64 3
            AND table_name = '%s' and table_schema = '%s' order by index_name, column_name", $table['name'], $table['schema'])
65
        );
66
    }
67
68 2
    protected function getSchemata()
69
    {
70 2
        $defaultSchema = $this->driver->getDefaultSchema();
71 2
        if ($defaultSchema == '') {
72
            $schemata = $this->driver->query(
73
                "select schema_name as name from information_schema.schemata
74
                where schema_name <> 'information_schema' order by schema_name"
75
            );
76
        } else {
77
            $schemata = [
78
                [
79 2
                    'name' => $defaultSchema,
80
                ],
81
            ];
82
        }
83
84 2
        return $schemata;
85
    }
86
87 3
    protected function hasAutoIncrementingKey(&$table)
88
    {
89 3
        $auto = false;
90 3
        $found = $this->driver->query(
91 3
            sprintf(
92 3
                "select column_name as name
93
                from information_schema.columns
94
                where table_name = '%s' and table_schema='%s' and extra = 'auto_increment'",
95 3
                $table['name'],
96 3
                $table['schema']
97
            )
98
        );
99
100 3
        if (count($found) > 0) {
101 2
            $auto = true;
102
        }
103
104 3
        return $auto;
105
    }
106
}
107