github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/vent/sqlsol/spec_loader.go (about)

     1  package sqlsol
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hyperledger/burrow/txs"
     7  	"github.com/hyperledger/burrow/vent/types"
     8  )
     9  
    10  type SpecOpt uint64
    11  
    12  const (
    13  	Block SpecOpt = 1 << iota
    14  	Tx
    15  )
    16  
    17  const (
    18  	None    SpecOpt = 0
    19  	BlockTx         = Block | Tx
    20  )
    21  
    22  func (so SpecOpt) Enabled(opt SpecOpt) bool {
    23  	return so&opt > 0
    24  }
    25  
    26  // SpecLoader loads spec files and parses them
    27  func SpecLoader(specFileOrDirs []string, opts SpecOpt) (*Projection, error) {
    28  	var projection *Projection
    29  	var err error
    30  
    31  	if len(specFileOrDirs) == 0 {
    32  		return nil, fmt.Errorf("please provide a spec file or directory")
    33  	}
    34  
    35  	projection, err = NewProjectionFromFolder(specFileOrDirs...)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("error parsing spec: %v", err)
    38  	}
    39  
    40  	// add block & tx to tables definition
    41  	if opts.Enabled(Block) {
    42  		for k, v := range blockTables() {
    43  			projection.Tables[k] = v
    44  		}
    45  	}
    46  	if opts.Enabled(Tx) {
    47  		for k, v := range txTables() {
    48  			projection.Tables[k] = v
    49  		}
    50  	}
    51  
    52  	return projection, nil
    53  }
    54  
    55  // getBlockTxTablesDefinition returns block & transaction structures
    56  func blockTables() types.EventTables {
    57  	return types.EventTables{
    58  		tables.Block: &types.SQLTable{
    59  			Name: tables.Block,
    60  			Columns: []*types.SQLTableColumn{
    61  				{
    62  					Name:    columns.Height,
    63  					Type:    types.SQLColumnTypeVarchar,
    64  					Length:  100,
    65  					Primary: true,
    66  				},
    67  				{
    68  					Name:    columns.BlockHeader,
    69  					Type:    types.SQLColumnTypeJSON,
    70  					Primary: false,
    71  				},
    72  			},
    73  		},
    74  	}
    75  }
    76  
    77  func txTables() types.EventTables {
    78  	return types.EventTables{
    79  		tables.Tx: &types.SQLTable{
    80  			Name: tables.Tx,
    81  			Columns: []*types.SQLTableColumn{
    82  				// transaction table
    83  				{
    84  					Name:    columns.Height,
    85  					Type:    types.SQLColumnTypeVarchar,
    86  					Length:  100,
    87  					Primary: true,
    88  				},
    89  				{
    90  					Name:    columns.TxHash,
    91  					Type:    types.SQLColumnTypeVarchar,
    92  					Length:  txs.HashLengthHex,
    93  					Primary: true,
    94  				},
    95  				{
    96  					Name:    columns.TxIndex,
    97  					Type:    types.SQLColumnTypeNumeric,
    98  					Length:  0,
    99  					Primary: false,
   100  				},
   101  				{
   102  					Name:    columns.TxType,
   103  					Type:    types.SQLColumnTypeVarchar,
   104  					Length:  100,
   105  					Primary: false,
   106  				},
   107  				{
   108  					Name:    columns.Envelope,
   109  					Type:    types.SQLColumnTypeJSON,
   110  					Primary: false,
   111  				},
   112  				{
   113  					Name:    columns.Events,
   114  					Type:    types.SQLColumnTypeJSON,
   115  					Primary: false,
   116  				},
   117  				{
   118  					Name:    columns.Result,
   119  					Type:    types.SQLColumnTypeJSON,
   120  					Primary: false,
   121  				},
   122  				{
   123  					Name:    columns.Receipt,
   124  					Type:    types.SQLColumnTypeJSON,
   125  					Primary: false,
   126  				},
   127  				{
   128  					Name:    columns.Exception,
   129  					Type:    types.SQLColumnTypeJSON,
   130  					Primary: false,
   131  				},
   132  			},
   133  		},
   134  	}
   135  }