Passed
Push — develop ( 2d5edd...a33225 )
by Felipe
05:40
created

OperatorTrait::dropOperator()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 8
nop 2
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.47
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
/**
10
 * Common trait for operators manipulation.
11
 */
12
trait OperatorTrait
13
{
14
    /**
15
     * Returns a list of all operators in the database.
16
     *
17
     * @return \PHPPgAdmin\ADORecordSet All operators
18
     */
19
    public function getOperators()
20
    {
21
        $c_schema = $this->_schema;
22
        $this->clean($c_schema);
23
        // We stick with the subselects here, as you cannot ORDER BY a regtype
24
        $sql = "
25
            SELECT
26
                po.oid, po.oprname,
27
                (SELECT pg_catalog.format_type(oid, NULL) FROM pg_catalog.pg_type pt WHERE pt.oid=po.oprleft) AS oprleftname,
28
                (SELECT pg_catalog.format_type(oid, NULL) FROM pg_catalog.pg_type pt WHERE pt.oid=po.oprright) AS oprrightname,
29
                po.oprresult::pg_catalog.regtype AS resultname,
30
                pg_catalog.obj_description(po.oid, 'pg_operator') AS oprcomment
31
            FROM
32
                pg_catalog.pg_operator po
33
            WHERE
34
                po.oprnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname='{$c_schema}')
35
            ORDER BY
36
                po.oprname, oprleftname, oprrightname
37
        ";
38
39
        return $this->selectSet($sql);
40
    }
41
42
    /**
43
     * Drops an operator.
44
     *
45
     * @param mixed $operator_oid The OID of the operator to drop
46
     * @param bool  $cascade      True to cascade drop, false to restrict
47
     *
48
     * @return int 0 if operation was successful
49
     */
50
    public function dropOperator($operator_oid, $cascade)
51
    {
52
        // Function comes in with $object as operator OID
53
        $opr      = $this->getOperator($operator_oid);
54
        $f_schema = $this->_schema;
55
        $this->fieldClean($f_schema);
56
        $this->fieldClean($opr->fields['oprname']);
57
58
        $sql = "DROP OPERATOR \"{$f_schema}\".{$opr->fields['oprname']} (";
59
        // Quoting or formatting here???
60
        if ($opr->fields['oprleftname'] !== null) {
61
            $sql .= $opr->fields['oprleftname'].', ';
62
        } else {
63
            $sql .= 'NONE, ';
64
        }
65
66
        if ($opr->fields['oprrightname'] !== null) {
67
            $sql .= $opr->fields['oprrightname'].')';
68
        } else {
69
            $sql .= 'NONE)';
70
        }
71
72
        if ($cascade) {
73
            $sql .= ' CASCADE';
74
        }
75
76
        return $this->execute($sql);
77
    }
78
79
    /**
80
     * Returns all details for a particular operator.
81
     *
82
     * @param mixed $operator_oid The oid of the operator
83
     *
84
     * @return \PHPPgAdmin\ADORecordSet Function info
85
     */
86
    public function getOperator($operator_oid)
87
    {
88
        $this->clean($operator_oid);
89
90
        $sql = "
91
            SELECT
92
                po.oid, po.oprname,
93
                oprleft::pg_catalog.regtype AS oprleftname,
94
                oprright::pg_catalog.regtype AS oprrightname,
95
                oprresult::pg_catalog.regtype AS resultname,
96
                po.oprcanhash,
97
                oprcanmerge,
98
                oprcom::pg_catalog.regoperator AS oprcom,
99
                oprnegate::pg_catalog.regoperator AS oprnegate,
100
                po.oprcode::pg_catalog.regproc AS oprcode,
101
                po.oprrest::pg_catalog.regproc AS oprrest,
102
                po.oprjoin::pg_catalog.regproc AS oprjoin
103
            FROM
104
                pg_catalog.pg_operator po
105
            WHERE
106
                po.oid='{$operator_oid}'
107
        ";
108
109
        return $this->selectSet($sql);
110
    }
111
112
    /**
113
     *  Gets all opclasses.
114
     *
115
     * @return \PHPPgAdmin\ADORecordSet A recordset
116
     */
117
    public function getOpClasses()
118
    {
119
        $c_schema = $this->_schema;
120
        $this->clean($c_schema);
121
        $sql = "
122
            SELECT
123
                pa.amname, po.opcname,
124
                po.opcintype::pg_catalog.regtype AS opcintype,
125
                po.opcdefault,
126
                pg_catalog.obj_description(po.oid, 'pg_opclass') AS opccomment
127
            FROM
128
                pg_catalog.pg_opclass po, pg_catalog.pg_am pa, pg_catalog.pg_namespace pn
129
            WHERE
130
                po.opcmethod=pa.oid
131
                AND po.opcnamespace=pn.oid
132
                AND pn.nspname='{$c_schema}'
133
            ORDER BY 1,2
134
            ";
135
136
        return $this->selectSet($sql);
137
    }
138
139
    abstract public function fieldClean(&$str);
140
141
    abstract public function beginTransaction();
142
143
    abstract public function rollbackTransaction();
144
145
    abstract public function endTransaction();
146
147
    abstract public function execute($sql);
148
149
    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);
150
151
    abstract public function selectSet($sql);
152
153
    abstract public function clean(&$str);
154
155
    abstract public function phpBool($parameter);
156
157
    abstract public function hasCreateTableLikeWithConstraints();
158
159
    abstract public function hasCreateTableLikeWithIndexes();
160
161
    abstract public function hasTablespaces();
162
163
    abstract public function delete($table, $conditions, $schema = '');
164
165
    abstract public function fieldArrayClean(&$arr);
166
167
    abstract public function hasCreateFieldWithConstraints();
168
169
    abstract public function getAttributeNames($table, $atts);
170
}
171