package main import ( "errors" "log" "regexp" "strconv" "github.com/go-sql-driver/mysql" ) func initDB() { log.Println("Setting up database...") tx, err := db.Begin() if err != nil { log.Fatal(err) } defer tx.Rollback() _, err = tx.Exec("set foreign_key_checks = 0") failOnError(err, "initDB : set foreign_key_checks = 0") var name string rows, err := db.Query("show tables") failOnError(err, "initDB : show tables") for rows.Next() { err = rows.Scan(&name) failOnError(err, "initDB : show tables listing") if ok, _ := regexp.MatchString(`^.*_v$`, name); ok { _, err = tx.Exec("drop view " + name) failOnError(err, "initDB : drop view "+name) } else { _, err = tx.Exec("drop table " + name) failOnError(err, "initDB : drop table "+name) } } err = rows.Err() failOnError(err, "initDB : show tables listing end") rows.Close() _, err = tx.Exec("set foreign_key_checks = 1") failOnError(err, "initDB : set foreign_key_checks = 1") err = tx.Commit() failOnError(err, "initDB : commit cleanup") log.Println("initDB : Database cleaned up") _, err = db.Exec(`CREATE TABLE code_obj_type ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT ,intl_id VARCHAR(32) NOT NULL ,name VARCHAR(80) NOT NULL ,PRIMARY KEY (id) ,UNIQUE KEY (intl_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table code_obj_type") log.Println("initDB : code_obj_type created ...") _, err = db.Exec(`CREATE TABLE code_obj_sub_type ( 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_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 ,obj_sub_type_id SMALLINT UNSIGNED NOT NULL ,timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,PRIMARY KEY (id) ,FOREIGN KEY (obj_type_id) REFERENCES code_obj_type(id) ON DELETE CASCADE ,FOREIGN KEY (obj_sub_type_id) REFERENCES code_obj_sub_type(id) ON DELETE CASCADE ) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) 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) ,weight SMALLINT NOT NULL ,exchange TINYINT NOT NULL ,auction TINYINT NOT NULL ,craftable TINYINT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (intl_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_item") log.Println("initDB : obj_item created ...") _, err = db.Exec(`CREATE TABLE obj_craft ( obj_id BIGINT UNSIGNED NOT NULL ,cmd VARCHAR(32) ,mana INT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (cmd) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_craft") log.Println("initDB : obj_craft created ...") _, err = db.Exec(`CREATE TABLE obj_craft_item ( obj_id BIGINT UNSIGNED NOT NULL ,item_id BIGINT UNSIGNED NOT NULL ,quantity INT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (item_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (obj_id, item_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_craft_item") log.Println("initDB : obj_craft_item created ...") _, err = db.Exec(`CREATE TABLE obj_castle ( obj_id BIGINT UNSIGNED NOT NULL ,logo VARCHAR(32) NOT NULL ,name VARCHAR(80) NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_castle") log.Println("initDB : obj_guild created ...") _, err = db.Exec(`CREATE TABLE obj_guild ( obj_id BIGINT UNSIGNED NOT NULL ,tag VARCHAR(32) NOT NULL ,name VARCHAR(80) NOT NULL ,chat_id BIGINT ,deposit_chat_id BIGINT ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_guild") log.Println("initDB : obj_guild created ...") _, err = db.Exec(`CREATE TABLE obj_user ( obj_id BIGINT UNSIGNED NOT NULL ,name VARCHAR(80) NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_user") log.Println("initDB : obj_user created ...") _, err = db.Exec(`CREATE TABLE obj_war ( obj_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,start_time DATETIME NOT NULL ,end_time DATETIME NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_war") log.Println("initDB : obj_war created ...") _, err = db.Exec(`CREATE TABLE obj_msg ( obj_id BIGINT UNSIGNED NOT NULL ,msg_id BIGINT NOT NULL ,chat_id BIGINT NOT NULL ,user_id BIGINT NOT NULL CHECK (user_id > 0) ,sender_user_id BIGINT NOT NULL CHECK (sender_user_id > 0) ,date DATETIME NOT NULL ,text VARCHAR(4096) NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (msg_id, chat_id, user_id, sender_user_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg") log.Println("initDB : obj_msg created ...") _, err = db.Exec(`CREATE TABLE obj_msg_callback ( obj_id BIGINT UNSIGNED NOT NULL ,name VARCHAR(128) NOT NULL ,data VARBINARY(256) NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (obj_id, name) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_callback") log.Println("initDB : obj_msg_callback created ...") _, err = db.Exec(`CREATE TABLE obj_msg_pillage_inc ( obj_id BIGINT UNSIGNED NOT NULL ,attacker VARCHAR(32) ,guild VARCHAR(32) ,castle_id BIGINT UNSIGNED NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_report") log.Println("initDB : obj_msg_report created ...") _, err = db.Exec(`CREATE TABLE obj_msg_report ( obj_id BIGINT UNSIGNED NOT NULL ,war_id BIGINT UNSIGNED NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (war_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_report") log.Println("initDB : obj_msg_report created ...") _, err = db.Exec(`CREATE TABLE obj_war_report ( obj_id BIGINT UNSIGNED NOT NULL ,user_id BIGINT UNSIGNED NOT NULL ,war_id BIGINT UNSIGNED NOT NULL ,attack SMALLINT UNSIGNED NOT NULL ,defense SMALLINT UNSIGNED NOT NULL ,gold SMALLINT UNSIGNED NOT NULL ,stock SMALLINT UNSIGNED NOT NULL ,exp SMALLINT UNSIGNED NOT NULL ,stamina TINYINT NOT NULL ,crit TINYINT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (war_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (user_id, war_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_war_report") log.Println("initDB : obj_war_report created ...") _, err = db.Exec(`CREATE TABLE obj_msg_auction_announce ( obj_id BIGINT UNSIGNED NOT NULL ,lot_id BIGINT UNSIGNED NOT NULL ,item_id BIGINT UNSIGNED ,cond VARCHAR(32) ,quality VARCHAR(32) ,seller_castle_id BIGINT UNSIGNED ,seller_guild_id BIGINT UNSIGNED ,seller_id BIGINT UNSIGNED ,buyer_castle_id BIGINT UNSIGNED ,buyer_guild_id BIGINT UNSIGNED ,buyer_id BIGINT UNSIGNED ,price SMALLINT NOT NULL ,status VARCHAR(32) ,end DATETIME NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (seller_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE ,FOREIGN KEY (buyer_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE ,FOREIGN KEY (seller_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE ,FOREIGN KEY (buyer_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE ,FOREIGN KEY (seller_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE ,FOREIGN KEY (buyer_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE ,KEY (lot_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_auction_announce") log.Println("initDB : obj_msg_auction_announce created ...") _, err = db.Exec(`CREATE TABLE obj_msg_duel_fight ( obj_id BIGINT UNSIGNED NOT NULL ,win_castle_id BIGINT UNSIGNED NOT NULL ,win_guild_id BIGINT UNSIGNED ,win_user_id BIGINT UNSIGNED NOT NULL ,win_life SMALLINT NOT NULL ,loss_castle_id BIGINT UNSIGNED NOT NULL ,loss_guild_id BIGINT UNSIGNED ,loss_user_id BIGINT UNSIGNED NOT NULL ,loss_life SMALLINT NOT NULL ,exp INT UNSIGNED NOT NULL ,weapon VARCHAR(80) ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (win_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE ,FOREIGN KEY (loss_castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE ,FOREIGN KEY (win_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE ,FOREIGN KEY (loss_guild_id) REFERENCES obj_guild(obj_id) ON DELETE CASCADE ,FOREIGN KEY (win_user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE ,FOREIGN KEY (loss_user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_duel_fight") log.Println("initDB : obj_msg_duel_fight created ...") _, err = db.Exec(`CREATE TABLE obj_msg_auction_upd_req ( obj_id BIGINT UNSIGNED NOT NULL ,lot_id BIGINT UNSIGNED NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,KEY (lot_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_auction_upd_req") log.Println("initDB : obj_msg_auction_upd_req created ...") _, err = db.Exec(`CREATE TABLE obj_msg_auction_upd_ack ( obj_id BIGINT UNSIGNED NOT NULL ,seller VARCHAR(32) ,buyer VARCHAR(32) ,item VARCHAR(80) ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_auction_upd_ack") log.Println("initDB : obj_msg_auction_upd_ack created ...") _, err = db.Exec(`CREATE TABLE obj_msg_exp ( obj_id BIGINT UNSIGNED NOT NULL ,exp INT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_exp") log.Println("initDB : obj_msg_exp created ...") _, err = db.Exec(`CREATE TABLE obj_msg_gold ( obj_id BIGINT UNSIGNED NOT NULL ,gold INT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_gold") log.Println("initDB : obj_msg_gold created ...") _, err = db.Exec(`CREATE TABLE obj_msg_item ( obj_id BIGINT UNSIGNED NOT NULL ,item_id BIGINT UNSIGNED NOT NULL ,quantity INT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (item_id) REFERENCES obj_item(obj_id) ON DELETE CASCADE ,UNIQUE KEY (obj_id, item_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_item") log.Println("initDB : obj_msg_item created ...") _, err = db.Exec(`CREATE TABLE obj_msg_tribute ( obj_id BIGINT UNSIGNED NOT NULL ,user_id BIGINT UNSIGNED NOT NULL ,item_id BIGINT UNSIGNED NOT NULL ,quantity SMALLINT NOT NULL ,xp SMALLINT NOT NULL ,date DATETIME NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE ,FOREIGN KEY (item_id) REFERENCES obj_item(obj_id) ON DELETE CASCADE ,UNIQUE KEY (user_id, date) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_tribute") log.Println("initDB : obj_msg_tribute created ...") _, err = db.Exec(`CREATE TABLE obj_msg_shop_main ( obj_id BIGINT UNSIGNED NOT NULL ,name VARCHAR(80) ,link VARCHAR(80) ,number INT UNSIGNED NOT NULL ,user VARCHAR(80) ,mana INT UNSIGNED NOT NULL ,mana_total INT UNSIGNED NOT NULL ,class VARCHAR(80) ,castle_id BIGINT UNSIGNED NOT NULL ,fees VARCHAR(80) ,guru VARCHAR(80) ,open TINYINT NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (castle_id) REFERENCES obj_castle(obj_id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_msg_shop_main") log.Println("initDB : obj_msg_shop_main created ...") _, err = db.Exec(`CREATE TABLE obj_tribute ( tg_user_id BIGINT UNSIGNED NOT NULL ,item_id BIGINT UNSIGNED NOT NULL ,quantity INT NOT NULL ,exp INT NOT NULL ,date DATETIME NOT NULL ,FOREIGN KEY (item_id) REFERENCES obj_item(obj_id) ON DELETE CASCADE ,UNIQUE KEY (tg_user_id, item_id, quantity, exp, date) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_tribute") log.Println("initDB : obj_tribute created ...") _, err = db.Exec(`CREATE TABLE msg_rules ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT ,prio SMALLINT NOT NULL ,descn VARCHAR(32) NOT NULL ,rule VARCHAR(4096) NOT NULL ,msg_type_id SMALLINT UNSIGNED NOT NULL ,chat_id BIGINT NOT NULL ,user_id BIGINT NOT NULL ,UNIQUE KEY (id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table msg_rules") log.Println("initDB : msg_rules created ...") _, err = db.Exec(`CREATE TABLE obj_xp ( obj_id BIGINT UNSIGNED NOT NULL ,user_id BIGINT UNSIGNED NOT NULL ,val BIGINT UNSIGNED NOT NULL ,target BIGINT UNSIGNED NOT NULL ,level SMALLINT UNSIGNED NOT NULL ,date DATETIME NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE ,UNIQUE KEY (user_id, date) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_xp") log.Println("initDB : obj_xp created ...") _, err = db.Exec(`CREATE TABLE obj_quest ( obj_id BIGINT UNSIGNED NOT NULL ,user_id BIGINT UNSIGNED NOT NULL ,duration SMALLINT UNSIGNED NOT NULL ,date DATETIME NOT NULL ,exp INT UNSIGNED NOT NULL ,gold INT UNSIGNED NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,FOREIGN KEY (user_id) REFERENCES obj_user(obj_id) ON DELETE CASCADE ,UNIQUE KEY (user_id, date) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_quest") log.Println("initDB : obj_xp obj_quest ...") _, err = db.Exec(`CREATE TABLE obj_job ( obj_id BIGINT UNSIGNED NOT NULL ,priority SMALLINT NOT NULL ,user_id BIGINT UNSIGNED NOT NULL ,trigger_id BIGINT UNSIGNED NOT NULL ,schedule DATETIME NOT NULL ,is_done TINYINT NOT NULL ,in_work TINYINT NOT NULL ,seq_nr BIGINT UNSIGNED ,inserted TIMESTAMP ,pulled TIMESTAMP ,started TIMESTAMP ,ended TIMESTAMP ,timeout TIMESTAMP ,zipped TINYINT NOT NULL ,payload VARBINARY(20000) ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,KEY (is_done) ,KEY (in_work) ,KEY (user_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_job") log.Println("initDB : obj_job created ...") _, err = db.Exec(`CREATE TABLE obj_name ( obj_id BIGINT UNSIGNED NOT NULL ,name VARCHAR(80) ,priority INT UNSIGNED NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY(name) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_bin;`) failOnError(err, "initDB : create table obj_name") log.Println("initDB : obj_name created ...") log.Println("initDB : Database set up") } func initDBViews() { tx, err := db.Begin() if err != nil { log.Fatal(err) } defer tx.Rollback() _, err = tx.Exec("set foreign_key_checks = 0") failOnError(err, "initDBViews : set foreign_key_checks = 0") var name string rows, err := db.Query("show tables") failOnError(err, "initDBViews : show tables") for rows.Next() { err = rows.Scan(&name) failOnError(err, "initDBViews : show tables listing") if ok, _ := regexp.MatchString(`^.*_v$`, name); ok { _, err = tx.Exec("drop view " + name) failOnError(err, "initDBViews : drop view "+name) } } err = rows.Err() failOnError(err, "initDBViews : show tables listing end") rows.Close() _, err = tx.Exec("set foreign_key_checks = 1") failOnError(err, "initDBViews : set foreign_key_checks = 1") err = tx.Commit() failOnError(err, "initDBViews : commit cleanup") log.Println("initDBViews : Views cleaned up") _, err = db.Exec(`CREATE VIEW obj_user_v AS SELECT ou.obj_id ,ou.name COLLATE utf8mb4_unicode_ci AS name FROM obj_user ou;`) failOnError(err, "initDBViews : create view obj_user_v") log.Println("initDBViews : obj_user_v created ...") _, err = db.Exec(`CREATE VIEW obj_msg_v AS SELECT om.obj_id ,o.obj_sub_type_id AS msg_type_id ,cost.intl_id COLLATE utf8mb4_unicode_ci AS msg_type ,om.msg_id ,om.chat_id ,om.user_id ,om.sender_user_id ,om.date ,om.text COLLATE utf8mb4_unicode_ci AS text FROM obj_msg om ,obj o ,code_obj_sub_type cost WHERE om.obj_id = o.id AND cost.id = o.obj_sub_type_id;`) failOnError(err, "initDBViews : create view obj_msg_v") log.Println("initDBViews : obj_msg_v created ...") _, err = db.Exec(`CREATE VIEW msg_rules_v AS SELECT r.id ,r.prio ,r.descn COLLATE utf8mb4_unicode_ci AS descn ,r.rule COLLATE utf8mb4_unicode_ci AS rule ,cost.intl_id COLLATE utf8mb4_unicode_ci AS msg_type FROM msg_rules r ,code_obj_sub_type cost WHERE cost.id = r.msg_type_id;`) failOnError(err, "initDBViews : create table msg_rules_v") log.Println("initDBViews : msg_rules_v created ...") _, err = db.Exec(`CREATE VIEW obj_name_v AS SELECT obn.obj_id ,obn.name COLLATE utf8mb4_unicode_ci AS name ,obn.priority FROM obj_name obn;`) failOnError(err, "initDBViews : create view obj_name_v") log.Println("initDBViews : obj_name_v created ...") _, err = db.Exec(`CREATE VIEW obj_item_name_v AS SELECT obi.obj_id ,obi.intl_id COLLATE utf8mb4_unicode_ci AS intl_id ,obn.name COLLATE utf8mb4_unicode_ci AS name ,obn.priority FROM obj_name obn ,obj_item obi WHERE obi.obj_id = obn.obj_id ORDER BY obi.intl_id ASC;`) failOnError(err, "initDBViews : create view obj_item_name_v") log.Println("initDBViews : obj_item_name_v created ...") _, err = db.Exec(`CREATE VIEW obj_msg_vault_v AS SELECT obm.sender_user_id user_id ,obm.chat_id ,om.obj_sub_type_id msg_type_id ,omi.item_id ,oi.obj_sub_type_id item_type_id ,omi.quantity FROM obj om ,obj_msg obm ,obj oi ,obj_msg_item omi WHERE om.obj_type_id = ` + strconv.FormatInt(cacheObjType[`msg`], 10) + ` AND om.obj_sub_type_id in (` + strconv.FormatInt(cacheObjSubType[`msg_withdraw_rcv`], 10) + `, ` + strconv.FormatInt(cacheObjSubType[`msg_g_deposit_ack`], 10) + `) AND obm.obj_id = om.id AND omi.obj_id = om.id AND oi.id = omi.item_id ORDER BY obm.sender_user_id ASC ,omi.item_id ASC;`) failOnError(err, "initDBViews : create view obj_msg_vault_v") log.Println("initDBViews : obj_msg_vault_v created ...") _, err = db.Exec(`CREATE VIEW obj_msg_callback_v AS SELECT om.obj_id ,o.obj_sub_type_id AS msg_type_id ,cost.intl_id COLLATE utf8mb4_unicode_ci AS msg_type ,om.msg_id ,om.chat_id ,omc.name COLLATE utf8mb4_unicode_ci AS name ,CAST(omc.data as CHAR) data FROM obj_msg om ,obj o ,code_obj_sub_type cost ,obj_msg_callback omc WHERE om.obj_id = o.id AND cost.id = o.obj_sub_type_id AND omc.obj_id = o.id;`) failOnError(err, "initDBViews : create view obj_msg_callback_v") log.Println("initDBViews : obj_msg_callback_v created ...") _, err = db.Exec(`CREATE VIEW obj_shop_main_v AS SELECT omsm.obj_id ,omsm.name COLLATE utf8mb4_unicode_ci AS name ,omsm.link COLLATE utf8mb4_unicode_ci AS link ,omsm.number ,omsm.user COLLATE utf8mb4_unicode_ci AS user ,oc.name COLLATE utf8mb4_unicode_ci AS castle ,omsm.mana ,omsm.mana_total ,omsm.class COLLATE utf8mb4_unicode_ci AS class ,omsm.fees COLLATE utf8mb4_unicode_ci AS fees ,omsm.guru COLLATE utf8mb4_unicode_ci AS guru ,omsm.open ,omsm.castle_id FROM obj_msg_shop_main omsm ,obj_castle oc WHERE oc.obj_id = omsm.castle_id;`) failOnError(err, "initDBViews : create view obj_shop_main_v") log.Println("initDBViews : obj_shop_main_v created ...") _, err = db.Exec(`CREATE VIEW obj_job_v AS SELECT oj.obj_id ,o.obj_sub_type_id ,cost.intl_id COLLATE utf8mb4_unicode_ci AS obj_sub_type ,oj.is_done ,oj.in_work ,oj.trigger_id ,(SELECT costx.intl_id FROM code_obj_sub_type costx, obj ox WHERE ox.id = oj.trigger_id AND costx.id = ox.obj_sub_type_id) COLLATE utf8mb4_unicode_ci AS trigger_sub_type ,oj.inserted ,oj.ended FROM obj o ,obj_job oj ,code_obj_sub_type cost WHERE o.id = oj.obj_id AND cost.id = o.obj_sub_type_id;`) failOnError(err, "initDBViews : create view obj_job_v") log.Println("initDBViews : obj_shop_main_v created ...") log.Println("initDBViews : Views set up") } func insertObjType(intlId string, name string) error { stmt, err := db.Prepare(`INSERT INTO code_obj_type (intl_id, name) VALUES (?, ?);`) if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(intlId, name) if err != nil { return err } return nil } func insertObjSubType(intlId string, name string, objType string) error { stmt, err := db.Prepare(`INSERT INTO code_obj_sub_type SELECT null id, ? intl_id, ? name, cot.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 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 (?, ?, ?);`) if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(objId, itemId, quantity) if err != nil { return err } return nil } func insertMsgReport(objId int64, war_date int32, atk int32, def int32, exp int32, gold int32, stock int32, crit bool, stamina bool) error { objSubTypeId, err := getObjSubTypeId(objId) if err != nil { return err } if objSubTypeId != cacheObjSubType[`msg`] { return errors.New("Message is not of type Unknown") } /* obj_id BIGINT UNSIGNED NOT NULL ,war_date TIMESTAMP NOT NULL ,attack SMALLINT UNSIGNED NOT NULL ,defense SMALLINT UNSIGNED NOT NULL ,gold SMALLINT UNSIGNED NOT NULL ,stock MALLINT UNSIGNED NOT NULL ,exp SMALLINT UNSIGNED NOT NULL ,stamina TINYINT NOT NULL ,crit TINYINT NOT NULL stmt, err := db.Prepare(`INSERT INTO obj_msg_report (obj_id, war_date, attack, defense, gold, stock, exp, stamina, crit) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?FROM_UNIXTIME(?), ?);`) if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(objId, m.ID64, m.ChatID64, m.UserID64, m.SenderUserID64, m.Date, m.Text) if err != nil { return err } */ return nil } func insertMsgDuelFight(m *ChatWarsMessageDuelFight) error { objSubTypeId, err := getObjSubTypeId(m.ObjID64) if err != nil { return err } if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_duel_fight`] { return errors.New("Message type mismatch") } stmt, err := db.Prepare(`INSERT INTO obj_msg_duel_fight (obj_id, win_castle_id, win_guild_id, win_user_id, win_life, loss_castle_id, loss_guild_id, loss_user_id, loss_life, weapon) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`) if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(m.ObjID64, getObjCastleID(m.WinCastle), getObjGuildID(m.WinGuild), getObjUserID(m.WinUser), m.WinLife, getObjCastleID(m.LossCastle), getObjGuildID(m.LossGuild), getObjUserID(m.LossUser), m.LossLife, m.Weapon) if err != nil { return err } return nil } func insertMsgTributesStats(m *ChatWarsMessageTributesStatsAck) error { objSubTypeId, err := getObjSubTypeId(m.Msg.ObjID64) if err != nil { return err } if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_tributes_stats_ack`] { return errors.New("Message type mismatch") } stmt, err := db.Prepare(`INSERT INTO obj_tribute (tg_user_id, item_id, quantity, exp, date) VALUES (?, ?, ?, ?, ?);`) logOnError(err, "insertMsgTributesStats : Prepare") if err != nil { return err } defer stmt.Close() for _, t := range m.Tributes { _, err = stmt.Exec(m.Msg.TGUserID64, t.ItemID64, t.Quantity, t.Exp, t.Date) mysqlerr, ok := err.(*mysql.MySQLError) if ok && mysqlerr.Number == 1062 { // duplicate, expected } else { logOnError(err, "insertMsgTributesStats Exec") } } return nil } func insertMsgShopMainAck(m *ChatWarsMessageShopMainAck) error { objSubTypeId, err := getObjSubTypeId(m.Msg.ObjID64) if err != nil { return err } if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_shop_main_ack`] { return errors.New("Message type mismatch") } stmt, err := db.Prepare(`INSERT INTO obj_msg_shop_main (obj_id, name, link, number, user, mana, mana_total, class, castle_id, fees, guru, open) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`) logOnError(err, "insertMsgShopMainAck : Prepare") if err != nil { return err } defer stmt.Close() var open int if m.Open { open = 1 } else { open = 0 } _, err = stmt.Exec(m.Msg.ObjID64, m.Name, m.Link, m.ShopNumber, m.User, m.Mana, m.ManaTotal, m.Class, m.CastleID64, m.Fees, m.Guru, open) logOnError(err, "insertMsgShopMainAck Exec") return nil } func insertMsgAuctionAnnounce(m *ChatWarsMessageAuctionAnnounce) error { objSubTypeId, err := getObjSubTypeId(m.ObjID64) if err != nil { return err } if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_auction_announce`] { return errors.New("Message type mismatch") } stmt, err := db.Prepare(`INSERT INTO obj_msg_auction_announce (obj_id, lot_id, item_id, cond, quality, seller_id, seller_guild_id, seller_castle_id, buyer_id, buyer_guild_id, buyer_castle_id, price, status, end) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`) if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(m.ObjID64, m.LotID, m.ItemID64, m.Cond, m.Quality, m.SellerUserID64, m.SellerGuildID64, m.SellerCastleID64, m.BuyerUserID64, m.BuyerGuildID64, m.BuyerCastleID64, m.Price, m.Status, m.End) if err != nil { return err } return nil } func insertMsgPillageInc(m *ChatWarsMessagePillageInc) error { objSubTypeId, err := getObjSubTypeId(m.ObjID64) if err != nil { return err } if objSubTypeId != cacheObjSubType[`msg`] && objSubTypeId != cacheObjSubType[`msg_pillage_inc`] && objSubTypeId != cacheObjSubType[`msg_pillage_inc2`] { return errors.New("Message type mismatch") } stmt, err := db.Prepare(`INSERT INTO obj_msg_pillage_inc (obj_id, attacker, guild, castle_id) VALUES (?, ?, ?, ?);`) if err != nil { return err } defer stmt.Close() _, err = stmt.Exec(m.ObjID64, m.Attacker, m.Guild, getObjCastleID(m.Castle)) if err != nil { return err } return nil } func getSQLListID64(q string) []int64 { var ( id int64 ids []int64 ) rows, err := db.Query(q) logOnError(err, "getSQLListID64 : "+q) for rows.Next() { err = rows.Scan(&id) logOnError(err, "getSQLListID64 : scan next val") ids = append(ids, id) } err = rows.Err() logOnError(err, "getSQLListID64 : query end") rows.Close() return ids }