func GetLiveMatch() ([]modal.Modal, error) { var db *sqlx.DB var err error var matches []modal.Modal if db, err = sqlx.Open("mysql", user+":"+password+"@/Dota?parseTime=true"); err != nil { return matches, err } defer db.Close() rows, err := db.Queryx("select * from Live where deleted=?", deletedTime) if err != nil { return matches, err } for rows.Next() { var match modal.Live err = rows.StructScan(&match) if err != nil { return matches, err } log.WithFields(log.Fields{ "radiant id ": match.RadiantID, "dire id ": match.DireID, }).Info("Searching for Details in Score table") err = db.QueryRowx("SELECT * FROM Score where id=? LIMIT 1", match.RadiantID).StructScan(&match.Radiant) if err != nil { return matches, err } _, err = assignDataToPicksAndBans(db, &match) if err != nil { return matches, err } matches = append(matches, match) } return matches, nil }
func GetLeague() ([]modal.Modal, error) { var db *sqlx.DB var err error var leagues []modal.Modal if db, err = sqlx.Open("mysql", user+":"+password+"@/Dota?parseTime=true"); err != nil { return leagues, err } defer db.Close() // find league where deleted is 0000-00-00 00:00:00 rows, err := db.Queryx("select * from League where deleted = ?", deletedTime) if err != nil { return leagues, err } for rows.Next() { var league modal.League err = rows.StructScan(&league) if err != nil { return leagues, err } leagues = append(leagues, league) } return leagues, nil }
func GetHero() ([]modal.Modal, error) { var db *sqlx.DB var err error var heroes []modal.Modal if db, err = sqlx.Open("mysql", user+":"+password+"@/Dota?parseTime=true"); err != nil { return heroes, err } defer db.Close() rows, err := db.Queryx("select * from Hero") if err != nil { return heroes, err } for rows.Next() { var hero modal.Hero err = rows.StructScan(&hero) if err != nil { return heroes, err } heroes = append(heroes, hero) } return heroes, nil }
func GetAbility() ([]modal.Modal, error) { var db *sqlx.DB var err error var abilities []modal.Modal if db, err = sqlx.Open("mysql", user+":"+password+"@/Dota?parseTime=true"); err != nil { return abilities, err } defer db.Close() rows, err := db.Queryx("select * from Ability") if err != nil { return abilities, err } for rows.Next() { var ability modal.Ability err = rows.StructScan(&ability) if err != nil { return abilities, err } abilities = append(abilities, ability) } return abilities, nil }
func GetTeam() ([]modal.Modal, error) { var db *sqlx.DB var err error var teams []modal.Modal if db, err = sqlx.Open("mysql", user+":"+password+"@/Dota?parseTime=true"); err != nil { return teams, err } defer db.Close() rows, err := db.Queryx("select * from Team") if err != nil { return teams, err } for rows.Next() { var team modal.Team err = rows.StructScan(&team) if err != nil { return teams, err } teams = append(teams, team) } return teams, nil }
func (r *RDPG) waitForBDRNodes() (err error) { log.Trace(fmt.Sprintf(`rdpg.RDPG<%s>#waitForBDRNodes() waiting for all BDR nodes to be joined...`, ClusterID)) p := pg.NewPG(`127.0.0.1`, pgPort, `rdpg`, `rdpg`, pgPass) var db *sqlx.DB for { db, err = p.Connect() if err != nil { re := regexp.MustCompile("canceling statement due to conflict with recovery") if re.MatchString(err.Error()) { log.Error(fmt.Sprintf("rdpg.RDPG<%s>#waitForBDRNodes() p.Connect() (sleeping 2 seconds and trying again) ! %s", ClusterID, err)) time.Sleep(2 * time.Second) continue // Sleep 2 seconds and try again... } else { log.Error(fmt.Sprintf("rdpg.RDPG<%s>#waitForBDRNodes() p.Connect() ! %s", ClusterID, err)) return err } } else { break } } defer db.Close() for { nodes := []string{} sq := `SELECT node_name FROM bdr.bdr_nodes;` err = db.Select(&nodes, sq) if err != nil { if err == sql.ErrNoRows { log.Error(fmt.Sprintf("rdpg.RDPG<%s>#waitForBDRNodes() db.Select() %sq ! Sleeping 2 seconds and trying again.", ClusterID, sq)) time.Sleep(2 * time.Second) continue } log.Error(fmt.Sprintf("rdpg.RDPG<%s>#waitForBDRNodes() db.Select() ! %s", ClusterID, err)) return err } log.Trace(fmt.Sprintf(`rdpg.RDPG<%s>#waitForBDRNodes() nodes %+v`, ClusterID, nodes)) switch ClusterID { case "rdpgmc": if len(nodes) > 2 { return nil } default: // rdpgsc* if len(nodes) > 1 { return nil } } time.Sleep(2 * time.Second) } }
func GetJobs(db *sqlx.DB, jobs Jobs) Jobs { query := "SELECT * FROM `message_queue` WHERE `status` = ? ORDER BY `send_priority` DESC LIMIT 100" rows, err := db.Queryx(query, "pending") if err != nil { panic(err) } for rows.Next() { var job Job err = rows.StructScan(&job) if err != nil { panic(err) } jobs.Jobs = append(jobs.Jobs, job) } db.Close() return jobs }
package sql_test import ( "github.com/jmoiron/sqlx" "github.com/nicholasray/sandstone/lib/persistence" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "testing" ) var db *sqlx.DB func TestSql(t *testing.T) { RegisterFailHandler(Fail) RunSpecs(t, "Sql Suite") } var _ = BeforeSuite(func() { db = persistence.ConnectDb() }) var _ = AfterSuite(func() { db.Close() })
func main() { var ( debug bool confFile string configTest bool jobName string listJobs bool dontByte bool dont_id bool ) flag.BoolVar(&debug, "debug", false, "Enable Debug output") flag.StringVar(&confFile, "config", "my2mo.json", "Config file to read") flag.BoolVar(&configTest, "configtest", false, "Load and parse configs, and exit") flag.StringVar(&jobName, "job", "", "Name of the job to run") flag.BoolVar(&dontByte, "dontconvertbytes", false, "We automatically convert byte arrays to strings. Set this to prevent it.") flag.BoolVar(&dont_id, "dontconvertid", false, "We automatically convert any SQL column 'id' to mongo element '_id'. Set this to prevent it.") flag.BoolVar(&listJobs, "list", false, "List the jobs available") flag.Parse() if debug { debugOut = log.New(os.Stdout, "[DEBUG]", log.Lshortfile) } // Because of my silly logic, the config params are logically // inverse from the function parameters. Dealing with that here. var ( convertBytes bool = true convertId bool = true ) if dontByte { convertBytes = false } if dont_id { convertId = false } // Read config conf := loadFile(confFile) if configTest { // Just kicking the tires... fmt.Println("Config loaded and bootstrapped successfully...") os.Exit(0) } if listJobs { for _, job := range conf.Jobs { if job.Enabled == true { fmt.Printf("%s: %s\n", job.Name, job.Description) } } os.Exit(0) } else if jobName == "" { log.Fatal("--job is required") } // Grab the job, and let us get this party started job, ok := conf.GetJob(jobName) if ok == false { log.Fatalf("Job name %s is not valid, or disabled. Perhaps --list?\n", jobName) } else { // ensure writeoperation is one of insert, update, upsert if job.WriteOperation == "insert" || job.WriteOperation == "update" || job.WriteOperation == "upsert" { } else { log.Fatalf("WriteOperation of %s for job %s is invalid!\n", job.WriteOperation, job.Name) } // Mongo handle, right to the collection mongod, mongoc := mongoOpen(&job.Mongo) // SQL can vary var sqlc *sqlx.Rows var sqld *sqlx.DB // Test the SqlType found := false for k, f := range sqlList { if k == job.SqlType { found = true debugOut.Printf("Found SQL type %s\n", k) sqld, sqlc = f(&job.SQL, job.SqlColumns) break } } if found == false { log.Fatalf("SQL type of %s is not currently valid. Pick one of: "+joinKeys(sqlList)+"\n", job.SqlType) } writeCount := 0 errorCount := 0 // Let's do this for sqlc.Next() { res, err := sqlGetRow(sqlc, convertBytes, convertId) // we already outputted any error in sqlGetRow // so we'll just skip this write, and move along if err == nil { ok := mongoWriteRow(mongoc, job.WriteOperation, res) if ok { writeCount++ } else { errorCount++ } } else { errorCount++ } } // Clean up, explicitly mongod.Close() sqld.Close() totalCount := writeCount + errorCount log.Printf("Load completed with %d/%d rows written", writeCount, totalCount) } }
mockDb = &fakes.Db{} dbName := fmt.Sprintf("test_ducati_database_%x", rand.Int()) dbConnectionInfo := testsupport.GetDBConnectionInfo() testDatabase = dbConnectionInfo.CreateDatabase(dbName) var err error realDb, err = db.GetConnectionPool(testDatabase.URL()) Expect(err).NotTo(HaveOccurred()) dataStore, err = store.New(realDb) Expect(err).NotTo(HaveOccurred()) }) AfterEach(func() { if realDb != nil { Expect(realDb.Close()).To(Succeed()) } if testDatabase != nil { testDatabase.Destroy() } }) Describe("Connecting to the database and migrating", func() { Context("when the tables already exist", func() { It("succeeds", func() { _, err := store.New(realDb) Expect(err).NotTo(HaveOccurred()) }) }) Context("when the db operation fails", func() {