NewBatchSave creates a new BatchSave instance
(basicRes context.BasicRes, slotType reflect.Type, size int, tableName ...string)
| 49 | |
| 50 | // NewBatchSave creates a new BatchSave instance |
| 51 | func NewBatchSave(basicRes context.BasicRes, slotType reflect.Type, size int, tableName ...string) (*BatchSave, errors.Error) { |
| 52 | if slotType.Kind() != reflect.Ptr { |
| 53 | panic(errors.Default.New("slotType must be a pointer")) |
| 54 | } |
| 55 | db := basicRes.GetDal() |
| 56 | primaryKey := db.GetPrimaryKeyFields(slotType) |
| 57 | // check if it has primaryKey |
| 58 | if len(primaryKey) == 0 { |
| 59 | return nil, errors.Default.New(fmt.Sprintf("%s no primary key", slotType.String())) |
| 60 | } |
| 61 | tn := "" |
| 62 | if len(tableName) == 1 { |
| 63 | tn = tableName[0] |
| 64 | } |
| 65 | |
| 66 | logger := basicRes.GetLogger().Nested(slotType.String()) |
| 67 | return &BatchSave{ |
| 68 | basicRes: basicRes, |
| 69 | log: logger, |
| 70 | db: db, |
| 71 | slotType: slotType, |
| 72 | slots: reflect.MakeSlice(reflect.SliceOf(slotType), size, size), |
| 73 | size: size, |
| 74 | valueIndex: make(map[string]int), |
| 75 | primaryKey: primaryKey, |
| 76 | tableName: tn, |
| 77 | }, nil |
| 78 | } |
| 79 | |
| 80 | // Add record to cache. BatchSave would flush them into Database when cache is max out |
| 81 | func (c *BatchSave) Add(slot interface{}) errors.Error { |