// GetAuth is used by the supervisor to determine which users have access // to which models. func GetAuth(tx *sql.Tx) (*PredictionAuth, error) { q1 := `SELECT u.username, k.apikey, k.read_only_apikey FROM User u INNER JOIN ApiKey k ON u.user_id = k.user_id;` rows, err := tx.Query(q1) if err != nil { return nil, err } defer rows.Close() users := make(map[string]Apikeys) for rows.Next() { apikeys := Apikeys{} var username string err := rows.Scan(&username, &apikeys.Apikey, &apikeys.ReadOnly) if err != nil { return nil, err } users[username] = apikeys } if err := rows.Err(); err != nil { return nil, err } q2 := `SELECT shareduser.username, owner.username, m.modelname FROM ModelSharedUser s INNER JOIN User shareduser ON shareduser.user_id = s.shared_user_id INNER JOIN Model m ON m.model_id = s.model_id INNER JOIN User owner ON owner.user_id = m.user_id;` rows, err = tx.Query(q2) if err != nil { return nil, err } defer rows.Close() sharedModels := []SharedModel{} for rows.Next() { s := SharedModel{} err := rows.Scan(&s.User, &s.Owner, &s.Model) if err != nil { return nil, err } sharedModels = append(sharedModels, s) } if err := rows.Err(); err != nil { return nil, err } return &PredictionAuth{ Users: users, Shared: sharedModels, }, nil }
// chooseWorkUnits chooses up to a specified number of work units from // some work spec. func (w *worker) chooseWorkUnits(tx *sql.Tx, spec *workSpec, numUnits int) ([]*workUnit, error) { query := buildSelect([]string{ workUnitID, workUnitName, }, []string{ workUnitTable, }, []string{ inThisWorkSpec, workUnitAttempt + " IS NULL", }) query += fmt.Sprintf(" ORDER BY priority DESC, name ASC") query += fmt.Sprintf(" LIMIT %v", numUnits) query += " FOR UPDATE OF work_unit" rows, err := tx.Query(query, spec.id) if err != nil { return nil, err } var result []*workUnit err = scanRows(rows, func() error { unit := workUnit{spec: spec} if err := rows.Scan(&unit.id, &unit.name); err == nil { result = append(result, &unit) } return err }) if err != nil { return nil, err } return result, nil }
func ModelSharedUsers(tx *sql.Tx, username, modelname string) ([]SharedUser, error) { // first get all users q1 := `SELECT u.user_id, u.username FROM User u INNER JOIN ModelSharedUser s ON u.user_id = shared_user_id INNER JOIN Model m ON m.model_id = s.model_id INNER JOIN User u2 ON u2.user_id = m.user_id WHERE u2.username = ? AND m.modelname = ?; ` users := []SharedUser{} rows, err := tx.Query(q1, username, modelname) if err != nil { return nil, fmt.Errorf("query failed: %v", err) } for rows.Next() { u := SharedUser{} err := rows.Scan(&u.Id, &u.Name) if err != nil { return nil, fmt.Errorf("GetSharedUsersByModel: error scanning rows: %v", err) } users = append(users, u) } return users, nil }
func addLastAmongDup(tx *sql.Tx) error { // Upgrade from early version where lastAmongDup is missing. rows, err := tx.Query("select * from cmd limit 1") if err != nil { return err } defer rows.Close() hasLastAmongDup, err := hasColumn(rows, "lastAmongDup") if err != nil { return err } if !hasLastAmongDup { _, err := tx.Exec("alter table cmd add column lastAmongDup bool") if err != nil { return err } _, err = tx.Exec("update cmd set lastAmongDup = (rowid in (select max(rowid) from cmd group by content));") if err != nil { return err } } return nil }
// postgres does not have REPLACE INTO (upsert), so we use that custom // one for Set operations in batch instead func altBatchSet(tx *sql.Tx, key, value string) error { r, err := tx.Query("SELECT replaceinto($1, $2)", key, value) if err != nil { return err } return r.Close() }
// getLogEntries retrieves log entries in the range [from, to) func getLogEntries(tx *sql.Tx, from, to int) ([]*LogEntry, error) { var out []*LogEntry rows, err := tx.Query(`SELECT id, term, client_id, client_serial, operation, key, value `+ `FROM log WHERE id >= ? AND id < ? ORDER BY id ASC`, from, to) if err != nil { log.Printf("db error loading log entries [%d,%d): %v", from, to, err) return nil, err } for rows.Next() { l := new(LogEntry) out = append(out, l) err := rows.Scan( &l.ID, &l.Term, &l.ClientRequest.ClientID, &l.ClientRequest.ClientSerial, &l.ClientRequest.Operation, &l.ClientRequest.Key, &l.ClientRequest.Value) if err != nil { log.Printf("db error scanning log entry: %v", err) return nil, err } } if err := rows.Err(); err != nil { log.Printf("db error reading log entries: %v", err) return nil, err } return out, nil }
func getLoadBatch(tx *sql.Tx, batchUuid string) (*LoadBatch, error) { var batch LoadBatch batch.UUID = batchUuid rows, err := tx.Query("SELECT keyname, tablename FROM "+pendingLoadTable+" WHERE batch_uuid = $1", batchUuid) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var load Load err := rows.Scan(&load.KeyName, &load.TableName) if err != nil { log.Println("Scan threw an error!") return nil, err } batch.Loads = append(batch.Loads, load) } if len(batch.Loads) == 0 { return nil, noLoadsError } batch.TableName = batch.Loads[0].TableName return &batch, nil }
func loadQuery(tx *sql.Tx, q string, args ...interface{}) (*table, error) { rows, err := tx.Query(q, args...) if err != nil { return nil, err } defer rows.Close() names, err := rows.Columns() if err != nil { return nil, err } t := &table{Header: names} for rows.Next() { ints := []*interface{}{} refs := []interface{}{} for i := 0; i < len(names); i++ { var i interface{} refs = append(refs, &i) ints = append(ints, &i) } err = rows.Scan(refs...) if err != nil { return nil, err } r := row{} for _, v := range ints { r = append(r, valueToString(*v)) } t.Rows = append(t.Rows, r) } return t, rows.Err() }
func (d *Postgres) getLogTags(tx *sql.Tx, log *models.Log) error { var ( tag string tags []string ) query, args, dest := sqlbuilder.Select(). Dialect(sqlbuilder.Postgres). From(`"log_tag"`). Map(`"tag"`, &tag). Where(`"log_id" = ?`, log.ID). Build() rows, err := tx.Query(query, args...) if err != nil { return err } for rows.Next() { if err := rows.Scan(dest...); err != nil { return err } tags = append(tags, tag) } if err := rows.Err(); err != nil { return err } log.Tags = tags return nil }
// // MIGRATION TWO: DOWN // func twoDown(tx *sql.Tx) error { _, err := tx.Exec("ALTER TABLE foobar ADD COLUMN s varchar(255)") if err != nil { return err } rows, err := tx.Query("SELECT i,t FROM foobar") if err != nil { return err } forLater := make(map[int]string) for rows.Next() { var i int var t string if err := rows.Scan(&i, &t); err != nil { return err } forLater[i] = t } if err := rows.Err(); err != nil { return err } for i, t := range forLater { ///XXX Why does this not work with ? as placeholders for the values? _, err := tx.Exec(fmt.Sprintf("UPDATE foobar SET s = '%s' WHERE i = %d", t, i)) if err != nil { return err } } _, err = tx.Exec("ALTER TABLE foobar DROP COLUMN t") if err != nil { return err } return nil }
// FetchProfileID returns 0 if profile does not exist. Revision is used to // ensure the profile exists on the same site as the revision func FetchProfileID(tx *sql.Tx, profileName string, revisionID int64) int64 { var profileID int64 rows, err := tx.Query(` SELECT profile_id FROM profiles WHERE LOWER(profile_name) = $1 AND site_id = ( SELECT site_id FROM revisions r LEFT JOIN profiles p ON r.profile_id = p.profile_id WHERE revision_id = $2 ) ORDER BY profile_id ASC LIMIT 1 OFFSET 0`, strings.ToLower(profileName), revisionID, ) defer rows.Close() for rows.Next() { err = rows.Scan(&profileID) if err != nil { return 0 } } err = rows.Err() if err != nil { return 0 } rows.Close() return profileID }
// GetAllRemotes returns all the ACIInfos sorted by optional sortfields and // with ascending or descending order. func GetAllRemotes(tx *sql.Tx) ([]*Remote, error) { var remotes []*Remote query := "SELECT * from remote" rows, err := tx.Query(query) if err != nil { return nil, err } for rows.Next() { r := &Remote{} if err := remoteRowScan(rows, r); err != nil { return nil, err } remotes = append(remotes, r) } if err := rows.Err(); err != nil { return nil, err } return remotes, nil }
func findArticles(tx *sql.Tx, urls []string) ([]int, error) { out := []int{} sql := `SELECT DISTINCT article_id FROM article_url WHERE url IN (` + pgMarkerList(1, len(urls)) + `)` params := make([]interface{}, len(urls)) for i := 0; i < len(urls); i++ { params[i] = urls[i] } rows, err := tx.Query(sql, params...) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var artID int if err := rows.Scan(&artID); err != nil { return nil, err } out = append(out, artID) } if err := rows.Err(); err != nil { return nil, err } return out, nil }
func FindJournoByName(tx *sql.Tx, name string) ([]*Journo, error) { // TODO: use journo_alias table to do lookup! // KLUDGE ALERT: we're using refs to look up journos. This sucks, but // we're stuck with it until we transition over to a properly-populated journo_alias table // check first 20 possible refs. r := baseRef(name) if r == "" { return []*Journo{}, nil } refs := []interface{}{r} for i := 1; i < 20; i++ { refs = append(refs, fmt.Sprintf("%s-%d", r, i)) } sql := `SELECT id,ref,prettyname,lastname,firstname,created,status,oneliner,last_similar,modified,firstname_metaphone, lastname_metaphone, admin_notes, admin_tags,fake FROM journo WHERE ref IN (` + pgMarkerList(1, len(refs)) + `)` rows, err := tx.Query(sql, refs...) if err != nil { return nil, err } defer rows.Close() out := []*Journo{} for rows.Next() { var j Journo if err := rows.Scan(&j.ID, &j.Ref, &j.Prettyname, &j.Lastname, &j.Firstname, &j.Created, &j.Status, &j.Oneliner, &j.LastSimilar, &j.Modified, &j.FirstnameMetaphone, &j.LastnameMetaphone, &j.AdminNotes, &j.AdminTags, &j.Fake); err != nil { return nil, err } out = append(out, &j) } if err := rows.Err(); err != nil { return nil, err } return out, nil }
func processArticles(articleChannel *chan *[]Article, tx *sql.Tx, waitGroup *sync.WaitGroup) { waitGroup.Add(1) for articles := range *articleChannel { rows, err := tx.Query("select subscription_id, datetime(published), url from articles where published >= datetime(?) and subscription_id = ? order by datetime(published) asc", (*articles)[0].Published.Format(time.RFC3339), (*articles)[0].SubscriptionId) var existingArticles []Article if err != nil { log.Println("Article Download Error | Error retrieving existing articles " + err.Error()) } for rows.Next() { var article Article var dateString string rows.Scan(&article.SubscriptionId, &dateString, &article.Url) article.Published, err = time.Parse("2006-01-02 15:04:05", dateString) existingArticles = append(existingArticles, Article{}) copy(existingArticles[0+1:], existingArticles[0:]) existingArticles[0] = article } rows.Close() if len(existingArticles) == 0 { insertFinalArticleSlice(articles, tx) } else if (existingArticles[0].Published != (*articles)[len(*articles)-1].Published) && (existingArticles[0].Url != (*articles)[len(*articles)-1].Url) { index := sort.Search(len(*articles), func(i int) bool { return (*articles)[i].Published.Unix() > existingArticles[0].Published.Unix() }) newSlice := ((*articles)[index:]) insertFinalArticleSlice(&newSlice, tx) } } waitGroup.Done() }
// GetAllACIInfos returns all the ACIInfos sorted by optional sortfields and // with ascending or descending order. func GetAllACIInfos(tx *sql.Tx, sortfields []string, ascending bool) ([]*ACIInfo, error) { aciinfos := []*ACIInfo{} query := "SELECT * from aciinfo" if len(sortfields) > 0 { query += fmt.Sprintf(" ORDER BY %s ", strings.Join(sortfields, ", ")) if ascending { query += "ASC" } else { query += "DESC" } } rows, err := tx.Query(query) if err != nil { return nil, err } for rows.Next() { aciinfo := &ACIInfo{} if err := rows.Scan(&aciinfo.BlobKey, &aciinfo.AppName, &aciinfo.ImportTime, &aciinfo.Latest); err != nil { return nil, err } aciinfos = append(aciinfos, aciinfo) } if err := rows.Err(); err != nil { return nil, err } return aciinfos, err }
func getVIP(tx *sql.Tx, id uint64) (*vip, error) { qry := `SELECT A.id, INET_NTOA(B.address), A.active_host_id, A.standby_host_id FROM vip A JOIN ip B ON A.ip_id = B.id WHERE A.id = ? FOR UPDATE` row, err := tx.Query(qry, id) if err != nil { return nil, err } defer row.Close() // Emptry row? if !row.Next() { return nil, fmt.Errorf("unknown VIP (ID=%v)", id) } v := &vip{} var address string if err := row.Scan(&v.id, &address, &v.active, &v.standby); err != nil { return nil, err } v.address = net.ParseIP(address) if v.address == nil { return nil, fmt.Errorf("invalid IPv4 address: %v", address) } return v, nil }
func (dao *DaoService) provideBranchId(tx *sql.Tx, projectName string, branchName string) (int64, error) { projectId, err := dao.provideProjectId(tx, projectName) if err != nil { return 0, err } rows, err := tx.Query("SELECT branch_id FROM project_branches WHERE parent_project_id = ? AND branch_name = ?", projectId, branchName) if err != nil { return 0, err } var branchId int64 = 0 for rows.Next() { rows.Scan(&branchId) } if branchId == 0 { res, err := tx.Exec("INSERT INTO project_branches (parent_project_id, branch_name) VALUES (?, ?)", projectId, branchName) if err != nil { return 0, err } branchId, err = res.LastInsertId() if err != nil { return 0, err } } return branchId, nil }
func (*DaoService) provideProjectId(tx *sql.Tx, projectName string) (int64, error) { // Search for project with given project name rows, err := tx.Query("SELECT project_id FROM test_projects WHERE project_name = ?", projectName) if err != nil { return 0, err } var projectId int64 = 0 for rows.Next() { rows.Scan(&projectId) } // If such project is absent, then create it if projectId == 0 { res, err := tx.Exec("INSERT INTO test_projects (project_name) VALUES (?)", projectName) if err != nil { return 0, err } projectId, err = res.LastInsertId() if err != nil { return 0, err } } return projectId, nil }
// QueryTxToArray headers, data, error func QueryTxToArray(tx *sql.Tx, theCase string, sqlStatement string, sqlParams ...interface{}) ([]string, [][]string, error) { defer func() { if err := recover(); err != nil { fmt.Println(err) } }() var data [][]string var headers []string rows, err := tx.Query(sqlStatement, sqlParams...) if err != nil { fmt.Println("Error executing: ", sqlStatement) return headers, data, err } cols, _ := rows.Columns() if theCase == "lower" { colsLower := make([]string, len(cols)) for i, v := range cols { colsLower[i] = strings.ToLower(v) } headers = colsLower } else if theCase == "upper" { colsUpper := make([]string, len(cols)) for i, v := range cols { colsUpper[i] = strings.ToUpper(v) } headers = colsUpper } else if theCase == "camel" { colsCamel := make([]string, len(cols)) for i, v := range cols { colsCamel[i] = toCamel(v) } headers = colsCamel } else { headers = cols } rawResult := make([][]byte, len(cols)) dest := make([]interface{}, len(cols)) // A temporary interface{} slice for i := range rawResult { dest[i] = &rawResult[i] // Put pointers to each string in the interface slice } for rows.Next() { result := make([]string, len(cols)) rows.Scan(dest...) for i, raw := range rawResult { if raw == nil { result[i] = "" } else { result[i] = string(raw) } } data = append(data, result) } return headers, data, nil }
// getSequence get sequence number func getSequence(tx *sql.Tx) (id uint64, err error) { if _, err := tx.Query("UPDATE seq SET id = LAST_INSERT_ID(id + 1)"); err != nil { return 0, err } if err := tx.QueryRow("SELECT LAST_INSERT_ID()").Scan(&id); err != nil { return 0, err } return }
// chooseAndMakeAttempts, in one SQL query, finds work units to do for // a specific work spec, creates attempts for them, and returns the // corresponding attempt objects. func (w *worker) chooseAndMakeAttempts(tx *sql.Tx, spec *workSpec, numUnits int, now time.Time, length time.Duration) ([]coordinate.Attempt, error) { params := queryParams{} choose := buildSelect([]string{ workUnitID, workUnitName, }, []string{ workUnitTable, }, []string{ workUnitInSpec(¶ms, spec.id), workUnitHasNoAttempt, "NOT " + workUnitTooSoon(¶ms, now), }) choose += " ORDER BY priority DESC, name ASC" choose += fmt.Sprintf(" LIMIT %v", numUnits) expiration := now.Add(length) whatToInsert := buildSelect([]string{ "id", params.Param(spec.id), params.Param(w.id), params.Param(now), params.Param(expiration), }, []string{ "u", }, []string{}) attempts := "INSERT INTO " + attemptTable + "(work_unit_id, work_spec_id, worker_id, start_time, expiration_time) " + whatToInsert + " RETURNING id, work_unit_id" update := "UPDATE " + workUnitTable + " " + "SET active_attempt_id=a.id " + "FROM a, u " + "WHERE " + workUnitID + "=u.id AND a.work_unit_id=u.id " + "RETURNING u.id, u.name, a.id" query := "WITH u AS (" + choose + "), a AS (" + attempts + ") " + update rows, err := tx.Query(query, params...) if err != nil { return nil, err } var result []coordinate.Attempt err = scanRows(rows, func() error { unit := workUnit{spec: spec} attempt := attempt{unit: &unit, worker: w} if err := rows.Scan(&unit.id, &unit.name, &attempt.id); err == nil { result = append(result, &attempt) } return err }) if err != nil { return nil, err } return result, nil }
func printDB(tx *sql.Tx) { fmt.Println("\n\nDB:") rows, _ := tx.Query("SELECT * FROM vimages") for rows.Next() { rec, _ := recordFromRows(rows) fmt.Println(rec.path) } fmt.Println("") }
func executeLine(tx *sql.Tx, txnLine string) error { if tidb.IsQuery(txnLine) { rows, err := tx.Query(txnLine) if err != nil { return errors.Trace(err) } defer rows.Close() cols, err := rows.Columns() if err != nil { return errors.Trace(err) } values := make([][]byte, len(cols)) scanArgs := make([]interface{}, len(values)) for i := range values { scanArgs[i] = &values[i] } var datas [][]string for rows.Next() { err := rows.Scan(scanArgs...) if err != nil { return errors.Trace(err) } data := make([]string, len(cols)) for i, value := range values { if value == nil { data[i] = "NULL" } else { data[i] = string(value) } } datas = append(datas, data) } // For `cols` and `datas[i]` always has the same length, // no need to check return validity. result, _ := printer.GetPrintResult(cols, datas) fmt.Printf("%s", result) if err := rows.Err(); err != nil { return errors.Trace(err) } } else { // TODO: rows affected and last insert id _, err := tx.Exec(txnLine) if err != nil { return errors.Trace(err) } } return nil }
func AnyDuplicates(provider Provider, transaction *sql.Tx) (bool, error) { rows, err := transaction.Query("SELECT * FROM care_providers WHERE medicare_provider_number=?", provider.medicareProviderNumber) defer rows.Close() if err == nil && !rows.Next() { return false, err } else if err != nil { return false, err } else { return true, err } }
func (st *Storage) traceByID(tx *sql.Tx, id uint64) (tracer.RawTrace, error) { const selectTrace = ` SELECT spans.id, spans.trace_id, spans.time, spans.service_name, spans.operation_name, tags.key, tags.value, tags.time FROM spans LEFT JOIN tags ON spans.id = tags.span_id WHERE spans.trace_id = $1 ORDER BY spans.time ASC, spans.id, tags.time ASC` const selectRelations = ` SELECT r.span1_id, r.span2_id, r.kind FROM relations AS r JOIN spans ON spans.id = r.span1_id WHERE spans.trace_id = $1; ` rows, err := tx.Query(selectTrace, int64(id)) if err != nil { return tracer.RawTrace{}, err } spans, err := scanSpans(rows) if err != nil { return tracer.RawTrace{}, err } rows.Close() rows, err = tx.Query(selectRelations, int64(id)) if err != nil { return tracer.RawTrace{}, err } var rels []tracer.RawRelation var parent, child int64 var kind string for rows.Next() { if err := rows.Scan(&parent, &child, &kind); err != nil { return tracer.RawTrace{}, err } rels = append(rels, tracer.RawRelation{ ParentID: uint64(parent), ChildID: uint64(child), Kind: kind, }) } if err := rows.Err(); err != nil { return tracer.RawTrace{}, err } return tracer.RawTrace{ TraceID: id, Spans: spans, Relations: rels, }, nil }
func GetAll(tx *sql.Tx, limit int, offset int) ([]model.Group, error) { rows, err := tx.Query(selectQuery+"ORDER BY createdAt DESC LIMIT ? OFFSET ?", limit, offset) if err != nil { return nil, err } rs, err := getAll(rows) if err != nil { return nil, err } return rs, nil }
func GetByUserUID(tx *sql.Tx, uid string, limit int, offset int) ([]model.Group, error) { rows, err := tx.Query(selectQuery+"WHERE userUID = unhex(?) ORDER BY createdAt DESC LIMIT ? OFFSET ?", uid, limit, offset) if err != nil { return nil, err } rs, err := getAll(rows) if err != nil { return nil, err } return rs, nil }
func GetOlderThan(tx *sql.Tx, t int) ([]model.File, error) { now := time.Now().UTC().Add(-1 * time.Minute * time.Duration(t)) rows, err := tx.Query(selectQuery+"WHERE createdAt < ? AND tmp = true ORDER BY createdAt DESC", now) if err != nil { return nil, err } rs, err := getAll(rows) if err != nil { return nil, err } return rs, nil }
func getPortVIPs(tx *sql.Tx, portID uint64) (result []vip, err error) { qry := `SELECT A.id, INET_NTOA(C.address), A.active_host_id, A.standby_host_id FROM vip A JOIN host B ON A.active_host_id = B.id JOIN ip C ON A.ip_id = C.id WHERE B.port_id = ? FOR UPDATE` rows, err := tx.Query(qry, portID) if err != nil { return nil, err } defer rows.Close() for rows.Next() { var id, active, standby uint64 var address string if err := rows.Scan(&id, &address, &active, &standby); err != nil { return nil, err } ip := net.ParseIP(address) if ip == nil { return nil, fmt.Errorf("invalid IPv4 address: %v", address) } result = append(result, vip{id, ip, active, standby}) } if err := rows.Err(); err != nil { return nil, err } return result, nil }