Passed
Push — master ( f3b317...5b2bef )
by Felipe
07:25 queued 03:38
created

DomainTrait::createDomain()   C

Complexity

Conditions 11
Paths 96

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 29
nc 96
nop 7

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.41
5
 */
6
7
namespace PHPPgAdmin\Database;
8
9
/**
10
 * Common trait for domains manipulation.
11
 */
12
trait DomainTrait
13
{
14
    /**
15
     * Gets all information for a single domain.
16
     *
17
     * @param string $domain The name of the domain to fetch
18
     *
19
     * @return \PHPPgAdmin\ADORecordSet A recordset
20
     */
21
    public function getDomain($domain)
22
    {
23
        $c_schema = $this->_schema;
24
        $this->clean($c_schema);
25
        $this->clean($domain);
26
27
        $sql = "
28
            SELECT
29
                t.typname AS domname,
30
                pg_catalog.format_type(t.typbasetype, t.typtypmod) AS domtype,
31
                t.typnotnull AS domnotnull,
32
                t.typdefault AS domdef,
33
                pg_catalog.pg_get_userbyid(t.typowner) AS domowner,
34
                pg_catalog.obj_description(t.oid, 'pg_type') AS domcomment
35
            FROM
36
                pg_catalog.pg_type t
37
            WHERE
38
                t.typtype = 'd'
39
                AND t.typname = '{$domain}'
40
                AND t.typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
41
                    WHERE nspname = '{$c_schema}')";
42
43
        return $this->selectSet($sql);
44
    }
45
46
    /**
47
     * Return all domains in current schema.  Excludes domain constraints.
48
     *
49
     * @return \PHPPgAdmin\ADORecordSet All tables, sorted alphabetically
50
     */
51
    public function getDomains()
52
    {
53
        $c_schema = $this->_schema;
54
        $this->clean($c_schema);
55
56
        $sql = "
57
            SELECT
58
                t.typname AS domname,
59
                pg_catalog.format_type(t.typbasetype, t.typtypmod) AS domtype,
60
                t.typnotnull AS domnotnull,
61
                t.typdefault AS domdef,
62
                pg_catalog.pg_get_userbyid(t.typowner) AS domowner,
63
                pg_catalog.obj_description(t.oid, 'pg_type') AS domcomment
64
            FROM
65
                pg_catalog.pg_type t
66
            WHERE
67
                t.typtype = 'd'
68
                AND t.typnamespace = (SELECT oid FROM pg_catalog.pg_namespace
69
                    WHERE nspname='{$c_schema}')
70
            ORDER BY t.typname";
71
72
        return $this->selectSet($sql);
73
    }
74
75
    /**
76
     * Get domain constraints.
77
     *
78
     * @param string $domain The name of the domain whose constraints to fetch
79
     *
80
     * @return \PHPPgAdmin\ADORecordSet A recordset
81
     */
82
    public function getDomainConstraints($domain)
83
    {
84
        $c_schema = $this->_schema;
85
        $this->clean($c_schema);
86
        $this->clean($domain);
87
88
        $sql = "
89
            SELECT
90
                conname,
91
                contype,
92
                pg_catalog.pg_get_constraintdef(oid, true) AS consrc
93
            FROM
94
                pg_catalog.pg_constraint
95
            WHERE
96
                contypid = (
97
                    SELECT oid FROM pg_catalog.pg_type
98
                    WHERE typname='{$domain}'
99
                        AND typnamespace = (
100
                            SELECT oid FROM pg_catalog.pg_namespace
101
                            WHERE nspname = '{$c_schema}')
102
                )
103
            ORDER BY conname";
104
105
        return $this->selectSet($sql);
106
    }
107
108
    /**
109
     * Creates a domain.
110
     *
111
     * @param string $domain  The name of the domain to create
112
     * @param string $type    The base type for the domain
113
     * @param string $length  Optional type length
114
     * @param bool   $array   True for array type, false otherwise
115
     * @param bool   $notnull True for NOT NULL, false otherwise
116
     * @param string $default Default value for domain
117
     * @param string $check   A CHECK constraint if there is one
118
     *
119
     * @return int 0 if operation was successful
120
     */
121
    public function createDomain($domain, $type, $length, $array, $notnull, $default, $check)
122
    {
123
        $f_schema = $this->_schema;
124
        $this->fieldClean($f_schema);
125
        $this->fieldClean($domain);
126
127
        $sql = "CREATE DOMAIN \"{$f_schema}\".\"{$domain}\" AS ";
128
129
        if ($length == '') {
130
            $sql .= $type;
131
        } else {
132
            switch ($type) {
133
                // Have to account for weird placing of length for with/without
134
                // time zone types
135
                case 'timestamp with time zone':
136
                case 'timestamp without time zone':
137
                    $qual = substr($type, 9);
138
                    $sql .= "timestamp({$length}){$qual}";
139
140
                    break;
141
                case 'time with time zone':
142
                case 'time without time zone':
143
                    $qual = substr($type, 4);
144
                    $sql .= "time({$length}){$qual}";
145
146
                    break;
147
                default:
148
                    $sql .= "{$type}({$length})";
149
            }
150
        }
151
152
        // Add array qualifier, if requested
153
        if ($array) {
154
            $sql .= '[]';
155
        }
156
157
        if ($notnull) {
158
            $sql .= ' NOT NULL';
159
        }
160
161
        if ($default != '') {
162
            $sql .= " DEFAULT {$default}";
163
        }
164
165
        if ($this->hasDomainConstraints() && $check != '') {
166
            $sql .= " CHECK ({$check})";
167
        }
168
169
        return $this->execute($sql);
170
    }
171
172
    /**
173
     * Alters a domain.
174
     *
175
     * @param string $domain     The domain to alter
176
     * @param string $domdefault The domain default
177
     * @param bool   $domnotnull True for NOT NULL, false otherwise
178
     * @param string $domowner   The domain owner
179
     *
180
     * @return bool|int 0 success
181
     */
182
    public function alterDomain($domain, $domdefault, $domnotnull, $domowner)
183
    {
184
        $f_schema = $this->_schema;
185
        $this->fieldClean($f_schema);
186
        $this->fieldClean($domain);
187
        $this->fieldClean($domowner);
188
189
        $status = $this->beginTransaction();
190
        if ($status != 0) {
191
            $this->rollbackTransaction();
192
193
            return -1;
194
        }
195
196
        // Default
197
        if ($domdefault == '') {
198
            $sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" DROP DEFAULT";
199
        } else {
200
            $sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" SET DEFAULT {$domdefault}";
201
        }
202
203
        $status = $this->execute($sql);
204
        if ($status != 0) {
205
            $this->rollbackTransaction();
206
207
            return -2;
208
        }
209
210
        // NOT NULL
211
        if ($domnotnull) {
212
            $sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" SET NOT NULL";
213
        } else {
214
            $sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" DROP NOT NULL";
215
        }
216
217
        $status = $this->execute($sql);
218
        if ($status != 0) {
219
            $this->rollbackTransaction();
220
221
            return -3;
222
        }
223
224
        // Owner
225
        $sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" OWNER TO \"{$domowner}\"";
226
227
        $status = $this->execute($sql);
228
        if ($status != 0) {
229
            $this->rollbackTransaction();
230
231
            return -4;
232
        }
233
234
        return $this->endTransaction();
235
    }
236
237
    /**
238
     * Drops a domain.
239
     *
240
     * @param string $domain  The name of the domain to drop
241
     * @param string $cascade True to cascade drop, false to restrict
242
     *
243
     * @return int 0 if operation was successful
244
     */
245
    public function dropDomain($domain, $cascade)
246
    {
247
        $f_schema = $this->_schema;
248
        $this->fieldClean($f_schema);
249
        $this->fieldClean($domain);
250
251
        $sql = "DROP DOMAIN \"{$f_schema}\".\"{$domain}\"";
252
        if ($cascade) {
253
            $sql .= ' CASCADE';
254
        }
255
256
        return $this->execute($sql);
257
    }
258
259
    /**
260
     * Adds a check constraint to a domain.
261
     *
262
     * @param string $domain     The domain to which to add the check
263
     * @param string $definition The definition of the check
264
     * @param string $name       (optional) The name to give the check, otherwise default name is assigned
265
     *
266
     * @return int 0 if operation was successful
267
     */
268
    public function addDomainCheckConstraint($domain, $definition, $name = '')
269
    {
270
        $f_schema = $this->_schema;
271
        $this->fieldClean($f_schema);
272
        $this->fieldClean($domain);
273
        $this->fieldClean($name);
274
275
        $sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" ADD ";
276
        if ($name != '') {
277
            $sql .= "CONSTRAINT \"{$name}\" ";
278
        }
279
280
        $sql .= "CHECK ({$definition})";
281
282
        return $this->execute($sql);
283
    }
284
285
    /**
286
     * Drops a domain constraint.
287
     *
288
     * @param string $domain     The domain from which to remove the constraint
289
     * @param string $constraint The constraint to remove
290
     * @param bool   $cascade    True to cascade, false otherwise
291
     *
292
     * @return int 0 if operation was successful
293
     */
294
    public function dropDomainConstraint($domain, $constraint, $cascade)
295
    {
296
        $f_schema = $this->_schema;
297
        $this->fieldClean($f_schema);
298
        $this->fieldClean($domain);
299
        $this->fieldClean($constraint);
300
301
        $sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" DROP CONSTRAINT \"{$constraint}\"";
302
        if ($cascade) {
303
            $sql .= ' CASCADE';
304
        }
305
306
        return $this->execute($sql);
307
    }
308
309
    abstract public function fieldClean(&$str);
310
311
    abstract public function beginTransaction();
312
313
    abstract public function rollbackTransaction();
314
315
    abstract public function endTransaction();
316
317
    abstract public function execute($sql);
318
319
    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);
320
321
    abstract public function selectSet($sql);
322
323
    abstract public function clean(&$str);
324
325
    abstract public function hasDomainConstraints();
326
}
327