refactor: enhance extraction query handling; add support for JSON column extraction and wildcard patterns
This commit is contained in:
@@ -6,8 +6,11 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/config"
|
||||
dbdialects "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper/db_dialects"
|
||||
"git.ksdemosapps.com/kylesoda/go-migrate/internal/app/models"
|
||||
mssql "github.com/microsoft/go-mssqldb"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -177,25 +180,53 @@ func (mw *mssqlDbWrapper) SaveMassive(ctx context.Context, schema string, table
|
||||
return rowsAffected, nil
|
||||
}
|
||||
|
||||
func (mw *mssqlDbWrapper) QueryFromObject(ctx context.Context, q ExtractionQuery) (RowsResult, error) {
|
||||
func buildExtractQueryMssql(q ExtractionQuery) (string, error) {
|
||||
var sbQuery strings.Builder
|
||||
|
||||
sbQuery.WriteString("SELECT ")
|
||||
|
||||
if len(q.Columns) == 0 {
|
||||
sbQuery.WriteString("*")
|
||||
} else {
|
||||
for i, col := range q.Columns {
|
||||
fmt.Fprintf(&sbQuery, "[%s]", col.Name())
|
||||
hasRegularColumns := len(q.Columns) > 0
|
||||
hasJsonColumns := len(q.FromJsonColumns) > 0
|
||||
|
||||
resolvedJson := make(map[string][]config.FromJsonItem, len(q.FromJsonColumns))
|
||||
if hasJsonColumns {
|
||||
for _, jsonConfig := range q.FromJsonColumns {
|
||||
actualColumnName, err := findColumnByPattern(q.Columns, jsonConfig.Column)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resolvedJson[actualColumnName] = append(resolvedJson[actualColumnName], jsonConfig)
|
||||
}
|
||||
}
|
||||
|
||||
selectParts := make([]string, 0, len(q.Columns)+len(q.FromJsonColumns))
|
||||
if hasRegularColumns {
|
||||
for _, col := range q.Columns {
|
||||
jsonConfigs, isJsonColumn := resolvedJson[col.Name()]
|
||||
if isJsonColumn {
|
||||
for _, jsonConfig := range jsonConfigs {
|
||||
jsonPath := buildJsonPathMssql(jsonConfig.Field)
|
||||
jsonExpr := fmt.Sprintf("JSON_VALUE([%s], '%s') AS [%s]", col.Name(), jsonPath, col.Name())
|
||||
selectParts = append(selectParts, jsonExpr)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
colExpr := fmt.Sprintf("[%s]", col.Name())
|
||||
switch col.Type() {
|
||||
case "GEOMETRY":
|
||||
fmt.Fprintf(&sbQuery, ".STAsBinary() AS [%s]", col.Name())
|
||||
colExpr = fmt.Sprintf("[%s].STAsBinary() AS [%s]", col.Name(), col.Name())
|
||||
}
|
||||
selectParts = append(selectParts, colExpr)
|
||||
}
|
||||
} else if !hasJsonColumns {
|
||||
selectParts = append(selectParts, "*")
|
||||
}
|
||||
|
||||
if i < len(q.Columns)-1 {
|
||||
sbQuery.WriteString(", ")
|
||||
}
|
||||
for i, part := range selectParts {
|
||||
sbQuery.WriteString(part)
|
||||
if i < len(selectParts)-1 {
|
||||
sbQuery.WriteString(", ")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,9 +262,39 @@ func (mw *mssqlDbWrapper) QueryFromObject(ctx context.Context, q ExtractionQuery
|
||||
|
||||
fmt.Fprintf(&sbQuery, " ORDER BY [%s] ASC", q.PrimaryKey)
|
||||
|
||||
queryString := sbQuery.String()
|
||||
return sbQuery.String(), nil
|
||||
}
|
||||
|
||||
// logrus.Debugf("Query: %s", queryString)
|
||||
func findColumnByPattern(columns []models.ColumnType, pattern string) (string, error) {
|
||||
if pattern == "" {
|
||||
return "", fmt.Errorf("column pattern cannot be empty")
|
||||
}
|
||||
|
||||
if before, ok := strings.CutSuffix(pattern, "*"); ok {
|
||||
prefix := before
|
||||
for _, col := range columns {
|
||||
if strings.HasPrefix(col.Name(), prefix) {
|
||||
return col.Name(), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("no column found matching pattern '%s'", pattern)
|
||||
}
|
||||
|
||||
for _, col := range columns {
|
||||
if col.Name() == pattern {
|
||||
return col.Name(), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("column '%s' not found in table columns", pattern)
|
||||
}
|
||||
|
||||
func (mw *mssqlDbWrapper) QueryFromObject(ctx context.Context, q ExtractionQuery) (RowsResult, error) {
|
||||
queryString, err := buildExtractQueryMssql(q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logrus.Debugf("Query: %s", queryString)
|
||||
|
||||
var queryArgs []any
|
||||
|
||||
@@ -247,3 +308,11 @@ func (mw *mssqlDbWrapper) QueryFromObject(ctx context.Context, q ExtractionQuery
|
||||
|
||||
return mw.Query(ctx, queryString, queryArgs...)
|
||||
}
|
||||
|
||||
func buildJsonPathMssql(field string) string {
|
||||
if len(field) > 0 && field[0] == '.' {
|
||||
field = field[1:]
|
||||
}
|
||||
|
||||
return "$." + field
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user