From 00ac535827c9a11f86d3781c4cabaf7c063a5117 Mon Sep 17 00:00:00 2001 From: shoopea Date: Mon, 17 Feb 2020 18:05:30 +0800 Subject: [PATCH] update for classes --- obj.go | 10 ++++++++ sql.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/obj.go b/obj.go index a6063d5..5c61109 100644 --- a/obj.go +++ b/obj.go @@ -19,6 +19,8 @@ var ( cacheObjType map[string]int64 cacheObjSubType map[string]int64 + cacheObjClassif map[string]ObjClassif + cacheObjClass map[string]ObjClass cacheObjJob map[int64]Job muxObjJob sync.Mutex ) @@ -34,6 +36,14 @@ func initCache(initDB bool) { err = loadObjSubType() logOnError(err, "initCache : caching obj_sub_type") + log.Println("Caching obj_classif ..") + err = loadObjClassif() + logOnError(err, "initCache : caching obj_classif") + + log.Println("Caching obj_class ..") + err = loadObjClass() + logOnError(err, "initCache : caching obj_class") + log.Println("Caching guilds ..") err = loadObjGuild() logOnError(err, "initCache : caching guilds") diff --git a/sql.go b/sql.go index 032d47d..3e7723b 100644 --- a/sql.go +++ b/sql.go @@ -71,6 +71,30 @@ func initDB() { failOnError(err, "initDB : create table code_obj_sub_type") log.Println("initDB : code_obj_sub_type created ...") + _, err = db.Exec(`CREATE TABLE code_obj_classif ( + id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT + ,intl_id VARCHAR(32) NOT NULL + ,name VARCHAR(80) NOT NULL + ,obj_type_id SMALLINT UNSIGNED NOT NULL + ,PRIMARY KEY (id) + ,UNIQUE KEY (intl_id) + ,FOREIGN KEY (obj_type_id) REFERENCES code_obj_type(id) ON DELETE CASCADE + ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) + failOnError(err, "initDB : create table code_obj_type") + log.Println("initDB : code_obj_classif created ...") + + _, err = db.Exec(`CREATE TABLE code_obj_class ( + id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT + ,intl_id VARCHAR(32) NOT NULL + ,name VARCHAR(80) NOT NULL + ,obj_classif_id SMALLINT UNSIGNED NOT NULL + ,PRIMARY KEY (id) + ,UNIQUE KEY (intl_id) + ,FOREIGN KEY (obj_classif_id) REFERENCES code_obj_classif(id) ON DELETE CASCADE + ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) + failOnError(err, "initDB : create table code_obj_class") + log.Println("initDB : code_obj_class created ...") + _, err = db.Exec(`CREATE TABLE obj ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,obj_type_id SMALLINT UNSIGNED NOT NULL @@ -83,6 +107,18 @@ func initDB() { failOnError(err, "initDB : create table obj") log.Println("initDB : obj created ...") + _, err = db.Exec(`CREATE TABLE obj_class ( + obj_id BIGINT UNSIGNED NOT NULL + ,obj_classif_id SMALLINT UNSIGNED NOT NULL + ,obj_class_id SMALLINT UNSIGNED NOT NULL + ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE + ,FOREIGN KEY (obj_classif_id) REFERENCES code_obj_classif(id) ON DELETE CASCADE + ,FOREIGN KEY (obj_class_id) REFERENCES code_obj_class(id) ON DELETE CASCADE + ,UNIQUE KEY (obj_id, obj_classif_id) + ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) + failOnError(err, "initDB : create table obj_class") + log.Println("initDB : obj_class created ...") + _, err = db.Exec(`CREATE TABLE obj_item ( obj_id BIGINT UNSIGNED NOT NULL ,intl_id VARCHAR(32) @@ -634,6 +670,42 @@ func insertObjSubType(intlId string, name string, objType string) error { return nil } +func insertObjClassif(intlId string, name string, objType string) error { + stmt, err := db.Prepare(`INSERT INTO code_obj_classif + SELECT null id, ? intl_id, ? name, cot.id obj_type_id + FROM code_obj_type cot + WHERE cot.intl_id = ?;`) + if err != nil { + return err + } + defer stmt.Close() + + _, err = stmt.Exec(intlId, name, objType) + if err != nil { + return err + } + + return nil +} + +func insertObjClass(intlId string, name string, objClassif string) error { + stmt, err := db.Prepare(`INSERT INTO code_obj_class + SELECT null id, ? intl_id, ? name, coc.id obj_classif_id + FROM code_obj_classif coc + WHERE coc.intl_id = ?;`) + if err != nil { + return err + } + defer stmt.Close() + + _, err = stmt.Exec(intlId, name, objClassif) + if err != nil { + return err + } + + return nil +} + func insertMsgItem(objId int64, itemId int64, quantity int64) error { stmt, err := db.Prepare(`INSERT INTO obj_msg_item (obj_id, item_id, quantity) VALUES (?, ?, ?);`)