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