本文整理汇总了Golang中github.com/youtube/vitess/go/vt/sqlparser.Parse函数的典型用法代码示例。如果您正苦于以下问题:Golang Parse函数的具体用法?Golang Parse怎么用?Golang Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Parse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: validateQuery
// Ensure that the input query is a Select statement that contains no Join,
// GroupBy, OrderBy, Limit or Distinct operations. Also ensure that the
// source table is present in the schema and has at least one primary key.
func (qs *QuerySplitter) validateQuery() error {
statement, err := sqlparser.Parse(qs.query.Sql)
if err != nil {
return err
}
var ok bool
qs.sel, ok = statement.(*sqlparser.Select)
if !ok {
return fmt.Errorf("not a select statement")
}
if qs.sel.Distinct != "" || qs.sel.GroupBy != nil ||
qs.sel.Having != nil || len(qs.sel.From) != 1 ||
qs.sel.OrderBy != nil || qs.sel.Limit != nil ||
qs.sel.Lock != "" {
return fmt.Errorf("unsupported query")
}
node, ok := qs.sel.From[0].(*sqlparser.AliasedTableExpr)
if !ok {
return fmt.Errorf("unsupported query")
}
qs.tableName = sqlparser.GetTableName(node.Expr)
if qs.tableName == "" {
return fmt.Errorf("not a simple table expression")
}
tableInfo, ok := qs.schemaInfo.tables[qs.tableName]
if !ok {
return fmt.Errorf("can't find table in schema")
}
if len(tableInfo.PKColumns) == 0 {
return fmt.Errorf("no primary keys")
}
qs.pkCol = tableInfo.GetPKColumn(0).Name
return nil
}
开发者ID:nangong92t,项目名称:go_src,代码行数:37,代码来源:query_splitter.go
示例2: GetStreamExecPlan
// GetStreamExecPlan generates a ExecPlan given a sql query and a TableGetter.
func GetStreamExecPlan(sql string, getTable TableGetter) (plan *ExecPlan, err error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
plan = &ExecPlan{
PlanID: PlanSelectStream,
FullQuery: GenerateFullQuery(statement),
}
switch stmt := statement.(type) {
case *sqlparser.Select:
if stmt.Lock != "" {
return nil, errors.New("select with lock not allowed for streaming")
}
tableName, _ := analyzeFrom(stmt.From)
// This will block usage of NEXTVAL.
if tableName == "dual" {
return nil, errors.New("select from dual not allowed for streaming")
}
if tableName != "" {
plan.setTableInfo(tableName, getTable)
}
case *sqlparser.Union:
// pass
default:
return nil, fmt.Errorf("'%v' not allowed for streaming", sqlparser.String(stmt))
}
return plan, nil
}
开发者ID:aaijazi,项目名称:vitess,代码行数:34,代码来源:plan.go
示例3: BuildPlan
func BuildPlan(query string, schema *Schema) *Plan {
statement, err := sqlparser.Parse(query)
if err != nil {
return &Plan{
ID: NoPlan,
Reason: err.Error(),
Original: query,
}
}
noplan := &Plan{
ID: NoPlan,
Reason: "too complex",
Original: query,
}
var plan *Plan
switch statement := statement.(type) {
case *sqlparser.Select:
plan = buildSelectPlan(statement, schema)
case *sqlparser.Insert:
plan = buildInsertPlan(statement, schema)
case *sqlparser.Update:
plan = buildUpdatePlan(statement, schema)
case *sqlparser.Delete:
plan = buildDeletePlan(statement, schema)
case *sqlparser.Union, *sqlparser.Set, *sqlparser.DDL, *sqlparser.Other:
return noplan
default:
panic("unexpected")
}
plan.Original = query
return plan
}
开发者ID:plobsing,项目名称:vitess,代码行数:32,代码来源:plan.go
示例4: Build
// Build builds a plan for a query based on the specified vschema.
// It's the main entry point for this package.
func Build(query string, vschema VSchema) (*engine.Plan, error) {
statement, err := sqlparser.Parse(query)
if err != nil {
return nil, err
}
plan := &engine.Plan{
Original: query,
}
switch statement := statement.(type) {
case *sqlparser.Select:
plan.Instructions, err = buildSelectPlan(statement, vschema)
case *sqlparser.Insert:
plan.Instructions, err = buildInsertPlan(statement, vschema)
case *sqlparser.Update:
plan.Instructions, err = buildUpdatePlan(statement, vschema)
case *sqlparser.Delete:
plan.Instructions, err = buildDeletePlan(statement, vschema)
case *sqlparser.Union, *sqlparser.Set, *sqlparser.DDL, *sqlparser.Other:
return nil, errors.New("unsupported construct")
default:
panic("unexpected statement type")
}
if err != nil {
return nil, err
}
return plan, nil
}
开发者ID:CowLeo,项目名称:vitess,代码行数:29,代码来源:builder.go
示例5: newSplitParams
// newSplitParams validates and initializes all the fields except splitCount and
// numRowsPerQueryPart. It contains the common code for the constructors above.
func newSplitParams(sql string, bindVariables map[string]interface{}, splitColumns []string,
schema map[string]*schema.Table) (*SplitParams, error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, fmt.Errorf("splitquery: failed parsing query: '%v', err: '%v'", sql, err)
}
selectAST, ok := statement.(*sqlparser.Select)
if !ok {
return nil, fmt.Errorf("splitquery: not a select statement")
}
if selectAST.Distinct != "" || selectAST.GroupBy != nil ||
selectAST.Having != nil || len(selectAST.From) != 1 ||
selectAST.OrderBy != nil || selectAST.Limit != nil ||
selectAST.Lock != "" {
return nil, fmt.Errorf("splitquery: unsupported query: %v", sql)
}
var aliasedTableExpr *sqlparser.AliasedTableExpr
aliasedTableExpr, ok = selectAST.From[0].(*sqlparser.AliasedTableExpr)
if !ok {
return nil, fmt.Errorf("splitquery: unsupported FROM clause in query: %v", sql)
}
tableName := sqlparser.GetTableName(aliasedTableExpr.Expr)
if tableName == "" {
return nil, fmt.Errorf("splitquery: unsupported FROM clause in query"+
" (must be a simple table expression): %v", sql)
}
tableSchema, ok := schema[tableName]
if tableSchema == nil {
return nil, fmt.Errorf("splitquery: can't find table in schema")
}
if len(splitColumns) == 0 {
splitColumns = getPrimaryKeyColumns(tableSchema)
if len(splitColumns) == 0 {
panic(fmt.Sprintf("getPrimaryKeyColumns() returned an empty slice. %+v", tableSchema))
}
}
if !areColumnsAPrefixOfAnIndex(splitColumns, tableSchema) {
return nil, fmt.Errorf("splitquery: split-columns must be a prefix of the columns composing"+
" an index. Sql: %v, split-columns: %v", sql, splitColumns)
}
// Get the split-columns types.
splitColumnTypes := make([]querypb.Type, 0, len(splitColumns))
for _, splitColumn := range splitColumns {
i := tableSchema.FindColumn(splitColumn)
if i == -1 {
return nil, fmt.Errorf("can't find split-column: %v", splitColumn)
}
splitColumnTypes = append(splitColumnTypes, tableSchema.Columns[i].Type)
}
return &SplitParams{
sql: sql,
bindVariables: bindVariables,
splitColumns: splitColumns,
splitColumnTypes: splitColumnTypes,
selectAST: selectAST,
splitTableSchema: tableSchema,
}, nil
}
开发者ID:aaijazi,项目名称:vitess,代码行数:62,代码来源:split_params.go
示例6: GetStreamExecPlan
// GetStreamExecPlan generates a ExecPlan given a sql query and a TableGetter.
func GetStreamExecPlan(sql string, getTable TableGetter) (plan *ExecPlan, err error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
plan = &ExecPlan{
PlanId: PLAN_SELECT_STREAM,
FullQuery: GenerateFullQuery(statement),
}
switch stmt := statement.(type) {
case *sqlparser.Select:
if stmt.Lock != "" {
return nil, errors.New("select with lock disallowed with streaming")
}
tableName, _ := analyzeFrom(stmt.From)
if tableName != "" {
plan.setTableInfo(tableName, getTable)
}
case *sqlparser.Union:
// pass
default:
return nil, fmt.Errorf("'%v' not allowed for streaming", sqlparser.String(stmt))
}
return plan, nil
}
开发者ID:pranjal5215,项目名称:vitess,代码行数:30,代码来源:plan.go
示例7: buildPlan
func buildPlan(sql string) (plan *RoutingPlan, err error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
return getRoutingPlan(statement)
}
开发者ID:chinna1986,项目名称:vitess,代码行数:7,代码来源:router.go
示例8: TestSelect2
func TestSelect2(t *testing.T) {
schema, err := LoadFile(locateFile("schema_test.json"))
if err != nil {
t.Fatal(err)
}
for tcase := range iterateExecFile("select2_cases.txt") {
statement, err := sqlparser.Parse(tcase.input)
if err != nil {
t.Error(err)
continue
}
sel, ok := statement.(*sqlparser.Select)
if !ok {
t.Errorf("unexpected type: %T", statement)
continue
}
plan, _, err := buildSelectPlan2(sel, schema)
if err != nil {
t.Error(err)
continue
}
bout, err := json.Marshal(plan)
if err != nil {
panic(fmt.Sprintf("Error marshalling %v: %v", plan, err))
}
out := string(bout)
if out != tcase.output {
t.Errorf("Line:%v\n%s\n%s", tcase.lineno, tcase.output, out)
}
// Comment these line out to see the expected outputs
// bout, err = json.MarshalIndent(plan, "", " ")
// fmt.Printf("%s\n", bout)
}
}
开发者ID:Rastusik,项目名称:vitess,代码行数:34,代码来源:select2_test.go
示例9: GetExecPlan
// GetExecPlan generates a ExecPlan given a sql query and a TableGetter.
func GetExecPlan(sql string, getTable TableGetter) (plan *ExecPlan, err error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
plan, err = analyzeSQL(statement, getTable)
if err != nil {
return nil, err
}
if plan.PlanId == PLAN_PASS_DML {
log.Warningf("PASS_DML: %s", sql)
}
return plan, nil
}
开发者ID:pranjal5215,项目名称:vitess,代码行数:15,代码来源:plan.go
示例10: parseDDLs
func parseDDLs(sqls []string) ([]*sqlparser.DDL, error) {
parsedDDLs := make([]*sqlparser.DDL, len(sqls))
for i, sql := range sqls {
stat, err := sqlparser.Parse(sql)
if err != nil {
return nil, fmt.Errorf("failed to parse sql: %s, got error: %v", sql, err)
}
ddl, ok := stat.(*sqlparser.DDL)
if !ok {
return nil, fmt.Errorf("schema change works for DDLs only, but get non DDL statement: %s", sql)
}
parsedDDLs[i] = ddl
}
return parsedDDLs, nil
}
开发者ID:erzel,项目名称:vitess,代码行数:15,代码来源:tablet_executor.go
示例11: DDLParse
func DDLParse(sql string) (plan *DDLPlan) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return &DDLPlan{Action: ""}
}
stmt, ok := statement.(*sqlparser.DDL)
if !ok {
return &DDLPlan{Action: ""}
}
return &DDLPlan{
Action: stmt.Action,
TableName: string(stmt.Table),
NewName: string(stmt.NewName),
}
}
开发者ID:chinna1986,项目名称:vitess,代码行数:15,代码来源:ddl.go
示例12: analyze
func analyze(line []byte) {
for _, ignore := range ignores {
if bytes.HasPrefix(line, ignore) {
return
}
}
dml := string(bytes.TrimRight(line, "\n"))
ast, err := sqlparser.Parse(dml)
if err != nil {
log.Errorf("Error parsing %s", dml)
return
}
bindIndex = 0
buf := sqlparser.NewTrackedBuffer(FormatWithBind)
buf.Myprintf("%v", ast)
addQuery(buf.ParsedQuery().Query)
}
开发者ID:chinna1986,项目名称:vitess,代码行数:17,代码来源:query_analyzer.go
示例13: TestSplitParams
func TestSplitParams(t *testing.T) {
for _, testCase := range splitParamsTestCases {
var splitParams *SplitParams
var err error
if testCase.NumRowsPerQueryPart != 0 {
splitParams, err = NewSplitParamsGivenNumRowsPerQueryPart(
testCase.SQL,
testCase.BindVariables,
testCase.SplitColumnNames,
testCase.NumRowsPerQueryPart,
testSchema)
} else {
splitParams, err = NewSplitParamsGivenSplitCount(
testCase.SQL,
testCase.BindVariables,
testCase.SplitColumnNames,
testCase.SplitCount,
testSchema)
}
if testCase.ExpectedErrorRegex != nil {
if !testCase.ExpectedErrorRegex.MatchString(err.Error()) {
t.Errorf("Testcase: %+v, want: %+v, got: %+v", testCase, testCase.ExpectedErrorRegex, err)
}
continue
}
// Here, we don't expect an error.
if err != nil {
t.Errorf("TestCase: %+v, want: %+v, got: %+v", testCase, nil, err)
continue
}
if splitParams == nil {
t.Errorf("TestCase: %+v, got nil splitParams", testCase)
continue
}
expectedSplitParams := testCase.ExpectedSplitParams
// We don't require testCaset.ExpectedSplitParams to specify common expected fields like 'sql',
// so we compute them here and store them in 'expectedSplitParams'.
expectedSplitParams.sql = testCase.SQL
expectedSplitParams.bindVariables = testCase.BindVariables
statement, _ := sqlparser.Parse(testCase.SQL)
expectedSplitParams.selectAST = statement.(*sqlparser.Select)
if !reflect.DeepEqual(&expectedSplitParams, splitParams) {
t.Errorf("TestCase: %+v, want: %+v, got: %+v", testCase, expectedSplitParams, *splitParams)
}
}
}
开发者ID:CowLeo,项目名称:vitess,代码行数:46,代码来源:split_params_test.go
示例14: Validate
// Validate validates a list of sql statements
func (exec *TabletExecutor) Validate(ctx context.Context, sqls []string) error {
if exec.isClosed {
return fmt.Errorf("executor is closed")
}
parsedDDLs := make([]*sqlparser.DDL, len(sqls))
for i, sql := range sqls {
stat, err := sqlparser.Parse(sql)
if err != nil {
return fmt.Errorf("failed to parse sql: %s, got error: %v", sql, err)
}
ddl, ok := stat.(*sqlparser.DDL)
if !ok {
return fmt.Errorf("schema change works for DDLs only, but get non DDL statement: %s", sql)
}
parsedDDLs[i] = ddl
}
return exec.detectBigSchemaChanges(ctx, parsedDDLs)
}
开发者ID:pranjal5215,项目名称:vitess,代码行数:19,代码来源:tablet_executor.go
示例15: InvalidateForUnrecognized
// InvalidateForUnrecognized performs best effort rowcache invalidation
// for unrecognized statements.
func (qe *QueryEngine) InvalidateForUnrecognized(sql string) {
statement, err := sqlparser.Parse(sql)
if err != nil {
log.Errorf("Error: %v: %s", err, sql)
internalErrors.Add("Invalidation", 1)
return
}
var table *sqlparser.TableName
switch stmt := statement.(type) {
case *sqlparser.Insert:
// Inserts don't affect rowcache.
return
case *sqlparser.Update:
table = stmt.Table
case *sqlparser.Delete:
table = stmt.Table
default:
log.Errorf("Unrecognized: %s", sql)
internalErrors.Add("Invalidation", 1)
return
}
// Ignore cross-db statements.
if table.Qualifier != nil && string(table.Qualifier) != qe.dbconfig.DbName {
return
}
// Ignore if it's an uncached table.
tableName := string(table.Name)
tableInfo := qe.schemaInfo.GetTable(tableName)
if tableInfo == nil {
log.Errorf("Table %s not found: %s", tableName, sql)
internalErrors.Add("Invalidation", 1)
return
}
if tableInfo.CacheType == schema.CACHE_NONE {
return
}
// Treat the statement as a DDL.
// It will conservatively invalidate all rows of the table.
log.Warningf("Treating '%s' as DDL for table %s", sql, tableName)
qe.schemaInfo.CreateOrUpdateTable(tableName)
}
开发者ID:chinna1986,项目名称:vitess,代码行数:46,代码来源:query_engine.go
示例16: validateQuery
// Ensure that the input query is a Select statement that contains no Join,
// GroupBy, OrderBy, Limit or Distinct operations. Also ensure that the
// source table is present in the schema and has at least one primary key.
func (qs *QuerySplitter) validateQuery() error {
statement, err := sqlparser.Parse(qs.sql)
if err != nil {
return err
}
var ok bool
qs.sel, ok = statement.(*sqlparser.Select)
if !ok {
return fmt.Errorf("not a select statement")
}
if qs.sel.Distinct != "" || qs.sel.GroupBy != nil ||
qs.sel.Having != nil || len(qs.sel.From) != 1 ||
qs.sel.OrderBy != nil || qs.sel.Limit != nil ||
qs.sel.Lock != "" {
return fmt.Errorf("unsupported query")
}
node, ok := qs.sel.From[0].(*sqlparser.AliasedTableExpr)
if !ok {
return fmt.Errorf("unsupported query")
}
qs.tableName = sqlparser.GetTableName(node.Expr)
if qs.tableName == "" {
return fmt.Errorf("not a simple table expression")
}
tableInfo, ok := qs.schemaInfo.tables[qs.tableName]
if !ok {
return fmt.Errorf("can't find table in schema")
}
if len(tableInfo.PKColumns) == 0 {
return fmt.Errorf("no primary keys")
}
if qs.splitColumn != "" {
for _, index := range tableInfo.Indexes {
for _, column := range index.Columns {
if qs.splitColumn == column {
return nil
}
}
}
return fmt.Errorf("split column is not indexed or does not exist in table schema, SplitColumn: %s, TableInfo.Table: %v", qs.splitColumn, tableInfo.Table)
}
qs.splitColumn = tableInfo.GetPKColumn(0).Name
return nil
}
开发者ID:littleyang,项目名称:vitess,代码行数:47,代码来源:query_splitter.go
示例17: handleUnrecognizedEvent
func (rci *RowcacheInvalidator) handleUnrecognizedEvent(sql string) {
statement, err := sqlparser.Parse(sql)
if err != nil {
log.Errorf("Error: %v: %s", err, sql)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
return
}
var table *sqlparser.TableName
switch stmt := statement.(type) {
case *sqlparser.Insert:
// Inserts don't affect rowcache.
return
case *sqlparser.Update:
table = stmt.Table
case *sqlparser.Delete:
table = stmt.Table
default:
log.Errorf("Unrecognized: %s", sql)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
return
}
// Ignore cross-db statements.
if table.Qualifier != "" && string(table.Qualifier) != rci.qe.dbconfigs.App.DbName {
return
}
// Ignore if it's an uncached table.
tableName := string(table.Name)
tableInfo := rci.qe.schemaInfo.GetTable(tableName)
if tableInfo == nil {
log.Errorf("Table %s not found: %s", tableName, sql)
rci.qe.queryServiceStats.InternalErrors.Add("Invalidation", 1)
return
}
if tableInfo.CacheType == schema.CacheNone {
return
}
// Treat the statement as a DDL.
// It will conservatively invalidate all rows of the table.
log.Warningf("Treating '%s' as DDL for table %s", sql, tableName)
rci.qe.schemaInfo.CreateOrUpdateTable(context.Background(), tableName)
}
开发者ID:tjyang,项目名称:vitess,代码行数:44,代码来源:rowcache_invalidator.go
示例18: SQLExecute
// SQLExecute is part of the SQLExecuter interface.
func (se *splitQuerySQLExecuter) SQLExecute(
sql string, bindVariables map[string]interface{},
) (*sqltypes.Result, error) {
// We need to parse the query since we're dealing with bind-vars.
// TODO(erez): Add an SQLExecute() to SQLExecuterInterface that gets a parsed query so that
// we don't have to parse the query again here.
ast, err := sqlparser.Parse(sql)
if err != nil {
return nil, fmt.Errorf("splitQuerySQLExecuter: parsing sql failed with: %v", err)
}
parsedQuery := sqlparser.GenerateParsedQuery(ast)
// We clone "bindVariables" since fullFetch() changes it.
return se.queryExecutor.fullFetch(
se.conn,
parsedQuery,
utils.CloneBindVariables(bindVariables),
nil /* buildStreamComment */)
}
开发者ID:jmptrader,项目名称:vitess,代码行数:20,代码来源:tabletserver.go
示例19: Validate
// Validate validates a list of sql statements.
func (exec *TabletExecutor) Validate(ctx context.Context, sqls []string) error {
if exec.isClosed {
return fmt.Errorf("executor is closed")
}
parsedDDLs := make([]*sqlparser.DDL, len(sqls))
for i, sql := range sqls {
stat, err := sqlparser.Parse(sql)
if err != nil {
return fmt.Errorf("failed to parse sql: %s, got error: %v", sql, err)
}
ddl, ok := stat.(*sqlparser.DDL)
if !ok {
return fmt.Errorf("schema change works for DDLs only, but get non DDL statement: %s", sql)
}
parsedDDLs[i] = ddl
}
bigSchemaChange, err := exec.detectBigSchemaChanges(ctx, parsedDDLs)
if bigSchemaChange && exec.allowBigSchemaChange {
log.Warning("Processing big schema change. This may cause visible MySQL downtime.")
return nil
}
return err
}
开发者ID:littleyang,项目名称:vitess,代码行数:24,代码来源:tablet_executor.go
示例20: Normalize
func Normalize(sql string) (string, map[string]interface{}, error) {
if sql == "" || sql[0] == '#' || sql[0] == '/' {
return "", nil, nil
} else {
lstr := strings.ToLower(sql)
if strings.HasPrefix(lstr, "begin") {
return "begin", nil, nil
}
if strings.HasPrefix(lstr, "commit") {
return "commit", nil, nil
}
if strings.HasPrefix(lstr, "rollback") {
return "rollback", nil, nil
}
if strings.HasPrefix(lstr, "use") {
return "", nil, nil
}
}
tree, err := sqlparser.Parse(sql)
if err != nil {
return "", nil, err
}
return NormalizeTree(tree)
}
开发者ID:johnvilsack,项目名称:golang-stuff,代码行数:24,代码来源:normalizer.go
注:本文中的github.com/youtube/vitess/go/vt/sqlparser.Parse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论