Commit f61b5030 authored by dagal's avatar dagal
Browse files

Switch GORM for SQLX

Hopefully using a real ORM will help me obtain lines with stop IDs...
parent e0e67848
package main
import (
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
func ReadStops(db *sqlx.DB) {
func ReadStops(db *gorm.DB) {
stopSlice := []Stop{}
err := db.Select(&stopSlice, "SELECT ctr_id, name, lat, lon FROM stop WHERE ctr_id IS NOT NULL ORDER BY ctr_id")
LogAndQuit(err)
db.Find(&stopSlice)
for _, s := range stopSlice {
stops[s.Id] = s
stops[s.ID] = s
}
}
func ReadTime(db *gorm.DB) {
// this toy ORM can't return min(date) without breaking...
var journey Journey
func ReadTime(db *sqlx.DB) {
var strTime string
err := db.Get(&strTime, "SELECT min(start_date) FROM journey")
LogAndQuit(err)
println(strTime)
start_time, err = time.Parse(time.RFC3339[0:19], strTime)
LogAndQuit(err)
println(start_time.Format("2006-01-02"))
db.Order("start_date").Limit(1).Find(&journey)
start_time = journey.StartDate
err = db.Get(&strTime, "SELECT max(start_date) FROM journey")
LogAndQuit(err)
println(strTime)
end_time, err = time.Parse(time.RFC3339[0:19], strTime)
LogAndQuit(err)
println(end_time.Format("2006-01-02"))
journey = Journey{}
db.Order("start_date desc").Limit(1).Find(&journey)
end_time = journey.StartDate
}
......@@ -3,8 +3,8 @@ package main
import (
"flag"
"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"time"
)
......@@ -19,6 +19,8 @@ func main() {
db := connectDB()
defer db.Close()
db.SingularTable(true)
db.AutoMigrate(&Stop{}, &Journey{})
ReadStops(db)
ReadTime(db)
CTRLoad()
......@@ -37,12 +39,12 @@ func main() {
router.GET("/", ShowIndex)
router.GET("/stop_stats/:id", GetStopStats)
router.GET("/xy", GetXY)
err := router.Run()
err := router.Run(":8088")
LogAndQuit(err)
}
func connectDB() *sqlx.DB {
db, err := sqlx.Connect("sqlite3", "file:data/trippy.db?cache=shared&mode=ro")
func connectDB() *gorm.DB {
db, err := gorm.Open("sqlite3", "file:data/trippy.db?cache=shared&mode=ro")
LogAndQuit(err)
return db
}
......@@ -2,13 +2,22 @@ package main
import (
"github.com/paulmach/go.geojson"
"time"
)
type Stop struct {
Id uint32 `db:"ctr_id"`
Name string `db:"name"`
Lat float64 `db:"lat"`
Lon float64 `db:"lon"`
//gorm.Model
ID uint32 `gorm:"column:ctr_id"`
Name string
Lat float64
Lon float64
}
type Journey struct {
//gorm.Model
LineId string `gorm:"primary_key"`
JourneyId uint32 `gorm:"primary_key"`
StartDate time.Time
}
type Geometry interface {
......@@ -17,7 +26,7 @@ type Geometry interface {
func (s Stop) ToGeoJSON() *geojson.Feature {
f := geojson.NewPointFeature([]float64{s.Lon, s.Lat})
f.Properties["id"] = s.Id
f.Properties["id"] = s.ID
f.Properties["name"] = s.Name
return f
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment