github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/optional/select/autocomplete.go (about)

     1  package sqlselect
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/lmorg/murex/lang"
     8  	"github.com/lmorg/murex/lang/types"
     9  )
    10  
    11  func dynamicAutocomplete(p *lang.Process, confFailColMismatch, confTableIncHeadings bool) error {
    12  	dt := p.Stdin.GetDataType()
    13  	p.Stdout.SetDataType(types.Json)
    14  
    15  	inBytes, _ := p.Stdin.Stats()
    16  	if inBytes > 1024*1024*10 { // 10MB
    17  		return fmt.Errorf("file too large to unmarshal")
    18  	}
    19  
    20  	var completions []string
    21  	parameters := strings.ToUpper(p.Parameters.StringAll())
    22  
    23  	if confTableIncHeadings {
    24  		v, err := lang.UnmarshalData(p, dt)
    25  		if err != nil {
    26  			return fmt.Errorf("unable to unmarshal STDIN: %s", err.Error())
    27  		}
    28  		switch v := v.(type) {
    29  		case [][]string:
    30  			completions = v[0]
    31  
    32  		case [][]interface{}:
    33  			completions = make([]string, len(v[0]))
    34  			for i := range completions {
    35  				completions[i] = fmt.Sprint(v[0][i])
    36  			}
    37  
    38  		default:
    39  			return fmt.Errorf("unable to convert the following data structure into a table: %T", v)
    40  		}
    41  	}
    42  
    43  	if rxQuery.MatchString(parameters) {
    44  		completions = append(completions,
    45  			"=", ">", ">=", "<", "<=", "<>", "!=", "not", "like",
    46  			"AND", "OR",
    47  			"ORDER BY", "GROUP BY",
    48  		)
    49  	} else {
    50  		s, err := p.Variables.GetString("ISMETHOD")
    51  		if !types.IsTrue([]byte(s), 0) && err == nil {
    52  			completions = append(completions, "FROM")
    53  
    54  			last, _ := p.Parameters.String(p.Parameters.Len() - 1)
    55  			if strings.ToUpper(last) == "FROM" {
    56  				completions = append(completions, "@IncFiles")
    57  			}
    58  		}
    59  
    60  		completions = append(completions, "*", "WHERE", "ORDER BY", "GROUP BY")
    61  	}
    62  
    63  	b, err := lang.MarshalData(p, types.Json, completions)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	_, err = p.Stdout.Write(b)
    68  	return err
    69  }