blob: 5b68228133a4a1583db102778ca498c67d3b50df [file] [log] [blame]
avm9996383f8f292021-08-24 18:26:52 +02001package db
2
3import (
4 "context"
5 "database/sql"
6 "fmt"
7
8 pb "gomodules.avm99963.com/twpt-server/api_proto"
9)
10
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +020011func 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
avm9996383f8f292021-08-24 18:26:52 +020023func 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
35func 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ínez25e12112021-08-25 13:48:06 +020044 return nil, fmt.Errorf("ListFeatures: %v", err)
avm9996383f8f292021-08-24 18:26:52 +020045 }
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ínez25e12112021-08-25 13:48:06 +020052 return nil, fmt.Errorf("ListFeatures: %v", err)
avm9996383f8f292021-08-24 18:26:52 +020053 }
54 features = append(features, &f)
55 }
56 if err := rows.Err(); err != nil {
Adrià Vilanova Martínez25e12112021-08-25 13:48:06 +020057 return nil, fmt.Errorf("ListFeatures: %v", err)
avm9996383f8f292021-08-24 18:26:52 +020058 }
59 return features, nil
60}
61
62func 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
67func UpdateFeature(db *sql.DB, ctx context.Context, id int32, f *pb.Feature) error {
Adrià Vilanova Martínez28bd8032021-08-24 19:22:11 +020068 _, err := db.ExecContext(ctx, "UPDATE Feature SET feat_type = ? WHERE feat_id = ?", f.Type, id)
avm9996383f8f292021-08-24 18:26:52 +020069 return err
70}