mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-01 22:02:20 +00:00
44 lines
731 B
Go
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
|
|
}
|