本文整理汇总了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
}
示例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
}