initial commit
This commit is contained in:
commit
213b1aad6c
714 changed files with 590265 additions and 0 deletions
76
service/database/database.go
Normal file
76
service/database/database.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
Package database is the middleware between the app database and the code. All data (de)serialization (save/load) from a
|
||||
persistent database are handled here. Database specific logic should never escape this package.
|
||||
|
||||
To use this package you need to apply migrations to the database if needed/wanted, connect to it (using the database
|
||||
data source name from config), and then initialize an instance of AppDatabase from the DB connection.
|
||||
|
||||
For example, this code adds a parameter in `webapi` executable for the database data source name (add it to the
|
||||
main.WebAPIConfiguration structure):
|
||||
|
||||
DB struct {
|
||||
Filename string `conf:""`
|
||||
}
|
||||
|
||||
This is an example on how to migrate the DB and connect to it:
|
||||
|
||||
// Start Database
|
||||
logger.Println("initializing database support")
|
||||
db, err := sql.Open("sqlite3", "./foo.db")
|
||||
if err != nil {
|
||||
logger.WithError(err).Error("error opening SQLite DB")
|
||||
return fmt.Errorf("opening SQLite: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
logger.Debug("database stopping")
|
||||
_ = db.Close()
|
||||
}()
|
||||
|
||||
Then you can initialize the AppDatabase and pass it to the api package.
|
||||
*/
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// AppDatabase is the high level interface for the DB
|
||||
type AppDatabase interface {
|
||||
GetName() (string, error)
|
||||
SetName(name string) error
|
||||
|
||||
Ping() error
|
||||
}
|
||||
|
||||
type appdbimpl struct {
|
||||
c *sql.DB
|
||||
}
|
||||
|
||||
// New returns a new instance of AppDatabase based on the SQLite connection `db`.
|
||||
// `db` is required - an error will be returned if `db` is `nil`.
|
||||
func New(db *sql.DB) (AppDatabase, error) {
|
||||
if db == nil {
|
||||
return nil, errors.New("database is required when building a AppDatabase")
|
||||
}
|
||||
|
||||
// Check if table exists. If not, the database is empty, and we need to create the structure
|
||||
var tableName string
|
||||
err := db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name='example_table';`).Scan(&tableName)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
sqlStmt := `CREATE TABLE example_table (id INTEGER NOT NULL PRIMARY KEY, name TEXT);`
|
||||
_, err = db.Exec(sqlStmt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating database structure: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &appdbimpl{
|
||||
c: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (db *appdbimpl) Ping() error {
|
||||
return db.c.Ping()
|
||||
}
|
8
service/database/get-name.go
Normal file
8
service/database/get-name.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package database
|
||||
|
||||
// GetName is an example that shows you how to query data
|
||||
func (db *appdbimpl) GetName() (string, error) {
|
||||
var name string
|
||||
err := db.c.QueryRow("SELECT name FROM example_table WHERE id=1").Scan(&name)
|
||||
return name, err
|
||||
}
|
7
service/database/set-name.go
Normal file
7
service/database/set-name.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package database
|
||||
|
||||
// SetName is an example that shows you how to execute insert/update
|
||||
func (db *appdbimpl) SetName(name string) error {
|
||||
_, err := db.c.Exec("INSERT INTO example_table (id, name) VALUES (1, ?)", name)
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue