-
Notifications
You must be signed in to change notification settings - Fork 0
/
so_contract.go
2136 lines (1854 loc) · 46 KB
/
so_contract.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package table
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"sync"
"github.com/coschain/contentos-go/common/encoding/kope"
"github.com/coschain/contentos-go/iservices"
prototype "github.com/coschain/contentos-go/prototype"
proto "github.com/golang/protobuf/proto"
)
////////////// SECTION Prefix Mark ///////////////
var (
ContractCreatedTimeTable uint32 = 1292005739
ContractApplyCountTable uint32 = 2694332342
ContractIdUniTable uint32 = 4175408872
ContractIdRow uint32 = 1374288427
)
////////////// SECTION Wrap Define ///////////////
type SoContractWrap struct {
dba iservices.IDatabaseRW
mainKey *prototype.ContractId
watcherFlag *ContractWatcherFlag
mKeyFlag int //the flag of the main key exist state in db, -1:has not judged; 0:not exist; 1:already exist
mKeyBuf []byte //the buffer after the main key is encoded with prefix
mBuf []byte //the value after the main key is encoded
mdFuncMap map[string]interface{}
}
func NewSoContractWrap(dba iservices.IDatabaseRW, key *prototype.ContractId) *SoContractWrap {
if dba == nil || key == nil {
return nil
}
result := &SoContractWrap{dba, key, nil, -1, nil, nil, nil}
result.initWatcherFlag()
return result
}
func (s *SoContractWrap) CheckExist() bool {
if s.dba == nil {
return false
}
if s.mKeyFlag != -1 {
//if you have already obtained the existence status of the primary key, use it directly
if s.mKeyFlag == 0 {
return false
}
return true
}
keyBuf, err := s.encodeMainKey()
if err != nil {
return false
}
res, err := s.dba.Has(keyBuf)
if err != nil {
return false
}
if res == false {
s.mKeyFlag = 0
} else {
s.mKeyFlag = 1
}
return res
}
func (s *SoContractWrap) MustExist(errMsgs ...interface{}) *SoContractWrap {
if !s.CheckExist() {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.MustExist: %v not found", s.mainKey), errMsgs...))
}
return s
}
func (s *SoContractWrap) MustNotExist(errMsgs ...interface{}) *SoContractWrap {
if s.CheckExist() {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.MustNotExist: %v already exists", s.mainKey), errMsgs...))
}
return s
}
func (s *SoContractWrap) initWatcherFlag() {
if s.watcherFlag == nil {
s.watcherFlag = new(ContractWatcherFlag)
*(s.watcherFlag) = ContractWatcherFlagOfDb(s.dba.ServiceId())
}
}
func (s *SoContractWrap) create(f func(tInfo *SoContract)) error {
if s.dba == nil {
return errors.New("the db is nil")
}
if s.mainKey == nil {
return errors.New("the main key is nil")
}
val := &SoContract{}
f(val)
if val.Id == nil {
val.Id = s.mainKey
}
if s.CheckExist() {
return errors.New("the main key is already exist")
}
keyBuf, err := s.encodeMainKey()
if err != nil {
return err
}
buf, err := proto.Marshal(val)
if err != nil {
return err
}
err = s.dba.Put(keyBuf, buf)
if err != nil {
return err
}
// update srt list keys
if err = s.insertAllSortKeys(val); err != nil {
s.delAllSortKeys(false, val)
s.dba.Delete(keyBuf)
return err
}
//update unique list
if sucNames, err := s.insertAllUniKeys(val); err != nil {
s.delAllSortKeys(false, val)
s.delUniKeysWithNames(sucNames, val)
s.dba.Delete(keyBuf)
return err
}
s.mKeyFlag = 1
// call watchers
s.initWatcherFlag()
if s.watcherFlag.AnyWatcher {
ReportTableRecordInsert(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, val)
}
return nil
}
func (s *SoContractWrap) Create(f func(tInfo *SoContract), errArgs ...interface{}) *SoContractWrap {
err := s.create(f)
if err != nil {
panic(bindErrorInfo(fmt.Errorf("SoContractWrap.Create failed: %s", err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) getMainKeyBuf() ([]byte, error) {
if s.mainKey == nil {
return nil, errors.New("the main key is nil")
}
if s.mBuf == nil {
var err error = nil
s.mBuf, err = kope.Encode(s.mainKey)
if err != nil {
return nil, err
}
}
return s.mBuf, nil
}
func (s *SoContractWrap) modify(f func(tInfo *SoContract)) error {
if !s.CheckExist() {
return errors.New("the SoContract table does not exist. Please create a table first")
}
oriTable := s.getContract()
if oriTable == nil {
return errors.New("fail to get origin table SoContract")
}
curTable := s.getContract()
if curTable == nil {
return errors.New("fail to create current table SoContract")
}
f(curTable)
//the main key is not support modify
if !reflect.DeepEqual(curTable.Id, oriTable.Id) {
return errors.New("primary key does not support modification")
}
s.initWatcherFlag()
modifiedFields, hasWatcher, err := s.getModifiedFields(oriTable, curTable)
if err != nil {
return err
}
if modifiedFields == nil || len(modifiedFields) < 1 {
return nil
}
//check whether modify sort and unique field to nil
err = s.checkSortAndUniFieldValidity(curTable, modifiedFields)
if err != nil {
return err
}
//check unique
err = s.handleFieldMd(FieldMdHandleTypeCheck, curTable, modifiedFields)
if err != nil {
return err
}
//delete sort and unique key
err = s.handleFieldMd(FieldMdHandleTypeDel, oriTable, modifiedFields)
if err != nil {
return err
}
//update table
err = s.updateContract(curTable)
if err != nil {
return err
}
//insert sort and unique key
err = s.handleFieldMd(FieldMdHandleTypeInsert, curTable, modifiedFields)
if err != nil {
return err
}
// call watchers
if hasWatcher {
ReportTableRecordUpdate(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, oriTable, curTable, modifiedFields)
}
return nil
}
func (s *SoContractWrap) Modify(f func(tInfo *SoContract), errArgs ...interface{}) *SoContractWrap {
err := s.modify(f)
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.Modify failed: %s", err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetAbi(p string, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.Abi = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetAbi( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetApplyCount(p uint32, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.ApplyCount = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetApplyCount( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetBalance(p *prototype.Coin, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.Balance = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetBalance( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetCode(p []byte, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.Code = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetCode( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetCreatedTime(p *prototype.TimePointSec, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.CreatedTime = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetCreatedTime( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetDescribe(p string, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.Describe = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetDescribe( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetHash(p *prototype.Sha256, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.Hash = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetHash( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetUpgradeable(p bool, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.Upgradeable = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetUpgradeable( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) SetUrl(p string, errArgs ...interface{}) *SoContractWrap {
err := s.modify(func(r *SoContract) {
r.Url = p
})
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.SetUrl( %v ) failed: %s", p, err.Error()), errArgs...))
}
return s
}
func (s *SoContractWrap) checkSortAndUniFieldValidity(curTable *SoContract, fields map[string]bool) error {
if curTable != nil && fields != nil && len(fields) > 0 {
if fields["CreatedTime"] && curTable.CreatedTime == nil {
return errors.New("sort field CreatedTime can't be modified to nil")
}
}
return nil
}
//Get all the modified fields in the table
func (s *SoContractWrap) getModifiedFields(oriTable *SoContract, curTable *SoContract) (map[string]bool, bool, error) {
if oriTable == nil {
return nil, false, errors.New("table info is nil, can't get modified fields")
}
hasWatcher := false
fields := make(map[string]bool)
if !reflect.DeepEqual(oriTable.Abi, curTable.Abi) {
fields["Abi"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasAbiWatcher
}
if !reflect.DeepEqual(oriTable.ApplyCount, curTable.ApplyCount) {
fields["ApplyCount"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasApplyCountWatcher
}
if !reflect.DeepEqual(oriTable.Balance, curTable.Balance) {
fields["Balance"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasBalanceWatcher
}
if !reflect.DeepEqual(oriTable.Code, curTable.Code) {
fields["Code"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasCodeWatcher
}
if !reflect.DeepEqual(oriTable.CreatedTime, curTable.CreatedTime) {
fields["CreatedTime"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasCreatedTimeWatcher
}
if !reflect.DeepEqual(oriTable.Describe, curTable.Describe) {
fields["Describe"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasDescribeWatcher
}
if !reflect.DeepEqual(oriTable.Hash, curTable.Hash) {
fields["Hash"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasHashWatcher
}
if !reflect.DeepEqual(oriTable.Upgradeable, curTable.Upgradeable) {
fields["Upgradeable"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasUpgradeableWatcher
}
if !reflect.DeepEqual(oriTable.Url, curTable.Url) {
fields["Url"] = true
hasWatcher = hasWatcher || s.watcherFlag.HasUrlWatcher
}
hasWatcher = hasWatcher || s.watcherFlag.WholeWatcher
return fields, hasWatcher, nil
}
func (s *SoContractWrap) handleFieldMd(t FieldMdHandleType, so *SoContract, fields map[string]bool) error {
if so == nil {
return errors.New("fail to modify empty table")
}
//there is no field need to modify
if fields == nil || len(fields) < 1 {
return nil
}
errStr := ""
if fields["Abi"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldAbi(so.Abi, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "Abi")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldAbi(so.Abi, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "Abi")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldAbi(so.Abi, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "Abi")
}
if !res {
return errors.New(errStr)
}
}
if fields["ApplyCount"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldApplyCount(so.ApplyCount, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "ApplyCount")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldApplyCount(so.ApplyCount, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "ApplyCount")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldApplyCount(so.ApplyCount, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "ApplyCount")
}
if !res {
return errors.New(errStr)
}
}
if fields["Balance"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldBalance(so.Balance, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "Balance")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldBalance(so.Balance, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "Balance")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldBalance(so.Balance, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "Balance")
}
if !res {
return errors.New(errStr)
}
}
if fields["Code"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldCode(so.Code, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "Code")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldCode(so.Code, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "Code")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldCode(so.Code, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "Code")
}
if !res {
return errors.New(errStr)
}
}
if fields["CreatedTime"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldCreatedTime(so.CreatedTime, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "CreatedTime")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldCreatedTime(so.CreatedTime, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "CreatedTime")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldCreatedTime(so.CreatedTime, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "CreatedTime")
}
if !res {
return errors.New(errStr)
}
}
if fields["Describe"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldDescribe(so.Describe, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "Describe")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldDescribe(so.Describe, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "Describe")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldDescribe(so.Describe, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "Describe")
}
if !res {
return errors.New(errStr)
}
}
if fields["Hash"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldHash(so.Hash, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "Hash")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldHash(so.Hash, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "Hash")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldHash(so.Hash, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "Hash")
}
if !res {
return errors.New(errStr)
}
}
if fields["Upgradeable"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldUpgradeable(so.Upgradeable, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "Upgradeable")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldUpgradeable(so.Upgradeable, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "Upgradeable")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldUpgradeable(so.Upgradeable, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "Upgradeable")
}
if !res {
return errors.New(errStr)
}
}
if fields["Url"] {
res := true
if t == FieldMdHandleTypeCheck {
res = s.mdFieldUrl(so.Url, true, false, false, so)
errStr = fmt.Sprintf("fail to modify exist value of %v", "Url")
} else if t == FieldMdHandleTypeDel {
res = s.mdFieldUrl(so.Url, false, true, false, so)
errStr = fmt.Sprintf("fail to delete sort or unique field %v", "Url")
} else if t == FieldMdHandleTypeInsert {
res = s.mdFieldUrl(so.Url, false, false, true, so)
errStr = fmt.Sprintf("fail to insert sort or unique field %v", "Url")
}
if !res {
return errors.New(errStr)
}
}
return nil
}
////////////// SECTION LKeys delete/insert ///////////////
func (s *SoContractWrap) delSortKeyCreatedTime(sa *SoContract) bool {
if s.dba == nil || s.mainKey == nil {
return false
}
val := SoListContractByCreatedTime{}
if sa == nil {
val.CreatedTime = s.GetCreatedTime()
val.Id = s.mainKey
} else {
val.CreatedTime = sa.CreatedTime
val.Id = sa.Id
}
subBuf, err := val.OpeEncode()
if err != nil {
return false
}
ordErr := s.dba.Delete(subBuf)
return ordErr == nil
}
func (s *SoContractWrap) insertSortKeyCreatedTime(sa *SoContract) bool {
if s.dba == nil || sa == nil {
return false
}
val := SoListContractByCreatedTime{}
val.Id = sa.Id
val.CreatedTime = sa.CreatedTime
buf, err := proto.Marshal(&val)
if err != nil {
return false
}
subBuf, err := val.OpeEncode()
if err != nil {
return false
}
ordErr := s.dba.Put(subBuf, buf)
return ordErr == nil
}
func (s *SoContractWrap) delSortKeyApplyCount(sa *SoContract) bool {
if s.dba == nil || s.mainKey == nil {
return false
}
val := SoListContractByApplyCount{}
if sa == nil {
val.ApplyCount = s.GetApplyCount()
val.Id = s.mainKey
} else {
val.ApplyCount = sa.ApplyCount
val.Id = sa.Id
}
subBuf, err := val.OpeEncode()
if err != nil {
return false
}
ordErr := s.dba.Delete(subBuf)
return ordErr == nil
}
func (s *SoContractWrap) insertSortKeyApplyCount(sa *SoContract) bool {
if s.dba == nil || sa == nil {
return false
}
val := SoListContractByApplyCount{}
val.Id = sa.Id
val.ApplyCount = sa.ApplyCount
buf, err := proto.Marshal(&val)
if err != nil {
return false
}
subBuf, err := val.OpeEncode()
if err != nil {
return false
}
ordErr := s.dba.Put(subBuf, buf)
return ordErr == nil
}
func (s *SoContractWrap) delAllSortKeys(br bool, val *SoContract) bool {
if s.dba == nil {
return false
}
res := true
if !s.delSortKeyCreatedTime(val) {
if br {
return false
} else {
res = false
}
}
if !s.delSortKeyApplyCount(val) {
if br {
return false
} else {
res = false
}
}
return res
}
func (s *SoContractWrap) insertAllSortKeys(val *SoContract) error {
if s.dba == nil {
return errors.New("insert sort Field fail,the db is nil ")
}
if val == nil {
return errors.New("insert sort Field fail,get the SoContract fail ")
}
if !s.insertSortKeyCreatedTime(val) {
return errors.New("insert sort Field CreatedTime fail while insert table ")
}
if !s.insertSortKeyApplyCount(val) {
return errors.New("insert sort Field ApplyCount fail while insert table ")
}
return nil
}
////////////// SECTION LKeys delete/insert //////////////
func (s *SoContractWrap) removeContract() error {
if s.dba == nil {
return errors.New("database is nil")
}
s.initWatcherFlag()
var oldVal *SoContract
if s.watcherFlag.AnyWatcher {
oldVal = s.getContract()
}
//delete sort list key
if res := s.delAllSortKeys(true, nil); !res {
return errors.New("delAllSortKeys failed")
}
//delete unique list
if res := s.delAllUniKeys(true, nil); !res {
return errors.New("delAllUniKeys failed")
}
//delete table
key, err := s.encodeMainKey()
if err != nil {
return fmt.Errorf("encodeMainKey failed: %s", err.Error())
}
err = s.dba.Delete(key)
if err == nil {
s.mKeyBuf = nil
s.mKeyFlag = -1
// call watchers
if s.watcherFlag.AnyWatcher && oldVal != nil {
ReportTableRecordDelete(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, oldVal)
}
return nil
} else {
return fmt.Errorf("database.Delete failed: %s", err.Error())
}
}
func (s *SoContractWrap) RemoveContract(errMsgs ...interface{}) *SoContractWrap {
err := s.removeContract()
if err != nil {
panic(bindErrorInfo(fmt.Sprintf("SoContractWrap.RemoveContract failed: %s", err.Error()), errMsgs...))
}
return s
}
////////////// SECTION Members Get/Modify ///////////////
func (s *SoContractWrap) GetAbi() string {
res := true
msg := &SoContract{}
if s.dba == nil {
res = false
} else {
key, err := s.encodeMainKey()
if err != nil {
res = false
} else {
buf, err := s.dba.Get(key)
if err != nil {
res = false
}
err = proto.Unmarshal(buf, msg)
if err != nil {
res = false
} else {
return msg.Abi
}
}
}
if !res {
var tmpValue string
return tmpValue
}
return msg.Abi
}
func (s *SoContractWrap) mdFieldAbi(p string, isCheck bool, isDel bool, isInsert bool,
so *SoContract) bool {
if s.dba == nil {
return false
}
if isCheck {
res := s.checkAbiIsMetMdCondition(p)
if !res {
return false
}
}
if isDel {
res := s.delFieldAbi(so)
if !res {
return false
}
}
if isInsert {
res := s.insertFieldAbi(so)
if !res {
return false
}
}
return true
}
func (s *SoContractWrap) delFieldAbi(so *SoContract) bool {
if s.dba == nil {
return false
}
return true
}
func (s *SoContractWrap) insertFieldAbi(so *SoContract) bool {
if s.dba == nil {
return false
}
return true
}
func (s *SoContractWrap) checkAbiIsMetMdCondition(p string) bool {
if s.dba == nil {
return false
}
return true
}
func (s *SoContractWrap) GetApplyCount() uint32 {
res := true
msg := &SoContract{}
if s.dba == nil {
res = false
} else {
key, err := s.encodeMainKey()
if err != nil {
res = false
} else {
buf, err := s.dba.Get(key)
if err != nil {
res = false
}
err = proto.Unmarshal(buf, msg)
if err != nil {
res = false
} else {
return msg.ApplyCount
}
}
}
if !res {
var tmpValue uint32
return tmpValue
}
return msg.ApplyCount
}
func (s *SoContractWrap) mdFieldApplyCount(p uint32, isCheck bool, isDel bool, isInsert bool,
so *SoContract) bool {
if s.dba == nil {
return false
}
if isCheck {
res := s.checkApplyCountIsMetMdCondition(p)
if !res {
return false
}
}
if isDel {
res := s.delFieldApplyCount(so)
if !res {
return false
}
}
if isInsert {
res := s.insertFieldApplyCount(so)
if !res {
return false
}
}
return true
}
func (s *SoContractWrap) delFieldApplyCount(so *SoContract) bool {
if s.dba == nil {
return false
}
if !s.delSortKeyApplyCount(so) {
return false
}
return true
}
func (s *SoContractWrap) insertFieldApplyCount(so *SoContract) bool {
if s.dba == nil {
return false
}
if !s.insertSortKeyApplyCount(so) {
return false
}
return true
}
func (s *SoContractWrap) checkApplyCountIsMetMdCondition(p uint32) bool {
if s.dba == nil {
return false
}
return true
}
func (s *SoContractWrap) GetBalance() *prototype.Coin {
res := true
msg := &SoContract{}
if s.dba == nil {
res = false
} else {
key, err := s.encodeMainKey()
if err != nil {
res = false
} else {
buf, err := s.dba.Get(key)
if err != nil {
res = false
}
err = proto.Unmarshal(buf, msg)
if err != nil {
res = false
} else {
return msg.Balance
}
}
}
if !res {
return nil
}
return msg.Balance
}
func (s *SoContractWrap) mdFieldBalance(p *prototype.Coin, isCheck bool, isDel bool, isInsert bool,
so *SoContract) bool {
if s.dba == nil {
return false
}
if isCheck {
res := s.checkBalanceIsMetMdCondition(p)
if !res {
return false
}
}
if isDel {
res := s.delFieldBalance(so)
if !res {
return false
}
}
if isInsert {
res := s.insertFieldBalance(so)
if !res {
return false
}
}
return true
}
func (s *SoContractWrap) delFieldBalance(so *SoContract) bool {
if s.dba == nil {
return false
}
return true
}
func (s *SoContractWrap) insertFieldBalance(so *SoContract) bool {
if s.dba == nil {
return false
}
return true
}
func (s *SoContractWrap) checkBalanceIsMetMdCondition(p *prototype.Coin) bool {
if s.dba == nil {
return false