From ff00bb140ee738e3b802d77f07f9098924f3de6a Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 11 Oct 2025 20:59:20 +0500 Subject: feat: database --- cmd/statsbot/main.go | 8 ++++++++ go.mod | 6 +++++- go.sum | 4 ++++ internal/database.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 internal/database.go diff --git a/cmd/statsbot/main.go b/cmd/statsbot/main.go index 4e4ac77..06f010c 100644 --- a/cmd/statsbot/main.go +++ b/cmd/statsbot/main.go @@ -4,9 +4,17 @@ import ( "log" "github.com/gempir/go-twitch-irc" + + stats "ilotterytea/tinystats/internal" ) func main() { + db, err := stats.NewDatabaseConnection("mysql", "kochan:kochan@/stats") + if err != nil { + log.Panicf("Failed to establish database connection: %v\n", err) + } + defer db.Close() + client := twitch.NewClient("justinfan65432", "12345") client.OnConnect(func() { diff --git a/go.mod b/go.mod index ee0f926..cd063f5 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,8 @@ module ilotterytea/tinystats go 1.22.2 -require github.com/gempir/go-twitch-irc v1.1.0 // indirect +require ( + filippo.io/edwards25519 v1.1.0 // indirect + github.com/gempir/go-twitch-irc v1.1.0 // indirect + github.com/go-sql-driver/mysql v1.9.3 // indirect +) diff --git a/go.sum b/go.sum index 2115fc4..87ae2b9 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/gempir/go-twitch-irc v1.1.0 h1:Q9gQGI/3yJzYwlYDlFsGJzWfpaqubMExfmBXNpOC6W0= github.com/gempir/go-twitch-irc v1.1.0/go.mod h1:Pc661rsUSmkQXvI9W2bNyLt4ZrMAgHZPnVwMQEJ0fdo= +github.com/go-sql-driver/mysql v1.9.3 h1:U/N249h2WzJ3Ukj8SowVFjdtZKfu9vlLZxjPXV1aweo= +github.com/go-sql-driver/mysql v1.9.3/go.mod h1:qn46aNg1333BRMNU69Lq93t8du/dwxI64Gl8i5p1WMU= diff --git a/internal/database.go b/internal/database.go new file mode 100644 index 0000000..353c117 --- /dev/null +++ b/internal/database.go @@ -0,0 +1,58 @@ +package stats + +import ( + "database/sql" + + _ "github.com/go-sql-driver/mysql" +) + +type DatabaseConnection struct { + db *sql.DB +} + +func NewDatabaseConnection(driver string, source string) (db *DatabaseConnection, err error) { + d, err := sql.Open("mysql", "kochan:kochan@/stats") + if err != nil { + return + } + db = &DatabaseConnection{db: d} + + return +} + +func (d *DatabaseConnection) Query(query string, args ...any) ([]map[string]string, error) { + data := []map[string]string{} + rows, err := d.db.Query(query, args...) + if err != nil { + return data, err + } + defer rows.Close() + + columns, err := rows.Columns() + if err != nil { + return data, err + } + + for rows.Next() { + vals := make([]any, len(columns)) + for i := range vals { + vals[i] = new([]byte) + } + + rows.Scan(vals...) + + record := make(map[string]string) + for i, col := range columns { + b := *vals[i].(*[]byte) + record[col] = string(b) + } + + data = append(data, record) + } + + return data, nil +} + +func (d *DatabaseConnection) Close() error { + return d.db.Close() +} -- cgit v1.2.3