func setupConnection(con_type string, driver string, dataSource string, maxIdle int, maxOpen int, trace bool) *gorp.DbMap { charset := "" if strings.Index(dataSource, "?") > -1 { charset = "&charset=utf8mb4,utf8" } else { charset = "?charset=utf8mb4,utf8" } db, err := dbsql.Open(driver, dataSource+charset) if err != nil { l4g.Critical("Failed to open sql connection to '%v' err:%v", dataSource, err) time.Sleep(time.Second) panic("Failed to open sql connection" + err.Error()) } l4g.Info("Pinging sql %v database at '%v'", con_type, dataSource) err = db.Ping() if err != nil { l4g.Critical("Failed to ping db err:%v", err) time.Sleep(time.Second) panic("Failed to open sql connection " + err.Error()) } db.SetMaxIdleConns(maxIdle) db.SetMaxOpenConns(maxOpen) var dbmap *gorp.DbMap if driver == "sqlite3" { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.SqliteDialect{}} } else if driver == "mysql" { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8MB4"}} } else { l4g.Critical("Failed to create dialect specific driver") time.Sleep(time.Second) panic("Failed to create dialect specific driver " + err.Error()) } if trace { dbmap.TraceOn("", sqltrace.New(os.Stdout, "sql-trace:", sqltrace.Lmicroseconds)) } return dbmap }
func setupConnection(con_type string, driver string, dataSource string, maxIdle int, maxOpen int, trace bool) *gorp.DbMap { db, err := dbsql.Open(driver, dataSource) if err != nil { l4g.Critical(utils.T("store.sql.open_conn.critical"), err) time.Sleep(time.Second) os.Exit(EXIT_DB_OPEN) } l4g.Info(utils.T("store.sql.pinging.info"), con_type) err = db.Ping() if err != nil { l4g.Critical(utils.T("store.sql.ping.critical"), err) time.Sleep(time.Second) os.Exit(EXIT_PING) } db.SetMaxIdleConns(maxIdle) db.SetMaxOpenConns(maxOpen) db.SetConnMaxLifetime(time.Duration(MAX_DB_CONN_LIFETIME) * time.Minute) var dbmap *gorp.DbMap if driver == "sqlite3" { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.SqliteDialect{}} } else if driver == model.DATABASE_DRIVER_MYSQL { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8MB4"}} } else if driver == model.DATABASE_DRIVER_POSTGRES { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.PostgresDialect{}} } else { l4g.Critical(utils.T("store.sql.dialect_driver.critical")) time.Sleep(time.Second) os.Exit(EXIT_NO_DRIVER) } if trace { dbmap.TraceOn("", sqltrace.New(os.Stdout, "sql-trace:", sqltrace.Lmicroseconds)) } return dbmap }
func setupConnection(con_type string, driver string, dataSource string, maxIdle int, maxOpen int, trace bool) *gorp.DbMap { db, err := dbsql.Open(driver, dataSource) if err != nil { l4g.Critical("Failed to open sql connection to err:%v", err) time.Sleep(time.Second) panic("Failed to open sql connection" + err.Error()) } l4g.Info("Pinging sql %v database", con_type) err = db.Ping() if err != nil { l4g.Critical("Failed to ping db err:%v", err) time.Sleep(time.Second) panic("Failed to open sql connection " + err.Error()) } db.SetMaxIdleConns(maxIdle) db.SetMaxOpenConns(maxOpen) var dbmap *gorp.DbMap if driver == "sqlite3" { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.SqliteDialect{}} } else if driver == model.DATABASE_DRIVER_MYSQL { //dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8MB4"}} dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.MySQLDialect{Engine: "MyISAM", Encoding: "UTF8"}} } else if driver == model.DATABASE_DRIVER_POSTGRES { dbmap = &gorp.DbMap{Db: db, TypeConverter: mattermConverter{}, Dialect: gorp.PostgresDialect{}} } else { l4g.Critical("Failed to create dialect specific driver") time.Sleep(time.Second) panic("Failed to create dialect specific driver " + err.Error()) } if trace { dbmap.TraceOn("", sqltrace.New(os.Stdout, "sql-trace:", sqltrace.Lmicroseconds)) } return dbmap }