Adapter   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 229
rs 9.76
c 0
b 0
f 0
wmc 33

19 Methods

Rating   Name   Duplication   Size   Complexity  
A sqlLimitOffset() 0 15 4
A sqlRenameColumn() 0 3 1
A sqlDropPrimaryKey() 0 3 1
A sqlRenameTable() 0 3 1
A sqlDropTable() 0 3 2
A sqlAlterColumn() 0 9 2
A getBoolean() 0 3 2
A sqlTruncateTable() 0 3 2
A quoteSimpleTableName() 0 3 2
A getLookupCollection() 0 3 1
A sqlResetSequence() 0 3 1
A sqlCheckIntegrity() 0 11 3
A sqlDropIndex() 0 3 1
A getRandomOrder() 0 3 1
A formatDateTime() 0 15 5
A getDate() 0 3 1
A sqlDropForeignKey() 0 3 1
A getDateTime() 0 3 1
A sqlAddColumn() 0 3 1
1
<?php
2
3
namespace Tsukasa\QueryBuilder\Database\Pgsql;
4
5
use Exception;
6
use Tsukasa\QueryBuilder\BaseAdapter;
7
use Tsukasa\QueryBuilder\Interfaces\IAdapter;
8
9
class Adapter extends BaseAdapter implements IAdapter
10
{
11
    /**
12
     * @param $tableName
13
     * @param bool $ifExists
14
     * @param bool $cascade
15
     * @return string
16
     */
17
    public function sqlDropTable($tableName, $ifExists = false, $cascade = false)
18
    {
19
        return parent::sqlDropTable($tableName, $ifExists, $cascade) . ($cascade ? ' CASCADE' : '');
20
    }
21
22
    /**
23
     * @param $tableName
24
     * @param bool $cascade
25
     * @return string
26
     */
27
    public function sqlTruncateTable($tableName, $cascade = false)
28
    {
29
        return parent::sqlTruncateTable($tableName, $cascade) . ($cascade ? ' CASCADE' : '');
30
    }
31
32
    /**
33
     * Creates a SQL statement for resetting the sequence value of a table's primary key.
34
     * The sequence will be reset such that the primary key of the next new row inserted
35
     * will have the specified value or 1.
36
     * @param string $sequenceName the name of the table whose primary key sequence will be reset
37
     * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
38
     * the next new row's primary key will have a value 1.
39
     * @return string the SQL statement for resetting sequence
40
     * @throws Exception if the table does not exist or there is no sequence associated with the table.
41
     */
42
    public function sqlResetSequence($sequenceName, $value)
43
    {
44
        return "SELECT SETVAL('" . $sequenceName . "', " . $this->quoteValue($value) . ',false)';
45
    }
46
47
    /**
48
     * @param $limit
49
     * @param null $offset
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $offset is correct as it would always require null to be passed?
Loading history...
50
     * @return mixed
51
     */
52
    public function sqlLimitOffset($limit = null, $offset = null)
53
    {
54
        if ($this->hasLimit($limit)) {
55
            $sql = 'LIMIT ' . $limit;
56
            if ($this->hasOffset($offset)) {
57
                $sql .= ' OFFSET ' . $offset;
58
            }
59
            return ' ' . $sql;
60
        }
61
62
        if ($this->hasOffset($offset)) {
63
            return ' LIMIT ALL OFFSET ' . $offset;
64
        }
65
66
        return '';
67
    }
68
    
69
    /**
70
     * Builds a SQL statement for enabling or disabling integrity check.
71
     * @param boolean $check whether to turn on or off the integrity check.
72
     * @param string $schema the schema of the tables.
73
     * @param string $table the table name.
74
     * @return string the SQL statement for checking integrity
75
     */
76
    public function sqlCheckIntegrity($check = true, $schema = '', $table = '')
77
    {
78
        $enable = $check ? 'ENABLE' : 'DISABLE';
79
        $tableNames = [$table];
80
        $sql = '';
81
        foreach ($tableNames as $tableName) {
82
            $tableName = '"' . $schema . '"."' . $tableName . '"';
83
            // ALL or USER
84
            $sql .= "ALTER TABLE $tableName $enable TRIGGER ALL; ";
85
        }
86
        return $sql;
87
    }
88
89
    /**
90
     * Builds a SQL statement for changing the definition of a column.
91
     * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
92
     * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
93
     * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract
94
     * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept
95
     * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null'
96
     * will become 'varchar(255) not null'. You can also use PostgreSQL-specific syntax such as `SET NOT NULL`.
97
     * @return string the SQL statement for changing the definition of a column.
98
     */
99
    public function sqlAlterColumn($table, $column, $type)
100
    {
101
        // https://github.com/yiisoft/yii2/issues/4492
102
        // http://www.postgresql.org/docs/9.1/static/sql-altertable.html
103
        if (!preg_match('/^(DROP|SET|RESET)\s+/i', $type)) {
104
            $type = 'TYPE ' . $type;
105
        }
106
        return 'ALTER TABLE ' . $this->quoteTableName($table) . ' ALTER COLUMN '
107
        . $this->quoteColumn($column) . ' ' . $type;
108
    }
109
110
    /**
111
     * @return LookupCollection
112
     */
113
    public function getLookupCollection()
114
    {
115
        return new LookupCollection();
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getRandomOrder()
122
    {
123
        return 'RANDOM()';
124
    }
125
126
    /**
127
     * Quotes a table name for use in a query.
128
     * A simple table name has no schema prefix.
129
     * @param string $name table name
130
     * @return string the properly quoted table name
131
     */
132
    public function quoteSimpleTableName($name)
133
    {
134
        return strpos($name, '"') !== false ? $name : '"' . $name . '"';
135
    }
136
137
    /**
138
     * @param $oldTableName
139
     * @param $newTableName
140
     * @return string
141
     */
142
    public function sqlRenameTable($oldTableName, $newTableName)
143
    {
144
        return 'ALTER TABLE ' . $this->quoteTableName($oldTableName) . ' RENAME TO ' . $this->quoteTableName($newTableName);
145
    }
146
147
    /**
148
     * @param $tableName
149
     * @param $name
150
     * @return string
151
     */
152
    public function sqlDropIndex($tableName, $name)
153
    {
154
        return 'DROP INDEX ' . $this->quoteColumn($name);
155
    }
156
157
    /**
158
     * @param string $tableName
159
     * @param string $name
160
     * @return string
161
     */
162
    public function sqlDropPrimaryKey($tableName, $name)
163
    {
164
        return 'ALTER TABLE ' . $this->quoteTableName($tableName) . ' DROP CONSTRAINT ' . $this->quoteColumn($name);
165
    }
166
167
    /**
168
     * @param $tableName
169
     * @param $oldName
170
     * @param $newName
171
     * @return mixed
172
     */
173
    public function sqlRenameColumn($tableName, $oldName, $newName)
174
    {
175
        return "ALTER TABLE {$this->quoteTableName($tableName)} RENAME COLUMN " . $this->quoteColumn($oldName) . ' TO ' . $this->quoteColumn($newName);
176
    }
177
178
    /**
179
     * @param $value
180
     * @return string
181
     */
182
    public function getBoolean($value = null)
183
    {
184
        return (bool)$value ? 'TRUE' : 'FALSE';
185
    }
186
187
    /**
188
     * @param $value
189
     * @param $format
190
     * @return string
191
     */
192
    protected function formatDateTime($value, $format)
193
    {
194
        if ($value instanceof \DateTime) {
195
            $value = $value->format($format);
196
        }
197
        elseif ($value === null) {
198
            $value = date($format);
199
        }
200
        elseif (is_numeric($value)) {
201
            $value = date($format, $value);
202
        }
203
        elseif (is_string($value)) {
204
            $value = date($format, strtotime($value));
205
        }
206
        return $value;
207
    }
208
209
    public function getDateTime($value = null)
210
    {
211
        return $this->formatDateTime($value, 'Y-m-d H:i:s');
212
    }
213
214
    public function getDate($value = null)
215
    {
216
        return $this->formatDateTime($value, 'Y-m-d');
217
    }
218
219
    /**
220
     * @param $tableName
221
     * @param $column
222
     * @param $type
223
     * @return string
224
     */
225
    public function sqlAddColumn($tableName, $column, $type)
226
    {
227
        return 'ALTER TABLE ' . $this->quoteTableName($tableName) . ' ADD ' . $this->quoteColumn($column) . ' ' . $type;
228
    }
229
230
    /**
231
     * @param $tableName
232
     * @param $name
233
     * @return mixed
234
     */
235
    public function sqlDropForeignKey($tableName, $name)
236
    {
237
        return 'ALTER TABLE ' . $this->quoteTableName($tableName) . ' DROP CONSTRAINT ' . $this->quoteColumn($name);
238
    }
239
}