diff options
author | Julien Dessaux | 2024-07-17 22:26:28 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-07-17 22:26:28 +0200 |
commit | c069de3955da41116934884b0874c71b57bd2cf3 (patch) | |
tree | 2650f5dc0621b2eeed9f1c0cd5c471a881df322a /golang/pkg | |
parent | [golang] fixed sql migrations (diff) | |
download | spacetraders-c069de3955da41116934884b0874c71b57bd2cf3.tar.gz spacetraders-c069de3955da41116934884b0874c71b57bd2cf3.tar.bz2 spacetraders-c069de3955da41116934884b0874c71b57bd2cf3.zip |
[go] refactoring
Diffstat (limited to '')
-rw-r--r-- | golang/pkg/database/db.go | 19 | ||||
-rw-r--r-- | golang/pkg/database/migrations.go | 5 | ||||
-rw-r--r-- | golang/pkg/database/tokens.go | 7 |
3 files changed, 23 insertions, 8 deletions
diff --git a/golang/pkg/database/db.go b/golang/pkg/database/db.go new file mode 100644 index 0000000..3e07db3 --- /dev/null +++ b/golang/pkg/database/db.go @@ -0,0 +1,19 @@ +package database + +import ( + "context" + "database/sql" +) + +type DB struct { + ctx context.Context + db *sql.DB +} + +func (db *DB) Exec(query string, args ...any) (sql.Result, error) { + return db.db.ExecContext(db.ctx, query, args...) +} + +func (db *DB) QueryRow(query string, args ...any) *sql.Row { + return db.db.QueryRowContext(db.ctx, query, args...) +} diff --git a/golang/pkg/database/migrations.go b/golang/pkg/database/migrations.go index 81cebaa..b167087 100644 --- a/golang/pkg/database/migrations.go +++ b/golang/pkg/database/migrations.go @@ -9,11 +9,6 @@ import ( _ "github.com/mattn/go-sqlite3" ) -type DB struct { - ctx context.Context - db *sql.DB -} - //go:embed sql/*.sql var schemaFiles embed.FS diff --git a/golang/pkg/database/tokens.go b/golang/pkg/database/tokens.go index 0787b5c..0356cb2 100644 --- a/golang/pkg/database/tokens.go +++ b/golang/pkg/database/tokens.go @@ -1,11 +1,12 @@ package database func (db *DB) AddToken(token string) error { - _, err := db.db.ExecContext(db.ctx, `INSERT INTO tokens(data) VALUES (?);`, token) + _, err := db.Exec(`INSERT INTO tokens(data) VALUES (?);`, token) return err } -func (db *DB) GetToken() (token string, err error) { - err = db.db.QueryRowContext(db.ctx, `SELECT data FROM tokens;`).Scan(&token) +func (db *DB) GetToken() (string, error) { + var token string + err := db.QueryRow(`SELECT data FROM tokens;`).Scan(&token) return token, err } |