feat: implement QueryFromObject method for mssql and postgres wrappers; enhance query building with limits and geometry support
This commit is contained in:
@@ -3,6 +3,8 @@ package dbwrapper
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
dbdialects "git.ksdemosapps.com/kylesoda/go-migrate/internal/app/db-wrapper/db-dialects"
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -127,3 +129,75 @@ func (pw *postgresDbWrapper) SaveMassive(ctx context.Context, schema string, tab
|
||||
|
||||
return affectedRows, nil
|
||||
}
|
||||
|
||||
func (pw *postgresDbWrapper) 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 {
|
||||
switch col.Type() {
|
||||
case "GEOMETRY":
|
||||
fmt.Fprintf(&sbQuery, `ST_AsEWKB("%s") AS "%s"`, col.Name(), col.Name())
|
||||
default:
|
||||
fmt.Fprintf(&sbQuery, `"%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 ")
|
||||
paramIdx := 1
|
||||
|
||||
if q.LowerLimit.IsValid {
|
||||
fmt.Fprintf(&sbQuery, `"%s"`, q.PrimaryKey)
|
||||
if q.LowerLimit.IsInclusive {
|
||||
sbQuery.WriteString(" >=")
|
||||
} else {
|
||||
sbQuery.WriteString(" >")
|
||||
}
|
||||
fmt.Fprintf(&sbQuery, " $%d", paramIdx)
|
||||
paramIdx++
|
||||
}
|
||||
|
||||
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(" <")
|
||||
}
|
||||
fmt.Fprintf(&sbQuery, " $%d", paramIdx)
|
||||
paramIdx++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(&sbQuery, ` ORDER BY "%s" ASC`, q.PrimaryKey)
|
||||
|
||||
queryString := sbQuery.String()
|
||||
|
||||
var queryArgs []any
|
||||
|
||||
if q.LowerLimit.IsValid {
|
||||
queryArgs = append(queryArgs, q.LowerLimit.Value)
|
||||
}
|
||||
|
||||
if q.UpperLimit.IsValid {
|
||||
queryArgs = append(queryArgs, q.UpperLimit.Value)
|
||||
}
|
||||
|
||||
return pw.Query(ctx, queryString, queryArgs...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user