Adapter::sqlCheckIntegrity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Tsukasa\QueryBuilder\Database\Mysql;
3
4
use Exception;
5
use Tsukasa\QueryBuilder\BaseAdapter;
6
use Tsukasa\QueryBuilder\Interfaces\IAdapter;
7
use Tsukasa\QueryBuilder\Interfaces\ISQLGenerator;
8
9
class Adapter extends BaseAdapter implements IAdapter, ISQLGenerator
10
{
11
    /**
12
     * Quotes a table name for use in a query.
13
     * A simple table name has no schema prefix.
14
     * @param string $name table name
15
     * @return string the properly quoted table name
16
     */
17
    public function quoteSimpleTableName($name)
18
    {
19
        return strpos($name, "`") !== false ? $name : "`" . $name . "`";
20
    }
21
22
    /**
23
     * Quotes a column name for use in a query.
24
     * A simple column name has no prefix.
25
     * @param string $name column name
26
     * @return string the properly quoted column name
27
     */
28
    public function quoteSimpleColumnName($name)
29
    {
30
        return strpos($name, '`') !== false || $name === '*' ? $name : '`' . $name . '`';
31
    }
32
33
    public function getLookupCollection()
34
    {
35
        return new LookupCollection();
36
    }
37
38
    public function getRandomOrder()
39
    {
40
        return 'RAND()';
41
    }
42
43
    /**
44
     * @param $oldTableName
45
     * @param $newTableName
46
     * @return string
47
     */
48
    public function sqlRenameTable($oldTableName, $newTableName)
49
    {
50
        return 'RENAME TABLE ' . $this->quoteTableName($oldTableName) . ' TO ' . $this->quoteTableName($newTableName);
51
    }
52
53
    /**
54
     * Builds a SQL statement for removing a primary key constraint to an existing table.
55
     * @param string $name the name of the primary key constraint to be removed.
56
     * @param string $table the table that the primary key constraint will be removed from.
57
     * @return string the SQL statement for removing a primary key constraint from an existing table.
58
     */
59
    public function sqlDropPrimaryKey($table, $name)
60
    {
61
        return 'ALTER TABLE ' . $this->quoteTableName($table) . ' DROP PRIMARY KEY';
62
    }
63
64
    /**
65
     * @param $tableName
66
     * @param $name
67
     * @return string
68
     */
69
    public function sqlDropIndex($tableName, $name)
70
    {
71
        return 'DROP INDEX ' . $this->quoteColumn($name) . ' ON ' . $this->quoteTableName($tableName);
72
    }
73
74
    /**
75
     * @param $tableName
76
     * @param $name
77
     * @return mixed
78
     */
79
    public function sqlDropForeignKey($tableName, $name)
80
    {
81
        return 'ALTER TABLE ' . $this->quoteTableName($tableName) . ' DROP FOREIGN KEY ' . $this->quoteColumn($name);
82
    }
83
84
    /**
85
     * @param $value
86
     * @return string
87
     */
88
    public function getBoolean($value = null)
89
    {
90
        if (is_bool($value)) {
91
            return (int)$value;
92
        }
93
94
        return $value ? 1 : 0;
95
    }
96
97
    protected function formatDateTime($value, $format)
98
    {
99
        if ($value instanceof \DateTime) {
100
            $value = $value->format($format);
101
        }
102
        elseif ($value === null) {
103
            $value = date($format);
104
        }
105
        elseif (is_numeric($value)) {
106
            $value = date($format, $value);
107
        }
108
        elseif (is_string($value)) {
109
            $value = date($format, strtotime($value));
110
        }
111
        return $value;
112
    }
113
114
    public function getDateTime($value = null)
115
    {
116
        return $this->formatDateTime($value, "Y-m-d H:i:s");
117
    }
118
119
    public function getDate($value = null)
120
    {
121
        return $this->formatDateTime($value, "Y-m-d");
122
    }
123
124
    /**
125
     * @param $tableName
126
     * @param $column
127
     * @param $type
128
     * @return string
129
     */
130
    public function sqlAddColumn($tableName, $column, $type)
131
    {
132
        return 'ALTER TABLE ' . $this->quoteTableName($tableName) . ' ADD ' . $this->quoteColumn($column) . ' ' . $type;
133
    }
134
135
    /**
136
     * @param $tableName
137
     * @param $value
138
     * @return string
139
     * @internal param $sequenceName
140
     */
141
    public function sqlResetSequence($tableName, $value)
142
    {
143
        return 'ALTER TABLE ' . $this->quoteTableName($tableName) . ' AUTO_INCREMENT=' . $this->quoteValue($value);
144
    }
145
146
    /**
147
     * @param bool $check
148
     * @param string $schema
149
     * @param string $table
150
     * @return string
151
     */
152
    public function sqlCheckIntegrity($check = true, $schema = '', $table = '')
153
    {
154
        return 'SET FOREIGN_KEY_CHECKS = ' . $this->getBoolean($check);
155
    }
156
157
    public function sqlLimitOffset($limit = null, $offset = null)
158
    {
159
        if ($this->hasLimit($limit)) {
160
            $sql = 'LIMIT ' . $limit;
161
            if ($this->hasOffset($offset)) {
162
                $sql .= ' OFFSET ' . $offset;
163
            }
164
            return ' ' . $sql;
165
        }
166
167
        if ($this->hasOffset($offset)) {
168
            // limit is not optional in MySQL
169
            // http://stackoverflow.com/a/271650/1106908
170
            // http://dev.mysql.com/doc/refman/5.0/en/select.html#idm47619502796240
171
            return ' LIMIT ' . $offset . ', 18446744073709551615'; // 2^64-1
172
        }
173
174
        return '';
175
    }
176
177
    /**
178
     * @param $tableName
179
     * @param $oldName
180
     * @param $newName
181
     * @return string
182
     * @throws Exception
183
     */
184
    public function sqlRenameColumn($tableName, $oldName, $newName)
185
    {
186
        $quotedTable = $this->quoteTableName($tableName);
187
        $row = $this->driver->query('SHOW CREATE TABLE ' . $quotedTable)->fetch();
0 ignored issues
show
Bug introduced by
The method query() does not exist on null. ( Ignorable by Annotation )

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

187
        $row = $this->driver->/** @scrutinizer ignore-call */ query('SHOW CREATE TABLE ' . $quotedTable)->fetch();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
188
        if ($row === false) {
189
            throw new Exception("Unable to find column '$oldName' in table '$tableName'.");
190
        }
191
        if (isset($row['Create Table'])) {
192
            $sql = $row['Create Table'];
193
        }
194
        else {
195
            $row = array_values($row);
196
            $sql = $row[1];
197
        }
198
        if (preg_match_all('/^\s*`(.*?)`\s+(.*?),?$/m', $sql, $matches)) {
199
            foreach ($matches[1] as $i => $c) {
200
                if ($c === $oldName) {
201
                    return "ALTER TABLE {$quotedTable} CHANGE "
202
                    . $this->quoteColumn($oldName) . ' '
203
                    . $this->quoteColumn($newName) . ' '
204
                    . $matches[2][$i];
205
                }
206
            }
207
        }
208
209
        return "ALTER TABLE {$quotedTable} CHANGE " . $this->quoteColumn($oldName) . ' ' . $this->quoteColumn($newName);
210
    }
211
212
    /**
213
     * Prepare value for db
214
     * @param $value
215
     * @return int
216
     */
217
    public function prepareValue($value)
218
    {
219
        if (is_bool($value)) {
220
            return (int)$value;
221
        }
222
        return parent::prepareValue($value);
223
    }
224
}