From e79aff64580502b2fbcd1c8e44ba360f4c7d2059 Mon Sep 17 00:00:00 2001 From: Jinghan Ying Date: Thu, 2 Dec 2021 20:06:33 +0800 Subject: [PATCH] feat(database/metadata): implement mysql method createEntity Implementation of createEntity is pretty different between postgres and mysql, reimplement it for mysql. --- internal/database/metadata/mysql/entity.go | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 internal/database/metadata/mysql/entity.go diff --git a/internal/database/metadata/mysql/entity.go b/internal/database/metadata/mysql/entity.go new file mode 100644 index 000000000..c367e17c9 --- /dev/null +++ b/internal/database/metadata/mysql/entity.go @@ -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 +}