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


Golang SelectStmt.Text方法代码示例

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


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

示例1: convertSelect

func convertSelect(converter *expressionConverter, s *ast.SelectStmt) (*stmts.SelectStmt, error) {
	oldSelect := &stmts.SelectStmt{
		Distinct: s.Distinct,
		Text:     s.Text(),
	}
	oldSelect.Fields = make([]*field.Field, len(s.Fields.Fields))
	for i, val := range s.Fields.Fields {
		oldField := &field.Field{}
		oldField.AsName = val.AsName.O
		var err error
		if val.Expr != nil {
			oldField.Expr, err = convertExpr(converter, val.Expr)
			if err != nil {
				return nil, errors.Trace(err)
			}
			if oldField.AsName == "" {
				innerExpr := getInnerFromParentheses(val.Expr)
				switch innerExpr.(type) {
				case *ast.ColumnNameExpr:
					// Do not set column name as name and remove parentheses.
					oldField.Expr = converter.exprMap[innerExpr]
				case *ast.ValueExpr:
					if innerExpr.Text() != "" {
						oldField.AsName = innerExpr.Text()
					} else {
						oldField.AsName = val.Text()
					}
				default:
					oldField.AsName = val.Text()
				}
			}
		} else if val.WildCard != nil {
			str := "*"
			if val.WildCard.Table.O != "" {
				str = val.WildCard.Table.O + ".*"
				if val.WildCard.Schema.O != "" {
					str = val.WildCard.Schema.O + "." + str
				}
			}
			oldField.Expr = &expression.Ident{CIStr: model.NewCIStr(str)}
		}
		oldSelect.Fields[i] = oldField
	}
	var err error
	if s.From != nil {
		oldSelect.From, err = convertJoin(converter, s.From.TableRefs)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.Where != nil {
		oldSelect.Where = &rsets.WhereRset{}
		oldSelect.Where.Expr, err = convertExpr(converter, s.Where)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}

	if s.GroupBy != nil {
		oldSelect.GroupBy, err = convertGroupBy(converter, s.GroupBy)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.Having != nil {
		oldSelect.Having, err = convertHaving(converter, s.Having)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.OrderBy != nil {
		oldSelect.OrderBy, err = convertOrderBy(converter, s.OrderBy)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.Limit != nil {
		if s.Limit.Offset > 0 {
			oldSelect.Offset = &rsets.OffsetRset{Count: s.Limit.Offset}
		}
		if s.Limit.Count > 0 {
			oldSelect.Limit = &rsets.LimitRset{Count: s.Limit.Count}
		}
	}
	switch s.LockTp {
	case ast.SelectLockForUpdate:
		oldSelect.Lock = coldef.SelectLockForUpdate
	case ast.SelectLockInShareMode:
		oldSelect.Lock = coldef.SelectLockInShareMode
	case ast.SelectLockNone:
		oldSelect.Lock = coldef.SelectLockNone
	}
	return oldSelect, nil
}
开发者ID:yzl11,项目名称:vessel,代码行数:94,代码来源:convert_stmt.go

示例2: convertSelect

func convertSelect(converter *expressionConverter, s *ast.SelectStmt) (*stmts.SelectStmt, error) {
	oldSelect := &stmts.SelectStmt{
		Distinct: s.Distinct,
		Text:     s.Text(),
	}
	oldSelect.Fields = make([]*field.Field, len(s.Fields.Fields))
	for i, val := range s.Fields.Fields {
		oldField := &field.Field{}
		oldField.AsName = val.AsName.O
		var err error
		if val.Expr != nil {
			oldField.Expr, err = convertExpr(converter, val.Expr)
			if err != nil {
				return nil, errors.Trace(err)
			}
			// TODO: handle parenthesesed column name expression, which should not set AsName.
			if _, ok := oldField.Expr.(*expression.Ident); !ok && oldField.AsName == "" {
				oldField.AsName = val.Text()
			}
		} else if val.WildCard != nil {
			str := "*"
			if val.WildCard.Table.O != "" {
				str = val.WildCard.Table.O + ".*"
				if val.WildCard.Schema.O != "" {
					str = val.WildCard.Schema.O + "." + str
				}
			}
			oldField.Expr = &expression.Ident{CIStr: model.NewCIStr(str)}
		}
		oldSelect.Fields[i] = oldField
	}
	var err error
	if s.From != nil {
		oldSelect.From, err = convertJoin(converter, s.From.TableRefs)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.Where != nil {
		oldSelect.Where = &rsets.WhereRset{}
		oldSelect.Where.Expr, err = convertExpr(converter, s.Where)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}

	if s.GroupBy != nil {
		oldSelect.GroupBy, err = convertGroupBy(converter, s.GroupBy)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.Having != nil {
		oldSelect.Having, err = convertHaving(converter, s.Having)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.OrderBy != nil {
		oldSelect.OrderBy, err = convertOrderBy(converter, s.OrderBy)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	if s.Limit != nil {
		if s.Limit.Offset > 0 {
			oldSelect.Offset = &rsets.OffsetRset{Count: s.Limit.Offset}
		}
		if s.Limit.Count > 0 {
			oldSelect.Limit = &rsets.LimitRset{Count: s.Limit.Count}
		}
	}
	switch s.LockTp {
	case ast.SelectLockForUpdate:
		oldSelect.Lock = coldef.SelectLockForUpdate
	case ast.SelectLockInShareMode:
		oldSelect.Lock = coldef.SelectLockInShareMode
	case ast.SelectLockNone:
		oldSelect.Lock = coldef.SelectLockNone
	}
	return oldSelect, nil
}
开发者ID:zebozhuang,项目名称:tidb,代码行数:82,代码来源:convert_stmt.go


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