Test Failed
Push — add-integration-tests ( dde5b7...e98ac5 )
by Frank
02:26
created

database.*MySQL.Version   A

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
dl 0
loc 14
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 6
rs 10
nop 0
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 implements 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
// Version reports the actual version of the MySQL database.
40
func (mysql *MySQL) Version() (string, error) {
41
	var version string
42
	err := mysql.Get(&version, `
43
		SELECT
44
			CONCAT(
45
				@@version, ' ', 
46
				@@version_comment, ', ', 
47
				@@version_compile_os, ' ', 
48
				@@version_compile_machine) as version
49
	`)
50
	if err != nil {
51
		return "", err
52
	}
53
	return version, nil
54
}
55
56
// GetDriverImportLibrary returns the golang sql driver specific fot the MySQL database
57
func (mysql *MySQL) GetDriverImportLibrary() string {
58
	return `"github.com/go-sql-driver/mysql"`
59
}
60
61
// GetTables gets all tables for a given database by name
62
func (mysql *MySQL) GetTables() (tables []*Table, err error) {
63
64
	err = mysql.Select(&tables, `
65
		SELECT table_name AS table_name
66
		FROM information_schema.tables
67
		WHERE table_type = 'BASE TABLE'
68
		AND table_schema = ?
69
		ORDER BY table_name
70
	`, mysql.DbName)
71
72
	if mysql.Verbose {
73
		if err != nil {
74
			fmt.Println("> Error at GetTables()")
75
			fmt.Printf("> schema: %q\r\n", mysql.DbName)
76
		}
77
	}
78
79
	return tables, err
80
}
81
82
// PrepareGetColumnsOfTableStmt prepares the statement for retrieving the columns of a specific table for a given database
83
func (mysql *MySQL) PrepareGetColumnsOfTableStmt() (err error) {
84
85
	mysql.GetColumnsOfTableStmt, err = mysql.Preparex(`
86
		SELECT
87
		  ordinal_position AS ordinal_position,
88
		  column_name AS column_name,
89
		  data_type AS data_type,
90
		  column_default AS column_default,
91
		  is_nullable AS is_nullable,
92
		  character_maximum_length AS character_maximum_length,
93
		  numeric_precision AS numeric_precision,
94
		  column_key AS column_key,
95
		  extra AS extra
96
		FROM information_schema.columns
97
		WHERE table_name = ?
98
		AND table_schema = ?
99
		ORDER BY ordinal_position
100
	`)
101
102
	return err
103
}
104
105
// GetColumnsOfTable executes the statement for retrieving the columns of a specific table for a given database
106
func (mysql *MySQL) GetColumnsOfTable(table *Table) (err error) {
107
108
	err = mysql.GetColumnsOfTableStmt.Select(&table.Columns, table.Name, mysql.DbName)
109
110
	if mysql.Settings.Verbose {
111
		if err != nil {
112
			fmt.Printf("> Error at GetColumnsOfTable(%v)\r\n", table.Name)
113
			fmt.Printf("> schema: %q\r\n", mysql.Schema)
114
			fmt.Printf("> dbName: %q\r\n", mysql.DbName)
115
		}
116
	}
117
118
	return err
119
}
120
121
// IsPrimaryKey checks if column belongs to primary key
122
func (mysql *MySQL) IsPrimaryKey(column Column) bool {
123
	return strings.Contains(column.ColumnKey, "PRI")
124
}
125
126
// IsAutoIncrement checks if column is a auto_increment column
127
func (mysql *MySQL) IsAutoIncrement(column Column) bool {
128
	return strings.Contains(column.Extra, "auto_increment")
129
}
130
131
// GetStringDatatypes returns the string data types for the MySQL database
132
func (mysql *MySQL) GetStringDatatypes() []string {
133
	return []string{
134
		"char",
135
		"varchar",
136
		"binary",
137
		"varbinary",
138
	}
139
}
140
141
// IsString returns true if column is of type string for the MySQL database
142
func (mysql *MySQL) IsString(column Column) bool {
143
	return mysql.IsStringInSlice(column.DataType, mysql.GetStringDatatypes())
144
}
145
146
// GetTextDatatypes returns the text data types for the MySQL database
147
func (mysql *MySQL) GetTextDatatypes() []string {
148
	return []string{
149
		"tinytext",
150
		"text",
151
		"mediumtext",
152
		"longtext",
153
		"tinyblob",
154
		"blob",
155
		"mediumblob",
156
		"longblob",
157
	}
158
}
159
160
// IsText returns true if column is of type text for the MySQL database
161
func (mysql *MySQL) IsText(column Column) bool {
162
	return mysql.IsStringInSlice(column.DataType, mysql.GetTextDatatypes())
163
}
164
165
// GetIntegerDatatypes returns the integer data types for the MySQL database
166
func (mysql *MySQL) GetIntegerDatatypes() []string {
167
	return []string{
168
		"tinyint",
169
		"smallint",
170
		"mediumint",
171
		"int",
172
		"bigint",
173
	}
174
}
175
176
// IsInteger returns true if column is of type integer for the MySQL database
177
func (mysql *MySQL) IsInteger(column Column) bool {
178
	return mysql.IsStringInSlice(column.DataType, mysql.GetIntegerDatatypes())
179
}
180
181
// GetFloatDatatypes returns the float data types for the MySQL database
182
func (mysql *MySQL) GetFloatDatatypes() []string {
183
	return []string{
184
		"numeric",
185
		"decimal",
186
		"float",
187
		"real",
188
		"double precision",
189
	}
190
}
191
192
// IsFloat returns true if column is of type float for the MySQL database
193
func (mysql *MySQL) IsFloat(column Column) bool {
194
	return mysql.IsStringInSlice(column.DataType, mysql.GetFloatDatatypes())
195
}
196
197
// GetTemporalDatatypes returns the temporal data types for the MySQL database
198
func (mysql *MySQL) GetTemporalDatatypes() []string {
199
	return []string{
200
		"time",
201
		"timestamp",
202
		"date",
203
		"datetime",
204
		"year",
205
	}
206
}
207
208
// IsTemporal returns true if column is of type temporal for the MySQL database
209
func (mysql *MySQL) IsTemporal(column Column) bool {
210
	return mysql.IsStringInSlice(column.DataType, mysql.GetTemporalDatatypes())
211
}
212
213
// GetTemporalDriverDataType returns the time data type specific for the MySQL database
214
func (mysql *MySQL) GetTemporalDriverDataType() string {
215
	return "mysql.NullTime"
216
}
217