mirror of
https://github.com/idanoo/go-mastodon-stats
synced 2025-07-03 06:42:19 +00:00
Test adding MySQL
This commit is contained in:
parent
44a6a2b634
commit
ee2ab2b7a2
9 changed files with 111 additions and 11 deletions
44
internal/gomastodonstats/mysql.go
Normal file
44
internal/gomastodonstats/mysql.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
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@%s/%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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue