Skip to content

Instantly share code, notes, and snippets.

@mattc41190
Created September 18, 2019 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattc41190/bfca7827ebbc898edaf08220d5eb3855 to your computer and use it in GitHub Desktop.
Save mattc41190/bfca7827ebbc898edaf08220d5eb3855 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
"log"
"os"
goqu "github.com/doug-martin/goqu/v9"
_ "github.com/go-sql-driver/mysql"
)
// Store holds data related to storing information in a persistence layer
// Like -- Totally a SQL database but we will use more abstract cause we are extra smart
type Store struct {
GDB *goqu.Database
}
// makeStore will create a connection to a database, assign that connection to a goqu Database,
// then create and return a Store which carries the goqu Database
func makeStore() Store {
name := os.Getenv("DEMO_DB")
pw := os.Getenv("DEMO_PW")
mysqlDB, err := sql.Open("mysql", fmt.Sprintf("root:%s@/%s", pw, name))
if err != nil {
log.Fatal(err.Error())
}
dialect := goqu.Dialect("mysql")
db := dialect.DB(mysqlDB)
return Store{GDB: db}
}
func main() {
store := makeStore()
allSQL, _, _ := store.GDB.From("users").ToSQL()
log.Println(allSQL)
whereSQL, _, _ := store.GDB.From("users").Where(goqu.Ex{"name": "doug"}).ToSQL()
log.Println(whereSQL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment