feat: implement QueryFromObject method for mssql and postgres wrappers; enhance query building with limits and geometry support
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
dbdialects "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper/db-dialects"
|
||||
mssql "github.com/microsoft/go-mssqldb"
|
||||
@@ -175,3 +176,72 @@ func (mw *mssqlDbWrapper) SaveMassive(ctx context.Context, schema string, table
|
||||
|
||||
return rowsAffected, nil
|
||||
}
|
||||
|
||||
func (mw *mssqlDbWrapper) QueryFromObject(ctx context.Context, q ExtractionQuery) (RowsResult, 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())
|
||||
|
||||
switch col.Type() {
|
||||
case "GEOMETRY":
|
||||
fmt.Fprintf(&sbQuery, ".STAsBinary() AS [%s]", col.Name())
|
||||
}
|
||||
|
||||
if i < len(q.columns)-1 {
|
||||
sbQuery.WriteString(", ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(&sbQuery, " FROM [%s].[%s]", q.Schema, q.Table)
|
||||
|
||||
if q.LowerLimit.IsValid || q.UpperLimit.IsValid {
|
||||
sbQuery.WriteString(" WHERE ")
|
||||
|
||||
if q.LowerLimit.IsValid {
|
||||
fmt.Fprintf(&sbQuery, "[%s]", q.PrimaryKey)
|
||||
if q.LowerLimit.IsInclusive {
|
||||
sbQuery.WriteString(" >=")
|
||||
} else {
|
||||
sbQuery.WriteString(" >")
|
||||
}
|
||||
sbQuery.WriteString(" @min")
|
||||
}
|
||||
|
||||
if q.LowerLimit.IsValid && q.UpperLimit.IsValid {
|
||||
sbQuery.WriteString(" AND ")
|
||||
}
|
||||
|
||||
if q.UpperLimit.IsValid {
|
||||
fmt.Fprintf(&sbQuery, "[%s]", q.PrimaryKey)
|
||||
if q.UpperLimit.IsInclusive {
|
||||
sbQuery.WriteString(" <=")
|
||||
} else {
|
||||
sbQuery.WriteString(" <")
|
||||
}
|
||||
sbQuery.WriteString(" @max")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(&sbQuery, " ORDER BY [%s] ASC", q.PrimaryKey)
|
||||
|
||||
queryString := sbQuery.String()
|
||||
|
||||
var queryArgs []any
|
||||
|
||||
if q.LowerLimit.IsValid {
|
||||
queryArgs = append(queryArgs, sql.Named("min", q.LowerLimit.Value))
|
||||
}
|
||||
|
||||
if q.UpperLimit.IsValid {
|
||||
queryArgs = append(queryArgs, sql.Named("max", q.UpperLimit.Value))
|
||||
}
|
||||
|
||||
return mw.Query(ctx, queryString, queryArgs...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user