本文整理匯總了Golang中github.com/influxdata/influxdb/influxql.ParseStatement函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseStatement函數的具體用法?Golang ParseStatement怎麽用?Golang ParseStatement使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseStatement函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: mustParseStatement
func mustParseStatement(s string) influxql.Statement {
stmt, err := influxql.ParseStatement(s)
if err != nil {
panic(err)
}
return stmt
}
示例2: execQuery
func (s *Service) execQuery(q, cluster string) (kapacitor.DBRP, *influxdb.Response, error) {
// Parse query to determine dbrp
dbrp := kapacitor.DBRP{}
stmt, err := influxql.ParseStatement(q)
if err != nil {
return dbrp, nil, err
}
if slct, ok := stmt.(*influxql.SelectStatement); ok && len(slct.Sources) == 1 {
if m, ok := slct.Sources[0].(*influxql.Measurement); ok {
dbrp.Database = m.Database
dbrp.RetentionPolicy = m.RetentionPolicy
}
}
if dbrp.Database == "" || dbrp.RetentionPolicy == "" {
return dbrp, nil, errors.New("could not determine database and retention policy. Is the query fully qualified?")
}
if s.InfluxDBService == nil {
return dbrp, nil, errors.New("InfluxDB not configured, cannot record query")
}
// Query InfluxDB
con, err := s.InfluxDBService.NewNamedClient(cluster)
if err != nil {
return dbrp, nil, errors.Wrap(err, "failed to get InfluxDB client")
}
query := influxdb.Query{
Command: q,
}
resp, err := con.Query(query)
if err != nil {
return dbrp, nil, errors.Wrap(err, "InfluxDB query failed")
}
return dbrp, resp, nil
}
示例3: TestParse_Errors
// Parse statements that might appear valid but should return an error.
// If allowed to execute, at least some of these statements would result in a panic.
func TestParse_Errors(t *testing.T) {
for _, tt := range []struct {
tmpl string
good string
bad string
}{
// Second argument to derivative must be duration
{tmpl: `SELECT derivative(f, %s) FROM m`, good: "1h", bad: "true"},
} {
good := fmt.Sprintf(tt.tmpl, tt.good)
if _, err := influxql.ParseStatement(good); err != nil {
t.Fatalf("statement %q should have parsed correctly but returned error: %s", good, err)
}
bad := fmt.Sprintf(tt.tmpl, tt.bad)
if _, err := influxql.ParseStatement(bad); err == nil {
t.Fatalf("statement %q should have resulted in a parse error but did not", bad)
}
}
}
示例4: processExecuteStatementRequest
func (s *Service) processExecuteStatementRequest(buf []byte) error {
// Unmarshal the request.
var req ExecuteStatementRequest
if err := req.UnmarshalBinary(buf); err != nil {
return err
}
// Parse the InfluxQL statement.
stmt, err := influxql.ParseStatement(req.Statement())
if err != nil {
return err
}
return s.executeStatement(stmt, req.Database())
}
示例5: influxTag
func influxTag(args []parse.Node) (parse.Tags, error) {
st, err := influxql.ParseStatement(args[1].(*parse.StringNode).Text)
if err != nil {
return nil, err
}
s, ok := st.(*influxql.SelectStatement)
if !ok {
return nil, fmt.Errorf("influx: expected select statement")
}
t := make(parse.Tags, len(s.Dimensions))
for _, d := range s.Dimensions {
if _, ok := d.Expr.(*influxql.Call); ok {
continue
}
t[d.String()] = struct{}{}
}
return t, nil
}
示例6: ParseStatements
// ParseStatements takes a configFile and returns a slice of Statements
func ParseStatements(file string) ([]statement.Statement, error) {
seq := []statement.Statement{}
f, err := os.Open(file)
check(err)
s := NewScanner(f)
for {
t, l := s.Scan()
if t == EOF {
break
}
_, err := influxql.ParseStatement(l)
if err == nil {
seq = append(seq, &statement.InfluxqlStatement{Query: l, StatementID: stressql.RandStr(10)})
} else if t == BREAK {
continue
} else {
f := strings.NewReader(l)
p := stressql.NewParser(f)
s, err := p.Parse()
if err != nil {
return nil, err
}
seq = append(seq, s)
}
}
f.Close()
return seq, nil
}
示例7: TestRewriteStatement
//.........這裏部分代碼省略.........
{
stmt: `SHOW MEASUREMENTS`,
s: `SELECT _name AS "name" FROM _measurements`,
},
{
stmt: `SHOW MEASUREMENTS WITH MEASUREMENT = cpu`,
s: `SELECT _name AS "name" FROM _measurements WHERE _name = 'cpu'`,
},
{
stmt: `SHOW MEASUREMENTS WITH MEASUREMENT =~ /c.*/`,
s: `SELECT _name AS "name" FROM _measurements WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW MEASUREMENTS WHERE region = 'uswest'`,
s: `SELECT _name AS "name" FROM _measurements WHERE region = 'uswest'`,
},
{
stmt: `SHOW MEASUREMENTS WITH MEASUREMENT = cpu WHERE region = 'uswest'`,
s: `SELECT _name AS "name" FROM _measurements WHERE (_name = 'cpu') AND (region = 'uswest')`,
},
{
stmt: `SHOW SERIES`,
s: `SELECT "key" FROM _series`,
},
{
stmt: `SHOW SERIES FROM cpu`,
s: `SELECT "key" FROM _series WHERE _name = 'cpu'`,
},
{
stmt: `SHOW SERIES FROM mydb.myrp1.cpu`,
s: `SELECT "key" FROM mydb.myrp1._series WHERE _name = 'cpu'`,
},
{
stmt: `SHOW SERIES FROM mydb.myrp1./c.*/`,
s: `SELECT "key" FROM mydb.myrp1._series WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW TAG KEYS`,
s: `SELECT tagKey FROM _tagKeys`,
},
{
stmt: `SHOW TAG KEYS FROM cpu`,
s: `SELECT tagKey FROM _tagKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW TAG KEYS FROM /c.*/`,
s: `SELECT tagKey FROM _tagKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW TAG KEYS FROM cpu WHERE region = 'uswest'`,
s: `SELECT tagKey FROM _tagKeys WHERE (_name = 'cpu') AND (region = 'uswest')`,
},
{
stmt: `SHOW TAG KEYS FROM mydb.myrp1.cpu`,
s: `SELECT tagKey FROM mydb.myrp1._tagKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW TAG KEYS FROM mydb.myrp1./c.*/`,
s: `SELECT tagKey FROM mydb.myrp1._tagKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW TAG KEYS FROM mydb.myrp1.cpu WHERE region = 'uswest'`,
s: `SELECT tagKey FROM mydb.myrp1._tagKeys WHERE (_name = 'cpu') AND (region = 'uswest')`,
},
{
stmt: `SHOW TAG VALUES WITH KEY = region`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE _tagKey = 'region'`,
},
{
stmt: `SHOW TAG VALUES FROM cpu WITH KEY = region`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE (_name = 'cpu') AND (_tagKey = 'region')`,
},
{
stmt: `SHOW TAG VALUES FROM cpu WITH KEY IN (region, host)`,
s: `SELECT _tagKey AS "key", value FROM _tags WHERE (_name = 'cpu') AND (_tagKey = 'region' OR _tagKey = 'host')`,
},
{
stmt: `SHOW TAG VALUES FROM mydb.myrp1.cpu WITH KEY IN (region, host)`,
s: `SELECT _tagKey AS "key", value FROM mydb.myrp1._tags WHERE (_name = 'cpu') AND (_tagKey = 'region' OR _tagKey = 'host')`,
},
{
stmt: `SELECT value FROM cpu`,
s: `SELECT value FROM cpu`,
},
}
for _, test := range tests {
stmt, err := influxql.ParseStatement(test.stmt)
if err != nil {
t.Errorf("error parsing statement: %s", err)
} else {
stmt, err = influxql.RewriteStatement(stmt)
if err != nil {
t.Errorf("error rewriting statement: %s", err)
} else if s := stmt.String(); s != test.s {
t.Errorf("error rendering string. expected %s, actual: %s", test.s, s)
}
}
}
}
示例8: TestRewriteStatement
func TestRewriteStatement(t *testing.T) {
tests := []struct {
stmt string
s string
}{
{
stmt: `SHOW FIELD KEYS`,
s: `SELECT fieldKey, fieldType FROM _fieldKeys`,
},
{
stmt: `SHOW FIELD KEYS FROM cpu`,
s: `SELECT fieldKey, fieldType FROM _fieldKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW FIELD KEYS FROM /c.*/`,
s: `SELECT fieldKey, fieldType FROM _fieldKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW FIELD KEYS FROM mydb.myrp2.cpu`,
s: `SELECT fieldKey, fieldType FROM mydb.myrp2._fieldKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW FIELD KEYS FROM mydb.myrp2./c.*/`,
s: `SELECT fieldKey, fieldType FROM mydb.myrp2._fieldKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW SERIES`,
s: `SELECT "key" FROM _series`,
},
{
stmt: `SHOW SERIES FROM cpu`,
s: `SELECT "key" FROM _series WHERE _name = 'cpu'`,
},
{
stmt: `SHOW SERIES FROM mydb.myrp1.cpu`,
s: `SELECT "key" FROM mydb.myrp1._series WHERE _name = 'cpu'`,
},
{
stmt: `SHOW SERIES FROM mydb.myrp1./c.*/`,
s: `SELECT "key" FROM mydb.myrp1._series WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW TAG KEYS`,
s: `SELECT tagKey FROM _tagKeys`,
},
{
stmt: `SHOW TAG KEYS FROM cpu`,
s: `SELECT tagKey FROM _tagKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW TAG KEYS FROM /c.*/`,
s: `SELECT tagKey FROM _tagKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW TAG KEYS FROM cpu WHERE region = 'uswest'`,
s: `SELECT tagKey FROM _tagKeys WHERE (_name = 'cpu') AND (region = 'uswest')`,
},
{
stmt: `SHOW TAG KEYS FROM mydb.myrp1.cpu`,
s: `SELECT tagKey FROM mydb.myrp1._tagKeys WHERE _name = 'cpu'`,
},
{
stmt: `SHOW TAG KEYS FROM mydb.myrp1./c.*/`,
s: `SELECT tagKey FROM mydb.myrp1._tagKeys WHERE _name =~ /c.*/`,
},
{
stmt: `SHOW TAG KEYS FROM mydb.myrp1.cpu WHERE region = 'uswest'`,
s: `SELECT tagKey FROM mydb.myrp1._tagKeys WHERE (_name = 'cpu') AND (region = 'uswest')`,
},
{
stmt: `SELECT value FROM cpu`,
s: `SELECT value FROM cpu`,
},
}
for _, test := range tests {
stmt, err := influxql.ParseStatement(test.stmt)
if err != nil {
t.Errorf("error parsing statement: %s", err)
} else {
stmt, err = influxql.RewriteStatement(stmt)
if err != nil {
t.Errorf("error rewriting statement: %s", err)
} else if s := stmt.String(); s != test.s {
t.Errorf("error rendering string. expected %s, actual: %s", test.s, s)
}
}
}
}
示例9: influxQueryDuration
// influxQueryDuration adds time WHERE clauses to query for the given start and end durations.
func influxQueryDuration(now time.Time, query, start, end, groupByInterval string) (string, error) {
sd, err := opentsdb.ParseDuration(start)
if err != nil {
return "", err
}
ed, err := opentsdb.ParseDuration(end)
if end == "" {
ed = 0
} else if err != nil {
return "", err
}
st, err := influxql.ParseStatement(query)
if err != nil {
return "", err
}
s, ok := st.(*influxql.SelectStatement)
if !ok {
return "", fmt.Errorf("influx: expected select statement")
}
isTime := func(n influxql.Node) bool {
v, ok := n.(*influxql.VarRef)
if !ok {
return false
}
s := strings.ToLower(v.Val)
return s == "time"
}
influxql.WalkFunc(s.Condition, func(n influxql.Node) {
b, ok := n.(*influxql.BinaryExpr)
if !ok {
return
}
if isTime(b.LHS) || isTime(b.RHS) {
err = fmt.Errorf("influx query must not contain time in WHERE")
}
})
if err != nil {
return "", err
}
//Add New BinaryExpr for time clause
startExpr := &influxql.BinaryExpr{
Op: influxql.GTE,
LHS: &influxql.VarRef{Val: "time"},
RHS: &influxql.TimeLiteral{Val: now.Add(time.Duration(-sd))},
}
stopExpr := &influxql.BinaryExpr{
Op: influxql.LTE,
LHS: &influxql.VarRef{Val: "time"},
RHS: &influxql.TimeLiteral{Val: now.Add(time.Duration(-ed))},
}
if s.Condition != nil {
s.Condition = &influxql.BinaryExpr{
Op: influxql.AND,
LHS: s.Condition,
RHS: &influxql.BinaryExpr{
Op: influxql.AND,
LHS: startExpr,
RHS: stopExpr,
},
}
} else {
s.Condition = &influxql.BinaryExpr{
Op: influxql.AND,
LHS: startExpr,
RHS: stopExpr,
}
}
// parse last argument
if len(groupByInterval) > 0 {
gbi, err := time.ParseDuration(groupByInterval)
if err != nil {
return "", err
}
s.Dimensions = append(s.Dimensions,
&influxql.Dimension{Expr: &influxql.Call{
Name: "time",
Args: []influxql.Expr{&influxql.DurationLiteral{Val: gbi}},
},
})
}
// emtpy aggregate windows should be purged from the result
// this default resembles the opentsdb results.
if s.Fill == influxql.NullFill {
s.Fill = influxql.NoFill
s.FillValue = nil
}
return s.String(), nil
}
示例10: TestService_Open_LinkSubscriptions
//.........這裏部分代碼省略.........
i++
}
defaultConfigs := NewDefaultTestConfigs(clusterNames)
s, as, cs := NewTestService(defaultConfigs, "localhost", tc.useTokens)
// Define the active vars
var activeClusters map[string]clusterInfo
var tokens []string
var subChanges subChanged
var createSubs map[string]bool
var dropSubs map[string]bool
var grantedTokens map[tokenGrant]bool
var revokedTokens map[string]bool
// Setup functions
cs.QueryFunc = func(clusterName string, q influxcli.Query) (*influxcli.Response, error) {
log.Println("query:", q.Command)
switch {
case q.Command == "SHOW DATABASES":
dbs := make([][]interface{}, 0, len(activeClusters[clusterName].dbrps))
for db := range activeClusters[clusterName].dbrps {
dbs = append(dbs, []interface{}{db})
}
return &influxcli.Response{
Results: []influxcli.Result{{
Series: []models.Row{
{
Values: dbs,
},
},
}},
}, nil
case strings.HasPrefix(q.Command, "SHOW RETENTION POLICIES ON"):
stmt, _ := influxql.ParseStatement(q.Command)
if show, ok := stmt.(*influxql.ShowRetentionPoliciesStatement); ok {
var rps [][]interface{}
for _, rp := range activeClusters[clusterName].dbrps[show.Database] {
rps = append(rps, []interface{}{rp})
}
return &influxcli.Response{
Results: []influxcli.Result{{
Series: []models.Row{
{
Values: rps,
},
},
}},
}, nil
}
return nil, fmt.Errorf("invalid show rp query: %s", q.Command)
case q.Command == "SHOW SUBSCRIPTIONS":
result := influxcli.Result{}
for db, subs := range activeClusters[clusterName].subs {
series := models.Row{
Name: db,
Columns: []string{
"name",
"retention_policy",
"mode",
"destinations",
},
}
for _, rp := range subs {
var destinations []interface{}
switch {
case subChanges.NoDests: