2020-08-30 07:16:50 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Datastore interface {
|
|
|
|
AllDevices() ([]*Device, error)
|
2020-08-30 21:42:01 +02:00
|
|
|
InsertDevice(*Device) error
|
2020-08-30 07:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type DB struct {
|
|
|
|
*sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDB(dataSourceName string) (*DB, error) {
|
|
|
|
db, err := sql.Open("sqlite3", dataSourceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = db.Ping(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-13 18:37:22 +02:00
|
|
|
sqlStmt := `CREATE TABLE IF NOT EXISTS devices (topic TEXT not null primary key,
|
|
|
|
customerID TEXT,
|
|
|
|
deviceID TEXT,
|
|
|
|
projectName TEXT,
|
|
|
|
mac TEXT,
|
|
|
|
sn TEXT,
|
|
|
|
lastMsg TEXT);`
|
2020-08-30 07:16:50 +02:00
|
|
|
_, err = db.Exec(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DB{db}, nil
|
|
|
|
}
|