當前位置: 首頁>>代碼示例>>Golang>>正文


Golang parser.ParseTraditional函數代碼示例

本文整理匯總了Golang中github.com/cockroachdb/cockroach/sql/parser.ParseTraditional函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseTraditional函數的具體用法?Golang ParseTraditional怎麽用?Golang ParseTraditional使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了ParseTraditional函數的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: parseAndNormalizeExpr

func parseAndNormalizeExpr(t *testing.T, sql string) (parser.Expr, qvalMap) {
	q, err := parser.ParseTraditional("SELECT " + sql)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	expr := q[0].(*parser.Select).Exprs[0].Expr
	expr, err = (parser.EvalContext{}).NormalizeExpr(expr)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}

	// Perform qualified name resolution because {analyze,simplify}Expr want
	// expressions containing qvalues.
	s := &scanNode{}
	s.desc = testTableDesc()
	s.visibleCols = s.desc.Columns

	if err := s.desc.AllocateIDs(); err != nil {
		t.Fatal(err)
	}

	expr, err = s.resolveQNames(expr)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	if _, err := parser.TypeCheckExpr(expr); err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	return expr, s.qvals
}
開發者ID:kumarh1982,項目名稱:cockroach,代碼行數:30,代碼來源:analyze_test.go

示例2: TestRetryResolveQNames

// Test that we can resolve the qnames in an expression that has already been
// resolved.
func TestRetryResolveQNames(t *testing.T) {
	defer leaktest.AfterTest(t)

	q, err := parser.ParseTraditional(`SELECT COUNT(a)`)
	if err != nil {
		t.Fatal(err)
	}
	expr := q[0].(*parser.Select).Exprs[0].Expr

	for i := 0; i < 2; i++ {
		s := &scanNode{}
		s.desc = testTableDesc()
		s.visibleCols = s.desc.Columns
		if err := s.desc.AllocateIDs(); err != nil {
			t.Fatal(err)
		}

		_, err = s.resolveQNames(expr)
		if err != nil {
			t.Fatal(err)
		}
		if len(s.qvals) != 1 {
			t.Fatalf("%d: expected 1 qvalue, but found %d", i, len(s.qvals))
		}
		if _, ok := s.qvals[ColumnID(1)]; !ok {
			t.Fatalf("%d: unable to find qvalue for column ID 1", i)
		}
	}
}
開發者ID:mbertschler,項目名稱:cockroach,代碼行數:31,代碼來源:scan_test.go

示例3: parseAndNormalizeExpr

func parseAndNormalizeExpr(t *testing.T, sql string) (parser.Expr, qvalMap) {
	q, err := parser.ParseTraditional("SELECT " + sql)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	expr := q[0].(*parser.Select).Exprs[0].Expr
	r, err := parser.NormalizeExpr(expr)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}

	// Perform qualified name resolution because {analyze,simplify}Expr want
	// expressions containing qvalues.
	s := &scanNode{}
	s.desc = &TableDescriptor{
		Columns: []ColumnDescriptor{
			{Name: "a", ID: 1, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "b", ID: 2, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "c", ID: 3, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "d", ID: 4, Type: ColumnType{Kind: ColumnType_INT}},
			{Name: "e", ID: 5, Type: ColumnType{Kind: ColumnType_INT}},
		},
	}
	s.visibleCols = s.desc.Columns

	r, err = s.resolveQNames(r)
	if err != nil {
		t.Fatalf("%s: %v", sql, err)
	}
	return r, s.qvals
}
開發者ID:Eric-Gaudiello,項目名稱:cockroach,代碼行數:31,代碼來源:analyze_test.go

示例4: query

func (p *planner) query(sql string) (planNode, error) {
	stmts, err := parser.ParseTraditional(sql)
	if err != nil {
		return nil, err
	}
	if len(stmts) != 1 {
		return nil, util.Errorf("expected single statement, found %d", len(stmts))
	}
	return p.makePlan(stmts[0])
}
開發者ID:BobbWu,項目名稱:cockroach,代碼行數:10,代碼來源:plan.go

示例5: TestPrimaryKeyUnspecified

func TestPrimaryKeyUnspecified(t *testing.T) {
	defer leaktest.AfterTest(t)
	stmt, err := parser.ParseTraditional(
		"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, 1)
	if err != nil {
		t.Fatal(err)
	}
	if err := desc.AllocateIDs(); err != errMissingPrimaryKey {
		t.Fatal(err)
	}
}
開發者ID:mbertschler,項目名稱:cockroach,代碼行數:19,代碼來源:table_test.go

示例6: createSystemTable

func createSystemTable(id ID, cmd string) TableDescriptor {
	stmts, err := parser.ParseTraditional(cmd)
	if err != nil {
		log.Fatal(err)
	}

	desc, err := makeTableDesc(stmts[0].(*parser.CreateTable))
	if err != nil {
		log.Fatal(err)
	}

	// Assign max privileges to root user.
	desc.Privileges = NewPrivilegeDescriptor(security.RootUser,
		SystemAllowedPrivileges[id])

	desc.ID = id
	if err := desc.AllocateIDs(); err != nil {
		log.Fatal(err)
	}

	return desc
}
開發者ID:elindsey,項目名稱:cockroach,代碼行數:22,代碼來源:system.go

示例7: TestMakeTableDescColumns

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

	testData := []struct {
		sqlType  string
		colType  ColumnType
		nullable bool
	}{
		{
			"BIT(1)",
			ColumnType{Kind: ColumnType_INT, Width: 1},
			true,
		},
		{
			"BOOLEAN",
			ColumnType{Kind: ColumnType_BOOL},
			true,
		},
		{
			"INT",
			ColumnType{Kind: ColumnType_INT},
			true,
		},
		{
			"FLOAT(3)",
			ColumnType{Kind: ColumnType_FLOAT, Precision: 3},
			true,
		},
		{
			"DECIMAL(5,6)",
			ColumnType{Kind: ColumnType_DECIMAL, Precision: 5, Width: 6},
			true,
		},
		{
			"DATE",
			ColumnType{Kind: ColumnType_DATE},
			true,
		},
		{
			"TIMESTAMP",
			ColumnType{Kind: ColumnType_TIMESTAMP},
			true,
		},
		{
			"INTERVAL",
			ColumnType{Kind: ColumnType_INTERVAL},
			true,
		},
		{
			"CHAR",
			ColumnType{Kind: ColumnType_STRING},
			true,
		},
		{
			"TEXT",
			ColumnType{Kind: ColumnType_STRING},
			true,
		},
		{
			"BLOB",
			ColumnType{Kind: ColumnType_BYTES},
			true,
		},
		{
			"INT NOT NULL",
			ColumnType{Kind: ColumnType_INT},
			false,
		},
		{
			"INT NULL",
			ColumnType{Kind: ColumnType_INT},
			true,
		},
	}
	for i, d := range testData {
		stmt, err := parser.ParseTraditional(
			"CREATE TABLE foo.test (a " + d.sqlType + " PRIMARY KEY)")
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		create := stmt[0].(*parser.CreateTable)
		if err := create.Table.NormalizeTableName(""); err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		schema, err := makeTableDesc(create, 1)
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		if !reflect.DeepEqual(d.colType, schema.Columns[0].Type) {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.colType, schema.Columns[0])
		}
		if d.nullable != schema.Columns[0].Nullable {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.nullable, schema.Columns[0].Nullable)
		}
	}
}
開發者ID:mbertschler,項目名稱:cockroach,代碼行數:96,代碼來源:table_test.go

示例8: TestMakeTableDescIndexes

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

	testData := []struct {
		sql     string
		primary IndexDescriptor
		indexes []IndexDescriptor
	}{
		{
			"a INT PRIMARY KEY",
			IndexDescriptor{
				Name:        PrimaryKeyIndexName,
				Unique:      true,
				ColumnNames: []string{"a"},
			},
			[]IndexDescriptor{},
		},
		{
			"a INT UNIQUE, b INT PRIMARY KEY",
			IndexDescriptor{
				Name:        "primary",
				Unique:      true,
				ColumnNames: []string{"b"},
			},
			[]IndexDescriptor{
				{
					Name:        "",
					Unique:      true,
					ColumnNames: []string{"a"},
				},
			},
		},
		{
			"a INT, b INT, CONSTRAINT c PRIMARY KEY (a, b)",
			IndexDescriptor{
				Name:        "c",
				Unique:      true,
				ColumnNames: []string{"a", "b"},
			},
			[]IndexDescriptor{},
		},
		{
			"a INT, b INT, CONSTRAINT c UNIQUE (b), PRIMARY KEY (a, b)",
			IndexDescriptor{
				Name:        "primary",
				Unique:      true,
				ColumnNames: []string{"a", "b"},
			},
			[]IndexDescriptor{
				{
					Name:        "c",
					Unique:      true,
					ColumnNames: []string{"b"},
				},
			},
		},
		{
			"a INT, b INT, PRIMARY KEY (a, b)",
			IndexDescriptor{
				Name:        PrimaryKeyIndexName,
				Unique:      true,
				ColumnNames: []string{"a", "b"},
			},
			[]IndexDescriptor{},
		},
	}
	for i, d := range testData {
		stmt, err := parser.ParseTraditional("CREATE TABLE foo.test (" + d.sql + ")")
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		create := stmt[0].(*parser.CreateTable)
		if err := create.Table.NormalizeTableName(""); err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		schema, err := makeTableDesc(create, 1)
		if err != nil {
			t.Fatalf("%d: %v", i, err)
		}
		if !reflect.DeepEqual(d.primary, schema.PrimaryIndex) {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.primary, schema.PrimaryIndex)
		}
		if !reflect.DeepEqual(d.indexes, append([]IndexDescriptor{}, schema.Indexes...)) {
			t.Fatalf("%d: expected %+v, but got %+v", i, d.indexes, schema.Indexes)
		}

	}
}
開發者ID:mbertschler,項目名稱:cockroach,代碼行數:88,代碼來源:table_test.go


注:本文中的github.com/cockroachdb/cockroach/sql/parser.ParseTraditional函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。