// GetMaxId returns max id of a specified table. func GetMaxId(tableName dna.String, db *sqlpg.DB) (dna.Int, error) { var maxid dna.Int err := db.QueryRow("SELECT max(id) FROM " + tableName).Scan(&maxid) switch { case err == sqlpg.ErrNoRows: return 0, err case err != nil: return 0, err default: return maxid, nil } }
// SelectLastMissingIds returns a list of ids which is missing from a table. // nLastIds is the total number of last ids in the table. // // Example: 3 last ids from table nssongs are 1,4,5. Therefore, the missing // ids whose range is from 1->5 are 2,3. func SelectLastMissingIds(tblName dna.String, nLastIds dna.Int, db *sqlpg.DB) (*dna.IntArray, error) { var min, max dna.Int query := dna.Sprintf("SELECT min(id), max(id) FROM (SELECT id FROM %v ORDER BY id DESC LIMIT %v) as AB", tblName, nLastIds) // dna.Log(query) db.QueryRow(query).Scan(&min, &max) // totalItem := max - min + 1 // var ids = make(dna.IntArray, totalItem, totalItem+100) // var idx = 0 // for i := min; i < max; i++ { // ids[idx] = i // idx += 1 // } return SelectMissingIdsWithRange(tblName, min, max, db) }