Golang DB.Query Examples

Golang DB.Query - 30 examples found. These are the top rated real world Golang examples of database/sql.DB.Query extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func GetOptions(db *sql.DB) (map[string][]Option, error) {
	options := make(map[string][]Option)

	rows, err := db.Query(`SELECT name, code, option_code, option_value , option_name FROM options`)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	for rows.Next() {
		var opt Option
		if err := rows.Scan(&opt.Name, &opt.Code, &opt.OptionCode, &opt.OptionValue, &opt.OptionName); err != nil {
			log.Println("scan failed:", err)
			return nil, err
		}

		if sel, ok := options[opt.Code]; ok {
			sel = append(sel, opt)
			options[opt.Code] = sel
		} else {
			sel = []Option{}
			sel = append(sel, opt)
			options[opt.Code] = sel
		}
	}

	return options, nil
}
Example #2
0
func runQuery(ctx context.Context, db *sql.DB, query string) (*sql.Rows, error) {
	done := make(chan struct{})
	var (
		rows   *sql.Rows
		errMsg error
	)
	go func() {
		for {
			rs, err := db.Query(query)
			if err != nil {
				errMsg = err
				time.Sleep(time.Second)
				continue
			} else {
				rows = rs
				errMsg = nil
				done <- struct{}{}
				break
			}
		}
	}()
	select {
	case <-done:
		return rows, errMsg
	case <-ctx.Done():
		return nil, fmt.Errorf("runQuery %s timed out with %v / %v", query, ctx.Err(), errMsg)
	}
}
Example #3
0
func getDeploymentLogEntries(db *sql.DB, d *models.Deployment) ([]*deploy.LogEntry, error) {
	entries := []*deploy.LogEntry{}

	rows, err := db.Query(deploymentLogEntriesStmt, d.Id)
	if err != nil {
		return entries, err
	}
	defer rows.Close()

	for rows.Next() {
		var entryType string
		e := &deploy.LogEntry{}

		err = rows.Scan(&e.Id, &e.DeploymentId, &entryType, &e.Origin, &e.Message, &e.Timestamp)
		if err != nil {
			return entries, err
		}

		e.EntryType = deploy.LogEntryType(entryType)

		entries = append(entries, e)
	}

	if err := rows.Err(); err != nil {
		return entries, err
	}

	return entries, nil
}
Example #4
0
func SelectBodys(db *sql.DB, ids []int) (map[int]string, error) {
	m := map[int]string{}
	strs := make([]string, len(ids))
	for i, id := range ids {
		strs[i] = fmt.Sprintf("%d", id)
	}
	clause := strings.Join(strs, ",")
	rows, err := db.Query(
		fmt.Sprintf(
			"SELECT id, body FROM reviews WHERE id IN (%s)",
			clause,
		),
	)
	if err != nil {
		return m, err
	}
	for rows.Next() {
		var id int
		var body string
		if err := rows.Scan(&id, &body); err != nil {
			return m, fmt.Errorf("SELECT error: %s", err)
		}
		m[id] = body
	}
	return m, nil
}
Example #5
0
func DataCheck(qry string, conn *sql.DB) bool {
	// conn, err := connect.GetJanusCon()
	// if err != nil {
	// 	panic(err)
	// }
	// defer conn.Close()

	rowscheck, err := conn.Query(qry)
	if err != nil {
		log.Printf(`Error with "%s": %s`, qry, err)
	}
	len := 0
	for rowscheck.Next() {
		len = len + 1
	}

	// conn.Close()

	if len > 0 {
		return true
	} else {
		return false
	}

}
Example #6
0
func InvSeries(db *sql.DB, simid []byte, agent int, iso int) (xys []XY, err error) {
	sql := `SELECT ti.Time,SUM(cmp.MassFrac * inv.Quantity) FROM (
				Compositions AS cmp
				INNER JOIN Inventories AS inv ON inv.QualId = cmp.QualId
				INNER JOIN TimeList AS ti ON (ti.Time >= inv.StartTime AND ti.Time < inv.EndTime)
			) WHERE (
				inv.SimId = ? AND inv.SimId = cmp.SimId AND ti.SimId = inv.SimId
				AND inv.AgentId = ? AND cmp.NucId = ?
			) GROUP BY ti.Time,cmp.NucId;`
	rows, err := db.Query(sql, simid, agent, iso)
	if err != nil {
		return nil, err
	}
	for rows.Next() {
		xy := XY{}
		if err := rows.Scan(&xy.X, &xy.Y); err != nil {
			return nil, err
		}
		xys = append(xys, xy)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return xys, nil
}