Documentation
¶
Overview ¶
Package models defines the database access objects.
Index ¶
- func CountRecords(tableName string, fields ...string) (count int)
- func DB() *sql.DB
- func In(query string, args []string, emptyDefault string) (rewritten string, argv []interface{})
- func Migrate(db *sql.DB) (err error)
- func Register(t Table)
- type LogModel
- func ListLatestTrainByCondition(cond string, args ...interface{}) []LogModel
- func ListLatestTrainForMatchedUnits(unitNo string) []LogModel
- func ListLatestTrainForMultiUnits(unitNoList []string) []LogModel
- func ListLatestUnitForMultiTrains(trainNoList []string) []LogModel
- func ListTrainsForSingleUnitNo(unitNo string) []LogModel
- func ListUnitsForSingleTrainNo(trainNo string) []LogModel
- type SerialModel
- type Table
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountRecords ¶
CountRecords takes a tableName and returns the number of rows in the table.
func DB ¶
DB returns an initialized database singleton.
Example ¶
package main
import (
"fmt"
"github.com/arnie97/emu-log/common"
"github.com/arnie97/emu-log/models"
)
func main() {
x := models.DB()
y := models.DB()
_, err := x.Exec(`SELECT 1;`)
resetTestDB()
c1 := models.CountRecords("emu_qr_code")
c2 := models.CountRecords("emu_log", "date")
fmt.Println("x == y: ", x == y)
fmt.Println("x != nil:", x != nil)
fmt.Println("error: ", err)
fmt.Println("count: ", c1, c2)
}
func resetTestDB() {
content := common.ReadMockFile("test.sql")
_, err := models.DB().Exec(string(content))
common.Must(err)
}
Output: x == y: true x != nil: true error: <nil> count: 74 28
Types ¶
type LogModel ¶
type LogModel struct {
Date string `json:"date"`
UnitNo string `json:"emu_no"`
TrainNo string `json:"train_no"`
}
func ListLatestTrainByCondition ¶
Example ¶
package main
import (
"fmt"
"github.com/arnie97/emu-log/adapters"
"github.com/arnie97/emu-log/common"
"github.com/arnie97/emu-log/models"
)
func main() {
resetTestDB()
serialModel := models.SerialModel{UnitNo: "CRH6A4002", Adapter: "F", Operator: "A", SerialNo: "002"}
serialModel.Add()
fmt.Println(models.ListSerials(adapters.MustGetAdapterByCode("F")))
fmt.Println(models.ListSerialsForSingleUnit("%J2015"))
fmt.Println(models.ListLatestSerialForMultiUnits(adapters.MustGetAdapterByCode("P")))
fmt.Println(models.ListLatestSerialForMultiUnits(adapters.MustGetAdapterByCode("M"), "H", "P"))
fmt.Println(models.ListUnitsForSingleTrainNo("D5461"))
models.LogModel{Date: "2020-11-20", UnitNo: "CRH6A4002", TrainNo: "D5464/1/4"}.Add()
fmt.Println(models.ListUnitsForSingleTrainNo("D5461"))
fmt.Println(models.ListLatestUnitForMultiTrains([]string{"D5461", "D3045", "G666", "D5464"}))
fmt.Println(models.ListTrainsForSingleUnitNo("%2015"))
fmt.Println(len(models.ListUnitsForSingleTrainNo("D3071")))
for i := 0; i < 10; i++ {
models.LogModel{UnitNo: "CRH2A2015", TrainNo: "D3074/D3071"}.Add()
}
fmt.Println(len(models.ListUnitsForSingleTrainNo("D3071")))
fmt.Println(models.ListLatestTrainForMultiUnits([]string{"CRH6A4002", "CR200J2040", "CRH2A2460"}))
fmt.Println(models.ListLatestTrainForMatchedUnits("%A%02%"))
mockInfo := adapters.JSONObject{}
serialModel.AddTrainOperationLogs(mockInfo)
mockInfo["trainCode"] = "C1040"
mockInfo["startDay"] = "20210212"
mockInfo["carCode"] = "CRH6A-A-0002"
serialModel.AddTrainOperationLogs(mockInfo)
mockInfo["carCode"] = "CRH6A-4002"
serialModel.AddTrainOperationLogs(mockInfo)
fmt.Println(models.ListTrainsForSingleUnitNo("CRH6AA0002"))
fmt.Println(models.ListTrainsForSingleUnitNo("CRH6A4002"))
}
func resetTestDB() {
content := common.ReadMockFile("test.sql")
_, err := models.DB().Exec(string(content))
common.Must(err)
}
Output: [{F A CRH6A4002 002} {F ? CH001 053} {F ? CRH2650 111} {F N CRH5A5075 472}] [{U H CR200J2015 PV0000064000} {U H CR200J2015 PV0000063800} {H H CR200J2015 PQ2098500} {H H CR200J2015 PQ0916500} {H H CR200J2015 PQ0916000}] [{P P CR400AF0207 51573000} {P P CR400AF2015 51742500}] [{M H CR300BF3010 H0140000} {M P CR400AF0207 CR400AF-0207-05-03A} {M P CR400AF2015 CR400AF-2015-03-10A} {M H CR400BF3010 H0035000}] [{2020-11-16 CR200J2015 D5464/1/4} {2020-11-14 CR200J2015 D5464/1/4} {2020-11-13 CR200J2040 D5464/1/4}] [{2020-11-20 CRH6A4002 D5464/1/4} {2020-11-16 CR200J2015 D5464/1/4} {2020-11-14 CR200J2015 D5464/1/4} {2020-11-13 CR200J2040 D5464/1/4}] [{2020-11-20 CRH6A4002 D5461} {2020-11-20 CRH6A4002 D5464} {2020-11-14 CR400AF0207 G666}] [{2020-11-17 CR200J2015 D5468/D5465} {2020-11-16 CR200J2015 D5456/D5457} {2020-11-16 CR200J2015 D5464/1/4} {2020-11-15 CR200J2015 D5466/D5467} {2020-11-15 CR200J2015 D5468/D5465} {2020-11-15 CR200J2015 D5462/D5463} {2020-11-15 CR200J2015 D5458/D5455} {2020-11-14 CR200J2015 D5456/D5457} {2020-11-14 CR200J2015 D5464/1/4} {2020-11-13 CR200J2015 D5466/D5467} {2020-11-13 CR200J2015 D5468/D5465} {2020-10-26 CR400AF2015 G666} {2020-11-18 CRH2A2015 D3220} {2020-11-18 CRH2A2015 D3205} {2020-11-16 CRH2A2015 D3074/D3071} {2020-11-15 CRH2A2015 D3074/D3071} {2020-11-15 CRH2A2015 D3072/D3073} {2020-11-10 CRH2A2015 D3206} {2020-11-10 CRH2A2015 D3219}] 2 3 [{2020-11-13 CR200J2040 D5464/1/4} {2020-11-20 CRH6A4002 D5464/1/4}] [{2020-11-15 CR400AF0207 G8907} {2020-11-20 CRH6A4002 D5464/1/4}] [] [{2021-02-12 CRH6A4002 C1040} {2020-11-20 CRH6A4002 D5464/1/4}]
func (LogModel) Add ¶
func (logModel LogModel) Add()
Add saves the log entry to the database, and update related records in the materialized view.
func (LogModel) Materialize ¶
Materialize updates the materialized view, which stores the last used unit for each half of train numbers.
type SerialModel ¶
type SerialModel struct {
Adapter string `json:"adapter"`
Operator string `json:"operator"`
UnitNo string `json:"emu_no"`
SerialNo string `json:"qr_code"`
}
func ListLatestSerialForMultiUnits ¶
func ListLatestSerialForMultiUnits(a adapters.Adapter, operators ...string) []SerialModel
ListLatestSerialForMultiUnits returns the most recently discovered serial number for each unit from the given adapter, but excluding those with known train schedules.
func ListSerials ¶
func ListSerials(a adapters.Adapter) []SerialModel
ListSerials returns all known serials and corresponding units of a given adapter from the database.
func ListSerialsForSingleUnit ¶
func ListSerialsForSingleUnit(unitNo string) []SerialModel
ListSerialsForSingleUnit returns all the known serials for one unit.
func (SerialModel) Add ¶
func (s SerialModel) Add()
Add inserts a recently discovered serial number into the database.
func (SerialModel) AddTrainOperationLogs ¶
func (s SerialModel) AddTrainOperationLogs(info adapters.JSONObject)
AddTrainOperationLogs creates related operation log records if possible.
func (SerialModel) Query ¶
func (s SerialModel) Query(sql string, args ...interface{}) (serials []SerialModel)
Query executes a SQL statement and returns all the result rows.
func (SerialModel) Schema ¶
func (SerialModel) Schema() string