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

PostgresqlDescriptor::getSchemata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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