1
|
|
|
package database |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"database/sql" |
5
|
|
|
"fmt" |
6
|
|
|
"net/url" |
7
|
|
|
"strings" |
8
|
|
|
|
9
|
|
|
"github.com/fraenky8/tables-to-go/pkg/settings" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// SQLite implemenmts the Database interface with help of generalDatabase |
13
|
|
|
type SQLite struct { |
14
|
|
|
*GeneralDatabase |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
// NewSQLite creates a new SQLite database |
18
|
|
|
func NewSQLite(s *settings.Settings) *SQLite { |
19
|
1 |
|
return &SQLite{ |
20
|
|
|
GeneralDatabase: &GeneralDatabase{ |
21
|
|
|
Settings: s, |
22
|
|
|
driver: dbTypeToDriverMap[s.DbType], |
23
|
|
|
}, |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
func (s *SQLite) Connect() (err error) { |
28
|
|
|
return s.GeneralDatabase.Connect(s.DSN()) |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
func (s *SQLite) DSN() string { |
32
|
1 |
|
if s.Settings.User == "" && s.Settings.Pswd == "" { |
33
|
1 |
|
return fmt.Sprintf("%v", s.Settings.DbName) |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
u, err := url.Parse(s.DbName) |
37
|
1 |
|
if err != nil { |
38
|
1 |
|
return fmt.Sprintf("%v", s.Settings.DbName) |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
query := u.Query() |
42
|
1 |
|
query.Set("_auth_user", s.Settings.User) |
43
|
1 |
|
query.Set("_auth_pass", s.Settings.Pswd) |
44
|
1 |
|
u.RawQuery = query.Encode() |
45
|
|
|
|
46
|
|
|
// SQLite driver expects a empty `_auth` request param |
47
|
1 |
|
return strings.ReplaceAll(u.RequestURI(), "_auth=&", "_auth&") |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
func (s *SQLite) GetDriverImportLibrary() string { |
51
|
|
|
return `"github.com/mattn/go-sqlite3"` |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
func (s *SQLite) GetTables() (tables []*Table, err error) { |
55
|
|
|
|
56
|
|
|
err = s.Select(&tables, ` |
57
|
|
|
SELECT name AS table_name |
58
|
|
|
FROM sqlite_master |
59
|
|
|
WHERE type = 'table' |
60
|
|
|
AND name NOT LIKE 'sqlite?_%' escape '?' |
61
|
|
|
`) |
62
|
|
|
|
63
|
|
|
if s.Verbose { |
64
|
|
|
if err != nil { |
65
|
|
|
fmt.Println("> Error at GetTables()") |
66
|
|
|
fmt.Printf("> database: %q\r\n", s.DbName) |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return tables, err |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
func (s *SQLite) PrepareGetColumnsOfTableStmt() (err error) { |
74
|
|
|
return nil |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func (s *SQLite) GetColumnsOfTable(table *Table) (err error) { |
78
|
|
|
|
79
|
|
|
rows, err := s.Queryx(` |
80
|
|
|
SELECT * |
81
|
|
|
FROM PRAGMA_TABLE_INFO('` + table.Name + `') |
82
|
|
|
`) |
83
|
|
|
if err != nil { |
84
|
|
|
if s.Verbose { |
85
|
|
|
fmt.Printf("> Error at GetColumnsOfTable(%v)\r\n", table.Name) |
86
|
|
|
fmt.Printf("> database: %q\r\n", s.DbName) |
87
|
|
|
} |
88
|
|
|
return err |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
type column struct { |
92
|
|
|
CID int `db:"cid"` |
93
|
|
|
Name string `db:"name"` |
94
|
|
|
DataType string `db:"type"` |
95
|
|
|
NotNull int `db:"notnull"` |
96
|
|
|
DefaultValue sql.NullString `db:"dflt_value"` |
97
|
|
|
PrimaryKey int `db:"pk"` |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
for rows.Next() { |
101
|
|
|
var col column |
102
|
|
|
err = rows.StructScan(&col) |
103
|
|
|
if err != nil { |
104
|
|
|
return err |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
isNullable := "YES" |
108
|
|
|
if col.NotNull == 1 { |
109
|
|
|
isNullable = "NO" |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
isPrimaryKey := "" |
113
|
|
|
if col.PrimaryKey == 1 { |
114
|
|
|
isPrimaryKey = "PK" |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
table.Columns = append(table.Columns, Column{ |
118
|
|
|
OrdinalPosition: col.CID, |
119
|
|
|
Name: col.Name, |
120
|
|
|
DataType: col.DataType, |
121
|
|
|
DefaultValue: col.DefaultValue, |
122
|
|
|
IsNullable: isNullable, |
123
|
|
|
CharacterMaximumLength: sql.NullInt64{}, |
124
|
|
|
NumericPrecision: sql.NullInt64{}, |
125
|
|
|
// reuse mysql column_key as primary key indicator |
126
|
|
|
ColumnKey: isPrimaryKey, |
127
|
|
|
Extra: "", |
128
|
|
|
ConstraintName: sql.NullString{}, |
129
|
|
|
ConstraintType: sql.NullString{}, |
130
|
|
|
}) |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return nil |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
func (s *SQLite) IsPrimaryKey(column Column) bool { |
137
|
|
|
return column.ColumnKey == "PK" |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
func (s *SQLite) IsAutoIncrement(column Column) bool { |
141
|
|
|
return column.ColumnKey == "PK" |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
func (s *SQLite) GetStringDatatypes() []string { |
145
|
|
|
return []string{ |
146
|
|
|
"text", |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
func (s *SQLite) IsString(column Column) bool { |
151
|
|
|
return s.IsStringInSlice(column.DataType, s.GetStringDatatypes()) |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
func (s *SQLite) GetTextDatatypes() []string { |
155
|
|
|
return []string{ |
156
|
|
|
"text", |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
func (s *SQLite) IsText(column Column) bool { |
161
|
|
|
return s.IsStringInSlice(column.DataType, s.GetTextDatatypes()) |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
func (s *SQLite) GetIntegerDatatypes() []string { |
165
|
|
|
return []string{ |
166
|
|
|
"integer", |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
func (s *SQLite) IsInteger(column Column) bool { |
171
|
|
|
return s.IsStringInSlice(column.DataType, s.GetIntegerDatatypes()) |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
func (s *SQLite) GetFloatDatatypes() []string { |
175
|
|
|
return []string{ |
176
|
|
|
"real", |
177
|
|
|
"numeric", |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
func (s *SQLite) IsFloat(column Column) bool { |
182
|
|
|
return s.IsStringInSlice(column.DataType, s.GetFloatDatatypes()) |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
func (s *SQLite) GetTemporalDatatypes() []string { |
186
|
|
|
return []string{} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
func (s *SQLite) IsTemporal(column Column) bool { |
190
|
|
|
return false |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
func (s *SQLite) GetTemporalDriverDataType() string { |
194
|
|
|
return "" |
195
|
|
|
} |
196
|
|
|
|