Browse Source

'fix'

apijson
u 2 years ago
parent
commit
7a6b1361cc
  1. 18
      types.go

18
types.go

@ -406,8 +406,10 @@ func (q *Query) FromSelect(selectStmt *sqlparser.Select) error {
for _, v := range selectStmt.SelectExprs { for _, v := range selectStmt.SelectExprs {
switch expr := v.(type) { switch expr := v.(type) {
case *sqlparser.StarExpr: case *sqlparser.StarExpr:
// log.Println("selectStmt.StarExpr", reflect.TypeOf(expr), jsonEncode(expr))
q.Fields = append(q.Fields, Field{ q.Fields = append(q.Fields, Field{
Name: "*",
Name: "*",
FromTable: expr.TableName.Name.String(),
}) })
case *sqlparser.AliasedExpr: case *sqlparser.AliasedExpr:
// log.Println("selectStmt.AliasedExpr", reflect.TypeOf(expr.Expr), jsonEncode(expr.Expr)) // log.Println("selectStmt.AliasedExpr", reflect.TypeOf(expr.Expr), jsonEncode(expr.Expr))
@ -446,10 +448,19 @@ func (q *Query) FromSelect(selectStmt *sqlparser.Select) error {
switch form := form.(type) { switch form := form.(type) {
case *sqlparser.AliasedTableExpr: //未联表 case *sqlparser.AliasedTableExpr: //未联表
q.From.Table = string(form.Expr.(sqlparser.TableName).Name.String())
if form.As.String() != "" { if form.As.String() != "" {
q.From.As = form.As.String() q.From.As = form.As.String()
} }
switch exp := form.Expr.(type) {
case sqlparser.TableName:
q.From.Table = string(exp.Name.String())
case *sqlparser.Subquery:
subQuery := &Query{}
if err := subQuery.FromSelect(exp.Select.(*sqlparser.Select)); err != nil {
return err
}
q.From.SubQuery = subQuery
}
case *sqlparser.JoinTableExpr: //有联表 case *sqlparser.JoinTableExpr: //有联表
//确定主表 //确定主表
@ -738,8 +749,5 @@ func (q *Query) ToSql() (string, error) {
return "", fmt.Errorf("failed to build query: %w", err) return "", fmt.Errorf("failed to build query: %w", err)
} }
buf := builder.NewWriter()
b.WriteTo(buf)
log.Println("NewWriter:", buf.String())
return b.ToBoundSQL() return b.ToBoundSQL()
} }
Loading…
Cancel
Save