Skip to content

Commit

Permalink
feat(database/metadata): implement mysql method createEntity
Browse files Browse the repository at this point in the history
Implementation of createEntity is pretty different between postgres
and mysql, reimplement it for mysql.
  • Loading branch information
jinghancc committed Dec 2, 2021
1 parent cb393cc commit e79aff6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/database/metadata/mysql/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mysql

import (
"context"
"fmt"

"github.com/go-sql-driver/mysql"
"github.com/oom-ai/oomstore/internal/database/metadata"
)

func createEntity(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateEntityOpt) (int, error) {
query := "INSERT INTO entity(name, length, description) VALUES(?, ?, ?)"
res, err := sqlxCtx.ExecContext(ctx, query, opt.EntityName, opt.Length, opt.Description)
if er, ok := err.(*mysql.MySQLError); ok {
if er.Number == ER_DUP_ENTRY {
return 0, fmt.Errorf("entity %s already exists", opt.EntityName)
}
}
entityID, err := res.LastInsertId()
if err != nil {
return 0, err
}
return int(entityID), err
}

0 comments on commit e79aff6

Please sign in to comment.