comparison pkg/imports/config.go @ 1739:61fa62f01f20

Configured imports: When GET an import deliver the extra attributes, too.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Jan 2019 17:04:02 +0100
parents 49e047c2106e
children 807569b08513
comparison
equal deleted inserted replaced
1738:8202d485331b 1739:61fa62f01f20
119 SELECT k, v 119 SELECT k, v
120 FROM waterway.import_configuration_attributes 120 FROM waterway.import_configuration_attributes
121 WHERE import_configuration_id = $1` 121 WHERE import_configuration_id = $1`
122 ) 122 )
123 123
124 func loadIDConfig(id int64) (*IDConfig, error) { 124 // LoadIDConfigContext loads an import configuration from database.
125 func LoadIDConfigContext(ctx context.Context, conn *sql.Conn, id int64) (*IDConfig, error) {
125 126
126 ctx := context.Background()
127 cfg := &IDConfig{ID: id} 127 cfg := &IDConfig{ID: id}
128 128 var kind ImportKind
129 err := auth.RunAs(ctx, configUser, func(conn *sql.Conn) error { 129 var cron, url sql.NullString
130 var kind ImportKind 130 err := conn.QueryRowContext(ctx, loadConfigSQL, id).Scan(
131 var cron, url sql.NullString 131 &cfg.User,
132 if err := conn.QueryRowContext(ctx, loadConfigSQL, id).Scan( 132 &kind,
133 &cfg.User, 133 &cfg.SendEMail,
134 &kind, 134 &cfg.AutoAccept,
135 &cfg.SendEMail, 135 &cron,
136 &cfg.AutoAccept, 136 &url,
137 &cron, 137 )
138 &url,
139 ); err != nil {
140 return err
141 }
142 cfg.Kind = ImportKind(kind)
143 if cron.Valid {
144 c := CronSpec(cron.String)
145 cfg.Cron = &c
146 }
147 if url.Valid {
148 cfg.URL = &url.String
149 }
150 // load the extra attributes.
151 rows, err := conn.QueryContext(ctx, loadConfigAttributesSQL, id)
152 if err != nil {
153 return err
154 }
155 defer rows.Close()
156 var attributes common.Attributes
157 for rows.Next() {
158 var k, v string
159 if err = rows.Scan(&k, &v); err != nil {
160 return err
161 }
162 if attributes == nil {
163 attributes = common.Attributes{}
164 }
165 attributes[k] = v
166 }
167 if err = rows.Err(); err != nil {
168 return err
169 }
170 if len(attributes) > 0 {
171 cfg.Attributes = attributes
172 }
173 return nil
174 })
175 138
176 switch { 139 switch {
177 case err == sql.ErrNoRows: 140 case err == sql.ErrNoRows:
178 return nil, nil 141 return nil, nil
179 case err != nil: 142 case err != nil:
180 return nil, err 143 return nil, err
181 } 144 }
182 145
146 cfg.Kind = ImportKind(kind)
147 if cron.Valid {
148 c := CronSpec(cron.String)
149 cfg.Cron = &c
150 }
151 if url.Valid {
152 cfg.URL = &url.String
153 }
154 // load the extra attributes.
155 rows, err := conn.QueryContext(ctx, loadConfigAttributesSQL, id)
156 if err != nil {
157 return nil, err
158 }
159 defer rows.Close()
160 var attributes common.Attributes
161 for rows.Next() {
162 var k, v string
163 if err = rows.Scan(&k, &v); err != nil {
164 return nil, err
165 }
166 if attributes == nil {
167 attributes = common.Attributes{}
168 }
169 attributes[k] = v
170 }
171 if err = rows.Err(); err != nil {
172 return nil, err
173 }
174 if len(attributes) > 0 {
175 cfg.Attributes = attributes
176 }
183 return cfg, nil 177 return cfg, nil
184 } 178 }
179
180 func loadIDConfig(id int64) (*IDConfig, error) {
181 return loadIDConfigContext(context.Background(), id)
182 }
183
184 func loadIDConfigContext(ctx context.Context, id int64) (*IDConfig, error) {
185 var cfg *IDConfig
186 err := auth.RunAs(ctx, configUser, func(conn *sql.Conn) error {
187 var err error
188 cfg, err = LoadIDConfigContext(ctx, conn, id)
189 return err
190 })
191 return cfg, err
192 }