-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprimarydatasets.go
201 lines (182 loc) · 5.83 KB
/
primarydatasets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package dbs
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
"github.com/dmwm/dbs2go/utils"
)
// PrimaryDatasets DBS API
func (a *API) PrimaryDatasets() error {
var args []interface{}
var conds []string
conds, args = AddParam("primary_ds_name", "P.PRIMARY_DS_NAME", a.Params, conds, args)
conds, args = AddParam("primary_ds_type", "PT.PRIMARY_DS_TYPE", a.Params, conds, args)
// get SQL statement from static area
stm := getSQL("primarydatasets")
stm = WhereClause(stm, conds)
// use generic query API to fetch the results from DB
err := executeAll(a.Writer, a.Separator, stm, args...)
if err != nil {
return Error(err, QueryErrorCode, "", "dbs.primarydatasets.PrimaryDataset")
}
return nil
}
// PrimaryDatasets represents Primary Datasets DBS DB table
type PrimaryDatasets struct {
PRIMARY_DS_ID int64 `json:"primary_ds_id"`
PRIMARY_DS_NAME string `json:"primary_ds_name" validate:"required"`
PRIMARY_DS_TYPE_ID int64 `json:"primary_ds_type_id" validate:"required,number,gt=0"`
CREATION_DATE int64 `json:"creation_date" validate:"required,number,gt=0"`
CREATE_BY string `json:"create_by" validate:"required"`
}
// Insert implementation of PrimaryDatasets
func (r *PrimaryDatasets) Insert(tx *sql.Tx) error {
var tid int64
var err error
if r.PRIMARY_DS_ID == 0 {
if DBOWNER == "sqlite" {
tid, err = LastInsertID(tx, "PRIMARY_DATASETS", "primary_ds_id")
r.PRIMARY_DS_ID = tid + 1
} else {
tid, err = IncrementSequence(tx, "SEQ_PDS")
r.PRIMARY_DS_ID = tid
}
if err != nil {
return Error(err, LastInsertErrorCode, "", "dbs.primarydatasets.Insert")
}
}
// set defaults and validate the record
r.SetDefaults()
err = r.Validate()
if err != nil {
log.Println("unable to validate record", err)
return Error(err, ValidateErrorCode, "", "dbs.primarydatasets.Insert")
}
// get SQL statement from static area
stm := getSQL("insert_primary_datasets")
if utils.VERBOSE > 0 {
log.Printf("Insert PrimaryDatasets\n%s\n%+v", stm, r)
}
_, err = tx.Exec(
stm,
r.PRIMARY_DS_ID,
r.PRIMARY_DS_NAME,
r.PRIMARY_DS_TYPE_ID,
r.CREATION_DATE,
r.CREATE_BY)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unablt to insert PrimaryDatasets", err)
}
return Error(err, InsertErrorCode, "", "dbs.primarydatasets.Insert")
}
return nil
}
// Validate implementation of PrimaryDatasets
func (r *PrimaryDatasets) Validate() error {
if err := RecordValidator.Struct(*r); err != nil {
return DecodeValidatorError(r, err)
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATION_DATE)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.primarydatasets.Validate")
}
return nil
}
// SetDefaults implements set defaults for PrimaryDatasets
func (r *PrimaryDatasets) SetDefaults() {
if r.CREATION_DATE == 0 {
r.CREATION_DATE = Date()
}
}
// Decode implementation for PrimaryDatasets
func (r *PrimaryDatasets) Decode(reader io.Reader) error {
// init record with given data record
data, err := io.ReadAll(reader)
if err != nil {
log.Println("fail to read data", err)
return Error(err, ReaderErrorCode, "", "dbs.primarydatasets.Decode")
}
err = json.Unmarshal(data, &r)
// decoder := json.NewDecoder(r)
// err := decoder.Decode(&rec)
if err != nil {
log.Println("fail to decode data", err)
return Error(err, UnmarshalErrorCode, "", "dbs.primarydatasets.Decode")
}
return nil
}
// PrimaryDatasetRecord represents primary dataset record
type PrimaryDatasetRecord struct {
PRIMARY_DS_NAME string `json:"primary_ds_name" validate:"required"`
PRIMARY_DS_TYPE string `json:"primary_ds_type" validate:"required"`
CREATION_DATE int64 `json:"creation_date" validate:"required,number,gt=0"`
CREATE_BY string `json:"create_by" validate:"required"`
}
// InsertPrimaryDatasets DBS API
func (a *API) InsertPrimaryDatasets() error {
// read given input
data, err := io.ReadAll(a.Reader)
if err != nil {
log.Println("fail to read data", err)
return Error(err, ReaderErrorCode, "", "dbs.primarydatasets.InsertPrimaryDatasets")
}
rec := PrimaryDatasetRecord{CREATE_BY: a.CreateBy}
err = json.Unmarshal(data, &rec)
if err != nil {
log.Println("fail to decode data", err)
return Error(err, UnmarshalErrorCode, "", "dbs.primarydatasets.InsertPrimaryDatasets")
}
pdst := rec.PRIMARY_DS_TYPE
pdsname := rec.PRIMARY_DS_NAME
trec := PrimaryDSTypes{PRIMARY_DS_TYPE: pdst}
prec := PrimaryDatasets{
PRIMARY_DS_NAME: pdsname,
CREATION_DATE: rec.CREATION_DATE,
CREATE_BY: rec.CREATE_BY,
}
// start transaction
tx, err := DB.Begin()
if err != nil {
return Error(err, TransactionErrorCode, "", "dbs.primarydatasets.InsertPrimaryDatasets")
}
defer tx.Rollback()
// check if our data already exist in DB
if IfExist(tx, "PRIMARY_DATASETS", "primary_ds_id", "primary_ds_name", pdsname) {
if a.Writer != nil {
a.Writer.Write([]byte(`[]`))
}
return nil
}
// check if PrimaryDSType exists in DB
pdstID, err := GetID(tx, "PRIMARY_DS_TYPES", "primary_ds_type_id", "primary_ds_type", pdst)
if err != nil {
if utils.VERBOSE > 0 {
log.Println("unable to look-up primary_ds_type_id for", pdst, "error", err, "will insert...")
}
// insert PrimaryDSType record
err = trec.Insert(tx)
if err != nil {
return Error(err, InsertErrorCode, "", "dbs.primarydatasets.InsertPrimaryDatasets")
}
pdstID = trec.PRIMARY_DS_TYPE_ID
}
// init all foreign Id's in output config record
prec.PRIMARY_DS_TYPE_ID = pdstID
err = prec.Insert(tx)
if err != nil {
return Error(err, InsertErrorCode, "", "dbs.primarydatasets.InsertPrimaryDatasets")
}
// commit transaction
err = tx.Commit()
if err != nil {
log.Println("fail to insert primarydatasets", err)
return Error(err, CommitErrorCode, "", "dbs.primarydatasets.InsertPrimaryDatasets")
}
if a.Writer != nil {
a.Writer.Write([]byte(`[]`))
}
return nil
}