Skip to content

Instantly share code, notes, and snippets.

@geoah
Created January 21, 2015 01:49
Show Gist options
  • Save geoah/0ccd16aed172b8f67a97 to your computer and use it in GitHub Desktop.
Save geoah/0ccd16aed172b8f67a97 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
"log"
"github.com/coopernurse/gorp"
_ "github.com/ziutek/mymysql/autorc"
_ "github.com/ziutek/mymysql/godrv"
_ "github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/thrsafe"
)
var dbmap *gorp.DbMap
func main() {
for i := 0; i < 5; i++ {
go func(i int) {
fmt.Printf("+ Starting with %d\n", i)
var item *Item
db := GetDbSession()
db.SelectOne(&item, "SELECT * FROM items LIMIT 1")
// Do something
// time.Sleep(2 * time.Millisecond)
item.Processed = true
db.Update(&item)
log.Printf("- Done with %d\n", i)
}(i)
}
select {}
}
func GetDbSession() *gorp.DbMap {
if dbmap != nil {
return dbmap
}
db, err := sql.Open("mymysql", "tcp:127.0.0.1:3306*gorptest/root/root")
checkErr(err, "sql.Open failed")
dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
dbmap.AddTableWithName(Item{}, "shows").SetKeys(false, "id")
err = dbmap.CreateTablesIfNotExists()
checkErr(err, "Create tables failed")
return dbmap
}
type Item struct {
ID int `json:"id" db:"id"`
Processed bool `json:"processed" db:"processed"`
}
func checkErr(err error, msg string) {
if err != nil {
log.Fatalln(msg, err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment