Create create resource in the db
(ctx context.Context, resource *schema.Resource)
| 626 | |
| 627 | //Create create resource in the db |
| 628 | func (tx *Transaction) CreateContext(ctx context.Context, resource *schema.Resource) error { |
| 629 | defer tx.measureTime(time.Now(), resource.Schema().ID, "create") |
| 630 | |
| 631 | var cols []string |
| 632 | var values []interface{} |
| 633 | db := tx.db |
| 634 | s := resource.Schema() |
| 635 | data := resource.Data() |
| 636 | q := sq.Insert(quote(s.GetDbTableName())) |
| 637 | for _, attr := range s.Properties { |
| 638 | //TODO(nati) support optional value |
| 639 | if _, ok := data[attr.ID]; ok { |
| 640 | handler := db.handler(&attr) |
| 641 | cols = append(cols, quote(attr.ID)) |
| 642 | encoded, err := handler.encode(&attr, data[attr.ID]) |
| 643 | if err != nil { |
| 644 | return fmt.Errorf("SQL Create encoding error: %s", err) |
| 645 | } |
| 646 | values = append(values, encoded) |
| 647 | } |
| 648 | } |
| 649 | q = q.Columns(cols...).Values(values...) |
| 650 | sql, args, err := q.ToSql() |
| 651 | if err != nil { |
| 652 | return err |
| 653 | } |
| 654 | return tx.exec(ctx, sql, args...) |
| 655 | } |
| 656 | |
| 657 | func (tx *Transaction) updateQuery(resource *schema.Resource) (sq.UpdateBuilder, error) { |
| 658 | s := resource.Schema() |