forked from manhcuongbk56/cypher-go-dsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_statement_builder.go
733 lines (665 loc) · 24.1 KB
/
default_statement_builder.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
package cypher
import (
"errors"
"golang.org/x/xerrors"
)
type DefaultStatementBuilder struct {
err error
currentSinglePartElements []Visitable
multipartElements []MultiPartElement
currentOngoingMatch MatchBuilder
currentOngoingUpdate DefaultStatementWithUpdateBuilder
currentOngoingCall ProcedureCallBuilder
}
func DefaultStatementBuilderCreate() DefaultStatementBuilder {
return DefaultStatementBuilder{
currentSinglePartElements: make([]Visitable, 0),
multipartElements: make([]MultiPartElement, 0),
}
}
func DefaultStatementBuilderCreate1(currentOngoingCall ProcedureCallBuilder) DefaultStatementBuilder {
return DefaultStatementBuilder{
currentOngoingCall: currentOngoingCall,
currentSinglePartElements: make([]Visitable, 0),
multipartElements: make([]MultiPartElement, 0),
}
}
func DefaultStatementBuilderError(err error) DefaultStatementBuilder {
return DefaultStatementBuilder{
err: err,
}
}
func (d DefaultStatementBuilder) Where(condition Condition) OngoingReadingWithWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if condition != nil && condition.GetError() != nil {
return DefaultStatementBuilderError(condition.GetError())
}
d.currentOngoingMatch.conditionBuilder.Where(condition)
return d
}
func (d DefaultStatementBuilder) WhereConditionContainer(container ConditionContainer) OngoingReadingWithWhere {
return d.Where(container.Get())
}
func (d DefaultStatementBuilder) AddWith(with with) DefaultStatementBuilder {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if with.GetError() != nil {
return DefaultStatementBuilderError(with.GetError())
}
if with.isNotNil() {
d.multipartElements = append(d.multipartElements, MultiPartElementCreate(d.BuildListOfVisitable(true), with))
}
return d
}
func (d DefaultStatementBuilder) WherePattern(pattern RelationshipPattern) OngoingReadingWithWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if pattern != nil && pattern.GetError() != nil {
return DefaultStatementBuilderError(pattern.GetError())
}
return d.Where(RelationshipPatternConditionCreate(pattern))
}
func (d DefaultStatementBuilder) ReturningByString(variables ...string) OngoingReadingAndReturn {
return d.Returning(CreateSymbolicNameByString(variables...)...)
}
func (d DefaultStatementBuilder) ReturningByNamed(variables ...Named) OngoingReadingAndReturn {
if d.err != nil {
return DefaultStatementWithReturnBuilderError(d.err)
}
for _, variable := range variables {
if variable != nil && variable.GetError() != nil {
return DefaultStatementWithReturnBuilderError(variable.GetError())
}
}
return d.Returning(CreateSymbolicNameByNamed(variables...)...)
}
func (d DefaultStatementBuilder) ReturningDistinctByString(variables ...string) OngoingReadingAndReturn {
if d.err != nil {
return DefaultStatementWithReturnBuilderError(d.err)
}
return d.ReturningDistinct(CreateSymbolicNameByString(variables...)...)
}
func (d DefaultStatementBuilder) ReturningDistinctByNamed(variables ...Named) OngoingReadingAndReturn {
if d.err != nil {
return DefaultStatementWithReturnBuilderError(d.err)
}
for _, variable := range variables {
if variable != nil && variable.GetError() != nil {
return DefaultStatementWithReturnBuilderError(variable.GetError())
}
}
return d.ReturningDistinct(CreateSymbolicNameByNamed(variables...)...)
}
func (d DefaultStatementBuilder) WithByString(variables ...string) OrderableOngoingReadingAndWithWithoutWhere {
if d.err != nil {
return DefaultStatementWithWithBuilderError(d.err)
}
return d.With(CreateSymbolicNameByString(variables...)...)
}
func (d DefaultStatementBuilder) WithByNamed(variables ...Named) OrderableOngoingReadingAndWithWithoutWhere {
if d.err != nil {
return DefaultStatementWithWithBuilderError(d.err)
}
for _, variable := range variables {
if variable != nil && variable.GetError() != nil {
return DefaultStatementWithWithBuilderError(variable.GetError())
}
}
return d.With(CreateSymbolicNameByNamed(variables...)...)
}
func (d DefaultStatementBuilder) With(expressions ...Expression) OrderableOngoingReadingAndWithWithoutWhere {
if d.err != nil {
return DefaultStatementWithWithBuilderError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementWithWithBuilderError(expression.GetError())
}
}
return d.WithDefault(false, expressions...)
}
func (d DefaultStatementBuilder) WithDistinctByString(variables ...string) OrderableOngoingReadingAndWithWithoutWhere {
if d.err != nil {
return DefaultStatementWithWithBuilderError(d.err)
}
return d.WithDistinct(CreateSymbolicNameByString(variables...)...)
}
func (d DefaultStatementBuilder) WithDistinctByNamed(variables ...Named) OrderableOngoingReadingAndWithWithoutWhere {
if d.err != nil {
return DefaultStatementWithWithBuilderError(d.err)
}
for _, variable := range variables {
if variable != nil && variable.GetError() != nil {
return DefaultStatementWithWithBuilderError(variable.GetError())
}
}
return d.WithDistinct(CreateSymbolicNameByNamed(variables...)...)
}
func (d DefaultStatementBuilder) WithDistinct(expressions ...Expression) OrderableOngoingReadingAndWithWithoutWhere {
if d.err != nil {
return DefaultStatementWithWithBuilderError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementWithWithBuilderError(expression.GetError())
}
}
return d.WithDefault(true, expressions...)
}
func (d DefaultStatementBuilder) WithDefault(distinct bool, expressions ...Expression) OrderableOngoingReadingAndWithWithoutWhere {
if d.err != nil {
return DefaultStatementWithWithBuilderError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementWithWithBuilderError(expression.GetError())
}
}
ongoingMatchAndWith := DefaultStatementWithWithBuilderCreate(&d, distinct)
ongoingMatchAndWith.addExpression(expressions...)
return ongoingMatchAndWith
}
func (d DefaultStatementBuilder) DeleteByString(variables ...string) OngoingUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
return d.Delete(CreateSymbolicNameByString(variables...)...)
}
func (d DefaultStatementBuilder) DeleteByNamed(variables ...Named) OngoingUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, variable := range variables {
if variable != nil && variable.GetError() != nil {
return DefaultStatementBuilderError(variable.GetError())
}
}
return d.Delete(CreateSymbolicNameByNamed(variables...)...)
}
func (d DefaultStatementBuilder) Delete(expressions ...Expression) OngoingUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementBuilderError(expression.GetError())
}
}
return d.Update(UPDATE_TYPE_DELETE, ExpressionsToVisitables(expressions))
}
func (d DefaultStatementBuilder) DetachDeleteByString(variables ...string) OngoingUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
return d.DetachDelete(CreateSymbolicNameByString(variables...)...)
}
func (d DefaultStatementBuilder) DetachDeleteByNamed(variables ...Named) OngoingUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, variable := range variables {
if variable != nil && variable.GetError() != nil {
return DefaultStatementBuilderError(variable.GetError())
}
}
return d.DetachDelete(CreateSymbolicNameByNamed(variables...)...)
}
func (d DefaultStatementBuilder) DetachDelete(expressions ...Expression) OngoingUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementBuilderError(expression.GetError())
}
}
return d.Update(UPDATE_TYPE_DETACH_DELETE, ExpressionsToVisitables(expressions))
}
func (d DefaultStatementBuilder) Merge(patterns ...PatternElement) OngoingUpdateAndExposesSet {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, pattern := range patterns {
if pattern != nil && pattern.GetError() != nil {
return DefaultStatementBuilderError(pattern.GetError())
}
}
return d.Update(UPDATE_TYPE_MERGE, PatternElementsToVisitables(patterns))
}
func (d DefaultStatementBuilder) Set(expressions ...Expression) BuildableStatementAndOngoingMatchAndUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementBuilderError(expression.GetError())
}
}
d.CloseCurrentOngoingUpdate()
return DefaultStatementWithUpdateBuilderCreate2(&d, UPDATE_TYPE_SET, expressions...)
}
func (d DefaultStatementBuilder) SetWithNamed(variable Named, expression Expression) BuildableStatementAndOngoingMatchAndUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if variable != nil && variable.GetError() != nil {
return DefaultStatementBuilderError(variable.GetError())
}
if expression != nil && expression.GetError() != nil {
return DefaultStatementBuilderError(expression.GetError())
}
return d.Set(variable.GetSymbolicName(), expression)
}
func (d DefaultStatementBuilder) SetByNode(node Node, labels ...string) BuildableStatementAndOngoingMatchAndUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if node.GetError() != nil {
return DefaultStatementBuilderError(node.GetError())
}
return DefaultStatementWithUpdateBuilderCreate2(&d, UPDATE_TYPE_SET, OperationSetLabel(node, labels...))
}
func (d DefaultStatementBuilder) RemoveByNode(node Node, labels ...string) BuildableStatementAndOngoingMatchAndUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if node.GetError() != nil {
return DefaultStatementBuilderError(node.GetError())
}
return DefaultStatementWithUpdateBuilderCreate2(&d, UPDATE_TYPE_REMOVE, OperationRemove(node, labels...))
}
func (d DefaultStatementBuilder) Remove(properties ...Property) BuildableStatementAndOngoingMatchAndUpdate {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, property := range properties {
if property.GetError() != nil {
return DefaultStatementBuilderError(property.GetError())
}
}
expressions := make([]Expression, len(properties))
for i := range properties {
expressions[i] = properties[i]
}
return DefaultStatementWithUpdateBuilderCreate2(&d, UPDATE_TYPE_REMOVE, expressions...)
}
func (d DefaultStatementBuilder) Unwinds(expressions ...Expression) OngoingUnwind {
if d.err != nil {
return DefaultOngoingUnwindError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultOngoingUnwindError(expression.GetError())
}
}
return d.Unwind(ListOf(expressions...))
}
func (d DefaultStatementBuilder) UnwindByString(variable string) OngoingUnwind {
if d.err != nil {
return DefaultOngoingUnwindError(d.err)
}
return d.Unwind(ASymbolic(variable))
}
func (d DefaultStatementBuilder) Unwind(expression Expression) OngoingUnwind {
if d.err != nil {
return DefaultOngoingUnwindError(d.err)
}
if expression != nil && expression.GetError() != nil {
return DefaultOngoingUnwindError(expression.GetError())
}
d.CloseCurrentOngoingMatch()
return DefaultOngoingUnwindCreate(&d, expression)
}
func (d DefaultStatementBuilder) Call(statement Statement) OngoingReadingWithoutWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if statement != nil && statement.GetError() != nil {
return DefaultStatementBuilderError(statement.GetError())
}
if d.err != nil {
return d
}
d.CloseCurrentOngoingCall()
d.CloseCurrentOngoingMatch()
d.CloseCurrentOngoingUpdate()
singlePart := SubqueryCall(statement)
if singlePart.GetError() != nil {
d.err = singlePart.GetError()
return d
}
d.currentSinglePartElements = append(d.currentSinglePartElements, singlePart)
return d
}
func (d DefaultStatementBuilder) Call1(namespaceAndProcedure ...string) OngoingInQueryCallWithoutArguments {
if d.err != nil {
return InQueryCallBuilder{err: d.err}
}
d.CloseCurrentOngoingMatch()
d.CloseCurrentOngoingCall()
inQueryCallBuilder := InQueryCallBuilderCreate(&d, ProcedureNameCreate(namespaceAndProcedure...))
d.currentOngoingCall = inQueryCallBuilder
return inQueryCallBuilder
}
func (d DefaultStatementBuilder) AsCondition() Condition {
if !d.currentOngoingMatch.notNil || len(d.currentSinglePartElements) > 0 {
panic("only simple MATCH statements can be used as existential subqueries")
}
return ExistentialSubqueryExists(d.currentOngoingMatch.buildMatch())
}
func (d DefaultStatementBuilder) And(condition Condition) OngoingReadingWithWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
d.currentOngoingMatch.conditionBuilder.And(condition)
return d
}
func (d DefaultStatementBuilder) AndPattern(pattern RelationshipPattern) OngoingReadingWithWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if pattern != nil && pattern.GetError() != nil {
return DefaultStatementBuilderError(pattern.GetError())
}
return d.And(RelationshipPatternConditionCreate(pattern))
}
func (d DefaultStatementBuilder) Or(condition Condition) OngoingReadingWithWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if condition != nil && condition.GetError() != nil {
return DefaultStatementBuilderError(condition.GetError())
}
d.currentOngoingMatch.conditionBuilder.Or(condition)
return d
}
func (d DefaultStatementBuilder) OrPattern(pattern RelationshipPattern) OngoingReadingWithWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
if pattern != nil && pattern.GetError() != nil {
return DefaultStatementBuilderError(pattern.GetError())
}
return d.Or(RelationshipPatternConditionCreate(pattern))
}
func (d DefaultStatementBuilder) OptionalMatch(patterns ...PatternElement) OngoingReadingWithoutWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, pattern := range patterns {
if pattern != nil && pattern.GetError() != nil {
return DefaultStatementBuilderError(pattern.GetError())
}
}
return d.MatchDefault(true, patterns...)
}
func (d DefaultStatementBuilder) Create(elements ...PatternElement) OngoingUpdateAndExposesSet {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, element := range elements {
if element != nil && element.GetError() != nil {
return DefaultStatementBuilderError(element.GetError())
}
}
return d.Update(UPDATE_TYPE_CREATE, PatternElementsToVisitables(elements))
}
func (d DefaultStatementBuilder) OnCreate() OngoingMergeAction {
return d.OngoingOnAfterMerge(ON_CREATE)
}
func (d DefaultStatementBuilder) OnMatch() OngoingMergeAction {
return d.OngoingOnAfterMerge(ON_MATCH)
}
func (d *DefaultStatementBuilder) OngoingOnAfterMerge(mergeType MERGE_TYPE) OngoingMergeAction {
if d.err != nil {
return OngoingMergeActionInBuilderError(d.err)
}
if _, isSupports := d.currentOngoingUpdate.builder.(SupportsActionsOnTheUpdatingClause); !isSupports ||
!d.currentOngoingUpdate.notNil {
return OngoingMergeActionInBuilderError(errors.New("merge must have been invoked before defining an event"))
}
return OngoingMergeActionInBuilderCreate(d, mergeType)
}
func (d DefaultStatementBuilder) Match(patterns ...PatternElement) OngoingReadingWithoutWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, pattern := range patterns {
if pattern != nil && pattern.GetError() != nil {
return DefaultStatementBuilderError(pattern.GetError())
}
}
return d.MatchDefault(false, patterns...)
}
func (d DefaultStatementBuilder) MatchDefault(optional bool, patterns ...PatternElement) OngoingReadingWithoutWhere {
if d.err != nil {
return DefaultStatementBuilderError(d.err)
}
for _, pattern := range patterns {
if pattern != nil && pattern.GetError() != nil {
return DefaultStatementBuilderError(pattern.GetError())
}
}
d.CloseCurrentOngoingMatch()
d.CloseCurrentOngoingCall()
d.currentOngoingMatch = MatchBuilderCreate(optional)
d.currentOngoingMatch.patternList = append(d.currentOngoingMatch.patternList, patterns...)
return d
}
func (d *DefaultStatementBuilder) CloseCurrentOngoingMatch() {
if !d.currentOngoingMatch.notNil {
return
}
d.currentSinglePartElements = append(d.currentSinglePartElements, d.currentOngoingMatch.buildMatch())
d.currentOngoingMatch = MatchBuilder{}
}
func (d *DefaultStatementBuilder) CloseCurrentOngoingCall() {
if d.currentOngoingCall == nil || !d.currentOngoingCall.isNotNil() {
return
}
builtCall, err := d.currentOngoingCall.Build()
if err != nil {
d.err = err
return
}
d.currentSinglePartElements = append(d.currentSinglePartElements, builtCall)
d.currentOngoingCall = InQueryCallBuilder{}
}
func (d *DefaultStatementBuilder) CloseCurrentOngoingUpdate() {
if !d.currentOngoingUpdate.notNil {
return
}
d.currentSinglePartElements = append(d.currentSinglePartElements, d.currentOngoingUpdate.builder.build())
d.currentOngoingUpdate = DefaultStatementWithUpdateBuilder{}
}
func (d DefaultStatementBuilder) Returning(expressions ...Expression) OngoingReadingAndReturn {
return d.ReturningDefault(false, expressions...)
}
func (d DefaultStatementBuilder) ReturningDistinct(expression ...Expression) OngoingReadingAndReturn {
return d.ReturningDefault(true, expression...)
}
func (d DefaultStatementBuilder) ReturningDefault(distinct bool, expressions ...Expression) OngoingReadingAndReturn {
if d.err != nil {
return DefaultStatementWithReturnBuilderError(d.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementWithReturnBuilderError(expression.GetError())
}
}
withReturnBuilder := DefaultStatementWithReturnBuilder{
distinct: distinct,
defaultBuilder: &d,
}
withReturnBuilder.AddExpression(expressions...)
return withReturnBuilder
}
func (d DefaultStatementBuilder) Build() (Statement, error) {
if d.err != nil {
return nil, d.err
}
return d.BuildImpl(false, Return{}), nil
}
func (d DefaultStatementBuilder) BuildImpl(clearCurrentBuildSteps bool, returning Return) Statement {
singlePartQuery := SinglePartQueryCreate(d.BuildListOfVisitable(clearCurrentBuildSteps), returning)
if len(d.multipartElements) == 0 {
return singlePartQuery
}
return MultiPartQueryCreate(d.multipartElements, singlePartQuery)
}
func (d *DefaultStatementBuilder) BuildListOfVisitable(clearAfter bool) []Visitable {
visitables := make([]Visitable, 0)
visitables = append(visitables, d.currentSinglePartElements...)
if d.currentOngoingMatch.notNil {
visitables = append(visitables, d.currentOngoingMatch.buildMatch())
}
if d.currentOngoingUpdate.isNotNil() {
visitables = append(visitables, d.currentOngoingUpdate.builder.build())
}
if d.currentOngoingCall != nil && d.currentOngoingCall.isNotNil() {
builtCall, err := d.currentOngoingCall.Build()
if err != nil {
d.err = err
return nil
}
visitables = append(visitables, builtCall)
}
if clearAfter {
d.currentOngoingMatch = MatchBuilder{}
d.currentOngoingUpdate = DefaultStatementWithUpdateBuilder{}
d.currentOngoingCall = InQueryCallBuilder{}
d.currentSinglePartElements = make([]Visitable, 0)
}
return visitables
}
func (d *DefaultStatementBuilder) addUpdatingClause(clause UpdatingClause) DefaultStatementBuilder {
d.CloseCurrentOngoingMatch()
d.currentSinglePartElements = append(d.currentSinglePartElements, clause)
return *d
}
func (d *DefaultStatementBuilder) Update(updateType UpdateType, pattern []Visitable) DefaultStatementBuilder {
d.CloseCurrentOngoingMatch()
d.CloseCurrentOngoingCall()
d.CloseCurrentOngoingUpdate()
d.currentOngoingUpdate = DefaultStatementWithUpdateBuilderCreate1(d, updateType, pattern)
return *d
}
func getUpdatingClauseBuilder(updateType UpdateType, patternOrExpression ...Visitable) (UpdatingClauseBuilder, error) {
mergeOrCreate := updateType == UPDATE_TYPE_MERGE || updateType == UPDATE_TYPE_CREATE
if mergeOrCreate {
patternElements := make([]PatternElement, 0)
for _, visitable := range patternOrExpression {
patternElements := append(patternElements, visitable.(PatternElement))
if updateType == UPDATE_TYPE_CREATE {
return CreateBuilderCreate(patternElements), nil
} else {
return MergeBuilderCreate(patternElements), nil
}
}
} else {
expressions := make([]Expression, 0)
for _, visitable := range patternOrExpression {
expressions = append(expressions, visitable.(Expression))
}
var expressionList ExpressionList
if updateType == UPDATE_TYPE_SET {
preparedExpression, err := prepareSetExpression(expressions)
if err != nil {
return RemoveBuilder{}, err
}
expressionList = ExpressionListCreate(preparedExpression)
} else {
expressionList = ExpressionListCreate(expressions)
}
switch updateType {
case UPDATE_TYPE_DETACH_DELETE:
return DeleteBuilderCreate(expressionList, true), nil
case UPDATE_TYPE_DELETE:
return DeleteBuilderCreate(expressionList, false), nil
case UPDATE_TYPE_SET:
return SetBuilderCreate(expressionList), nil
case UPDATE_TYPE_REMOVE:
return RemoveBuilderCreate(expressionList), nil
default:
return RemoveBuilder{}, xerrors.Errorf("unsupported update type %s", updateType)
}
}
//Just return to make compiler happy
return RemoveBuilder{}, xerrors.Errorf("unexpected behavior")
}
func prepareSetExpression(possibleSetOperations []Expression) ([]Expression, error) {
propertyOperations := make([]Expression, 0)
listOfExpressions := make([]Expression, 0)
for _, possibleSetOperation := range possibleSetOperations {
if operation, isOperation := possibleSetOperation.(Operation); isOperation {
propertyOperations = append(propertyOperations, operation)
} else {
listOfExpressions = append(listOfExpressions, possibleSetOperation)
}
}
if len(listOfExpressions)%2 != 0 {
return nil, xerrors.New("the list of expression to OperationSet must be even")
}
for i := 0; i < len(listOfExpressions); i += 2 {
propertyOperations = append(propertyOperations, OperationSet(listOfExpressions[i], listOfExpressions[i+1]))
}
return propertyOperations, nil
}
//Implement OngoingMergeAction
type OngoingMergeActionInBuilder struct {
defaultBuilder *DefaultStatementBuilder
mergeType MERGE_TYPE
err error
}
func OngoingMergeActionInBuilderCreate(defaultBuilder *DefaultStatementBuilder, mergeType MERGE_TYPE) OngoingMergeActionInBuilder {
if defaultBuilder.err != nil {
return OngoingMergeActionInBuilderError(defaultBuilder.err)
}
return OngoingMergeActionInBuilder{
defaultBuilder: defaultBuilder,
mergeType: mergeType,
}
}
func OngoingMergeActionInBuilderError(err error) OngoingMergeActionInBuilder {
return OngoingMergeActionInBuilder{
err: err,
}
}
func (o OngoingMergeActionInBuilder) GetErr() error {
return o.err
}
func (o OngoingMergeActionInBuilder) Set(expressions ...Expression) OngoingMatchAndUpdateAndBuildableStatementAndExposesMergeAction {
if o.err != nil {
return DefaultStatementBuilderError(o.err)
}
for _, expression := range expressions {
if expression != nil && expression.GetError() != nil {
return DefaultStatementBuilderError(expression.GetError())
}
}
support := o.defaultBuilder.currentOngoingUpdate.builder.(SupportsActionsOnTheUpdatingClause)
newSupport, err := support.on(o.mergeType, expressions...)
newMergeBuilder, _ := newSupport.(MergeBuilder)
if err != nil {
o.defaultBuilder.err = err
return o.defaultBuilder
}
o.defaultBuilder.currentOngoingUpdate.builder = newMergeBuilder
return o.defaultBuilder
}
func (o OngoingMergeActionInBuilder) SetWithNamed(variable Named, expression Expression) OngoingMatchAndUpdateAndBuildableStatementAndExposesMergeAction {
if o.err != nil {
return DefaultStatementBuilderError(o.err)
}
if variable != nil && variable.GetError() != nil {
return DefaultStatementBuilderError(variable.GetError())
}
if expression != nil && expression.GetError() != nil {
return DefaultStatementBuilderError(expression.GetError())
}
return o.Set(variable.GetSymbolicName(), expression)
}