MCPcopy Create free account
hub / github.com/cloudwan/gohan / CreateContext

Method CreateContext

db/sql/sql.go:628–655  ·  view source on GitHub ↗

Create create resource in the db

(ctx context.Context, resource *schema.Resource)

Source from the content-addressed store, hash-verified

626
627//Create create resource in the db
628func (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
657func (tx *Transaction) updateQuery(resource *schema.Resource) (sq.UpdateBuilder, error) {
658 s := resource.Schema()

Callers 1

CreateMethod · 0.95

Calls 11

measureTimeMethod · 0.95
execMethod · 0.95
InsertMethod · 0.80
GetDbTableNameMethod · 0.80
handlerMethod · 0.80
ValuesMethod · 0.80
quoteFunction · 0.70
SchemaMethod · 0.65
encodeMethod · 0.65
ErrorfMethod · 0.65
DataMethod · 0.45

Tested by

no test coverage detected