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

TablespaceTrait::getTablespaces()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.47
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
/**
10
 * Common trait for tablespaces manipulation.
11
 */
12
trait TablespaceTrait
13
{
14
    /**
15
     * Retrieves information for all tablespaces.
16
     *
17
     * @param bool $all Include all tablespaces (necessary when moving objects back to the default space)
18
     *
19
     * @return \PHPPgAdmin\ADORecordSet A recordset
20
     */
21
    public function getTablespaces($all = false)
22
    {
23
        $conf = $this->conf;
24
25
        $sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, pg_catalog.pg_tablespace_location(oid) as spclocation,
26
                    (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pg_tablespace.oid=pd.objoid AND pd.classoid='pg_tablespace'::regclass) AS spccomment
27
                    FROM pg_catalog.pg_tablespace";
28
29
        if (!$conf['show_system'] && !$all) {
30
            $sql .= ' WHERE spcname NOT LIKE $$pg\_%$$';
31
        }
32
33
        $sql .= ' ORDER BY spcname';
34
35
        return $this->selectSet($sql);
36
    }
37
38
    // Misc functions
39
40
    /**
41
     * Retrieves a tablespace's information.
42
     *
43
     * @param string $spcname
44
     *
45
     * @return \PHPPgAdmin\ADORecordSet A recordset
46
     */
47
    public function getTablespace($spcname)
48
    {
49
        $this->clean($spcname);
50
51
        $sql = "SELECT spcname, pg_catalog.pg_get_userbyid(spcowner) AS spcowner, pg_catalog.pg_tablespace_location(oid) as spclocation,
52
                    (SELECT description FROM pg_catalog.pg_shdescription pd WHERE pg_tablespace.oid=pd.objoid AND pd.classoid='pg_tablespace'::regclass) AS spccomment
53
                    FROM pg_catalog.pg_tablespace WHERE spcname='{$spcname}'";
54
55
        return $this->selectSet($sql);
56
    }
57
58
    /**
59
     * Creates a tablespace.
60
     *
61
     * @param string $spcname  The name of the tablespace to create
62
     * @param string $spcowner The owner of the tablespace. '' for current
63
     * @param string $spcloc   The directory in which to create the tablespace
64
     * @param string $comment
65
     *
66
     * @return int 0 success
67
     */
68
    public function createTablespace($spcname, $spcowner, $spcloc, $comment = '')
69
    {
70
        $this->fieldClean($spcname);
71
        $this->clean($spcloc);
72
73
        $sql = "CREATE TABLESPACE \"{$spcname}\"";
74
75
        if ($spcowner != '') {
76
            $this->fieldClean($spcowner);
77
            $sql .= " OWNER \"{$spcowner}\"";
78
        }
79
80
        $sql .= " LOCATION '{$spcloc}'";
81
82
        $status = $this->execute($sql);
83
        if ($status != 0) {
84
            return -1;
85
        }
86
87
        if ($comment != '' && $this->hasSharedComments()) {
0 ignored issues
show
Bug introduced by
It seems like hasSharedComments() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        if ($comment != '' && $this->/** @scrutinizer ignore-call */ hasSharedComments()) {
Loading history...
88
            $status = $this->setComment('TABLESPACE', $spcname, '', $comment);
89
            if ($status != 0) {
90
                return -2;
91
            }
92
        }
93
94
        return 0;
95
    }
96
97
    /**
98
     * Alters a tablespace.
99
     *
100
     * @param string $spcname The name of the tablespace
101
     * @param string $name    The new name for the tablespace
102
     * @param string $owner   The new owner for the tablespace
103
     * @param string $comment
104
     *
105
     * @return bool|int 0 success
106
     */
107
    public function alterTablespace($spcname, $name, $owner, $comment = '')
108
    {
109
        $this->fieldClean($spcname);
110
        $this->fieldClean($name);
111
        $this->fieldClean($owner);
112
113
        // Begin transaction
114
        $status = $this->beginTransaction();
115
        if ($status != 0) {
116
            return -1;
117
        }
118
119
        // Owner
120
        $sql    = "ALTER TABLESPACE \"{$spcname}\" OWNER TO \"{$owner}\"";
121
        $status = $this->execute($sql);
122
        if ($status != 0) {
123
            $this->rollbackTransaction();
124
125
            return -2;
126
        }
127
128
        // Rename (only if name has changed)
129
        if ($name != $spcname) {
130
            $sql    = "ALTER TABLESPACE \"{$spcname}\" RENAME TO \"{$name}\"";
131
            $status = $this->execute($sql);
132
            if ($status != 0) {
133
                $this->rollbackTransaction();
134
135
                return -3;
136
            }
137
138
            $spcname = $name;
139
        }
140
141
        // Set comment if it has changed
142
        if (trim($comment) != '' && $this->hasSharedComments()) {
143
            $status = $this->setComment('TABLESPACE', $spcname, '', $comment);
144
            if ($status != 0) {
145
                return -4;
146
            }
147
        }
148
149
        return $this->endTransaction();
150
    }
151
152
    /**
153
     * Drops a tablespace.
154
     *
155
     * @param string $spcname The name of the domain to drop
156
     *
157
     * @return int 0 if operation was successful
158
     */
159
    public function dropTablespace($spcname)
160
    {
161
        $this->fieldClean($spcname);
162
163
        $sql = "DROP TABLESPACE \"{$spcname}\"";
164
165
        return $this->execute($sql);
166
    }
167
168
    abstract public function fieldClean(&$str);
169
170
    abstract public function beginTransaction();
171
172
    abstract public function rollbackTransaction();
173
174
    abstract public function endTransaction();
175
176
    abstract public function execute($sql);
177
178
    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);
179
180
    abstract public function selectSet($sql);
181
182
    abstract public function clean(&$str);
183
184
    abstract public function phpBool($parameter);
185
186
    abstract public function hasCreateTableLikeWithConstraints();
187
188
    abstract public function hasCreateTableLikeWithIndexes();
189
190
    abstract public function hasTablespaces();
191
192
    abstract public function delete($table, $conditions, $schema = '');
193
194
    abstract public function fieldArrayClean(&$arr);
195
196
    abstract public function hasCreateFieldWithConstraints();
197
198
    abstract public function getAttributeNames($table, $atts);
199
}
200