@@ -60,62 +60,121 @@ func (s *StringService) Load(ctx context.Context, key string, max int64) ([]stri
60
60
}
61
61
62
62
func (s * StringService ) Save (ctx context.Context , values []string ) (int64 , error ) {
63
- mainScope := BatchStatement {}
64
63
driver := s .Driver
65
- for _ , e := range values {
66
- mainScope .Values = append (mainScope .Values , e )
64
+ l := len (values )
65
+ if l == 0 {
66
+ return 0 , nil
67
67
}
68
- query := ""
69
- holders := BuildPlaceHolders (len (mainScope .Values ), s .BuildParam )
70
- if driver == DriverPostgres {
71
- query = fmt .Sprintf ("insert into %s (%s) values %s on conflict do nothing" ,
72
- s .Table ,
73
- s .Field ,
74
- holders ,
75
- )
68
+ if driver == DriverPostgres || driver == DriverMysql {
69
+ ps := make ([]string , 0 )
70
+ p := make ([]interface {}, 0 )
71
+ for _ , str := range values {
72
+ p = append (p , str )
73
+ }
74
+ if driver == DriverPostgres {
75
+ for i := 1 ; i <= l ; i ++ {
76
+ ps = append (ps , "(" + BuildDollarParam (i ) + ")" )
77
+ }
78
+ } else {
79
+ for i := 1 ; i <= l ; i ++ {
80
+ ps = append (ps , "(?)" )
81
+ }
82
+ }
83
+ var query string
84
+ if driver == DriverPostgres {
85
+ query = fmt .Sprintf ("insert into %s (%s) values %s on conflict do nothing" , s .Table , s .Field , strings .Join (ps , "," ))
86
+ } else {
87
+ query = fmt .Sprintf ("insert ignore into %s (%s) values %s" , s .Table , s .Field , strings .Join (ps , "," ))
88
+ }
89
+ tx , err := s .DB .Begin ()
90
+ if err != nil {
91
+ return - 1 , err
92
+ }
93
+ res , err := tx .ExecContext (ctx , query , p ... )
94
+ if err != nil {
95
+ er := tx .Rollback ()
96
+ if er != nil {
97
+ return - 1 , er
98
+ }
99
+ return - 1 , err
100
+ }
101
+ err = tx .Commit ()
102
+ if err != nil {
103
+ return - 1 , err
104
+ }
105
+ return res .RowsAffected ()
76
106
} else if driver == DriverSqlite3 {
77
- query = fmt .Sprintf ("insert or ignore into %s (%s) values %s" ,
78
- s .Table ,
79
- s .Field ,
80
- holders ,
81
- )
82
- } else if driver == DriverMysql {
83
- query = fmt .Sprintf ("insert ignore %s (%s) values %s " ,
84
- s .Table ,
85
- s .Field ,
86
- holders ,
87
- )
88
- } else if driver == DriverOracle || driver == DriverMssql {
89
- onDupe := s .Table + "." + s .Field + " = " + "temp." + s .Field
90
- value := "temp." + s .Field
91
- query = fmt .Sprintf ("merge into %s using (values %s) as temp (%s) on %s when not matched then insert (%s) values (%s);" ,
92
- s .Table ,
93
- holders ,
94
- s .Field ,
95
- onDupe ,
96
- s .Field ,
97
- value ,
98
- )
107
+ tx , err := s .DB .Begin ()
108
+ if err != nil {
109
+ return - 1 , err
110
+ }
111
+ var c int64
112
+ c = 0
113
+ for _ , e := range values {
114
+ query := fmt .Sprintf ("insert or ignore into %s (%s) values (?)" , s .Table , s .Field )
115
+ res , err := tx .ExecContext (ctx , query , e )
116
+ if err != nil {
117
+ er := tx .Rollback ()
118
+ if er != nil {
119
+ return - 1 , er
120
+ }
121
+ return - 1 , err
122
+ }
123
+ a , err := res .RowsAffected ()
124
+ if err != nil {
125
+ return - 1 , err
126
+ }
127
+ c = c + a
128
+ }
129
+ err = tx .Commit ()
130
+ if err != nil {
131
+ return - 1 , err
132
+ }
133
+ return c , nil
99
134
} else {
100
- return 0 , fmt .Errorf ("unsupported db vendor, current vendor is %s" , driver )
101
- }
102
- mainScope .Query = query
103
- x , err := s .DB .ExecContext (ctx , mainScope .Query , mainScope .Values ... )
104
- if err != nil {
105
- return 0 , err
135
+ mainScope := BatchStatement {}
136
+ for _ , e := range values {
137
+ mainScope .Values = append (mainScope .Values , e )
138
+ }
139
+ query := ""
140
+ holders := BuildPlaceHolders (len (mainScope .Values ), s .BuildParam )
141
+ if driver == DriverOracle || driver == DriverMssql {
142
+ onDupe := s .Table + "." + s .Field + " = " + "temp." + s .Field
143
+ value := "temp." + s .Field
144
+ query = fmt .Sprintf ("merge into %s using (values %s) as temp (%s) on %s when not matched then insert (%s) values (%s);" ,
145
+ s .Table ,
146
+ holders ,
147
+ s .Field ,
148
+ onDupe ,
149
+ s .Field ,
150
+ value ,
151
+ )
152
+ } else {
153
+ return 0 , fmt .Errorf ("unsupported db vendor, current vendor is %s" , driver )
154
+ }
155
+ mainScope .Query = query
156
+ x , err := s .DB .ExecContext (ctx , mainScope .Query , mainScope .Values ... )
157
+ if err != nil {
158
+ return 0 , err
159
+ }
160
+ return x .RowsAffected ()
106
161
}
107
- return x .RowsAffected ()
108
162
}
109
163
110
164
func (s * StringService ) Delete (ctx context.Context , values []string ) (int64 , error ) {
111
165
var arrValue []string
112
166
le := len (values )
167
+ buildParam := GetBuild (s .DB )
168
+ p := make ([]interface {}, 0 )
169
+ for _ , str := range values {
170
+ p = append (p , str )
171
+ }
113
172
for i := 1 ; i <= le ; i ++ {
114
- param := BuildParam (i )
173
+ param := buildParam (i )
115
174
arrValue = append (arrValue , param )
116
175
}
117
176
query := `delete from ` + s .Table + ` where ` + s .Field + ` in (` + strings .Join (arrValue , "," ) + `)`
118
- x , err := s .DB .ExecContext (ctx , query )
177
+ x , err := s .DB .ExecContext (ctx , query , p ... )
119
178
if err != nil {
120
179
return 0 , err
121
180
}
0 commit comments