From 52940c7c9232a97ec5c3536828c96aabf6e5027d Mon Sep 17 00:00:00 2001 From: Andreas Mieke Date: Mon, 23 Mar 2020 21:08:30 +0100 Subject: [PATCH] Adding database and CSV importer --- go.mod | 4 +++- go.sum | 30 ++++++++++++++++++++++++++ internal/database/database.go | 34 ++++++++++++++++++++++++++++++ internal/database/database_test.go | 26 +++++++++++++++++++++++ tools/csvimport/main.go | 32 ++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 go.sum create mode 100644 internal/database/database.go create mode 100644 internal/database/database_test.go create mode 100644 tools/csvimport/main.go diff --git a/go.mod b/go.mod index 83b090a..246824d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.1750studios.com/ToddShepard/DB640 -go 1.13 +go 1.14 + +require github.com/jinzhu/gorm v1.9.12 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d6a0c30 --- /dev/null +++ b/go.sum @@ -0,0 +1,30 @@ +github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= +github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= +github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA= +github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= +github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= +github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= +github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw= +github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd h1:GGJVjV8waZKRHrgwvtH66z9ZGVurTD1MT0n1Bb+q4aM= +golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= diff --git a/internal/database/database.go b/internal/database/database.go new file mode 100644 index 0000000..723a58b --- /dev/null +++ b/internal/database/database.go @@ -0,0 +1,34 @@ +package database + +import ( + "github.com/jinzhu/gorm" + + // SQLite dialect for gorm + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + +// Betriebsstelle defines the database model of the Betriebsstelle table +type Betriebsstelle struct { + gorm.Model + Code string `gorm:"unique_index"` + Name string +} + +// Db is the GORM database handle +var Db *gorm.DB + +// Open opens the database connection with given dialect and connection string +func Open(dialect string, connection string) (err error) { + Db, err = gorm.Open(dialect, connection) + if err != nil { + return err + } + Db.AutoMigrate(&Betriebsstelle{}) + return nil +} + +// Close closes the GORM database handle +func Close() { + Db.Close() + Db = nil +} diff --git a/internal/database/database_test.go b/internal/database/database_test.go new file mode 100644 index 0000000..b2a659b --- /dev/null +++ b/internal/database/database_test.go @@ -0,0 +1,26 @@ +package database + +import ( + "testing" +) + +func TestOpenWrongDialect(t *testing.T) { + err := Open("nonexistent", ":memory:") + if err == nil { + t.Error("Expected error (invalid driver), got none") + } +} + +func TestOpen(t *testing.T) { + err := Open("sqlite3", ":memory:") + if err != nil { + t.Fatalf("Cannot open database, error: %+v", err) + } +} + +func TestClose(t *testing.T) { + Close() + if Db != nil { + t.Error("Database object not nil after closing") + } +} diff --git a/tools/csvimport/main.go b/tools/csvimport/main.go new file mode 100644 index 0000000..3c5ffc6 --- /dev/null +++ b/tools/csvimport/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "flag" + "log" + + "git.1750studios.com/ToddShepard/DB640/internal/csvparser" + "git.1750studios.com/ToddShepard/DB640/internal/database" +) + +func main() { + input := flag.String("i", "", "Specifies CSV input file") + dialect := flag.String("d", "sqlite3", "Dialect for the database connection") + connection := flag.String("c", "./csvfile.db", "Connection string for the database connection") + flag.Parse() + if *input == "" { + log.Fatalf("Please specify the input file!") + } + err := database.Open(*dialect, *connection) + if err != nil { + log.Fatalf("Could not connect to database, error: %+v", err) + } + lines, err := csvparser.ParseFile(*input) + if err != nil { + log.Fatalf("Could not parse file, error: %+v", err) + } + for _, line := range lines { + database.Db.Create(&database.Betriebsstelle{Code: line.Code, Name: line.Name}) + log.Printf("Inserted %s with name %s into database!", line.Code, line.Name) + } + database.Close() +}