-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-helpers.go
130 lines (108 loc) · 2.69 KB
/
db-helpers.go
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
package main
import (
"database/sql"
"errors"
"strings"
_ "github.com/go-sql-driver/mysql"
)
// connectionString -> user:pass@tcp(:3306)/test
func connectionString() string {
var str string
str = env["DB_USERNAME"] + ":" + env["DB_PASSWORD"] +
"@(:" + env["DB_PORT"] + ")/" +
env["DB_DATABASE"] + "?charset=utf8"
return str
}
// getServerVersion ...
func getServerVersion(db *sql.DB) (string, error) {
var serverVersion sql.NullString
if err := db.QueryRow("SELECT version()").Scan(&serverVersion); err != nil {
return "", err
}
return serverVersion.String, nil
}
// getTables ...
func getTables(db *sql.DB) ([]string, error) {
tables := make([]string, 0)
// Get table list
rows, err := db.Query("SHOW TABLES")
defer rows.Close()
if err != nil {
return tables, err
}
// Read result
for rows.Next() {
var table sql.NullString
if err := rows.Scan(&table); err != nil {
return tables, err
}
tables = append(tables, table.String)
}
return tables, rows.Err()
}
// createTableSQL ...
func createTableSQL(db *sql.DB, name string) (string, error) {
// Get table creation SQL
var tableReturn sql.NullString
var tableSql sql.NullString
err := db.QueryRow("SHOW CREATE TABLE "+name).Scan(&tableReturn, &tableSql)
if err != nil {
return "", err
}
if tableReturn.String != name {
return "", errors.New("Returned table is not the same as requested table")
}
return tableSql.String, nil
}
// createTableValues
func createTableValues(db *sql.DB, name string) (string, error) {
// Get Data
rows, err := db.Query("SELECT * FROM " + name)
if err != nil {
return "", err
}
defer rows.Close()
// Get columns
columns, err := rows.Columns()
if err != nil {
return "", err
}
if len(columns) == 0 {
return "", errors.New("No columns in table " + name + ".")
}
// Read data
dataText := make([]string, 0)
for rows.Next() {
// Init temp data storage
data := make([]*sql.NullString, len(columns))
ptrs := make([]interface{}, len(columns))
for i := range data {
ptrs[i] = &data[i]
}
// Read data
if err := rows.Scan(ptrs...); err != nil {
return "", err
}
dataStrings := make([]string, len(columns))
for key, value := range data {
if value != nil && value.Valid {
dataStrings[key] = "'" + value.String + "'"
} else {
dataStrings[key] = "null"
}
}
dataText = append(dataText, "("+strings.Join(dataStrings, ",")+")")
}
return strings.Join(dataText, ", "), rows.Err()
}
func createTable(db *sql.DB, name string) (*Table, error) {
var err error
t := &Table{Name: name}
if t.SQL, err = createTableSQL(db, name); err != nil {
return nil, err
}
if t.Values, err = createTableValues(db, name); err != nil {
return nil, err
}
return t, nil
}