Skip to content

Commit

Permalink
patched type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Jul 28, 2022
1 parent 7cdb6e2 commit a0825da
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/ast/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ outer:
}
}
} else {
typer = &ColumnType{ColumnName: text}
typer = &ColumnType{ColumnName: strings.ToLower(text)}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ast/testdata/case009/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
name: tID
required: true
typer:
columnname: T1.ID
columnname: t1.id
source: |-
SELECT *, (
SELECT abc
Expand Down
28 changes: 26 additions & 2 deletions cmd/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"database/sql"
"fmt"
"github.com/viant/sqlx/io"
rdata "github.com/viant/toolbox/data"
"github.com/viant/velty/ast/expr"
"github.com/viant/velty/parser"
"strings"
)

Expand All @@ -30,9 +33,29 @@ func (s *serverBuilder) updateTableColumnTypes(ctx context.Context, table *Table
}

func (s *serverBuilder) updatedColumns(table *Table, prefix, tableName string, db *sql.DB) {
SQL := "SELECT * FROM " + tableName + " WHERE 1 = 0"
parse, err := parser.Parse([]byte(tableName))
var args []interface{}
expandMap := &rdata.Map{}

if err == nil {
if anIndex := strings.Index(tableName, "SELECT"); anIndex != -1 {

for _, statement := range parse.Stmt {
switch actual := statement.(type) {
case *expr.Select:
expandMap.SetValue(actual.FullName[1:], "?")
args = append(args, 0)
}
}

tableName = expandMap.ExpandAsText(tableName)
}
}

SQL := "SELECT * FROM " + tableName + " t WHERE 1 = 0"

fmt.Printf("checking %v ...\n", tableName)
query, err := db.QueryContext(context.Background(), SQL)
query, err := db.QueryContext(context.Background(), SQL, args...)
if err != nil {
s.logger.Write([]byte(fmt.Sprintf("error occured while updating table %v columns: %v", tableName, err)))
return
Expand All @@ -55,6 +78,7 @@ func (s *serverBuilder) updatedColumns(table *Table, prefix, tableName string, d
if key != "" {
key += "."
}

key += column.Name()
table.ColumnTypes[strings.ToLower(key)] = columnType
}
Expand Down
13 changes: 11 additions & 2 deletions view/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type (
//TODO add secure password storage
db func() (*sql.DB, error)
initialized bool
DBConfig
*DBConfig
mux sync.Mutex
}

Expand All @@ -35,6 +35,7 @@ type (
ConnMaxIdleTimeMs int `json:",omitempty" yaml:",omitempty"`
MaxOpenConns int `json:",omitempty" yaml:",omitempty"`
ConnMaxLifetimeMs int `json:",omitempty" yaml:",omitempty"`
TimeoutTime int `json:",omitempty" yaml:",omitempty"`
}
)

Expand Down Expand Up @@ -67,6 +68,10 @@ func (c *Connector) Init(ctx context.Context, connectors Connectors) error {
c.inherit(connector)
}

if c.DBConfig == nil {
c.DBConfig = &DBConfig{}
}

if err := c.Validate(); err != nil {
return err
}
Expand Down Expand Up @@ -98,7 +103,7 @@ func (c *Connector) DB(ctx context.Context) (*sql.DB, error) {
}

c.mux.Lock()
c.db = aDbPool.DB(ctx, c.Driver, dsn, &c.DBConfig)
c.db = aDbPool.DB(ctx, c.Driver, dsn, c.DBConfig)
aDB, err := c.db()
c.mux.Unlock()

Expand Down Expand Up @@ -142,6 +147,10 @@ func (c *Connector) inherit(connector *Connector) {
if c.Name == "" {
c.Name = connector.Name
}

if c.DBConfig == nil {
c.DBConfig = connector.DBConfig
}
}

func (c *Connector) setDriverOptions(secret *scy.Secret) {
Expand Down
5 changes: 5 additions & 0 deletions view/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ func (d *db) ctxWithTimeout(duration time.Duration) context.Context {

func (p *dbPool) DB(ctx context.Context, driver, dsn string, config *DBConfig) func() (*sql.DB, error) {
builder := &strings.Builder{}

if config == nil {
config = &DBConfig{}
}

builder.WriteString(strconv.Itoa(config.ConnMaxLifetimeMs))
builder.WriteByte('#')
builder.WriteString(strconv.Itoa(config.MaxIdleConns))
Expand Down
15 changes: 10 additions & 5 deletions view/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ func detectColumnsSQL(source string, v *View) (string, []interface{}, error) {

SQL := sb.String()
if source != v.Name && source != v.Table {
discover := metadata.EnrichWithDiscover(source, false)
replacement := rdata.Map{}
replacement.Put(keywords.AndCriteria[1:], "\n\n AND 1=0 ")
replacement.Put(keywords.WhereCriteria[1:], "\n\n WHERE 1=0 ")
SQL = replacement.ExpandAsText(discover)
SQL = ExpandWithFalseCondition(source)
}

var placeholders []interface{}
Expand All @@ -172,6 +168,15 @@ func detectColumnsSQL(source string, v *View) (string, []interface{}, error) {
return SQL, placeholders, nil
}

func ExpandWithFalseCondition(source string) string {
discover := metadata.EnrichWithDiscover(source, false)
replacement := rdata.Map{}
replacement.Put(keywords.AndCriteria[1:], "\n\n AND 1=0 ")
replacement.Put(keywords.WhereCriteria[1:], "\n\n WHERE 1=0 ")
SQL := replacement.ExpandAsText(discover)
return SQL
}

func expandWithZeroValues(SQL string, template *Template) (string, error) {
expandMap := rdata.Map{}
for _, parameter := range template.Parameters {
Expand Down

0 comments on commit a0825da

Please sign in to comment.