/
updateProfile.go
171 lines (145 loc) · 4.62 KB
/
updateProfile.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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package db
import (
"database/sql"
)
// UpdateProfile insert new or replace existing profile in profile_lst and profile_option tables.
//
// It always replacing all existing rows by delete from profile_lst and profile_option where profile_name = profile.Name
// and insert new rows into profile_lst and profile_option tables.
func UpdateProfile(dbConn *sql.DB, profile *ProfileMeta) error {
// source is empty: nothing to do, exit
if profile == nil || profile.Name == "" {
return nil
}
// do update in transaction scope
trx, err := dbConn.Begin()
if err != nil {
return err
}
if err = doDeleteProfile(trx, profile.Name); err != nil {
trx.Rollback()
return err
}
if err = doInsertProfile(trx, profile); err != nil {
trx.Rollback()
return err
}
trx.Commit()
return nil
}
// DeleteProfile delete rows from profile in profile_lst and profile_option tables.
func DeleteProfile(dbConn *sql.DB, name string) error {
// do update in transaction scope
trx, err := dbConn.Begin()
if err != nil {
return err
}
if err = doDeleteProfile(trx, name); err != nil {
trx.Rollback()
return err
}
trx.Commit()
return nil
}
// doDeleteProfile delete profile rows from profile_lst and profile_option tables.
// It does update as part of transaction.
func doDeleteProfile(trx *sql.Tx, name string) error {
// delete existing profile
qn := toQuoted(name)
err := TrxUpdate(trx, "DELETE FROM profile_option WHERE profile_name = "+qn)
if err != nil {
return err
}
err = TrxUpdate(trx, "DELETE FROM profile_lst WHERE profile_name = "+qn)
if err != nil {
return err
}
return nil
}
// doInsertProfile insert new profile in profile_lst and profile_option tables.
// It does update as part of transaction.
func doInsertProfile(trx *sql.Tx, profile *ProfileMeta) error {
// exit if profile name is empty
if profile.Name == "" {
return nil
}
// insert profile name
qn := toQuotedMax(profile.Name, nameDbMax)
err := TrxUpdate(trx, "INSERT INTO profile_lst (profile_name) VALUES ("+qn+")")
if err != nil {
return err
}
// insert profile options
for key, val := range profile.Opts {
err = TrxUpdate(trx, "INSERT INTO profile_option (profile_name, option_key, option_value)"+
" VALUES ("+qn+", "+toQuotedMax(key, nameDbMax)+", "+toQuotedMax(val, optionDbMax)+")")
if err != nil {
return err
}
}
return nil
}
// UpdateProfileOption insert new or replace existing profile option in profile_option table.
// It is also will insert profile name into profile_lst table if such name does not already exist.
func UpdateProfileOption(dbConn *sql.DB, name, key, val string) error {
// source is empty: nothing to do, exit
if name == "" || key == "" {
return nil
}
// do update in transaction scope
trx, err := dbConn.Begin()
if err != nil {
return err
}
if err = doUpdateProfileOption(trx, name, key, val); err != nil {
trx.Rollback()
return err
}
trx.Commit()
return nil
}
// DeleteProfileOption delete row from profile_option table.
func DeleteProfileOption(dbConn *sql.DB, name, key string) error {
// do delete in transaction scope
trx, err := dbConn.Begin()
if err != nil {
return err
}
if err = TrxUpdate(trx,
"DELETE FROM profile_option WHERE profile_name = "+toQuoted(name)+" AND option_key = "+toQuoted(key)); err != nil {
trx.Rollback()
return err
}
trx.Commit()
return nil
}
// doUpdateProfileOption insert new or replace existing profile option in profile_option table.
// It is also will insert profile name into profile_lst table if such name does not already exist.
// It does update as part of transaction.
func doUpdateProfileOption(trx *sql.Tx, name, key, val string) error {
// insert profile name if not already exist
qn := toQuotedMax(name, nameDbMax)
err := TrxUpdate(trx,
"INSERT INTO profile_lst (profile_name)"+
" SELECT "+qn+" FROM profile_lst S"+
" WHERE NOT EXISTS "+
" ("+
" SELECT * FROM profile_lst E WHERE E.profile_name = "+qn+
")")
if err != nil {
return err
}
// delete existing profile_option row and insert new option
err = TrxUpdate(trx, "DELETE FROM profile_option WHERE profile_name = "+qn+" AND option_key = "+toQuoted(key))
if err != nil {
return err
}
err = TrxUpdate(trx, "INSERT INTO profile_option (profile_name, option_key, option_value)"+
" VALUES ("+qn+", "+toQuotedMax(key, nameDbMax)+", "+toQuotedMax(val, optionDbMax)+")")
if err != nil {
return err
}
return nil
}