package database import ( "context" "database/sql" "fmt" rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules" datasql "edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/database/sql" ) func (ds Dataset) ReadCommand(ctx context.Context, name string) (rulesengine.Command, error) { var namedb string var comid string row := ds.db.QueryRowContext(ctx, datasql.SelectCommandByName, name) err := row.Scan(&comid, &namedb) if err != nil { if err != sql.ErrNoRows { return rulesengine.Command{}, fmt.Errorf("error in data:ReadCommand: %v", err) // returns 500 } } return rulesengine.Command{Name: namedb, ID: comid}, nil } func (ds Dataset) ReadAllCommands(ctx context.Context) ([]rulesengine.Command, error) { return ds.readAllCommands(ctx) } // Takes in a list of command names and returns all commands in db that match. // If the names list is empty, then all commands are returned. func (ds Dataset) ReadCommandsWithFilter(ctx context.Context, names []string) ([]rulesengine.Command, error) { if len(names) != 0 { return ds.readCommands(ctx, names) } return ds.readAllCommands(ctx) } func (ds Dataset) readCommands(ctx context.Context, names []string) ([]rulesengine.Command, error) { results, err := ds.readNames(ctx, names, datasql.SelectCommandsByName) if err != nil { return nil, fmt.Errorf("error in data:ReadCommands: %v", err) } var commands []rulesengine.Command for _, result := range results { commands = append(commands, rulesengine.Command{ ID: result.id, Name: result.name, }) } return commands, nil } // Returns list of all commands with their IDs stored in the database // or an error on exec. func (ds Dataset) readAllCommands(ctx context.Context) ([]rulesengine.Command, error) { rows, err := ds.db.QueryContext(ctx, datasql.SelectAllCommands) if err != nil { return nil, fmt.Errorf("error in data:ReadCommands: %v", err) } defer rows.Close() return scanCommandRows(rows) } func scanCommandRows(rows *sql.Rows) ([]rulesengine.Command, error) { defer rows.Close() res := []rulesengine.Command{} for rows.Next() { var name string var comid string err := rows.Scan(&comid, &name) if err != nil { return nil, fmt.Errorf("error in data:scanCommandRows: %v", err) } res = append(res, rulesengine.Command{Name: name, ID: comid}) } err := rows.Err() if err != nil { err = fmt.Errorf("error in data:scanCommandRows on rows.Err: %v ", err) } return res, err } // Delete command takes a name for a command and removes it from a given row. // returns the number of rows affected by the deletion or an error on exec. func (ds Dataset) DeleteCommand(ctx context.Context, name string) (rulesengine.DeleteResult, error) { res, err := ds.deleteValue(ctx, datasql.DeleteCommand, name) if err != nil { return rulesengine.DeleteResult{}, err } if len(res.Errors) == 0 && res.RowsAffected == 0 { res.Errors = append(res.Errors, rulesengine.Error{Type: rulesengine.UnknownCommand, Command: name}) } return res, err } // AddCommands takes a list of commands and stores them. Return an error. Conflicts are not returned. func (ds Dataset) AddCommands(ctx context.Context, names []string) (rulesengine.AddNameResult, error) { return ds.addNames(ctx, names, datasql.InsertCommand) }