Issues (33)

src/Database/Sqlite/Adapter.php (3 issues)

1
<?php
2
3
namespace Tsukasa\QueryBuilder\Database\Sqlite;
4
5
use Exception;
6
use Tsukasa\QueryBuilder\BaseAdapter;
7
use Tsukasa\QueryBuilder\Exception\NotSupportedException;
8
use Tsukasa\QueryBuilder\Interfaces\IAdapter;
9
use Tsukasa\QueryBuilder\Interfaces\ISQLGenerator;
10
11
class Adapter extends BaseAdapter implements IAdapter, ISQLGenerator
12
{
13
    /**
14
     * Quotes a table name for use in a query.
15
     * A simple table name has no schema prefix.
16
     * @param string $name table name
17
     * @return string the properly quoted table name
18
     */
19
    public function quoteSimpleTableName($name)
20
    {
21
        return strpos($name, "`") !== false ? $name : "`" . $name . "`";
22
    }
23
24
    /**
25
     * Quotes a column name for use in a query.
26
     * A simple column name has no prefix.
27
     * @param string $name column name
28
     * @return string the properly quoted column name
29
     */
30
    public function quoteSimpleColumnName($name)
31
    {
32
        return strpos($name, '`') !== false || $name === '*' ? $name : '`' . $name . '`';
33
    }
34
35
    public function getLookupCollection()
36
    {
37
        return new LookupCollection();
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getRandomOrder()
44
    {
45
        return 'RANDOM()';
46
    }
47
48
    /**
49
     * @param $oldTableName
50
     * @param $newTableName
51
     * @return string
52
     */
53
    public function sqlRenameTable($oldTableName, $newTableName)
54
    {
55
        return 'ALTER TABLE ' . $this->quoteTableName($oldTableName) . ' RENAME TO ' . $this->quoteTableName($newTableName);
56
    }
57
58
    /**
59
     * @param $tableName
60
     * @param bool $cascade
61
     * @return string
62
     */
63
    public function sqlTruncateTable($tableName, $cascade = false)
64
    {
65
        return "DELETE FROM " . $this->quoteTableName($tableName);
66
    }
67
68
    /**
69
     * @param $tableName
70
     * @param $name
71
     * @return string
72
     */
73
    public function sqlDropIndex($tableName, $name)
74
    {
75
        return 'DROP INDEX ' . $this->quoteColumn($name);
76
    }
77
78
    /**
79
     * @param $tableName
80
     * @param $column
81
     * @return string
82
     * @throws Exception
83
     */
84
    public function sqlDropColumn($tableName, $column)
85
    {
86
        throw new NotSupportedException('not supported by SQLite');
87
    }
88
89
    /**
90
     * @param $tableName
91
     * @param $oldName
92
     * @param $newName
93
     * @return string
94
     * @throws Exception
95
     */
96
    public function sqlRenameColumn($tableName, $oldName, $newName)
97
    {
98
        throw new NotSupportedException('not supported by SQLite');
99
    }
100
101
    /**
102
     * @param $tableName
103
     * @param $name
104
     * @return string
105
     * @throws Exception
106
     */
107
    public function sqlDropForeignKey($tableName, $name)
108
    {
109
        throw new NotSupportedException('not supported by SQLite');
110
    }
111
112
    /**
113
     * @param $tableName
114
     * @param $name
115
     * @param $columns
116
     * @param $refTable
117
     * @param $refColumns
118
     * @param null $delete
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $delete is correct as it would always require null to be passed?
Loading history...
119
     * @param null $update
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $update is correct as it would always require null to be passed?
Loading history...
120
     * @return string
121
     * @throws Exception
122
     */
123
    public function sqlAddForeignKey($tableName, $name, $columns, $refTable, $refColumns, $delete = null, $update = null)
124
    {
125
        throw new NotSupportedException('not supported by SQLite');
126
    }
127
128
    /**
129
     * @param $tableName
130
     * @param $column
131
     * @param $type
132
     * @return string
133
     * @throws Exception
134
     */
135
    public function sqlAlterColumn($tableName, $column, $type)
136
    {
137
        throw new NotSupportedException('not supported by SQLite');
138
    }
139
140
    /**
141
     * @param $tableName
142
     * @param $name
143
     * @param $columns
144
     * @return string
145
     * @throws Exception
146
     */
147
    public function sqlAddPrimaryKey($tableName, $name, $columns)
148
    {
149
        throw new NotSupportedException('not supported by SQLite');
150
    }
151
152
    /**
153
     * @param $tableName
154
     * @param $name
155
     * @return string
156
     * @throws Exception
157
     */
158
    public function sqlDropPrimaryKey($tableName, $name)
159
    {
160
        throw new NotSupportedException('not supported by SQLite');
161
    }
162
163
    /**
164
     * @param $value
165
     * @return string
166
     */
167
    public function getBoolean($value = null)
168
    {
169
        return (bool)$value ? 1 : 0;
170
    }
171
172
    /**
173
     * @param $value string|\DateTime
174
     * @param $format string
175
     * @return string
176
     */
177
    protected function formatDateTime($value, $format)
178
    {
179
        if ($value instanceof \DateTime) {
180
            $value = $value->format($format);
181
        }
182
        elseif ($value === null) {
183
            $value = date($format);
184
        }
185
        elseif (is_numeric($value)) {
186
            $value = date($format, $value);
187
        }
188
        elseif (is_string($value)) {
189
            $value = date($format, strtotime($value));
190
        }
191
        return (string)$value;
192
    }
193
194
    public function getDateTime($value = null)
195
    {
196
        return $this->formatDateTime($value, "Y-m-d H:i:s");
197
    }
198
199
    public function getDate($value = null)
200
    {
201
        return $this->formatDateTime($value, "Y-m-d");
202
    }
203
204
    /**
205
     * @param $limit
206
     * @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...
207
     * @return mixed
208
     */
209
    public function sqlLimitOffset($limit = null, $offset = null)
210
    {
211
        if ($this->hasLimit($limit)) {
212
            $sql = 'LIMIT ' . $limit;
213
            if ($this->hasOffset($offset)) {
214
                $sql .= ' OFFSET ' . $offset;
215
            }
216
            return ' ' . $sql;
217
        }
218
219
        if ($this->hasOffset($offset)) {
220
            // limit is not optional in SQLite
221
            // http://www.sqlite.org/syntaxdiagrams.html#select-stmt
222
            // If the LIMIT expression evaluates to a negative value, then there
223
            // is no upper bound on the number of rows returned.
224
            // -1 or 9223372036854775807 2^63-1
225
            return ' LIMIT 9223372036854775807 OFFSET ' . $offset; // 2^63-1
226
        }
227
228
        return '';
229
    }
230
231
    /**
232
     * @param $tableName
233
     * @param $column
234
     * @param $type
235
     * @return string
236
     */
237
    public function sqlAddColumn($tableName, $column, $type)
238
    {
239
        return 'ALTER TABLE ' . $this->quoteTableName($tableName) . ' ADD COLUMN ' . $this->quoteColumn($column) . ' ' . $type;
240
    }
241
242
    /**
243
     * @param $sequenceName
244
     * @param $value
245
     * @return string
246
     */
247
    public function sqlResetSequence($sequenceName, $value = null)
248
    {
249
        return 'UPDATE sqlite_sequence SET seq=' . $this->quoteValue($value) . ' WHERE name=' . $this->quoteValue($sequenceName);
250
    }
251
252
    /**
253
     * @param bool $check
254
     * @param string $schema
255
     * @param string $table
256
     * @return string
257
     */
258
    public function sqlCheckIntegrity($check = true, $schema = '', $table = '')
259
    {
260
        return 'PRAGMA foreign_keys=' . $this->getBoolean($check);
261
    }
262
}