1
|
|
|
package database |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"strings" |
6
|
|
|
|
7
|
|
|
"github.com/fraenky8/tables-to-go/pkg/settings" |
8
|
|
|
|
9
|
|
|
// MySQL database driver |
10
|
|
|
_ "github.com/go-sql-driver/mysql" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
// MySQL implemenmts the Database interface with help of generalDatabase |
14
|
|
|
type MySQL struct { |
15
|
|
|
*GeneralDatabase |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// NewMySQL creates a new MySQL database |
19
|
|
|
func NewMySQL(s *settings.Settings) *MySQL { |
20
|
|
|
return &MySQL{ |
21
|
|
|
GeneralDatabase: &GeneralDatabase{ |
22
|
|
|
Settings: s, |
23
|
|
|
driver: dbTypeToDriverMap[s.DbType], |
24
|
|
|
}, |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
// Connect connects to the database by the given data source name (dsn) of the concrete database |
29
|
|
|
func (mysql *MySQL) Connect() error { |
30
|
|
|
return mysql.GeneralDatabase.Connect(mysql.DSN()) |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// DSN creates the DSN String to connect to this database |
34
|
|
|
func (mysql *MySQL) DSN() string { |
35
|
|
|
return fmt.Sprintf("%v:%v@tcp(%v:%v)/%v", |
36
|
|
|
mysql.Settings.User, mysql.Settings.Pswd, mysql.Settings.Host, mysql.Settings.Port, mysql.Settings.DbName) |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// GetDriverImportLibrary returns the golang sql driver specific fot the MySQL database |
40
|
|
|
func (mysql *MySQL) GetDriverImportLibrary() string { |
41
|
|
|
return `"github.com/go-sql-driver/mysql"` |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// GetTables gets all tables for a given database by name |
45
|
|
|
func (mysql *MySQL) GetTables() (tables []*Table, err error) { |
46
|
|
|
|
47
|
|
|
err = mysql.Select(&tables, ` |
48
|
|
|
SELECT table_name AS table_name |
49
|
|
|
FROM information_schema.tables |
50
|
|
|
WHERE table_type = 'BASE TABLE' |
51
|
|
|
AND table_schema = ? |
52
|
|
|
ORDER BY table_name |
53
|
|
|
`, mysql.DbName) |
54
|
|
|
|
55
|
|
|
if mysql.Verbose { |
56
|
|
|
if err != nil { |
57
|
|
|
fmt.Println("> Error at GetTables()") |
58
|
|
|
fmt.Printf("> schema: %q\r\n", mysql.DbName) |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return tables, err |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// PrepareGetColumnsOfTableStmt prepares the statement for retrieving the columns of a specific table for a given database |
66
|
|
|
func (mysql *MySQL) PrepareGetColumnsOfTableStmt() (err error) { |
67
|
|
|
|
68
|
|
|
mysql.GetColumnsOfTableStmt, err = mysql.Preparex(` |
69
|
|
|
SELECT |
70
|
|
|
ordinal_position AS ordinal_position, |
71
|
|
|
column_name AS column_name, |
72
|
|
|
data_type AS data_type, |
73
|
|
|
column_default AS column_default, |
74
|
|
|
is_nullable AS is_nullable, |
75
|
|
|
character_maximum_length AS character_maximum_length, |
76
|
|
|
numeric_precision AS numeric_precision, |
77
|
|
|
column_key AS column_key, |
78
|
|
|
extra AS extra |
79
|
|
|
FROM information_schema.columns |
80
|
|
|
WHERE table_name = ? |
81
|
|
|
AND table_schema = ? |
82
|
|
|
ORDER BY ordinal_position |
83
|
|
|
`) |
84
|
|
|
|
85
|
|
|
return err |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// GetColumnsOfTable executes the statement for retrieving the columns of a specific table for a given database |
89
|
|
|
func (mysql *MySQL) GetColumnsOfTable(table *Table) (err error) { |
90
|
|
|
|
91
|
|
|
err = mysql.GetColumnsOfTableStmt.Select(&table.Columns, table.Name, mysql.DbName) |
92
|
|
|
|
93
|
|
|
if mysql.Settings.Verbose { |
94
|
|
|
if err != nil { |
95
|
|
|
fmt.Printf("> Error at GetColumnsOfTable(%v)\r\n", table.Name) |
96
|
|
|
fmt.Printf("> schema: %q\r\n", mysql.Schema) |
97
|
|
|
fmt.Printf("> dbName: %q\r\n", mysql.DbName) |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return err |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// IsPrimaryKey checks if column belongs to primary key |
105
|
|
|
func (mysql *MySQL) IsPrimaryKey(column Column) bool { |
106
|
|
|
return strings.Contains(column.ColumnKey, "PRI") |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// IsAutoIncrement checks if column is a auto_increment column |
110
|
|
|
func (mysql *MySQL) IsAutoIncrement(column Column) bool { |
111
|
|
|
return strings.Contains(column.Extra, "auto_increment") |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// GetStringDatatypes returns the string datatypes for the MySQL database |
115
|
|
|
func (mysql *MySQL) GetStringDatatypes() []string { |
116
|
|
|
return []string{ |
117
|
|
|
"char", |
118
|
|
|
"varchar", |
119
|
|
|
"binary", |
120
|
|
|
"varbinary", |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// IsString returns true if colum is of type string for the MySQL database |
125
|
|
|
func (mysql *MySQL) IsString(column Column) bool { |
126
|
|
|
return mysql.IsStringInSlice(column.DataType, mysql.GetStringDatatypes()) |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// GetTextDatatypes returns the text datatypes for the MySQL database |
130
|
|
|
func (mysql *MySQL) GetTextDatatypes() []string { |
131
|
|
|
return []string{ |
132
|
|
|
"text", |
133
|
|
|
"blob", |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// IsText returns true if colum is of type text for the MySQL database |
138
|
|
|
func (mysql *MySQL) IsText(column Column) bool { |
139
|
|
|
return mysql.IsStringInSlice(column.DataType, mysql.GetTextDatatypes()) |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// GetIntegerDatatypes returns the integer datatypes for the MySQL database |
143
|
|
|
func (mysql *MySQL) GetIntegerDatatypes() []string { |
144
|
|
|
return []string{ |
145
|
|
|
"tinyint", |
146
|
|
|
"smallint", |
147
|
|
|
"mediumint", |
148
|
|
|
"int", |
149
|
|
|
"bigint", |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// IsInteger returns true if colum is of type integer for the MySQL database |
154
|
|
|
func (mysql *MySQL) IsInteger(column Column) bool { |
155
|
|
|
return mysql.IsStringInSlice(column.DataType, mysql.GetIntegerDatatypes()) |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// GetFloatDatatypes returns the float datatypes for the MySQL database |
159
|
|
|
func (mysql *MySQL) GetFloatDatatypes() []string { |
160
|
|
|
return []string{ |
161
|
|
|
"numeric", |
162
|
|
|
"decimal", |
163
|
|
|
"float", |
164
|
|
|
"real", |
165
|
|
|
"double precision", |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// IsFloat returns true if colum is of type float for the MySQL database |
170
|
|
|
func (mysql *MySQL) IsFloat(column Column) bool { |
171
|
|
|
return mysql.IsStringInSlice(column.DataType, mysql.GetFloatDatatypes()) |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// GetTemporalDatatypes returns the temporal datatypes for the MySQL database |
175
|
|
|
func (mysql *MySQL) GetTemporalDatatypes() []string { |
176
|
|
|
return []string{ |
177
|
|
|
"time", |
178
|
|
|
"timestamp", |
179
|
|
|
"date", |
180
|
|
|
"datetime", |
181
|
|
|
"year", |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// IsTemporal returns true if colum is of type temporal for the MySQL database |
186
|
|
|
func (mysql *MySQL) IsTemporal(column Column) bool { |
187
|
|
|
return mysql.IsStringInSlice(column.DataType, mysql.GetTemporalDatatypes()) |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// GetTemporalDriverDataType returns the time data type specific for the MySQL database |
191
|
|
|
func (mysql *MySQL) GetTemporalDriverDataType() string { |
192
|
|
|
return "mysql.NullTime" |
193
|
|
|
} |
194
|
|
|
|