-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLibsqlSchemaBuilder.php
More file actions
134 lines (103 loc) · 4.41 KB
/
LibsqlSchemaBuilder.php
File metadata and controls
134 lines (103 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
namespace Libsql\Laravel\Database;
use Illuminate\Database\QueryException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\SQLiteBuilder;
use Libsql\Laravel\Exceptions\FeatureNotSupportedException;
class LibsqlSchemaBuilder extends SQLiteBuilder
{
public function __construct(protected LibsqlDatabase $db, \Illuminate\Database\Connection $connection)
{
parent::__construct($connection);
}
public function createDatabase($name)
{
throw new FeatureNotSupportedException('Creating database is not supported in LibSQL database.');
}
public function dropDatabaseIfExists($name)
{
throw new FeatureNotSupportedException('Dropping database is not supported in LibSQL database.');
}
protected function dropAllIndexes(): void
{
$statement = $this->db->prepare($this->grammar()->compileDropAllIndexes());
$results = $statement->query();
collect($results)->each(function (array $query) {
$query = array_values($query)[0];
$this->db->query($query);
});
}
public function dropAllTables(): void
{
$this->dropAllTriggers();
$this->dropAllIndexes();
$this->db->exec($this->grammar()->compileDisableForeignKeyConstraints());
$statement = $this->db->prepare($this->grammar()->compileDropAllTables());
$results = $statement->query();
collect($results)->each(function (array $query) {
$query = array_values($query)[0];
$this->db->query($query);
});
$this->db->exec($this->grammar()->compileEnableForeignKeyConstraints());
}
protected function dropAllTriggers(): void
{
$statement = $this->db->prepare($this->grammar()->compileDropAllTriggers());
$results = $statement->query();
collect($results)->each(function (array $query) {
$query = array_values($query)[0];
$this->db->query($query);
});
}
public function dropAllViews(): void
{
$statement = $this->db->prepare($this->grammar()->compileDropAllViews());
$results = $statement->query();
collect($results)->each(function (array $query) {
$query = array_values($query)[0];
$this->db->query($query);
});
}
public function getColumns($table)
{
$table = $this->connection->getTablePrefix() . $table;
$data = $this->connection->select("PRAGMA table_xinfo('{$table}')");
$columns = $this->connection->selectOne("SELECT sql FROM sqlite_master WHERE type='table' AND name='{$table}'");
$pattern = '/(?:\(|,)\s*[\'"`]?([a-zA-Z_][a-zA-Z0-9_]*)[\'"`]?\s+[a-zA-Z]+/i';
preg_match_all($pattern, $columns->sql, $matches);
$columnMatches = $matches[1] ?? [];
$delctypes = stdClassToArray($data);
foreach ($delctypes as $key => $value) {
if (isset($delctypes[$key]['name'])) {
$delctypes[$key]['name'] = $columnMatches[$key];
}
if (isset($delctypes[$key]['type'])) {
$type = strtolower($delctypes[$key]['type']);
$delctypes[$key]['type'] = $type;
$delctypes[$key]['type_name'] = $type;
}
if (isset($delctypes[$key]['notnull'])) {
$delctypes[$key]['nullable'] = $delctypes[$key]['notnull'] == 1 ? false : true;
}
if (isset($delctypes[$key]['dflt_value'])) {
$delctypes[$key]['default'] = $delctypes[$key]['dflt_value'] == 'NULL' ? null : new Expression(Str::wrap($delctypes[$key]['dflt_value'], '(', ')'));
}
if (isset($delctypes[$key]['pk'])) {
$delctypes[$key]['auto_increment'] = $delctypes[$key]['pk'] == 1 ? true : false;
}
$delctypes[$key]['collation'] = null;
$delctypes[$key]['comment'] = null;
$delctypes[$key]['generation'] = null;
}
$keyOrder = ['name', 'type_name', 'type', 'collation', 'nullable', 'default', 'auto_increment', 'comment', 'generation', 'pk', 'notnull', 'dflt_value', 'cid', 'hidden'];
$delctypes = reorderArrayKeys($delctypes, $keyOrder);
return $delctypes;
}
protected function grammar(): LibsqlSchemaGrammar
{
return new LibsqlSchemaGrammar($this->connection);
}
}