github.com/mithrandie/csvq@v1.18.1/lib/query/runtime_information.go (about)

     1  package query
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/mithrandie/csvq/lib/parser"
     8  	"github.com/mithrandie/csvq/lib/value"
     9  )
    10  
    11  const (
    12  	UncommittedInformation  = "UNCOMMITTED"
    13  	CreatedInformation      = "CREATED"
    14  	UpdatedInformation      = "UPDATED"
    15  	UpdatedViewsInformation = "UPDATED_VIEWS"
    16  	LoadedTablesInformation = "LOADED_TABLES"
    17  	WorkingDirectory        = "WORKING_DIRECTORY"
    18  	VersionInformation      = "VERSION"
    19  )
    20  
    21  var RuntimeInformatinList = []string{
    22  	UncommittedInformation,
    23  	CreatedInformation,
    24  	UpdatedInformation,
    25  	UpdatedViewsInformation,
    26  	LoadedTablesInformation,
    27  	WorkingDirectory,
    28  	VersionInformation,
    29  }
    30  
    31  func GetRuntimeInformation(tx *Transaction, expr parser.RuntimeInformation) (value.Primary, error) {
    32  	var p value.Primary
    33  
    34  	switch strings.ToUpper(expr.Name) {
    35  	case UncommittedInformation:
    36  		p = value.NewBoolean(!tx.UncommittedViews.IsEmpty())
    37  	case CreatedInformation:
    38  		p = value.NewInteger(int64(tx.UncommittedViews.CountCreatedTables()))
    39  	case UpdatedInformation:
    40  		p = value.NewInteger(int64(tx.UncommittedViews.CountUpdatedTables()))
    41  	case UpdatedViewsInformation:
    42  		p = value.NewInteger(int64(tx.UncommittedViews.CountUpdatedViews()))
    43  	case LoadedTablesInformation:
    44  		p = value.NewInteger(int64(tx.CachedViews.Len()))
    45  	case WorkingDirectory:
    46  		wd, err := os.Getwd()
    47  		if err != nil {
    48  			return p, err
    49  		}
    50  		p = value.NewString(wd)
    51  	case VersionInformation:
    52  		p = value.NewString(Version)
    53  	default:
    54  		return p, NewInvalidRuntimeInformationError(expr)
    55  	}
    56  
    57  	return p, nil
    58  }