-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreadsql.go
189 lines (170 loc) · 5.11 KB
/
readsql.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
mysqlpkg "database/sql"
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
// CampaignFields - Fields from MySQL campaigns table
type CampaignFields struct {
ID int64
Regions mysqlpkg.NullString
}
// BannerFields - Fields from MySQL campaigns table
type BannerFields struct {
ID int64
CampaignID mysqlpkg.NullInt64
Width mysqlpkg.NullInt64
Height mysqlpkg.NullInt64
}
// BannerVideoFields - Fields from MySQL campaigns table
type BannerVideoFields struct {
ID int64
CampaignID mysqlpkg.NullInt64
Width mysqlpkg.NullInt64
Height mysqlpkg.NullInt64
}
// Campaigns - Array of records with structure CampaignFields
type Campaigns []CampaignFields
// Banners - Array of records with structure BannerFields
type Banners []BannerFields
// BannerVideos - Array of records with structure BannerVideoFields
type BannerVideos []BannerVideoFields
var dbCampaignRecords = Campaigns{}
var dbBannerRecords = Banners{}
var dbBannerVideoRecords = BannerVideos{}
func readMySQLTables(mysqlHost string, mysqlDbname string, mysqlUser string, mysqlPassword string) bool {
// log1 := logger.GetLogger("readMySQLTables")
var retError = false
var dsn = mysqlUser + ":" + mysqlPassword + "@tcp(" + mysqlHost + ")/" + mysqlDbname
db, err := mysqlpkg.Open("mysql", dsn)
if err != nil {
fmt.Println(err.Error())
return true
}
defer db.Close()
// RTB4FREE SQL table formats
//recs := []CampaignFields{}
iface, _ := executeMySQLSelect(db, "SELECT id,regions FROM campaigns", "campaign") // rtb4free table
if camprecs, ok := iface.([]CampaignFields); ok {
dbCampaignRecords = camprecs
} else {
return true
}
iface, _ = executeMySQLSelect(db, "SELECT id,campaign_id, width,height FROM banners", "banner") // rtb4free table
if bannerrecs, ok := iface.([]BannerFields); ok {
dbBannerRecords = bannerrecs
} else {
return true
}
iface, _ = executeMySQLSelect(db, "SELECT id,campaign_id, vast_video_width,vast_video_height FROM banner_videos", "video") // rtb4free table
if videorecs, ok := iface.([]BannerVideoFields); ok {
dbBannerVideoRecords = videorecs
} else {
return true
}
return retError
}
func executeMySQLSelect(db *mysqlpkg.DB, selectStmt string, rtype string) (interface{}, error) {
log1 := logger.GetLogger("executeMySQLSelect")
var rvals interface{}
rows, err := db.Query(selectStmt)
if err != nil {
log1.Error(err.Error())
return rvals, errors.New("Query error -" + selectStmt)
}
defer rows.Close()
//if val, ok := rectype.(*[]CampaignFields); ok {
switch rtype {
case "campaign":
recs := []CampaignFields{}
count := 0
for rows.Next() {
rec := CampaignFields{}
err = rows.Scan(&rec.ID, &rec.Regions)
if err != nil {
log1.Error(err.Error())
return rvals, errors.New("Row error on select -" + selectStmt)
}
recs = append(recs, rec)
count++
}
rvals = recs
if err = rows.Err(); err != nil {
log1.Error(err.Error())
return rvals, errors.New("Rows error on select -" + selectStmt)
}
log1.Info(fmt.Sprintf("%d Campaign records read.", count))
case "banner":
recs := []BannerFields{}
count := 0
for rows.Next() {
rec := BannerFields{}
err = rows.Scan(&rec.ID, &rec.CampaignID, &rec.Width, &rec.Height)
if err != nil {
log1.Error(err.Error())
return rvals, errors.New("Row error on select -" + selectStmt)
}
recs = append(recs, rec)
count++
}
rvals = recs
if err = rows.Err(); err != nil {
log1.Error(err.Error())
return rvals, errors.New("Rows error on select -" + selectStmt)
}
logger.Info(fmt.Sprintf("%d banner records read.", count))
case "video":
recs := []BannerVideoFields{}
count := 0
for rows.Next() {
rec := BannerVideoFields{}
err = rows.Scan(&rec.ID, &rec.CampaignID, &rec.Width, &rec.Height)
if err != nil {
log1.Error(err.Error())
return rvals, errors.New("Row error on select -" + selectStmt)
}
recs = append(recs, rec)
count++
}
rvals = recs
if err = rows.Err(); err != nil {
log1.Error(err.Error())
return rvals, errors.New("Rows error on select -" + selectStmt)
}
log1.Info(fmt.Sprintf("%d video records read.", count))
default:
log1.Error("executeMySQLSelect can't find select type - ", rtype)
}
return rvals, nil
}
func (campaigns Campaigns) findID(ID int64) CampaignFields {
log1 := logger.GetLogger("Campaigns findID")
log1.Debug(fmt.Sprintf("Look in campaign records for id %d.", ID))
for _, v := range campaigns {
if v.ID == ID {
return v
}
}
return CampaignFields{}
}
func (banners Banners) findID(campID int64, ID int64) (BannerFields, bool) {
log1 := logger.GetLogger("Banners findID")
log1.Debug(fmt.Sprintf("Look in banner records for id %d.", ID))
for _, v := range banners {
if v.ID == ID && v.CampaignID.Int64 == campID {
return v, true
}
}
return BannerFields{}, false
}
func (videos BannerVideos) findID(campID int64, ID int64) (BannerVideoFields, bool) {
log1 := logger.GetLogger("BannerVideos findID")
log1.Debug(fmt.Sprintf("Look in video records for id %d.", ID))
for _, v := range videos {
if v.ID == ID && v.CampaignID.Int64 == campID {
return v, true
}
}
return BannerVideoFields{}, false
}