Skip to content

Commit

Permalink
Merge pull request #95 from d-ylee/feature-87-update_physics_group_na…
Browse files Browse the repository at this point in the history
…me_in_datasets

Initial feature for Issue #87

Allows a dataset's `physics_group_id` to be updated to another, existing physics_group
  • Loading branch information
d-ylee authored Apr 3, 2023
2 parents 928dc25 + 35687ca commit a4fdedb
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 71 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ test-integration:
DBS_API_PARAMETERS_FILE=../static/parameters.json \
DBS_READER_LEXICON_FILE=../static/lexicon_reader.json \
DBS_WRITER_LEXICON_FILE=../static/lexicon_writer.json \
DBS_DB_FILE=./dbfile \
DBS_DB_FILE=/tmp/dbs-test.db \
INTEGRATION_DATA_FILE=./data/integration/integration_data.json \
BULKBLOCKS_DATA_FILE=./data/integration/bulkblocks_data.json \
Expand Down
161 changes: 113 additions & 48 deletions dbs/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

// Datasets API
//
//gocyclo:ignore
func (a *API) Datasets() error {
if utils.VERBOSE > 1 {
Expand Down Expand Up @@ -355,6 +356,7 @@ func (r *Datasets) Insert(tx *sql.Tx) error {
}

// Validate implementation of Datasets
//
//gocyclo:ignore
func (r *Datasets) Validate() error {
if err := CheckPattern("dataset", r.DATASET); err != nil {
Expand Down Expand Up @@ -474,6 +476,7 @@ type DatasetRecord struct {
// - processing era info
// - output module config info
// - insert dataset info
//
//gocyclo:ignore
func (a *API) InsertDatasets() error {
// read given input
Expand Down Expand Up @@ -501,7 +504,7 @@ func (a *API) InsertDatasets() error {
// start transaction
tx, err := DB.Begin()
if err != nil {
msg := fmt.Sprintf("unable to get DB transaction")
msg := "unable to get DB transaction"
return Error(err, TransactionErrorCode, msg, "dbs.datasets.InsertDatasets")
}
defer tx.Rollback()
Expand Down Expand Up @@ -661,60 +664,91 @@ func (a *API) InsertDatasets() error {
return nil
}

// Validate POST/PUT Parameters
func ValidateParameter(params Record, key string) (string, error) {
var value string
value, err := getSingleValue(params, key)
if err != nil {
return "", Error(err, ParseErrorCode, "", "dbs.datasets.UpdateDatasets")
}
if value == "" {
msg := fmt.Sprintf("invalid %s parameter", key)
return "", Error(InvalidParamErr, ParametersErrorCode, msg, "dbs.datasets.UpdateDatasets")
}
if err := CheckPattern(key, value); err != nil {
msg := fmt.Sprintf("%s parameter pattern invalid", key)
return "", Error(err, PatternErrorCode, msg, "dbs.datasets.UpdateDatasets")
}
return value, nil
}

// UpdateDatasets DBS API
//
//gocyclo:ignore
func (a *API) UpdateDatasets() error {

// get accessTypeID from Access dataset types table
var args []interface{}

tmpl := make(Record)
tmpl["Owner"] = DBOWNER
tmpl["PhysicsGroup"] = false
tmpl["DatasetAccessType"] = false

// validate parameteres
var createBy string
if v, ok := a.Params["create_by"]; ok {
switch t := v.(type) {
case string:
createBy = t
case []string:
createBy = t[0]
if _, ok := a.Params["create_by"]; ok {
v, err := ValidateParameter(a.Params, "create_by")
if err != nil {
return Error(err, ValidateErrorCode, "", "dbs.datasets.UpdateDatasets")
}
createBy = v
}

date := time.Now().Unix()

var isValidDataset int64
var dataset string
var datasetAccessType string
if v, ok := a.Params["dataset"]; ok {
switch t := v.(type) {
case string:
dataset = t
case []string:
dataset = t[0]
var physicsGroupName string
// validate dataset_access_type parameter
if _, ok := a.Params["dataset_access_type"]; ok {
tmpl["DatasetAccessType"] = true
v, err := ValidateParameter(a.Params, "dataset_access_type")
if err != nil {
return Error(err, ValidateErrorCode, "", "dbs.datasets.UpdateDatasets")
}
datasetAccessType = v
}
if v, ok := a.Params["dataset_access_type"]; ok {
switch t := v.(type) {
case string:
datasetAccessType = t
case []string:
datasetAccessType = t[0]

// validate physics_group_name parameter
// ^[a-zA-Z0-9/][a-zA-Z0-9\\-_']*$
if _, ok := a.Params["physics_group_name"]; ok {
tmpl["PhysicsGroup"] = true
v, err := ValidateParameter(a.Params, "physics_group_name")
if err != nil {
return Error(err, ValidateErrorCode, "", "dbs.datasets.UpdateDatasets")
}
physicsGroupName = v
}
date := time.Now().Unix()

// validate input parameters
if dataset == "" {
msg := "invalid dataset parameter"
return Error(InvalidParamErr, ParametersErrorCode, msg, "dbs.datasets.UpdateDatasets")
}
if createBy == "" {
msg := "invalid create_by parameter"
return Error(InvalidParamErr, ParametersErrorCode, msg, "dbs.datasets.UpdateDatasets")
}
if datasetAccessType == "" {
msg := "invalid datasetAccessType parameter"
return Error(InvalidParamErr, ParametersErrorCode, msg, "dbs.datasets.UpdateDatasets")
}
if datasetAccessType == "VALID" {
isValidDataset = 1
// validate dataset parameter
if _, ok := a.Params["dataset"]; ok {
v, err := ValidateParameter(a.Params, "dataset")
if err != nil {
return Error(err, ValidateErrorCode, "", "dbs.datasets.UpdateDatasets")
}
dataset = v
if datasetAccessType == "VALID" {
isValidDataset = 1
}
}

// get SQL statement from static area
stm := getSQL("update_datasets")
// stm := getSQL("update_datasets")
stm, err := LoadTemplateSQL("update_datasets", tmpl)
if err != nil {
return Error(err, LoadErrorCode, "", "dbs.datasets.UpdateDatasets")
}
if utils.VERBOSE > 0 {
params := []string{dataset, datasetAccessType}
log.Printf("update Datasets\n%s\n%+v", stm, params)
Expand All @@ -727,19 +761,50 @@ func (a *API) UpdateDatasets() error {
return Error(err, TransactionErrorCode, "", "dbs.datasets.UpdateDatasets")
}
defer tx.Rollback()
accessTypeID, err := GetID(
tx,
"DATASET_ACCESS_TYPES",
"dataset_access_type_id",
"dataset_access_type",
datasetAccessType)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to find dataset_access_type_id for", datasetAccessType)

args = append(args, createBy)
args = append(args, date)

var physicsGroupID int64
if tmpl["PhysicsGroup"].(bool) {
physicsGroupID, err = GetID(
tx,
"PHYSICS_GROUPS",
"physics_group_id",
"physics_group_name",
physicsGroupName)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to find physics_group_id for", physicsGroupName)
}
return Error(err, GetIDErrorCode, "", "dbs.datasets.UpdateDatasets")
}
args = append(args, physicsGroupID)
}

// get accessTypeID from Access dataset types table
if tmpl["DatasetAccessType"].(bool) {
accessTypeID, err := GetID(
tx,
"DATASET_ACCESS_TYPES",
"dataset_access_type_id",
"dataset_access_type",
datasetAccessType)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to find dataset_access_type_id for", datasetAccessType)
}
return Error(err, GetIDErrorCode, "", "dbs.datasets.UpdateDatasets")
}
return Error(err, GetIDErrorCode, "", "dbs.datasets.UpdateDatasets")
args = append(args, accessTypeID)
args = append(args, isValidDataset)
}
_, err = tx.Exec(stm, createBy, date, accessTypeID, isValidDataset, dataset)

args = append(args, dataset)

// perform update
// _, err = tx.Exec(stm, createBy, date, accessTypeID, isValidDataset, physicsGroupID, dataset)
_, err = tx.Exec(stm, args...)
if err != nil {
if utils.VERBOSE > 0 {
log.Printf("unable to update %v", err)
Expand Down
8 changes: 5 additions & 3 deletions dbs/dbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ func getValues(params Record, key string) []string {
return v
case string:
return []string{v}
case interface{}:
return []string{fmt.Sprintf("%v", v)}
case []interface{}:
for _, val := range v {
out = append(out, fmt.Sprintf("%v", val))
}
return out
case interface{}:
return []string{fmt.Sprintf("%v", v)}
}
}
return out
Expand All @@ -281,7 +281,7 @@ func getSingleValue(params Record, key string) (string, error) {
if len(values) > 0 {
return values[0], nil
}
msg := fmt.Sprintf("no list is allowed for provided key: %s", key)
msg := fmt.Sprintf("list is not allowed for provided key: %s", key)
return "", Error(InvalidParamErr, ParseErrorCode, msg, "dbs.getSingleValue")
}

Expand Down Expand Up @@ -348,6 +348,7 @@ func CleanStatement(stm string) string {
// here we use http response writer in order to make encoder
// then we literally stream data with our encoder (i.e. write records
// to writer)
//
//gocyclo:ignore
func executeAll(w io.Writer, sep, stm string, args ...interface{}) error {
stm = CleanStatement(stm)
Expand Down Expand Up @@ -458,6 +459,7 @@ func executeAll(w io.Writer, sep, stm string, args ...interface{}) error {
}

// similar to executeAll function but it takes explicit set of columns and values
//
//gocyclo:ignore
func execute(
w io.Writer,
Expand Down
2 changes: 1 addition & 1 deletion static/lexicon_reader.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
{
"name": "physics_group",
"patterns": [
"^([a-zA-Z0-9\\-_]+)$"
"^[a-zA-Z0-9][a-zA-Z0-9\\-_]*$"
],
"length": -1
},
Expand Down
2 changes: 1 addition & 1 deletion static/lexicon_writer.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
{
"name": "physics_group",
"patterns": [
"^([a-zA-Z0-9\\-_]+)$"
"^[a-zA-Z0-9][a-zA-Z0-9\\-_]*$"
],
"length": -1
},
Expand Down
11 changes: 8 additions & 3 deletions static/sql/update_datasets.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
UPDATE {{.Owner}}.DATASETS
SET LAST_MODIFIED_BY=:myuser,
LAST_MODIFICATION_DATE=:mydate,
DATASET_ACCESS_TYPE_ID = :dataset_access_type_id,
IS_DATASET_VALID = :is_dataset_valid
LAST_MODIFICATION_DATE=:mydate
{{ if .PhysicsGroup }}
,PHYSICS_GROUP_ID = :physics_group_id
{{ end }}
{{ if .DatasetAccessType }}
,DATASET_ACCESS_TYPE_ID = :dataset_access_type_id
,IS_DATASET_VALID = :is_dataset_valid
{{ end }}
WHERE DATASET = :dataset
Loading

0 comments on commit a4fdedb

Please sign in to comment.