当前位置: 首页>>代码示例>>Golang>>正文


Golang parser.Parse函数代码示例

本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/parser.Parse函数的典型用法代码示例。如果您正苦于以下问题:Golang Parse函数的具体用法?Golang Parse怎么用?Golang Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: exec

// Send forwards the call for further processing.
func (s *Server) exec(call sqlwire.Call) {
	req := call.Args
	resp := call.Reply
	stmt, err := parser.Parse(req.Sql)
	if err != nil {
		resp.SetGoError(err)
		return
	}
	switch p := stmt.(type) {
	case *parser.CreateDatabase:
		s.CreateDatabase(p, req.Params, resp)
	case *parser.CreateTable:
		s.CreateTable(p, req.Params, resp)
	case *parser.Delete:
		s.Delete(p, req.Params, resp)
	case *parser.Insert:
		s.Insert(p, req.Params, resp)
	case *parser.Select:
		s.Select(p, req.Params, resp)
	case *parser.ShowColumns:
		s.ShowColumns(p, req.Params, resp)
	case *parser.ShowDatabases:
		s.ShowDatabases(p, req.Params, resp)
	case *parser.ShowIndex:
		s.ShowIndex(p, req.Params, resp)
	case *parser.ShowTables:
		s.ShowTables(p, req.Params, resp)
	case *parser.Update:
		s.Update(p, req.Params, resp)
	case *parser.Use:
		s.Use(p, req.Params, resp)
	default:
		resp.SetGoError(fmt.Errorf("unknown statement type: %T", stmt))
	}
}
开发者ID:Gardenya,项目名称:cockroach,代码行数:36,代码来源:server.go

示例2: Exec

func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
	stmt, err := parser.Parse(query)
	if err != nil {
		return nil, err
	}
	return c.exec(stmt, args)
}
开发者ID:Jaekyun,项目名称:cockroach,代码行数:7,代码来源:conn.go

示例3: Prepare

func (c *conn) Prepare(query string) (driver.Stmt, error) {
	s, err := parser.Parse(query)
	if err != nil {
		return nil, err
	}
	return &stmt{conn: c, stmt: s}, nil
}
开发者ID:Jaekyun,项目名称:cockroach,代码行数:7,代码来源:conn.go

示例4: Query

func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
	stmt, err := parser.Parse(query)
	if err != nil {
		return nil, err
	}
	return c.query(stmt, args)
}
开发者ID:Jaekyun,项目名称:cockroach,代码行数:7,代码来源:conn.go

示例5: exec

// exec executes the request. Any error encountered is returned; it is
// the caller's responsibility to update the response.
func (s *Server) exec(req driver.Request) (driver.Response, error) {
	var resp driver.Response

	// Pick up current session state.
	planner := planner{db: s.db}
	if req.Session != nil {
		// TODO(tschottdorf) will have to validate the Session information (for
		// instance, whether access to the stored database is permitted).
		if err := gogoproto.Unmarshal(req.Session, &planner.session); err != nil {
			return resp, err
		}
	}
	stmts, err := parser.Parse(req.Sql)
	if err != nil {
		return resp, err
	}
	for _, stmt := range stmts {
		// Bind all the placeholder variables in the stmt to actual values.
		if err := parser.FillArgs(stmt, parameters(req.Params)); err != nil {
			return resp, err
		}
		var plan planNode
		if plan, err = planner.makePlan(stmt); err != nil {
			return resp, err
		}

		result := driver.Result{
			Columns: plan.Columns(),
		}
		for plan.Next() {
			values := plan.Values()
			row := driver.Result_Row{}
			row.Values = make([]driver.Datum, 0, len(values))
			for _, val := range values {
				switch vt := val.(type) {
				case parser.DBool:
					row.Values = append(row.Values, driver.Datum{BoolVal: (*bool)(&vt)})
				case parser.DInt:
					row.Values = append(row.Values, driver.Datum{IntVal: (*int64)(&vt)})
				case parser.DFloat:
					row.Values = append(row.Values, driver.Datum{FloatVal: (*float64)(&vt)})
				case parser.DString:
					row.Values = append(row.Values, driver.Datum{StringVal: (*string)(&vt)})
				case parser.DNull:
					row.Values = append(row.Values, driver.Datum{})
				default:
					return resp, util.Errorf("unsupported datum: %T", val)
				}
			}
			result.Rows = append(result.Rows, row)
		}
		resp.Results = append(resp.Results, result)
	}

	// Update session state.
	resp.Session, err = gogoproto.Marshal(&planner.session)
	return resp, err
}
开发者ID:arypurnomoz,项目名称:cockroach,代码行数:60,代码来源:server.go

示例6: fuzzSingle

func fuzzSingle(stmt parser.Statement) (interestingness int) {
	var lastExpr parser.Expr
	rcvr := func() {
		if r := recover(); r != nil {
			if !expected(fmt.Sprintf("%v", r)) {
				fmt.Printf("Stmt: %s\n%s", stmt, spew.Sdump(stmt))
				if lastExpr != nil {
					fmt.Printf("Expr: %s", spew.Sdump(lastExpr))
				}
				panic(r)
			}
			// Anything that has expected errors in it is fine, but not as
			// interesting as things that go through.
			interestingness = 1
		}
	}
	defer rcvr()

	data0 := stmt.String()
	// TODO(tschottdorf): again, this is since we're ignoring stuff in the
	// grammar instead of erroring out on unsupported language. See:
	// https://github.com/cockroachdb/cockroach/issues/1949
	if strings.Contains(data0, "%!s(<nil>)") {
		return 0
	}
	stmt1, err := parser.Parse(data0)
	if err != nil {
		fmt.Printf("AST: %s", spew.Sdump(stmt))
		fmt.Printf("data0: %q\n", data0)
		panic(err)
	}
	interestingness = 2

	data1 := stmt1.String()
	// TODO(tschottdorf): due to the ignoring issue again.
	// if !reflect.DeepEqual(stmt, stmt1) {
	if data1 != data0 {
		fmt.Printf("data0: %q\n", data0)
		fmt.Printf("AST: %s", spew.Sdump(stmt))
		fmt.Printf("data1: %q\n", data1)
		fmt.Printf("AST: %s", spew.Sdump(stmt1))
		panic("not equal")
	}

	var v visitorFunc = func(e parser.Expr) parser.Expr {
		lastExpr = e
		if _, err := parser.EvalExpr(e); err != nil {
			panic(err)
		}
		return e
	}
	parser.WalkStmt(v, stmt)
	return
}
开发者ID:cockroachdb,项目名称:go-fuzz,代码行数:54,代码来源:main.go

示例7: exec

// Send forwards the call for further processing.
func (s *Server) exec(call sqlwire.Call) {
	req := call.Args
	resp := call.Reply
	// Pick up current session state.
	var session Session
	if req.Session != nil {
		if err := gogoproto.Unmarshal(req.Session, &session); err != nil {
			resp.SetGoError(err)
			return
		}
	}
	stmt, err := parser.Parse(req.Sql)
	if err != nil {
		resp.SetGoError(err)
		return
	}
	switch p := stmt.(type) {
	case *parser.CreateDatabase:
		s.CreateDatabase(&session, p, req.Params, resp)
	case *parser.CreateTable:
		s.CreateTable(&session, p, req.Params, resp)
	case *parser.Delete:
		s.Delete(&session, p, req.Params, resp)
	case *parser.Insert:
		s.Insert(&session, p, req.Params, resp)
	case *parser.Select:
		s.Select(&session, p, req.Params, resp)
	case *parser.ShowColumns:
		s.ShowColumns(&session, p, req.Params, resp)
	case *parser.ShowDatabases:
		s.ShowDatabases(&session, p, req.Params, resp)
	case *parser.ShowIndex:
		s.ShowIndex(&session, p, req.Params, resp)
	case *parser.ShowTables:
		s.ShowTables(&session, p, req.Params, resp)
	case *parser.Update:
		s.Update(&session, p, req.Params, resp)
	case *parser.Use:
		s.Use(&session, p, req.Params, resp)
	default:
		resp.SetGoError(fmt.Errorf("unknown statement type: %T", stmt))
	}
	// Update session state.
	if resp.Error != nil {
		return
	}
	payload, err := gogoproto.Marshal(&session)
	if err != nil {
		resp.SetGoError(util.Errorf("unable to marshal %+v to protobuf: %s", session, err))
		return
	}
	resp.Session = payload
}
开发者ID:greener98103,项目名称:cockroach,代码行数:54,代码来源:server.go

示例8: exec

// Send forwards the call for further processing.
func (s *Server) exec(call sqlwire.Call) error {
	req := call.Args
	resp := call.Reply
	// Pick up current session state.
	var session Session
	if req.Session != nil {
		// TODO(tschottdorf) will have to validate the Session information (for
		// instance, whether access to the stored database is permitted).
		if err := gogoproto.Unmarshal(req.Session, &session); err != nil {
			return err
		}
	}
	stmts, err := parser.Parse(req.Sql)
	if err != nil {
		return err
	}
	for _, stmt := range stmts {
		switch p := stmt.(type) {
		case *parser.CreateDatabase:
			err = s.CreateDatabase(&session, p, req.Params, resp)
		case *parser.CreateTable:
			err = s.CreateTable(&session, p, req.Params, resp)
		case *parser.Delete:
			err = s.Delete(&session, p, req.Params, resp)
		case *parser.Insert:
			err = s.Insert(&session, p, req.Params, resp)
		case *parser.Select:
			err = s.Select(&session, p, req.Params, resp)
		case *parser.Set:
			err = s.Set(&session, p, req.Params, resp)
		case *parser.ShowColumns:
			err = s.ShowColumns(&session, p, req.Params, resp)
		case *parser.ShowDatabases:
			err = s.ShowDatabases(&session, p, req.Params, resp)
		case *parser.ShowIndex:
			err = s.ShowIndex(&session, p, req.Params, resp)
		case *parser.ShowTables:
			err = s.ShowTables(&session, p, req.Params, resp)
		case *parser.Update:
			err = s.Update(&session, p, req.Params, resp)
		default:
			err = fmt.Errorf("unknown statement type: %T", stmt)
		}
		if err != nil {
			return err
		}
	}

	// Update session state.
	resp.Session, err = gogoproto.Marshal(&session)
	return err
}
开发者ID:zmoon111,项目名称:cockroach,代码行数:53,代码来源:server.go

示例9: Fuzz

// Fuzz is the entry point for go-fuzz. Run it via
//	  go-fuzz-build github.com/cockroachdb/go-fuzz/examples/parser && \
//    go-fuzz -bin=./parser-fuzz.zip -workdir=.
func Fuzz(data []byte) (interestingness int) {
	sql := string(data)
	stmts, err := parser.Parse(sql)
	if err != nil || stmts == nil {
		if stmts != nil {
			panic("stmt is not nil on error")
		}
		return
	}
	for _, stmt := range stmts {
		interestingness = fuzzSingle(stmt)
	}
	return
}
开发者ID:sjn1978,项目名称:go-fuzz,代码行数:17,代码来源:main.go

示例10: TestPrimaryKeyUnspecified

func TestPrimaryKeyUnspecified(t *testing.T) {
	defer leaktest.AfterTest(t)
	stmt, err := parser.Parse("CREATE TABLE test (a INT, b INT, CONSTRAINT c UNIQUE (b))")
	if err != nil {
		t.Fatal(err)
	}
	desc, err := makeTableDesc(stmt[0].(*parser.CreateTable))
	if err != nil {
		t.Fatal(err)
	}
	if err := desc.AllocateIDs(); err != structured.ErrMissingPrimaryKey {
		t.Fatal(err)
	}
}
开发者ID:ErikGrimes,项目名称:cockroach,代码行数:14,代码来源:table_test.go

示例11: exec

// Send forwards the call for further processing.
func (s *Server) exec(call sqlwire.Call) error {
	req := call.Args
	resp := call.Reply
	// Pick up current session state.
	var session Session
	if req.Session != nil {
		if err := gogoproto.Unmarshal(req.Session, &session); err != nil {
			return err
		}
	}
	stmt, err := parser.Parse(req.Sql)
	if err != nil {
		return err
	}
	switch p := stmt.(type) {
	case *parser.CreateDatabase:
		err = s.CreateDatabase(&session, p, req.Params, resp)
	case *parser.CreateTable:
		err = s.CreateTable(&session, p, req.Params, resp)
	case *parser.Delete:
		err = s.Delete(&session, p, req.Params, resp)
	case *parser.Insert:
		err = s.Insert(&session, p, req.Params, resp)
	case *parser.Select:
		err = s.Select(&session, p, req.Params, resp)
	case *parser.ShowColumns:
		err = s.ShowColumns(&session, p, req.Params, resp)
	case *parser.ShowDatabases:
		err = s.ShowDatabases(&session, p, req.Params, resp)
	case *parser.ShowIndex:
		err = s.ShowIndex(&session, p, req.Params, resp)
	case *parser.ShowTables:
		err = s.ShowTables(&session, p, req.Params, resp)
	case *parser.Update:
		err = s.Update(&session, p, req.Params, resp)
	case *parser.Use:
		s.Use(&session, p, req.Params, resp)
	default:
		err = fmt.Errorf("unknown statement type: %T", stmt)
	}
	if err != nil {
		return err
	}

	// Update session state.
	resp.Session, err = gogoproto.Marshal(&session)
	return err
}
开发者ID:backend2use,项目名称:cockroachdb,代码行数:49,代码来源:server.go

示例12: execStmts

// exec executes the request. Any error encountered is returned; it is
// the caller's responsibility to update the response.
func (s server) execStmts(sql string, params parameters, planMaker *planner) driver.Response {
	var resp driver.Response
	stmts, err := parser.Parse(sql, parser.Syntax(planMaker.session.Syntax))
	if err != nil {
		// A parse error occured: we can't determine if there were multiple
		// statements or only one, so just pretend there was one.
		resp.Results = append(resp.Results, rollbackTxnAndReturnResultWithError(planMaker, err))
		return resp
	}
	for _, stmt := range stmts {
		result, err := s.execStmt(stmt, params, planMaker)
		if err != nil {
			result = rollbackTxnAndReturnResultWithError(planMaker, err)
		}
		resp.Results = append(resp.Results, result)
	}
	return resp
}
开发者ID:slavau,项目名称:cockroach,代码行数:20,代码来源:server.go

示例13: TestPrimaryKeyUnspecified

func TestPrimaryKeyUnspecified(t *testing.T) {
	defer leaktest.AfterTest(t)
	stmt, err := parser.Parse("CREATE TABLE foo.test (a INT, b INT, CONSTRAINT c UNIQUE (b))")
	if err != nil {
		t.Fatal(err)
	}
	create := stmt[0].(*parser.CreateTable)
	if err := create.Table.NormalizeTableName(""); err != nil {
		t.Fatal(err)
	}
	desc, err := makeTableDesc(create)
	if err != nil {
		t.Fatal(err)
	}
	if err := desc.AllocateIDs(); err != ErrMissingPrimaryKey {
		t.Fatal(err)
	}
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:18,代码来源:table_test.go

示例14: TestMakeDatabaseDesc

func TestMakeDatabaseDesc(t *testing.T) {
	defer leaktest.AfterTest(t)

	stmt, err := parser.Parse("CREATE DATABASE test")
	if err != nil {
		t.Fatal(err)
	}
	desc := makeDatabaseDesc(stmt[0].(*parser.CreateDatabase))
	if desc.Name != "test" {
		t.Fatalf("expected Name == test, got %s", desc.Name)
	}
	// ID is not set yet.
	if desc.ID != 0 {
		t.Fatalf("expected ID == 0, got %d", desc.ID)
	}
	if len(desc.GetPrivileges().Users) != 1 {
		t.Fatalf("wrong number of privilege users, expected 1, got: %d", len(desc.GetPrivileges().Users))
	}
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:19,代码来源:database_test.go

示例15: init

func init() {
	const sql = `
CREATE TABLE system.namespace (
  "parentID" INT,
  "name"     CHAR,
  "id"       INT,
  PRIMARY KEY (parentID, name)
);

CREATE TABLE system.descriptor (
  "id"   INT PRIMARY KEY,
  "desc" BLOB
);
`
	stmts, err := parser.Parse(sql)
	if err != nil {
		log.Fatal(err)
	}

	NamespaceTable, err = makeTableDesc(stmts[0].(*parser.CreateTable))
	if err != nil {
		log.Fatal(err)
	}
	NamespaceTable.Privileges = SystemDB.Privileges
	NamespaceTable.ID = namespaceTableID
	if err := NamespaceTable.AllocateIDs(); err != nil {
		log.Fatal(err)
	}

	DescriptorTable, err = makeTableDesc(stmts[1].(*parser.CreateTable))
	if err != nil {
		log.Fatal(err)
	}
	DescriptorTable.Privileges = SystemDB.Privileges
	DescriptorTable.ID = descriptorTableID
	if err := DescriptorTable.AllocateIDs(); err != nil {
		log.Fatal(err)
	}
}
开发者ID:shitfSign,项目名称:cockroach,代码行数:39,代码来源:structured.go


注:本文中的github.com/cockroachdb/cockroach/sql/parser.Parse函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。