Skip to content

Commit

Permalink
feat(sql): create a mysql driver using gorm
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsimonemms committed Dec 12, 2022
1 parent dc018a5 commit 8cf19b2
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitpod.yml
Expand Up @@ -63,3 +63,8 @@ vscode:
- donjayamanne.git-extension-pack
- EditorConfig.EditorConfig
- golang.go

ports:
- port: 3306
name: MySQL
visibility: private
28 changes: 28 additions & 0 deletions cmd/db_sql_mysql.go
Expand Up @@ -16,15 +16,43 @@ limitations under the License.
package cmd

import (
"github.com/mrsimonemms/gobblr/pkg/drivers/sql"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var dbSqlMysqlOpts struct {
Database string
Host string
Password string
Port int
User string
}

// dbSqlMysqlCmd represents the mysql command
var dbSqlMysqlCmd = &cobra.Command{
Use: "mysql",
Short: "MySQL ingestion commands",
Run: func(cmd *cobra.Command, args []string) {
dbOpts.Driver = sql.MySQL(
dbSqlMysqlOpts.Database,
dbSqlMysqlOpts.Host,
dbSqlMysqlOpts.Password,
dbSqlMysqlOpts.Port,
dbSqlMysqlOpts.User,
)
},
}

func init() {
dbSqlCmd.AddCommand(dbSqlMysqlCmd)

viper.SetDefault("host", "localhost")
viper.SetDefault("port", 3306)
viper.SetDefault("username", "root")
dbSqlMysqlCmd.Flags().StringVarP(&dbSqlMysqlOpts.Database, "database", "d", viper.GetString("database"), "name of the database to use")
dbSqlMysqlCmd.Flags().StringVarP(&dbSqlMysqlOpts.Host, "host", "H", viper.GetString("host"), "database host name")
dbSqlMysqlCmd.Flags().StringVarP(&dbSqlMysqlOpts.Password, "password", "p", viper.GetString("password"), "database password")
dbSqlMysqlCmd.Flags().IntVarP(&dbSqlMysqlOpts.Port, "port", "P", viper.GetInt("port"), "database port")
dbSqlMysqlCmd.Flags().StringVarP(&dbSqlMysqlOpts.User, "username", "u", viper.GetString("username"), "database username")
}
5 changes: 5 additions & 0 deletions go.mod
Expand Up @@ -5,12 +5,17 @@ go 1.19
require (
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.2
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Expand Up @@ -62,6 +62,8 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down Expand Up @@ -125,6 +127,11 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
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.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down Expand Up @@ -472,6 +479,11 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ=
gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM=
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.24.2 h1:9wR6CFD+G8nOusLdvkZelOEhpJVwwHzpQOUM+REd6U0=
gorm.io/gorm v1.24.2/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
23 changes: 23 additions & 0 deletions pkg/drivers/sql/mysql.go
@@ -0,0 +1,23 @@
package sql

import (
"fmt"

"gorm.io/driver/mysql"
)

func MySQL(database string, host string, password string, port int, user string) *SQL {
var dsn string

if user != "" && password != "" {
dsn += fmt.Sprintf("%s:%s@", user, password)
}
if host != "" {
dsn += fmt.Sprintf("tcp(%s:%d)", host, port)
}
dsn += fmt.Sprintf("/%s", database)

return &SQL{
driver: mysql.Open(dsn),
}
}
43 changes: 43 additions & 0 deletions pkg/drivers/sql/sql.go
@@ -0,0 +1,43 @@
package sql

import (
"gorm.io/gorm"
)

type SQL struct {
driver gorm.Dialector
activeConnection *gorm.DB
}

func (db *SQL) Auth() (err error) {
// Connect to the database
if gormDb, err := gorm.Open(db.driver, &gorm.Config{}); err != nil {
return err
} else {
// Success - store the connection
db.activeConnection = gormDb
}

// Perform a simple query to ensure connectivity
rows, err := db.activeConnection.Raw("SELECT 1 + 1 AS result").Rows()
if err != nil {
return err
}
defer func() {
err = rows.Close()
}()

return err
}

func (db *SQL) Close() error {
return nil
}

func (db *SQL) InsertBulk(table string, data []map[string]interface{}) (int, error) {
return len(data), nil
}

func (db *SQL) Truncate(table string) error {
return nil
}

0 comments on commit 8cf19b2

Please sign in to comment.