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

     1  package query
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/mithrandie/csvq/lib/parser"
     9  	"github.com/mithrandie/csvq/lib/value"
    10  )
    11  
    12  func FormatTableName(s string) string {
    13  	if len(s) < 1 {
    14  		return ""
    15  	}
    16  	return strings.TrimSuffix(filepath.Base(s), filepath.Ext(s))
    17  }
    18  
    19  func FormatFieldIdentifier(e parser.QueryExpression) string {
    20  	if pt, ok := e.(parser.PrimitiveType); ok {
    21  		prefix := "@__PT:"
    22  		switch pt.Value.(type) {
    23  		case *value.String:
    24  			prefix = prefix + "S"
    25  		case *value.Integer:
    26  			prefix = prefix + "I"
    27  		case *value.Float:
    28  			prefix = prefix + "F"
    29  		case *value.Boolean:
    30  			prefix = prefix + "B"
    31  		case *value.Ternary:
    32  			prefix = prefix + "T"
    33  		case *value.Datetime:
    34  			prefix = prefix + "D"
    35  		case *value.Null:
    36  			prefix = prefix + "N"
    37  		}
    38  		return prefix + ":" + FormatFieldLabel(e)
    39  	}
    40  	if fr, ok := e.(parser.FieldReference); ok {
    41  		if col, ok := fr.Column.(parser.Identifier); ok {
    42  			return "@__IDENT:" + col.Literal
    43  		}
    44  	}
    45  	return e.String()
    46  }
    47  
    48  func FormatFieldLabel(e parser.QueryExpression) string {
    49  	if pt, ok := e.(parser.PrimitiveType); ok {
    50  		if s, ok := pt.Value.(*value.String); ok {
    51  			return s.Raw()
    52  		}
    53  		if dt, ok := pt.Value.(*value.Datetime); ok {
    54  			return dt.Format(time.RFC3339Nano)
    55  		}
    56  		return pt.Value.String()
    57  	}
    58  	if fr, ok := e.(parser.FieldReference); ok {
    59  		if col, ok := fr.Column.(parser.Identifier); ok {
    60  			return col.Literal
    61  		}
    62  	}
    63  	return e.String()
    64  }