...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/database/commands.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/database

     1  package database
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"fmt"
     7  
     8  	rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules"
     9  	datasql "edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/database/sql"
    10  )
    11  
    12  func (ds Dataset) ReadCommand(ctx context.Context, name string) (rulesengine.Command, error) {
    13  	var namedb string
    14  	var comid string
    15  	row := ds.db.QueryRowContext(ctx, datasql.SelectCommandByName, name)
    16  
    17  	err := row.Scan(&comid, &namedb)
    18  	if err != nil {
    19  		if err != sql.ErrNoRows {
    20  			return rulesengine.Command{}, fmt.Errorf("error in data:ReadCommand: %v", err) // returns 500
    21  		}
    22  	}
    23  	return rulesengine.Command{Name: namedb, ID: comid}, nil
    24  }
    25  
    26  func (ds Dataset) ReadAllCommands(ctx context.Context) ([]rulesengine.Command, error) {
    27  	return ds.readAllCommands(ctx)
    28  }
    29  
    30  // Takes in a list of command names and returns all commands in db that match.
    31  // If the names list is empty, then all commands are returned.
    32  func (ds Dataset) ReadCommandsWithFilter(ctx context.Context, names []string) ([]rulesengine.Command, error) {
    33  	if len(names) != 0 {
    34  		return ds.readCommands(ctx, names)
    35  	}
    36  	return ds.readAllCommands(ctx)
    37  }
    38  
    39  func (ds Dataset) readCommands(ctx context.Context, names []string) ([]rulesengine.Command, error) {
    40  	results, err := ds.readNames(ctx, names, datasql.SelectCommandsByName)
    41  	if err != nil {
    42  		return nil, fmt.Errorf("error in data:ReadCommands: %v", err)
    43  	}
    44  	var commands []rulesengine.Command
    45  	for _, result := range results {
    46  		commands = append(commands, rulesengine.Command{
    47  			ID:   result.id,
    48  			Name: result.name,
    49  		})
    50  	}
    51  	return commands, nil
    52  }
    53  
    54  // Returns list of all commands with their IDs stored in the database
    55  // or an error on exec.
    56  func (ds Dataset) readAllCommands(ctx context.Context) ([]rulesengine.Command, error) {
    57  	rows, err := ds.db.QueryContext(ctx, datasql.SelectAllCommands)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("error in data:ReadCommands: %v", err)
    60  	}
    61  	defer rows.Close()
    62  
    63  	return scanCommandRows(rows)
    64  }
    65  
    66  func scanCommandRows(rows *sql.Rows) ([]rulesengine.Command, error) {
    67  	defer rows.Close()
    68  	res := []rulesengine.Command{}
    69  	for rows.Next() {
    70  		var name string
    71  		var comid string
    72  		err := rows.Scan(&comid, &name)
    73  		if err != nil {
    74  			return nil, fmt.Errorf("error in data:scanCommandRows: %v", err)
    75  		}
    76  		res = append(res, rulesengine.Command{Name: name, ID: comid})
    77  	}
    78  	err := rows.Err()
    79  	if err != nil {
    80  		err = fmt.Errorf("error in data:scanCommandRows on rows.Err: %v ", err)
    81  	}
    82  	return res, err
    83  }
    84  
    85  // Delete command takes a name for a command and removes it from a given row.
    86  // returns the number of rows affected by the deletion or an error on exec.
    87  func (ds Dataset) DeleteCommand(ctx context.Context, name string) (rulesengine.DeleteResult, error) {
    88  	res, err := ds.deleteValue(ctx, datasql.DeleteCommand, name)
    89  	if err != nil {
    90  		return rulesengine.DeleteResult{}, err
    91  	}
    92  	if len(res.Errors) == 0 && res.RowsAffected == 0 {
    93  		res.Errors = append(res.Errors, rulesengine.Error{Type: rulesengine.UnknownCommand, Command: name})
    94  	}
    95  	return res, err
    96  }
    97  
    98  // AddCommands takes a list of commands and stores them. Return an error. Conflicts are not returned.
    99  func (ds Dataset) AddCommands(ctx context.Context, names []string) (rulesengine.AddNameResult, error) {
   100  	return ds.addNames(ctx, names, datasql.InsertCommand)
   101  }
   102  

View as plain text