Skip to content

Commit

Permalink
patched table detection
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Aug 3, 2023
1 parent 16137d8 commit 60be5ff
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions view/column/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/viant/datly/shared"
"github.com/viant/sqlparser"
"github.com/viant/sqlparser/expr"
"github.com/viant/sqlx/io"
"github.com/viant/sqlx/io/config"
"github.com/viant/sqlx/metadata/sink"
Expand Down Expand Up @@ -172,8 +173,10 @@ func readSinkColumns(ctx context.Context, db *sql.DB, table string) ([]sink.Colu
return nil, err
}
columns, err := config.Columns(ctx, session, db, table)
if len(columns) == 0 {
return inferColumnWithSQL(ctx, db, "SELECT * FROM "+table+" WHERE 1 = 0", []interface{}{}, map[string]sink.Column{})
if len(columns) == 0 && table != "" {
if columns, e := inferColumnWithSQL(ctx, db, "SELECT * FROM "+table+" WHERE 1 = 0", []interface{}{}, map[string]sink.Column{}); e == nil {
return columns, err
}
}
return columns, err
}
Expand All @@ -191,6 +194,18 @@ func parseQuery(SQL string) (string, string, sqlparser.Columns) {
return table, "", nil //use table metadata
}
sqlQuery.Limit = nil
if table != "" {
if sqlQuery.Qualify == nil || sqlQuery.Qualify.X == nil {
sqlQuery.Qualify = &expr.Qualify{X: falsePredicate()}
} else {
sqlQuery.Qualify = &expr.Qualify{
X: &expr.Binary{
X: falsePredicate(),
Op: "AND",
Y: sqlQuery.Qualify.X,
}}
}
}
sqlQuery.Offset = nil
SQL = sqlparser.Stringify(sqlQuery)
if table != "" {
Expand All @@ -200,6 +215,10 @@ func parseQuery(SQL string) (string, string, sqlparser.Columns) {
return table, SQL, queryColumn
}

func falsePredicate() *expr.Binary {
return &expr.Binary{X: &expr.Literal{Value: "1"}, Op: "=", Y: &expr.Literal{Value: "0"}}
}

func asColumns(sinkColumns []sink.Column) sqlparser.Columns {
var result sqlparser.Columns
for _, column := range sinkColumns {
Expand Down

0 comments on commit 60be5ff

Please sign in to comment.