Skip to content

Commit bbdd5af

Browse files
- Decoupling pretty close.
1 parent b1136b8 commit bbdd5af

File tree

1 file changed

+116
-69
lines changed

1 file changed

+116
-69
lines changed

internal/stackql/primitivebuilder/single_select_acquire.go

Lines changed: 116 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,56 @@ func newActionInsertResult(isHousekeepingDone bool, err error) ActionInsertResul
206206
}
207207
}
208208

209-
//nolint:nestif,funlen,gocognit // acceptable for now
210-
func (ss *SingleSelectAcquire) actionInsertPreparation(
209+
type itemsDTO struct {
210+
items interface{}
211+
ok bool
212+
isNilPayload bool
213+
}
214+
215+
type ItemisationResult interface {
216+
GetItems() (interface{}, bool)
217+
IsOk() bool
218+
IsNilPayload() bool
219+
}
220+
221+
func (id *itemsDTO) GetItems() (interface{}, bool) {
222+
return id.items, id.items != nil
223+
}
224+
225+
func (id *itemsDTO) IsOk() bool {
226+
return id.ok
227+
}
228+
229+
func (id *itemsDTO) IsNilPayload() bool {
230+
return id.isNilPayload
231+
}
232+
233+
func newItemisationResult(
234+
items interface{},
235+
ok bool,
236+
isNilPayload bool,
237+
) ItemisationResult {
238+
return &itemsDTO{
239+
items: items,
240+
ok: ok,
241+
isNilPayload: isNilPayload,
242+
}
243+
}
244+
245+
//nolint:nestif // apathy
246+
func itemise(
211247
target interface{},
212248
resErr error,
213-
housekeepingDone bool,
214-
tableName string,
215-
paramsUsed map[string]interface{},
216-
reqEncoding string,
217-
) ActionInsertResult {
249+
selectItemsKey string,
250+
) ItemisationResult {
218251
var items interface{}
219252
var ok bool
220253
logging.GetLogger().Infoln(fmt.Sprintf("SingleSelectAcquire.Execute() target = %v", target))
221254
switch pl := target.(type) {
222255
// add case for xml object,
223256
case map[string]interface{}:
224-
if ss.tableMeta.GetSelectItemsKey() != "" && ss.tableMeta.GetSelectItemsKey() != "/*" {
225-
items, ok = pl[ss.tableMeta.GetSelectItemsKey()]
257+
if selectItemsKey != "" && selectItemsKey != "/*" {
258+
items, ok = pl[selectItemsKey]
226259
if !ok {
227260
if resErr != nil {
228261
items = []interface{}{}
@@ -247,76 +280,85 @@ func (ss *SingleSelectAcquire) actionInsertPreparation(
247280
items = pl
248281
ok = true
249282
case nil:
250-
return newActionInsertResult(housekeepingDone, nil)
283+
return newItemisationResult(nil, false, true)
251284
}
252-
keys := make(map[string]map[string]interface{})
285+
return newItemisationResult(items, ok, false)
286+
}
253287

254-
//nolint:nestif // TODO: fix
255-
if ok {
256-
iArr, iErr := castItemsArray(items)
257-
if iErr != nil {
258-
return newActionInsertResult(housekeepingDone, iErr)
259-
}
260-
streamErr := ss.stream.Write(iArr)
261-
if streamErr != nil {
262-
return newActionInsertResult(housekeepingDone, streamErr)
263-
}
264-
if ok && len(iArr) > 0 {
265-
if !housekeepingDone && ss.insertPreparedStatementCtx != nil {
266-
_, execErr := ss.handlerCtx.GetSQLEngine().Exec(ss.insertPreparedStatementCtx.GetGCHousekeepingQueries())
267-
tcc := ss.insertPreparedStatementCtx.GetGCCtrlCtrs()
268-
tcc.SetTableName(tableName)
269-
//nolint:errcheck // TODO: fix
270-
ss.insertionContainer.SetTableTxnCounters(tableName, tcc)
271-
housekeepingDone = true
272-
if execErr != nil {
273-
return newActionInsertResult(housekeepingDone, execErr)
274-
}
288+
//nolint:nestif,gocognit // acceptable for now
289+
func (ss *SingleSelectAcquire) actionInsertPreparation(
290+
itemisationResult ItemisationResult,
291+
housekeepingDone bool,
292+
tableName string,
293+
paramsUsed map[string]interface{},
294+
reqEncoding string,
295+
) ActionInsertResult {
296+
items, _ := itemisationResult.GetItems()
297+
keys := make(map[string]map[string]interface{})
298+
iArr, iErr := castItemsArray(items)
299+
if iErr != nil {
300+
return newActionInsertResult(housekeepingDone, iErr)
301+
}
302+
streamErr := ss.stream.Write(iArr)
303+
if streamErr != nil {
304+
return newActionInsertResult(housekeepingDone, streamErr)
305+
}
306+
if len(iArr) > 0 {
307+
if !housekeepingDone && ss.insertPreparedStatementCtx != nil {
308+
_, execErr := ss.handlerCtx.GetSQLEngine().Exec(ss.insertPreparedStatementCtx.GetGCHousekeepingQueries())
309+
tcc := ss.insertPreparedStatementCtx.GetGCCtrlCtrs()
310+
tcc.SetTableName(tableName)
311+
//nolint:errcheck // TODO: fix
312+
ss.insertionContainer.SetTableTxnCounters(tableName, tcc)
313+
housekeepingDone = true
314+
if execErr != nil {
315+
return newActionInsertResult(housekeepingDone, execErr)
275316
}
317+
}
276318

277-
for i, item := range iArr {
278-
if item != nil {
279-
if len(paramsUsed) > 0 {
280-
for k, v := range paramsUsed {
281-
if _, itemOk := item[k]; !itemOk {
282-
item[k] = v
283-
}
319+
for i, item := range iArr {
320+
if item != nil {
321+
if len(paramsUsed) > 0 {
322+
for k, v := range paramsUsed {
323+
if _, itemOk := item[k]; !itemOk {
324+
item[k] = v
284325
}
285326
}
327+
}
286328

287-
logging.GetLogger().Infoln(
288-
fmt.Sprintf(
289-
"running insert with query = '''%s''', control parameters: %v",
290-
ss.insertPreparedStatementCtx.GetQuery(),
291-
ss.insertPreparedStatementCtx.GetGCCtrlCtrs(),
292-
),
293-
)
294-
r, rErr := ss.drmCfg.ExecuteInsertDML(
295-
ss.handlerCtx.GetSQLEngine(),
296-
ss.insertPreparedStatementCtx,
297-
item,
298-
reqEncoding,
299-
)
300-
logging.GetLogger().Infoln(
301-
fmt.Sprintf(
302-
"insert result = %v, error = %v",
303-
r,
304-
rErr,
305-
),
329+
logging.GetLogger().Infoln(
330+
fmt.Sprintf(
331+
"running insert with query = '''%s''', control parameters: %v",
332+
ss.insertPreparedStatementCtx.GetQuery(),
333+
ss.insertPreparedStatementCtx.GetGCCtrlCtrs(),
334+
),
335+
)
336+
r, rErr := ss.drmCfg.ExecuteInsertDML(
337+
ss.handlerCtx.GetSQLEngine(),
338+
ss.insertPreparedStatementCtx,
339+
item,
340+
reqEncoding,
341+
)
342+
logging.GetLogger().Infoln(
343+
fmt.Sprintf(
344+
"insert result = %v, error = %v",
345+
r,
346+
rErr,
347+
),
348+
)
349+
if rErr != nil {
350+
expandedErr := fmt.Errorf(
351+
"sql insert error: '%w' from query: %s",
352+
rErr,
353+
ss.insertPreparedStatementCtx.GetQuery(),
306354
)
307-
if rErr != nil {
308-
expandedErr := fmt.Errorf(
309-
"sql insert error: '%w' from query: %s",
310-
rErr,
311-
ss.insertPreparedStatementCtx.GetQuery(),
312-
)
313-
return newActionInsertResult(housekeepingDone, expandedErr)
314-
}
315-
keys[strconv.Itoa(i)] = item
355+
return newActionInsertResult(housekeepingDone, expandedErr)
316356
}
357+
keys[strconv.Itoa(i)] = item
317358
}
318359
}
319360
}
361+
320362
return newActionInsertResult(housekeepingDone, nil)
321363
}
322364

@@ -431,9 +473,14 @@ func (ss *SingleSelectAcquire) Build() error {
431473
ss.handlerCtx.LogHTTPResponseMap(res.GetProcessedBody())
432474
logging.GetLogger().Infoln(fmt.Sprintf("SingleSelectAcquire.Execute() response = %v", res))
433475

476+
itemisationResult := itemise(res.GetProcessedBody(), resErr, ss.tableMeta.GetSelectItemsKey())
477+
478+
if itemisationResult.IsNilPayload() {
479+
break
480+
}
481+
434482
insertPrepResult := ss.actionInsertPreparation(
435-
res.GetProcessedBody(),
436-
resErr,
483+
itemisationResult,
437484
housekeepingDone,
438485
tableName,
439486
paramsUsed,

0 commit comments

Comments
 (0)