CopyTableColumns can copy data from src table to dst table
( basicRes context.BasicRes, srcTableName string, dstTableName string, transform func(*S) (*D, errors.Error), )
| 299 | |
| 300 | // CopyTableColumns can copy data from src table to dst table |
| 301 | func CopyTableColumns[S any, D any]( |
| 302 | basicRes context.BasicRes, |
| 303 | srcTableName string, |
| 304 | dstTableName string, |
| 305 | transform func(*S) (*D, errors.Error), |
| 306 | ) (err errors.Error) { |
| 307 | db := basicRes.GetDal() |
| 308 | |
| 309 | cursor, err := db.Cursor(dal.From(srcTableName)) |
| 310 | if err != nil { |
| 311 | return errors.Default.Wrap(err, fmt.Sprintf("failed to load data from src table [%s]", srcTableName)) |
| 312 | } |
| 313 | if !reflect.ValueOf(cursor).IsNil() { |
| 314 | defer cursor.Close() |
| 315 | } |
| 316 | |
| 317 | batch, err := helper.NewBatchSave(basicRes, reflect.TypeOf(new(D)), 200, dstTableName) |
| 318 | if err != nil { |
| 319 | return errors.Default.Wrap(err, fmt.Sprintf("failed to instantiate BatchSave for table [%s]", srcTableName)) |
| 320 | } |
| 321 | defer batch.Close() |
| 322 | |
| 323 | if reflect.ValueOf(cursor).IsNil() { |
| 324 | return nil |
| 325 | } |
| 326 | for cursor.Next() { |
| 327 | srcTable := new(S) |
| 328 | err1 := db.Fetch(cursor, srcTable) |
| 329 | if err1 != nil { |
| 330 | return errors.Default.Wrap(err1, fmt.Sprintf("fail to load record from table [%s]", srcTableName)) |
| 331 | } |
| 332 | |
| 333 | dst, err1 := transform(srcTable) |
| 334 | if err1 != nil { |
| 335 | return errors.Default.Wrap(err1, fmt.Sprintf("failed to transform row %v", srcTable)) |
| 336 | } |
| 337 | |
| 338 | err1 = batch.Add(dst) |
| 339 | if err1 != nil { |
| 340 | return errors.Default.Wrap(err1, fmt.Sprintf("push to BatchSave failed %v", dstTableName)) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return err |
| 345 | } |
| 346 | |
| 347 | // PrimarykeyIsAutoIncrement check if the Primarykey is auto increment |
| 348 | func PrimarykeyIsAutoIncrement(db dal.Dal, tableName string) errors.Error { |