Completed
Push — dev ( bd1d19...3dc717 )
by James Ekow Abaka
02:49
created

MysqlDescriptor::getForeignKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 4
cts 4
cp 1
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2014 ekow.
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
32 3
    protected function getForeignKeys(&$table)
33
    {
34 3
        return $this->driver->query(
35 3
            sprintf("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 3
                    AND tc.table_name='%s' AND tc.table_schema='%s' order by kcu.constraint_name, kcu.column_name", $table['name'], $table['schema']
53
            )
54
        );
55
    }
56
57 3
    protected function getIndices(&$table)
58
    {
59 3
        return $this->driver->query(
60 3
            sprintf("SELECT table_name, column_name as `column`,index_name as `name` FROM information_schema.STATISTICS 
61
            WHERE INDEX_NAME not in (SELECT CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE)
62 3
            AND table_name = '%s' and table_schema = '%s' order by index_name, column_name", $table['name'], $table['schema'])
63
        );
64
    }
65
66 2
    protected function getSchemata()
67
    {
68 2
        $defaultSchema = $this->driver->getDefaultSchema();
69 2
        if ($defaultSchema == '') {
70
            $schemata = $this->driver->query(
71
                "select schema_name as name from information_schema.schemata
72
                where schema_name <> 'information_schema' order by schema_name"
73
            );
74
        } else {
75
            $schemata = array(
76
                array(
77 2
                    'name' => $defaultSchema
78
                )
79
            );
80
        }
81 2
        return $schemata;
82
    }
83
84 3
    protected function hasAutoIncrementingKey(&$table)
85
    {
86 3
        $auto = false;
87 3
        $found = $this->driver->query(
88 3
            sprintf(
89 3
                "select column_name as name
90
                from information_schema.columns
91 3
                where table_name = '%s' and table_schema='%s' and extra = 'auto_increment'", $table['name'], $table['schema']
92
            )
93
        );
94
95 3
        if (count($found) > 0) {
96 2
            $auto = true;
97
        }
98
99 3
        return $auto;
100
    }
101
}
102