package db

import (
	"World/common"
	"WorldTester/conf"
	"database/sql"
	"log"
	_ "time"

	_ "github.com/go-sql-driver/mysql"
)

var (
	m_game_db             *sql.DB
	club_create_tablename = "dtb_club_create_info"
	user_tablename        = "dtb_user_main"
	activity_tablename    = "dtb_activity_record"
	gameround_tablename   = "dtb_table_round"
	remarks_tablename     = "dtb_remarks"
)

func InitMysql() error {
	db, err := InitMysqlByConf(conf.GetGameDBConf())
	if err != nil {
		return err
	}
	m_game_db = db
	return nil
}

func InitMysqlByConf(cfg conf.MysqlConf) (*sql.DB, error) {
	url := cfg.User + ":" + cfg.Pwd + "@tcp(" + cfg.Ip + ":" + common.Itoa(cfg.Port) + ")/" + cfg.Database + "?charset=utf8"
	log.Println("InitMysqlByConf %s:", url)
	db, err := sql.Open("mysql", url)
	if err != nil {
		return nil, err
	}
	log.Println("InitMysqlByConf success!")
	return db, nil
}
