Completed
Pull Request — master (#4)
by James Ekow Abaka
02:32 queued 01:18
created

PostgresqlDescriptor::hasAutoIncrementingKey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 8
cp 0
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 20
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 PostgresqlDescriptor extends InformationSchemaDescriptor
30
{
31
    protected function cleanDefaultValue($defaultValue)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
32
    {
33
        // Deal with typecasts
34
        if (preg_match("/(?<value>.*)(::)(?<type>[a-zA-Z0-9\s]*)$/", $defaultValue, $matches)) {
35
            $value = $matches['value'];
36
37
            // If numeric
38
            if (is_numeric($value)) {
39
                return $value;
40
41
            // If its a string
42
            } elseif (preg_match("/'(?<string>.*)'/", $value, $matches)) {
43
                return $matches['string'];
44
45
            // null for anything else
46
            } else {
47
                return;
48
            }
49
50
            // Return the nextval as atiaa uses them to detect auto keys
51
        } elseif (preg_match("/nextval\(.*/", $defaultValue)) {
52
            return $defaultValue;
53
54
        // Return numeric default values
55
        } elseif (is_numeric($defaultValue)) {
56
            return $defaultValue;
57
58
        // Return null for anything else
59
        } else {
60
            return;
61
        }
62
    }
63
64
    /**
65
     * @note Query sourced from http://stackoverflow.com/questions/2204058/show-which-columns-an-index-is-on-in-postgresql
66
     *
67
     * @param type $table
68
     *
69
     * @return type
70
     */
71
    protected function getIndices(&$table)
72
    {
73
        return $this->driver->query(
74
            sprintf("select
75
                        t.relname as table_name,
76
                        i.relname as name,
77
                        a.attname as column
78
                    from
79
                        pg_class t,
80
                        pg_class i,
81
                        pg_index ix,
82
                        pg_attribute a,
83
                        pg_namespace n
84
                    where
85
                        t.oid = ix.indrelid
86
                        and i.oid = ix.indexrelid
87
                        and a.attrelid = t.oid
88
                        and a.attnum = ANY(ix.indkey)
89
                        and t.relkind = 'r'
90
                        and t.relname = '%s'
91
                        and n.nspname = '%s'
92
                        and i.relnamespace = n.oid
93
                            AND indisunique != 't'
94
                            AND indisprimary != 't'
95
                        order by i.relname, a.attname",
96
            $table['name'], $table['schema'])
97
        );
98
    }
99
100
    /**
101
     * @note Query sourced from http://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys
102
     *
103
     * @param type $table
104
     */
105
    protected function getForeignKeys(&$table)
106
    {
107
        return $this->driver->query(
108
            "SELECT distinct
109
                kcu.constraint_name as name,
110
                kcu.table_schema as schema,
111
                kcu.table_name as table, 
112
                kcu.column_name as column, 
113
                ccu.table_name AS foreign_table,
114
                ccu.table_schema AS foreign_schema,
115
                ccu.column_name AS foreign_column,
116
                rc.update_rule as on_update,
117
                rc.delete_rule as on_delete
118
119
            FROM 
120
                information_schema.table_constraints AS tc 
121
                JOIN information_schema.key_column_usage AS kcu
122
                  ON tc.constraint_name = kcu.constraint_name and tc.table_schema = kcu.table_schema and tc.table_name = kcu.table_name 
123
                JOIN information_schema.constraint_column_usage AS ccu
124
                  ON ccu.constraint_name = tc.constraint_name   and ccu.constraint_schema = tc.table_schema
125
                JOIN information_schema.referential_constraints AS rc
126
                  ON rc.constraint_name = tc.constraint_name and rc.constraint_schema = tc.table_schema
127
            WHERE constraint_type = 'FOREIGN KEY' 
128
                AND tc.table_name=:name AND tc.table_schema=:schema
129
                AND kcu.table_name=:name AND kcu.table_schema=:schema
130
                order by kcu.constraint_name, kcu.column_name",
131
                //$table['name'], $table['schema']
132
                ['name'=>$table['name'], 'schema'=>$table['schema']]
133
        );
134
    }
135
136
    public function getSchemata()
137
    {
138
        return $this->driver->query(
139
            "select schema_name as name from information_schema.schemata 
140
            where schema_name not like 'pg_temp%' and 
141
            schema_name not like 'pg_toast%' and 
142
            schema_name not in ('pg_catalog', 'information_schema')
143
            order by schema_name"
144
        );
145
    }
146
147
    protected function hasAutoIncrementingKey(&$table)
148
    {
149
        $auto = false;
150
        $primaryKey = reset($table['primary_key']);
151
        if (is_array($primaryKey)) {
152
            if (count($primaryKey) == 1 && substr_count($table['columns'][$primaryKey['columns'][0]]['default'], 'nextval')) {
153
                $table['columns'][$primaryKey['columns'][0]]['default'] = null;
154
                $auto = true;
155
            }
156
        }
157
158
        return $auto;
159
    }
160
}
161