-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.go
146 lines (120 loc) · 3.41 KB
/
Database.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"database/sql"
"fmt"
"github.com/blockloop/scan"
"github.com/emirpasic/gods/maps/hashmap"
_ "github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
)
func ConnectDB(dbsetting Model_DBSetting) (*sql.DB, error) {
connstr := fmt.Sprintf("%s:%s@tcp(%s)/%s", dbsetting.User, dbsetting.Pass, dbsetting.Host, dbsetting.Db)
db, err := sql.Open("mysql", connstr)
if err != nil {
return nil, err
} else {
return db, nil
}
}
// func GetLocalTables() []string {
// return GetAllTables(localdb, localdbname)
// }
func GetAllTables(db *sql.DB, dbname string) []string {
sql_tablelist := "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type = 'BASE TABLE' and table_schema=?"
var listtable = []string{}
res, err := db.Query(sql_tablelist, dbname)
if err == nil {
for res.Next() {
var tablename string
res.Scan(&tablename)
mapfields[tablename] = hashmap.New()
listtable = append(listtable, tablename)
}
} else {
logrus.Fatal(err)
}
return listtable
}
func GetAllTableAndFields(db *sql.DB, dbname string) {
sql_tablelist := "select table_name,column_name,column_type, data_type,column_default, is_nullable, column_key,extra,character_set_name,collation_name,numeric_precision from information_schema.columns where table_schema = ? order by table_name,ordinal_position"
rows, err := db.Query(sql_tablelist, dbname)
// sql_select := "SELECT %s FROM %s"
logrus.Info("SQL:", sql_tablelist)
if err == nil {
i := 0
for rows.Next() {
i = i + 1
tablename := ""
f := Model_FieldSetting{}
rows.Scan(&tablename, &f.Field, &f.Type, &f.DataType, &f.Default, &f.Null, &f.Key, &f.Extra, &f.CharacterSet, &f.CollationName, &f.NumericPrecision)
//exclude existing tenant field, so we can manage it ourself during import
if f.Field == "tenant_id" {
continue
} else {
fields := mapfields[tablename] //mapfields.Get(tablename)
// logrus.Warn("add ", tablename, ":", f.Field, ":", f.NumericPrecision.String)
ftype := "string"
if f.NumericPrecision.Valid == true {
ftype = "number"
} else if f.DataType == "date" || f.DataType == "datetime" {
ftype = f.DataType
}
if fields != nil {
fields.Put(f.Field, ftype)
}
}
}
}
for tablename, fields := range mapfields {
// logrus.Error(tablename, fields.Keys())
for i, fieldname := range fields.Keys() {
if i > 0 {
mapfieldstr[tablename] += ", "
}
mapfieldstr[tablename] += fmt.Sprintf("`%v`", fieldname)
}
}
}
// parse csv columns, create query statement
func parseColumns(TABLENAME string, columns []string, query *string) {
*query = "INSERT INTO " + TABLENAME + " ("
placeholder := "VALUES ("
for i, c := range columns {
if i == 0 {
*query += c
placeholder += "?"
} else {
*query += ", " + c
placeholder += ", ?"
}
}
placeholder += ")"
*query += ") " + placeholder
}
// convert []string to []interface{}
func string2interface(s []string) []interface{} {
i := make([]interface{}, len(s))
for k, v := range s {
i[k] = v
}
return i
}
func GetRemoteDatabases() (dbsettings []Model_DBSetting) {
sql := "SELECT * FROM tenant_master where isactive=1"
if RunMode == "append" {
sql += " and imported ='' "
}
// logrus.Fatal("Ping", sql, localdb)
res, err := localdb.Query(sql)
if err == nil {
err = scan.Rows(&dbsettings, res)
if err == nil {
return
} else {
logrus.Fatal(err)
}
} else {
logrus.Fatal(err)
}
return
}