comparison pkg/imports/config.go @ 2049:d1a680be7ae4 unify_imports

Imports. Re-enabled /imports/config GET.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 27 Jan 2019 14:27:37 +0100
parents 10a1e139d2e8
children a3ccef8ec304
comparison
equal deleted inserted replaced
2048:3c4b8f4815af 2049:d1a680be7ae4
34 } 34 }
35 35
36 ImportConfigOut struct { 36 ImportConfigOut struct {
37 ID int64 `json:"id"` 37 ID int64 `json:"id"`
38 Kind ImportKind `json:"kind"` 38 Kind ImportKind `json:"kind"`
39 User string `json:"user"`
39 Config interface{} `json:"config,omitempty"` 40 Config interface{} `json:"config,omitempty"`
40 } 41 }
41 42
42 PersistentConfig struct { 43 PersistentConfig struct {
43 ID int64 44 ID int64
76 77
77 loadPersistentConfigAttributesSQL = ` 78 loadPersistentConfigAttributesSQL = `
78 SELECT k, v 79 SELECT k, v
79 FROM import.import_configuration_attributes 80 FROM import.import_configuration_attributes
80 WHERE import_configuration_id = $1` 81 WHERE import_configuration_id = $1`
82
83 deleteImportConfiguationAttributesSQL = `
84 DELETE FROM import.import_configuration_attributes
85 WHERE import_configuration_id = $1`
86
87 updateImportConfigurationSQL = `
88 UPDATE import.import_configuration SET
89 username = $2,
90 kind = $3
91 WHERE id = $1`
92
93 selectImportConfigurationsByID = `
94 SELECT
95 c.id AS id,
96 username,
97 kind,
98 a.k,
99 a.v
100 FROM import.import_configuration c JOIN
101 import.import_configuration_attributes a
102 ON c.id = a.import_configuration_id
103 ORDER by c.id`
81 ) 104 )
105
106 func (pc *PersistentConfig) Update(ctx context.Context, tx *sql.Tx) error {
107 if _, err := tx.ExecContext(ctx, deleteImportConfiguationAttributesSQL, pc.ID); err != nil {
108 return err
109 }
110 return nil
111 }
82 112
83 func LoadPersistentConfigContext(ctx context.Context, conn *sql.Conn, id int64) (*PersistentConfig, error) { 113 func LoadPersistentConfigContext(ctx context.Context, conn *sql.Conn, id int64) (*PersistentConfig, error) {
84 114
85 cfg := &PersistentConfig{ID: id} 115 cfg := &PersistentConfig{ID: id}
86 116
133 cfg, err = LoadPersistentConfigContext(ctx, conn, id) 163 cfg, err = LoadPersistentConfigContext(ctx, conn, id)
134 return err 164 return err
135 }) 165 })
136 return cfg, err 166 return cfg, err
137 } 167 }
168
169 func ListAllPersistentConfigurationsContext(
170 ctx context.Context,
171 conn *sql.Conn,
172 fn func(*ImportConfigOut) error,
173 ) error {
174
175 rows, err := conn.QueryContext(ctx, selectImportConfigurationsByID)
176 if err != nil {
177 return err
178 }
179 defer rows.Close()
180
181 var (
182 first = true
183 lastID int64
184 pc PersistentConfig
185 k, v sql.NullString
186 )
187
188 send := func() error {
189 kind := JobKind(pc.Kind)
190 ctor := ImportModelForJobKind(kind)
191 if ctor == nil {
192 return fmt.Errorf("unable to deserialize kind '%s'", pc.Kind)
193 }
194 config := ctor()
195 pc.Attributes.Unmarshal(config)
196 return fn(&ImportConfigOut{
197 ID: pc.ID,
198 Kind: ImportKind(pc.Kind),
199 User: pc.User,
200 Config: config,
201 })
202 }
203
204 for rows.Next() {
205 if err := rows.Scan(&pc.ID, &pc.User, &k, &v); err != nil {
206 return err
207 }
208 if !first {
209 if lastID != pc.ID {
210 if err := send(); err != nil {
211 return err
212 }
213 pc.Attributes = nil
214 }
215 } else {
216 first = false
217 }
218
219 if k.Valid && v.Valid {
220 if pc.Attributes == nil {
221 pc.Attributes = common.Attributes{}
222 }
223 pc.Attributes.Set(k.String, v.String)
224 }
225
226 lastID = pc.ID
227 }
228
229 if err := rows.Err(); err != nil {
230 return err
231 }
232
233 err = nil
234 if !first {
235 err = send()
236 }
237 return err
238 }