// func JSONData(qry string, uri string, filename string) []byte { func AgeDataPoint(qry string, uri string, filename string, database string, collection string, conn *sql.DB, session *mgo.Session) error { // conn, err := connect.GetJanusCon() // if err != nil { // panic(err) // } // defer conn.Close() rows, err := conn.Query(qry) if err != nil { log.Printf(`Error with "%s": %s`, qry, err) } allResults := []janusRow{} i := 1 for rows.Next() { d := []JanusTestStuff{} var t JanusTestStuff err := sqlstruct.Scan(&t, rows) if err != nil { log.Print(err) } d = append(d, t) rowURL := fmt.Sprintf("%s/%s#row=%v", uri, filename, i) aRow := janusRow{rowURL, i, d} allResults = append(allResults, aRow) i = i + 1 } theTable := table{fmt.Sprintf("%s/%s", uri, filename), allResults} tableSet := []table{} tableSet = append(tableSet, theTable) final := cVSW{tableSet} // session, err := mgo.Dial("127.0.0.1") // if err != nil { // panic(err) // } // defer session.Close() // Optional. Switch the session to a Strong behavior. session.SetMode(mgo.Strong, true) c := session.DB(database).C(collection) err = c.Insert(&final) if err != nil { log.Printf("Janus func Error %v with %s\n", err, filename) } log.Printf("File: %s written", filename) jm, _ := json.MarshalIndent(final, "", " ") _ = utils.WriteFile(filename, jm) // session.Close() return nil }
// getComponents returns a course's sections in a given term at // a given institution. The sections are values of a map whose // keys are component strings ("LAB", "LEC", ...) func getComponents(course, term, institution string) ([][]Section, error) { db, err := sql.Open("postgres", "postgres://localhost:5432/classtime?sslmode=disable") if err != nil { return nil, err } if db == nil { return nil, errors.New("DB connection failed") } defer db.Close() var components [][]Section query := `SELECT * FROM section WHERE component=$1 AND course=$2 AND term=$3 AND institution=$4 ORDER BY course, component` courseAndSectionIdToSection := make(map[string]Section) for _, c := range componentTypes { rows, err := db.Query(query, c, course, term, institution) if err != nil { log.Fatal(err) } defer rows.Close() sections := []Section{} for rows.Next() { var section Section if err := sqlstruct.Scan(§ion, rows); err != nil { log.Fatal(err) continue } courseAndSectionIdToSection[section.Course.String+section.Section.String] = section sections = append(sections, section) } if len(sections) == 0 { continue } components = append(components, sections) } for _, sections := range components { for _, section := range sections { if section.AutoEnroll.String != "" { autoEnrollSection, ok := courseAndSectionIdToSection[section.Course.String+section.AutoEnroll.String] if ok { section.AutoEnrollComponent.Scan(autoEnrollSection.Component.String) } } } } return components, nil }
func updateLikes() { queryStr := fmt.Sprintf("SELECT %s FROM groupme_posts WHERE posted_at >= NOW() - '1 day'::INTERVAL", sqlstruct.Columns(gmPost{})) rows, err := config.DB.Query(queryStr) if err != nil { return } defer rows.Close() groupedPosts := make(map[string][]gmPost) for rows.Next() { var post gmPost err := sqlstruct.Scan(&post, rows) if err == nil { slice := groupedPosts[post.GroupID] slice = append(slice, post) groupedPosts[post.GroupID] = slice } } token := os.Getenv("groupMeID") updated := make(map[int]int) for key, group := range groupedPosts { getURL := "https://api.groupme.com/v3/groups/" + key + "/likes?period=day&token=" + token resp, _ := http.Get(getURL) body, _ := ioutil.ReadAll(resp.Body) var wrapper gmMessageWrapper json.Unmarshal(body, &wrapper) for _, message := range wrapper.Response.Messages { for _, post := range group { if post.MessageID == message.MessageID() { updated[post.ID] = len(message.FavoritedBy) } } } } tx, err := config.DB.Begin() if err != nil { return } stmt, _ := config.DB.Prepare("UPDATE groupme_posts SET likes=$1 WHERE id=$2") for updateID, likeCount := range updated { stmt.Exec(likeCount, updateID) } tx.Commit() }
// GetCertificate gets a CertificateRecord indexed by serial. func GetCertificate(db *sql.DB, serial string) (*CertificateRecord, error) { cr := new(CertificateRecord) rows, err := db.Query(fmt.Sprintf(selectSQL, sqlstruct.Columns(*cr)), serial) if err != nil { return nil, wrapCertStoreError(err) } defer rows.Close() if rows.Next() { return cr, wrapCertStoreError(sqlstruct.Scan(cr, rows)) } return nil, nil }
//Public Methods func FetchBot(id string) (Bot, error) { rows, err := config.DB.Query(fmt.Sprintf("SELECT %s FROM bots WHERE group_id = $1", sqlstruct.Columns(Bot{})), id) if err != nil { return Bot{}, err } defer rows.Close() rows.Next() var bot Bot err = sqlstruct.Scan(&bot, rows) return bot, err }
// GetOCSP retrieves a OCSPRecord from db by serial. func GetOCSP(db *sql.DB, serial string) (rr *OCSPRecord, err error) { rr = new(OCSPRecord) rows, err := db.Query(fmt.Sprintf(selectOCSPSQL, sqlstruct.Columns(*rr)), serial) if err != nil { return nil, wrapCertStoreError(err) } defer rows.Close() if rows.Next() { return rr, sqlstruct.Scan(rr, rows) } return nil, nil }
func (r ModelMysqlRepo) Query(query string) (models []Model, err error) { rows, err := r.Db.Query(query) if err != nil { log.Println(err) return } model := Model{} for rows.Next() { err = sqlstruct.Scan(&model, rows) models = append(models, model) } rows.Close() return }
func (r TagMysqlRepo) Query(query string) (tags []Tag, err error) { rows, err := r.Db.Query(query) if err != nil { log.Println(err) return } tag := Tag{} for rows.Next() { err = sqlstruct.Scan(&tag, rows) tags = append(tags, tag) } rows.Close() return }
func unmarshalBalls(rows *sql.Rows) (balls []Ball, err error) { for rows.Next() { var ball Ball err := sqlstruct.Scan(&ball, rows) if err != nil { log.Fatal(err) } ball.Date, err = timeFromSQLTimestamp(ball.Timestamp) if err != nil { log.Fatal(err) } balls = append(balls, ball) } return }
func (r MakeMysqlRepo) Query(query string) (makes []Make, err error) { rows, err := r.Db.Query(query) if err != nil { log.Println(err) return } make := Make{} for rows.Next() { err = sqlstruct.Scan(&make, rows) makes = append(makes, make) } rows.Close() return }
// GetUnexpiredOCSPs retrieves all unexpired OCSPRecord from db. func GetUnexpiredOCSPs(db *sql.DB) (rrs []*OCSPRecord, err error) { rr := new(OCSPRecord) rows, err := db.Query(fmt.Sprintf(selectAllUnexpiredOCSPSQL, sqlstruct.Columns(*rr))) if err != nil { return nil, wrapCertStoreError(err) } defer rows.Close() for rows.Next() { err = sqlstruct.Scan(rr, rows) if err != nil { return nil, wrapCertStoreError(err) } rrs = append(rrs, rr) } return rrs, nil }
// GetUnexpiredCertificates gets all unexpired certificate from db. func GetUnexpiredCertificates(db *sql.DB) (crs []*CertificateRecord, err error) { cr := new(CertificateRecord) rows, err := db.Query(fmt.Sprintf(selectAllUnexpiredSQL, sqlstruct.Columns(*cr))) if err != nil { return nil, wrapCertStoreError(err) } defer rows.Close() for rows.Next() { err = sqlstruct.Scan(cr, rows) if err != nil { return nil, wrapCertStoreError(err) } crs = append(crs, cr) } return crs, nil }
// GetCertificate gets a certdb.CertificateRecord indexed by serial. func (d *Accessor) GetCertificate(serial string) (*certdb.CertificateRecord, error) { err := d.checkDB() if err != nil { return nil, err } cr := new(certdb.CertificateRecord) rows, err := d.db.Query(fmt.Sprintf(selectSQL, sqlstruct.Columns(*cr)), serial) if err != nil { return nil, wrapSQLError(err) } defer rows.Close() if rows.Next() { return cr, wrapSQLError(sqlstruct.Scan(cr, rows)) } return nil, nil }
// GetOCSP retrieves a certdb.OCSPRecord from db by serial. func (d *Accessor) GetOCSP(serial string) (rr *certdb.OCSPRecord, err error) { err = d.checkDB() if err != nil { return nil, err } rr = new(certdb.OCSPRecord) rows, err := d.db.Query(fmt.Sprintf(selectOCSPSQL, sqlstruct.Columns(*rr)), serial) if err != nil { return nil, wrapSQLError(err) } defer rows.Close() if rows.Next() { return rr, sqlstruct.Scan(rr, rows) } return nil, nil }
func (r ModelMysqlRepo) Get(id int, joins []string) (model Model, err error) { query := fmt.Sprintf("select * from model where id=%d", id) rows, err := r.Db.Query(query) if err != nil { return } if !rows.Next() { err = ErrNoResults return } sqlstruct.Scan(&model, rows) err = rows.Err() rows.Close() return }
func (r MakeMysqlRepo) Get(id int, joins []string) (make Make, err error) { entities := "make" query := fmt.Sprintf("select * from %s where id=%d", entities, id) rows, err := r.Db.Query(query) if err != nil { return } if !rows.Next() { err = ErrNoResults return } sqlstruct.Scan(&make, rows) rows.Close() return }
func cacheFetch(whereStr string, values []interface{}) ([]Cached, error) { queryStr := fmt.Sprintf("SELECT %s FROM cached", sqlstruct.Columns(Cached{})) rows, err := config.DB.Query(queryStr+" "+whereStr, values...) if err != nil { return []Cached{}, err } defer rows.Close() actions := []Cached{} for rows.Next() { var act Cached err := sqlstruct.Scan(&act, rows) if err == nil { actions = append(actions, act) } } return actions, nil }
func (r TagMysqlRepo) GetAll(limit int, joins []string) (result []Tag, err error) { if limit == 0 { limit = MAX_MYSQL_RESULTS } query := fmt.Sprintf("select * from tag limit %d", limit) rows, err := r.Db.Query(query) if err != nil { log.Println(err) return } tag := Tag{} for rows.Next() { err = sqlstruct.Scan(&tag, rows) result = append(result, tag) } rows.Close() return }
func populateDataForPhonenumbers(rows *sql.Rows, languageCode string) (*model.Response, error) { countries := []*model.Country{} for rows.Next() { var data model.Phonenumber err := sqlstruct.Scan(&data, rows) if err != nil { panic(err) } countryPosition := -1 for i := 0; i < len(countries); i++ { if countries[i].Code == data.Country { countryPosition = i } } if countryPosition != -1 { countries[countryPosition].Phonenumbers = append(countries[countryPosition].Phonenumbers, &data) } else { newCountry := &model.Country{ Code: data.Country, Phonenumbers: []*model.Phonenumber{}, } newCountry.Phonenumbers = append(newCountry.Phonenumbers, &data) countries = append(countries, newCountry) } } err := rows.Err() if err != nil { log.Errorf("Error during the loop of rows, err: %s", err) } data := &model.Response{ Countries: countries, Language: languageCode, } return data, err }
func (r ModelMysqlRepo) GetAll(limit int, joins []string) (result []Model, err error) { if limit == 0 { limit = MAX_MYSQL_RESULTS } query := fmt.Sprintf("select * from model limit %d", limit) rows, err := r.Db.Query(query) if err != nil { log.Println(err) return } model := Model{} for rows.Next() { err = sqlstruct.Scan(&model, rows) result = append(result, model) } rows.Close() return }
func scanRowsUsingStruct(rows *sql.Rows, columns []string, structDest interface{}, batchSize int, dataChan chan data.JSON) { defer rows.Close() tableData := []map[string]interface{}{} for rows.Next() { err := sqlstruct.Scan(structDest, rows) if err != nil { sendErr(err, dataChan) } d, err := data.NewJSON(structDest) if err != nil { sendErr(err, dataChan) } entry := make(map[string]interface{}) err = data.ParseJSON(d, &entry) if err != nil { sendErr(err, dataChan) } tableData = append(tableData, entry) if batchSize > 0 && len(tableData) >= batchSize { sendTableData(tableData, dataChan) tableData = []map[string]interface{}{} } } if rows.Err() != nil { sendErr(rows.Err(), dataChan) } // Flush remaining tableData if len(tableData) > 0 { sendTableData(tableData, dataChan) } close(dataChan) // signal completion to caller }
// GetOCSP retrieves a certdb.OCSPRecord from db by serial. func (d *Accessor) GetOCSP(serial, aki string) (rrs []certdb.OCSPRecord, err error) { err = d.checkDB() if err != nil { return nil, err } rr := certdb.OCSPRecord{} rows, err := d.db.Query(fmt.Sprintf(selectOCSPSQL, sqlstruct.Columns(rr)), serial, aki) if err != nil { return nil, wrapSQLError(err) } defer rows.Close() if rows.Next() { err = sqlstruct.Scan(&rr, rows) if err != nil { return nil, wrapSQLError(err) } rrs = append(rrs, rr) } return rrs, nil }
// GetUnexpiredOCSPs retrieves all unexpired certdb.OCSPRecord from db. func (d *Accessor) GetUnexpiredOCSPs() (rrs []*certdb.OCSPRecord, err error) { err = d.checkDB() if err != nil { return nil, err } rr := new(certdb.OCSPRecord) rows, err := d.db.Query(fmt.Sprintf(selectAllUnexpiredOCSPSQL, sqlstruct.Columns(*rr))) if err != nil { return nil, wrapSQLError(err) } defer rows.Close() for rows.Next() { err = sqlstruct.Scan(rr, rows) if err != nil { return nil, wrapSQLError(err) } rrs = append(rrs, rr) } return rrs, nil }
// GetUnexpiredCertificates gets all unexpired certificate from db. func (d *Accessor) GetUnexpiredCertificates() (crs []*certdb.CertificateRecord, err error) { err = d.checkDB() if err != nil { return nil, err } cr := new(certdb.CertificateRecord) rows, err := d.db.Query(fmt.Sprintf(selectAllUnexpiredSQL, sqlstruct.Columns(*cr))) if err != nil { return nil, wrapSQLError(err) } defer rows.Close() for rows.Next() { err = sqlstruct.Scan(cr, rows) if err != nil { return nil, wrapSQLError(err) } crs = append(crs, cr) } return crs, nil }
// GetCertificate gets a certdb.CertificateRecord indexed by serial. func (d *Accessor) GetCertificate(serial, aki string) (crs []certdb.CertificateRecord, err error) { err = d.checkDB() if err != nil { return nil, err } cr := certdb.CertificateRecord{} rows, err := d.db.Query(fmt.Sprintf(selectSQL, sqlstruct.Columns(cr)), serial, aki) if err != nil { return nil, wrapSQLError(err) } defer rows.Close() if rows.Next() { err = sqlstruct.Scan(&cr, rows) if err != nil { return nil, wrapSQLError(err) } crs = append(crs, cr) } return crs, nil }
func populateDataForCountries(rows *sql.Rows) (*model.Response, error) { countries := []*model.Country{} for rows.Next() { var country model.Country err := sqlstruct.Scan(&country, rows) if err != nil { log.Errorf("Error in the parsing of a country, err: %s", err) } countries = append(countries, &country) } err := rows.Err() if err != nil { log.Errorf("Error during the loop of rows, err: %s", err) } data := &model.Response{ Countries: countries, Language: "", } return data, err }