Created
September 18, 2019 15:11
-
-
Save mattc41190/bfca7827ebbc898edaf08220d5eb3855 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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