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


Golang TableDescriptor.Alias方法代码示例

本文整理汇总了Golang中github.com/cockroachdb/cockroach/structured.TableDescriptor.Alias方法的典型用法代码示例。如果您正苦于以下问题:Golang TableDescriptor.Alias方法的具体用法?Golang TableDescriptor.Alias怎么用?Golang TableDescriptor.Alias使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/cockroachdb/cockroach/structured.TableDescriptor的用法示例。


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

示例1: Select

// Select selects rows from a single table.
// Privileges: READ on table
//   Notes: postgres requires SELECT. Also requires UPDATE on "FOR UPDATE".
//          mysql requires SELECT.
func (p *planner) Select(n *parser.Select) (planNode, error) {
	var desc *structured.TableDescriptor
	var index *structured.IndexDescriptor
	var visibleCols []structured.ColumnDescriptor

	switch len(n.From) {
	case 0:
		// desc remains nil.

	case 1:
		var err error
		desc, err = p.getAliasedTableDesc(n.From[0])
		if err != nil {
			return nil, err
		}

		if !desc.HasPrivilege(p.user, parser.PrivilegeRead) {
			return nil, fmt.Errorf("user %s does not have %s privilege on table %s",
				p.user, parser.PrivilegeRead, desc.Name)
		}

		// This is only kosher because we know that getAliasedDesc() succeeded.
		qname := n.From[0].(*parser.AliasedTableExpr).Expr.(*parser.QualifiedName)
		indexName := qname.Index()
		if indexName != "" && !strings.EqualFold(desc.PrimaryIndex.Name, indexName) {
			for i := range desc.Indexes {
				if strings.EqualFold(desc.Indexes[i].Name, indexName) {
					// Remove all but the matching index from the descriptor.
					desc.Indexes = desc.Indexes[i : i+1]
					index = &desc.Indexes[0]
					break
				}
			}
			if index == nil {
				return nil, fmt.Errorf("index \"%s\" not found", indexName)
			}
			// If the table was not aliased, use the index name instead of the table
			// name for fully-qualified columns in the expression.
			if n.From[0].(*parser.AliasedTableExpr).As == "" {
				desc.Alias = index.Name
			}
			// Strip out any columns from the table that are not present in the
			// index.
			indexColIDs := map[structured.ColumnID]struct{}{}
			for _, colID := range index.ColumnIDs {
				indexColIDs[colID] = struct{}{}
			}
			for _, col := range desc.Columns {
				if _, ok := indexColIDs[col.ID]; !ok {
					continue
				}
				visibleCols = append(visibleCols, col)
			}
		} else {
			index = &desc.PrimaryIndex
			visibleCols = desc.Columns
		}

	default:
		return nil, util.Errorf("TODO(pmattis): unsupported FROM: %s", n.From)
	}

	// Loop over the select expressions and expand them into the expressions
	// we're going to use to generate the returned column set and the names for
	// those columns.
	exprs := make([]parser.Expr, 0, len(n.Exprs))
	columns := make([]string, 0, len(n.Exprs))
	for _, e := range n.Exprs {
		// If a QualifiedName has a StarIndirection suffix we need to match the
		// prefix of the qualified name to one of the tables in the query and
		// then expand the "*" into a list of columns.
		if qname, ok := e.Expr.(*parser.QualifiedName); ok {
			if err := qname.NormalizeColumnName(); err != nil {
				return nil, err
			}
			if qname.IsStar() {
				if desc == nil {
					return nil, fmt.Errorf("\"%s\" with no tables specified is not valid", qname)
				}
				if e.As != "" {
					return nil, fmt.Errorf("\"%s\" cannot be aliased", qname)
				}
				tableName := qname.Table()
				if tableName != "" && !strings.EqualFold(desc.Alias, tableName) {
					return nil, fmt.Errorf("table \"%s\" not found", tableName)
				}

				if index != &desc.PrimaryIndex {
					for _, col := range index.ColumnNames {
						columns = append(columns, col)
						exprs = append(exprs, &parser.QualifiedName{Base: parser.Name(col)})
					}
				} else {
					for _, col := range desc.Columns {
						columns = append(columns, col.Name)
						exprs = append(exprs, &parser.QualifiedName{Base: parser.Name(col.Name)})
//.........这里部分代码省略.........
开发者ID:ajayaa,项目名称:cockroach,代码行数:101,代码来源:select.go


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