MCPcopy
hub / github.com/daptin/daptin / SetConfigValueFor

Method SetConfigValueFor

server/resource/cms_config.go:338–398  ·  view source on GitHub ↗
(key string, val interface{}, configtype string, transaction *sqlx.Tx)

Source from the content-addressed store, hash-verified

336}
337
338func (configStore *ConfigStore) SetConfigValueFor(key string, val interface{}, configtype string, transaction *sqlx.Tx) error {
339 var previousValue string
340
341 s, v, err := statementbuilder.Squirrel.Select("value").Prepared(true).
342 From(settingsTableName).
343 Where(goqu.Ex{"name": key}).
344 Where(goqu.Ex{"configstate": "enabled"}).
345 Where(goqu.Ex{"configtype": configtype}).
346 Where(goqu.Ex{"configenv": configStore.defaultEnv}).ToSQL()
347
348 CheckErr(err, "Failed to create config select query")
349
350 stmt1, err := transaction.Preparex(s)
351 if err != nil {
352 log.Errorf("[280] failed to prepare statment: %v", err)
353 return nil
354 }
355
356 errScan := stmt1.QueryRowx(v...).Scan(&previousValue)
357
358 err = stmt1.Close()
359 if err != nil {
360 log.Errorf("failed to close prepared statement: %v", err)
361 return err
362 }
363
364 if errScan != nil {
365
366 // row doesnt exist
367 s, v, err := statementbuilder.Squirrel.
368 Insert(settingsTableName).Prepared(true).Cols("name", "configstate", "configtype", "configenv", "value").
369 Vals([]interface{}{key, "enabled", configtype, configStore.defaultEnv, val}).ToSQL()
370
371 CheckErr(err, "failed to create config insert query")
372
373 _, err = transaction.Exec(s, v...)
374 CheckErr(err, "Failed to execute config insert query")
375 return err
376 } else {
377
378 // row already exists
379
380 s, v, err := statementbuilder.Squirrel.Update(settingsTableName).Prepared(true).
381 Set(goqu.Record{
382 "value": val,
383 "updated_at": time.Now(),
384 "previousvalue": previousValue,
385 }).
386 Where(goqu.Ex{"name": key}).
387 Where(goqu.Ex{"configstate": "enabled"}).
388 Where(goqu.Ex{"configtype": configtype}).
389 Where(goqu.Ex{"configenv": configStore.defaultEnv}).ToSQL()
390
391 CheckErr(err, "Failed to create config insert query")
392
393 _, err = transaction.Exec(s, v...)
394 CheckErr(err, "Failed to execute config update query")
395 return err

Callers 15

createServerFunction · 0.95
mainFunction · 0.95
MainFunction · 0.95
NewLanguageMiddlewareFunction · 0.80
InitializeImapResourcesFunction · 0.80
CheckSystemSecretsFunction · 0.80
CreateYjsStoreFunction · 0.80
InitializeFtpResourcesFunction · 0.80
CreateConfigHandlerFunction · 0.80

Calls 9

ScanMethod · 0.80
QueryRowxMethod · 0.80
ExecMethod · 0.80
SetMethod · 0.80
CheckErrFunction · 0.70
SelectMethod · 0.65
PreparexMethod · 0.65
CloseMethod · 0.65
UpdateMethod · 0.65

Tested by 1

createServerFunction · 0.76