github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/sys/sqlquery/utils.go (about)

     1  /*
     2   * Copyright (c) 2024-present Sigma-Soft, Ltd.
     3   * @author: Nikolay Nikitin
     4   */
     5  
     6  package sqlquery
     7  
     8  import (
     9  	"fmt"
    10  	"strconv"
    11  
    12  	"github.com/voedger/voedger/pkg/appdef"
    13  	"github.com/voedger/voedger/pkg/istructs"
    14  )
    15  
    16  // ParseQueryAppWs parses the query string and returns the application name and workspace ID if presents in the query.
    17  // Also, it returns the cleaned query string without the application name and workspace ID.
    18  //
    19  // The query string should have the following format:
    20  //
    21  //	select *|exp[, exp] from [appOwner.appName.][wsid.]tableQName[ params]
    22  func parseQueryAppWs(query string) (app istructs.AppQName, ws istructs.WSID, clean string, err error) {
    23  	const (
    24  		// 0 is original query
    25  
    26  		selectIdx int = 1 + iota
    27  		appIdx
    28  		wsIdx
    29  		tableIdx
    30  		paramsIdx
    31  
    32  		groupsCount
    33  	)
    34  
    35  	parts := selectQueryExp.FindStringSubmatch(query)
    36  	if len(parts) != groupsCount {
    37  		return istructs.NullAppQName, 0, "", fmt.Errorf("invalid query format: %s", query)
    38  	}
    39  
    40  	if appName := parts[appIdx]; appName != "" {
    41  		appName = appName[:len(parts[appIdx])-1]
    42  		own, n, err := appdef.ParseQualifiedName(appName, `.`)
    43  		if err != nil {
    44  			return istructs.NullAppQName, 0, "", err
    45  		}
    46  		app = istructs.NewAppQName(own, n)
    47  	}
    48  
    49  	if wsID := parts[wsIdx]; wsID != "" {
    50  		wsID = wsID[:len(parts[wsIdx])-1]
    51  		if id, err := strconv.ParseUint(wsID, 0, 0); err == nil {
    52  			ws = istructs.WSID(id)
    53  		} else {
    54  			return istructs.NullAppQName, 0, "", err
    55  		}
    56  	}
    57  
    58  	clean = parts[selectIdx] + parts[tableIdx] + parts[paramsIdx]
    59  
    60  	return app, ws, clean, nil
    61  }