本文整理匯總了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
}