MCPcopy Index your code
hub / github.com/micro/go-micro / Create

Method Create

model/postgres/postgres.go:93–109  ·  view source on GitHub ↗
(ctx context.Context, v interface{})

Source from the content-addressed store, hash-verified

91}
92
93func (d *postgresModel) Create(ctx context.Context, v interface{}) error {
94 schema, err := d.schema(v)
95 if err != nil {
96 return err
97 }
98 fields := model.StructToMap(schema, v)
99 cols, placeholders, values := buildInsert(schema, fields)
100 query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", quoteIdent(schema.Table), cols, placeholders)
101 _, err = d.db.ExecContext(ctx, query, values...)
102 if err != nil {
103 if strings.Contains(err.Error(), "duplicate key") || strings.Contains(err.Error(), "unique constraint") {
104 return model.ErrDuplicateKey
105 }
106 return fmt.Errorf("model/postgres: create: %w", err)
107 }
108 return nil
109}
110
111func (d *postgresModel) Read(ctx context.Context, key string, v interface{}) error {
112 schema, err := d.schema(v)

Callers

nothing calls this directly

Calls 6

schemaMethod · 0.95
StructToMapFunction · 0.92
quoteIdentFunction · 0.85
buildInsertFunction · 0.70
ErrorMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected