avm99963 | 83f8f29 | 2021-08-24 18:26:52 +0200 | [diff] [blame] | 1 | package db |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "fmt" |
| 7 | |
| 8 | pb "gomodules.avm99963.com/twpt-server/api_proto" |
| 9 | ) |
| 10 | |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 11 | func GetFeatureById(db *sql.DB, ctx context.Context, id int32) (*pb.Feature, error) { |
| 12 | query := db.QueryRowContext(ctx, "SELECT feat_id, codename, feat_type FROM Feature WHERE feat_id = ?", id) |
| 13 | var f pb.Feature |
| 14 | if err := query.Scan(&f.Id, &f.Codename, &f.Type); err != nil { |
| 15 | if err == sql.ErrNoRows { |
| 16 | return nil, nil |
| 17 | } |
| 18 | return nil, fmt.Errorf("Error while querying feature by codename: %v.", err) |
| 19 | } |
| 20 | return &f, nil |
| 21 | } |
| 22 | |
avm99963 | 83f8f29 | 2021-08-24 18:26:52 +0200 | [diff] [blame] | 23 | func GetFeatureByCodename(db *sql.DB, ctx context.Context, codename string) (*pb.Feature, error) { |
| 24 | query := db.QueryRowContext(ctx, "SELECT feat_id, codename, feat_type FROM Feature WHERE codename = ?", codename) |
| 25 | var f pb.Feature |
| 26 | if err := query.Scan(&f.Id, &f.Codename, &f.Type); err != nil { |
| 27 | if err == sql.ErrNoRows { |
| 28 | return nil, nil |
| 29 | } |
| 30 | return nil, fmt.Errorf("Error while querying feature by codename: %v.", err) |
| 31 | } |
| 32 | return &f, nil |
| 33 | } |
| 34 | |
| 35 | func ListFeatures(db *sql.DB, ctx context.Context, withDeprecatedFeatures bool) ([]*pb.Feature, error) { |
| 36 | var rows *sql.Rows |
| 37 | var err error |
| 38 | if withDeprecatedFeatures { |
| 39 | rows, err = db.QueryContext(ctx, "SELECT feat_id, codename, feat_type FROM Feature") |
| 40 | } else { |
| 41 | rows, err = db.QueryContext(ctx, "SELECT feat_id, codename, feat_type FROM Feature WHERE feat_type <> ?", pb.Feature_TYPE_DEPRECATED) |
| 42 | } |
| 43 | if err != nil { |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 44 | return nil, fmt.Errorf("ListFeatures: %v", err) |
avm99963 | 83f8f29 | 2021-08-24 18:26:52 +0200 | [diff] [blame] | 45 | } |
| 46 | defer rows.Close() |
| 47 | |
| 48 | var features []*pb.Feature |
| 49 | for rows.Next() { |
| 50 | var f pb.Feature |
| 51 | if err := rows.Scan(&f.Id, &f.Codename, &f.Type); err != nil { |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 52 | return nil, fmt.Errorf("ListFeatures: %v", err) |
avm99963 | 83f8f29 | 2021-08-24 18:26:52 +0200 | [diff] [blame] | 53 | } |
| 54 | features = append(features, &f) |
| 55 | } |
| 56 | if err := rows.Err(); err != nil { |
Adrià Vilanova Martínez | 25e1211 | 2021-08-25 13:48:06 +0200 | [diff] [blame] | 57 | return nil, fmt.Errorf("ListFeatures: %v", err) |
avm99963 | 83f8f29 | 2021-08-24 18:26:52 +0200 | [diff] [blame] | 58 | } |
| 59 | return features, nil |
| 60 | } |
| 61 | |
| 62 | func AddFeature(db *sql.DB, ctx context.Context, f *pb.Feature) error { |
| 63 | _, err := db.ExecContext(ctx, "INSERT INTO Feature (codename, feat_type) VALUES (?, ?)", f.Codename, f.Type) |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | func UpdateFeature(db *sql.DB, ctx context.Context, id int32, f *pb.Feature) error { |
Adrià Vilanova Martínez | 28bd803 | 2021-08-24 19:22:11 +0200 | [diff] [blame] | 68 | _, err := db.ExecContext(ctx, "UPDATE Feature SET feat_type = ? WHERE feat_id = ?", f.Type, id) |
avm99963 | 83f8f29 | 2021-08-24 18:26:52 +0200 | [diff] [blame] | 69 | return err |
| 70 | } |