go-mastodon-stats/internal/gomastodonstats/mysql.go
2023-05-08 23:03:03 +12:00

44 lines
731 B
Go

package gomastodonstats
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
// Returns valid DSL for PSQL
func getMySQLConnectionString(schema string) string {
return fmt.Sprintf(
"%s:%s@tcp(%s:3306)/%s",
MYSQL_USER,
MYSQL_PASS,
MYSQL_HOST,
schema,
)
}
// Returns DB connection
func getMySQLConnection(schema string) (*sql.DB, error) {
return sql.Open("mysql", getMySQLConnectionString(schema))
}
func runMySqlIntQuery(schema string, q string) (int, error) {
var res int
db, err := getMySQLConnection(schema)
if err != nil {
return res, err
}
rows, err := db.Query(q)
if err != nil {
return res, err
}
defer rows.Close()
for rows.Next() {
rows.Scan(&res)
}
return res, err
}