github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/mysql.go (about) 1 // +build !pgsql,!mssql 2 3 /* 4 * 5 * Gosora MySQL Interface 6 * Copyright Azareal 2016 - 2020 7 * 8 */ 9 package main 10 11 import ( 12 "log" 13 14 c "github.com/Azareal/Gosora/common" 15 qgen "github.com/Azareal/Gosora/query_gen" 16 _ "github.com/go-sql-driver/mysql" 17 "github.com/pkg/errors" 18 ) 19 20 var dbCollation = "utf8mb4_general_ci" 21 22 func init() { 23 dbAdapter = "mysql" 24 _initDatabase = initMySQL 25 } 26 27 func initMySQL() (err error) { 28 err = qgen.Builder.Init("mysql", map[string]string{ 29 "host": c.DbConfig.Host, 30 "port": c.DbConfig.Port, 31 "name": c.DbConfig.Dbname, 32 "username": c.DbConfig.Username, 33 "password": c.DbConfig.Password, 34 "collation": dbCollation, 35 }) 36 if err != nil { 37 return errors.WithStack(err) 38 } 39 40 // Set the number of max open connections 41 db = qgen.Builder.GetConn() 42 db.SetMaxOpenConns(64) 43 db.SetMaxIdleConns(32) 44 //db.SetConnMaxLifetime(time.Second * 60 * 5) // Just in case we accumulate some bad connections due to the MySQL driver being stupid 45 46 // Only hold connections open for five seconds to avoid accumulating a large number of stale connections 47 //db.SetConnMaxLifetime(5 * time.Second) 48 db.SetConnMaxLifetime(c.DBTimeout()) 49 50 // Build the generated prepared statements, we are going to slowly move the queries over to the query generator rather than writing them all by hand, this'll make it easier for us to implement database adapters for other databases like PostgreSQL, MSSQL, SQlite, etc. 51 err = _gen_mysql() 52 if err != nil { 53 return errors.WithStack(err) 54 } 55 56 // TODO: Is there a less noisy way of doing this for tests? 57 /*log.Print("Preparing getActivityFeedByWatcher statement.") 58 stmts.getActivityFeedByWatcher, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID, activity_stream.createdAt FROM `activity_stream_matches` INNER JOIN `activity_stream` ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE `watcher` = ? ORDER BY activity_stream.asid DESC LIMIT 16") 59 if err != nil { 60 return errors.WithStack(err) 61 }*/ 62 63 log.Print("Preparing getActivityFeedByWatcher statement.") 64 stmts.getActivityFeedByWatcher, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID, activity_stream.createdAt FROM activity_stream_matches INNER JOIN activity_stream ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE watcher=? ORDER BY activity_stream.asid DESC LIMIT ?") 65 if err != nil { 66 return errors.WithStack(err) 67 } 68 69 /*log.Print("Preparing getActivityFeedByWatcherAfter statement.") 70 stmts.getActivityFeedByWatcherAfter, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID, activity_stream.createdAt FROM activity_stream_matches INNER JOIN activity_stream ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE watcher=? AND asid => ? ORDER BY activity_stream.asid DESC LIMIT ?") 71 if err != nil { 72 return errors.WithStack(err) 73 }*/ 74 75 log.Print("Preparing getActivityCountByWatcher statement.") 76 stmts.getActivityCountByWatcher, err = db.Prepare("SELECT count(*) FROM activity_stream_matches INNER JOIN activity_stream ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE watcher=?") 77 if err != nil { 78 return errors.WithStack(err) 79 } 80 81 return nil 82 }