本文整理匯總了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))
}
}
示例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)
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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)
}
}
示例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
}
示例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
}
示例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)
}
}
示例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))
}
}
示例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)
}
}