本文整理汇总了Golang中github.com/youtube/vitess/go/vt/sqlparser.GetTableName函数的典型用法代码示例。如果您正苦于以下问题:Golang GetTableName函数的具体用法?Golang GetTableName怎么用?Golang GetTableName使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了GetTableName函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例2: buildUpdatePlan
func buildUpdatePlan(upd *sqlparser.Update, schema *Schema) *Plan {
plan := &Plan{
ID: NoPlan,
Rewritten: generateQuery(upd),
}
tablename := sqlparser.GetTableName(upd.Table)
plan.Table, plan.Reason = schema.LookupTable(tablename)
if plan.Reason != "" {
return plan
}
if plan.Table.Keyspace.ShardingScheme == Unsharded {
plan.ID = UpdateUnsharded
return plan
}
getWhereRouting(upd.Where, plan)
switch plan.ID {
case SelectSingleShardKey:
plan.ID = UpdateSingleShardKey
case SelectSingleLookup:
plan.ID = UpdateSingleLookup
case SelectMultiShardKey, SelectMultiLookup, SelectScatter:
plan.ID = NoPlan
plan.Reason = "too complex"
return plan
default:
panic("unexpected")
}
if isIndexChanging(upd.Exprs, plan.Table.Indexes) {
plan.ID = NoPlan
plan.Reason = "index is changing"
}
return plan
}
示例3: buildDeletePlan
func buildDeletePlan(del *sqlparser.Delete, schema *Schema) *Plan {
plan := &Plan{
ID: NoPlan,
Rewritten: generateQuery(del),
}
tablename := sqlparser.GetTableName(del.Table)
plan.Table, plan.Reason = schema.FindTable(tablename)
if plan.Reason != "" {
return plan
}
if !plan.Table.Keyspace.Sharded {
plan.ID = DeleteUnsharded
return plan
}
getWhereRouting(del.Where, plan, true)
switch plan.ID {
case SelectEqual:
plan.ID = DeleteEqual
plan.Subquery = generateDeleteSubquery(del, plan.Table)
case SelectIN, SelectScatter, SelectKeyrange:
plan.ID = NoPlan
plan.Reason = "delete has multi-shard where clause"
default:
panic("unexpected")
}
return plan
}
示例4: buildDeletePlan
func buildDeletePlan(del *sqlparser.Delete, schema *Schema) *Plan {
plan := &Plan{
ID: NoPlan,
Rewritten: generateQuery(del),
}
tablename := sqlparser.GetTableName(del.Table)
plan.Table, plan.Reason = schema.LookupTable(tablename)
if plan.Reason != "" {
return plan
}
if plan.Table.Keyspace.ShardingScheme == Unsharded {
plan.ID = DeleteUnsharded
return plan
}
getWhereRouting(del.Where, plan)
switch plan.ID {
case SelectSingleShardKey:
plan.ID = DeleteSingleShardKey
case SelectSingleLookup:
plan.ID = DeleteSingleLookup
case SelectMultiShardKey, SelectMultiLookup, SelectScatter:
plan.ID = NoPlan
plan.Reason = "too complex"
default:
panic("unexpected")
}
return plan
}
示例5: buildUpdatePlan
func buildUpdatePlan(upd *sqlparser.Update, schema *Schema) *Plan {
plan := &Plan{
ID: NoPlan,
Rewritten: generateQuery(upd),
}
tablename := sqlparser.GetTableName(upd.Table)
plan.Table, plan.Reason = schema.FindTable(tablename)
if plan.Reason != "" {
return plan
}
if !plan.Table.Keyspace.Sharded {
plan.ID = UpdateUnsharded
return plan
}
getWhereRouting(upd.Where, plan, true)
switch plan.ID {
case SelectEqual:
plan.ID = UpdateEqual
case SelectIN, SelectScatter, SelectKeyrange:
plan.ID = NoPlan
plan.Reason = "update has multi-shard where clause"
return plan
default:
panic("unexpected")
}
if isIndexChanging(upd.Exprs, plan.Table.ColVindexes) {
plan.ID = NoPlan
plan.Reason = "index is changing"
}
return plan
}
示例6: 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
}
示例7: analyzeDelete
func analyzeDelete(del *sqlparser.Delete, getTable TableGetter) (plan *ExecPlan, err error) {
plan = &ExecPlan{
PlanID: PlanPassDML,
FullQuery: GenerateFullQuery(del),
}
tableName := sqlparser.GetTableName(del.Table)
if tableName == "" {
plan.Reason = ReasonTable
return plan, nil
}
tableInfo, err := plan.setTableInfo(tableName, getTable)
if err != nil {
return nil, err
}
if len(tableInfo.Indexes) == 0 || tableInfo.Indexes[0].Name.Lowered() != "primary" {
log.Warningf("no primary key for table %s", tableName)
plan.Reason = ReasonTableNoIndex
return plan, nil
}
plan.OuterQuery = GenerateDeleteOuterQuery(del)
if pkValues := analyzeWhere(del.Where, tableInfo.Indexes[0]); pkValues != nil {
plan.PlanID = PlanDMLPK
plan.PKValues = pkValues
return plan, nil
}
plan.PlanID = PlanDMLSubquery
plan.Subquery = GenerateDeleteSubquery(del, tableInfo)
return plan, nil
}
示例8: buildInsertPlan
func buildInsertPlan(ins *sqlparser.Insert, schema *Schema) *Plan {
plan := &Plan{
ID: NoPlan,
Rewritten: generateQuery(ins),
}
tablename := sqlparser.GetTableName(ins.Table)
plan.Table, plan.Reason = schema.LookupTable(tablename)
if plan.Reason != "" {
return plan
}
if plan.Table.Keyspace.ShardingScheme == Unsharded {
plan.ID = InsertUnsharded
return plan
}
if len(ins.Columns) == 0 {
plan.Reason = "no column list"
return plan
}
var values sqlparser.Values
switch rows := ins.Rows.(type) {
case *sqlparser.Select, *sqlparser.Union:
plan.Reason = "subqueries not allowed"
return plan
case sqlparser.Values:
values = rows
default:
panic("unexpected")
}
if len(values) != 1 {
plan.Reason = "multi-row inserts not supported"
return plan
}
switch values[0].(type) {
case *sqlparser.Subquery:
plan.Reason = "subqueries not allowed"
return plan
}
row := values[0].(sqlparser.ValTuple)
if len(ins.Columns) != len(row) {
plan.Reason = "column list doesn't match values"
return plan
}
indexes := schema.Tables[tablename].Indexes
plan.ID = InsertSharded
plan.Values = make([]interface{}, 0, len(indexes))
for _, index := range indexes {
if err := buildIndexPlan(ins, tablename, index, plan); err != nil {
plan.ID = NoPlan
plan.Reason = err.Error()
return plan
}
}
// Query was rewritten
plan.Rewritten = generateQuery(ins)
return plan
}
示例9: analyzeFrom
func analyzeFrom(tableExprs sqlparser.TableExprs) (tablename string, hasHints bool) {
if len(tableExprs) > 1 {
return "", false
}
node, ok := tableExprs[0].(*sqlparser.AliasedTableExpr)
if !ok {
return "", false
}
return sqlparser.GetTableName(node.Expr), node.Hints != nil
}
示例10: analyzeFrom
func analyzeFrom(tableExprs sqlparser.TableExprs) string {
if len(tableExprs) > 1 {
return ""
}
node, ok := tableExprs[0].(*sqlparser.AliasedTableExpr)
if !ok {
return ""
}
return sqlparser.GetTableName(node.Expr)
}
示例11: analyzeUpdate
func analyzeUpdate(upd *sqlparser.Update, getTable TableGetter) (plan *ExecPlan, err error) {
// Default plan
plan = &ExecPlan{
PlanId: PLAN_PASS_DML,
FullQuery: GenerateFullQuery(upd),
}
tableName := sqlparser.GetTableName(upd.Table)
if tableName == "" {
plan.Reason = REASON_TABLE
return plan, nil
}
tableInfo, err := plan.setTableInfo(tableName, getTable)
if err != nil {
return nil, err
}
if len(tableInfo.Indexes) == 0 || tableInfo.Indexes[0].Name != "PRIMARY" {
log.Warningf("no primary key for table %s", tableName)
plan.Reason = REASON_TABLE_NOINDEX
return plan, nil
}
plan.SecondaryPKValues, err = analyzeUpdateExpressions(upd.Exprs, tableInfo.Indexes[0])
if err != nil {
if err == TooComplex {
plan.Reason = REASON_PK_CHANGE
return plan, nil
}
return nil, err
}
plan.PlanId = PLAN_DML_SUBQUERY
plan.OuterQuery = GenerateUpdateOuterQuery(upd, tableInfo.Indexes[0])
plan.Subquery = GenerateUpdateSubquery(upd, tableInfo)
conditions := analyzeWhere(upd.Where)
if conditions == nil {
plan.Reason = REASON_WHERE
return plan, nil
}
pkValues, err := getPKValues(conditions, tableInfo.Indexes[0])
if err != nil {
return nil, err
}
if pkValues != nil {
plan.PlanId = PLAN_DML_PK
plan.OuterQuery = plan.FullQuery
plan.PKValues = pkValues
return plan, nil
}
return plan, nil
}
示例12: buildMinMaxQuery
// buildMinMaxQuery returns the query to execute to get the minimum and maximum of the splitColumn.
// The query returned is:
// SELECT MIN(<splitColumn>), MAX(<splitColumn>) FROM <table>;
// where <table> is the table referenced in the original query (held in splitParams.sql).
func buildMinMaxQuery(splitParams *SplitParams) string {
// The SplitParams constructor should have already checked that the FROM clause of the query
// is a simple table expression, so this type-assertion should succeed.
tableName := sqlparser.GetTableName(
splitParams.selectAST.From[0].(*sqlparser.AliasedTableExpr).Expr)
if tableName == "" {
panic(fmt.Sprintf("Can't get tableName from query %v", splitParams.sql))
}
return fmt.Sprintf("select min(%v), max(%v) from %v",
splitParams.splitColumns[0],
splitParams.splitColumns[0],
tableName)
}
示例13: analyzeUpdate
func analyzeUpdate(upd *sqlparser.Update, getTable TableGetter) (plan *ExecPlan, err error) {
plan = &ExecPlan{
PlanID: PlanPassDML,
FullQuery: GenerateFullQuery(upd),
}
tableName := sqlparser.GetTableName(upd.Table)
if tableName == "" {
plan.Reason = ReasonTable
return plan, nil
}
tableInfo, err := plan.setTableInfo(tableName, getTable)
if err != nil {
return nil, err
}
if len(tableInfo.Indexes) == 0 || tableInfo.Indexes[0].Name != "PRIMARY" {
log.Warningf("no primary key for table %s", tableName)
plan.Reason = ReasonTableNoIndex
return plan, nil
}
plan.SecondaryPKValues, err = analyzeUpdateExpressions(upd.Exprs, tableInfo.Indexes[0])
if err != nil {
if err == ErrTooComplex {
plan.Reason = ReasonPKChange
return plan, nil
}
return nil, err
}
plan.OuterQuery = GenerateUpdateOuterQuery(upd)
if conditions := analyzeWhere(upd.Where); conditions != nil {
pkValues, err := getPKValues(conditions, tableInfo.Indexes[0])
if err != nil {
return nil, err
}
if pkValues != nil {
plan.PlanID = PlanDMLPK
plan.PKValues = pkValues
return plan, nil
}
}
plan.PlanID = PlanDMLSubquery
plan.Subquery = GenerateUpdateSubquery(upd, tableInfo)
return plan, nil
}
示例14: 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
}
示例15: analyzeDelete
func analyzeDelete(del *sqlparser.Delete, getTable TableGetter) (plan *ExecPlan, err error) {
plan = &ExecPlan{
PlanId: PLAN_PASS_DML,
FullQuery: GenerateFullQuery(del),
}
tableName := sqlparser.GetTableName(del.Table)
if tableName == "" {
plan.Reason = REASON_TABLE
return plan, nil
}
tableInfo, err := plan.setTableInfo(tableName, getTable)
if err != nil {
return nil, err
}
if len(tableInfo.Indexes) == 0 || tableInfo.Indexes[0].Name != "PRIMARY" {
log.Warningf("no primary key for table %s", tableName)
plan.Reason = REASON_TABLE_NOINDEX
return plan, nil
}
plan.OuterQuery = GenerateDeleteOuterQuery(del)
if conditions := analyzeWhere(del.Where); conditions != nil {
pkValues, err := getPKValues(conditions, tableInfo.Indexes[0])
if err != nil {
return nil, err
}
if pkValues != nil {
plan.PlanId = PLAN_DML_PK
plan.PKValues = pkValues
return plan, nil
}
}
plan.PlanId = PLAN_DML_SUBQUERY
plan.Subquery = GenerateDeleteSubquery(del, tableInfo)
return plan, nil
}