Add specific location via geometry column on pool
This commit is contained in:
parent
121b880783
commit
f97e769d4b
5 changed files with 111 additions and 5 deletions
|
|
@ -69,6 +69,15 @@ var Pools = Table[
|
|||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
Geometry: column{
|
||||
Name: "geometry",
|
||||
DBType: "geometry",
|
||||
Default: "NULL",
|
||||
Comment: "",
|
||||
Nullable: true,
|
||||
Generated: false,
|
||||
AutoIncr: false,
|
||||
},
|
||||
},
|
||||
Indexes: poolIndexes{
|
||||
PoolPkey: index{
|
||||
|
|
@ -125,11 +134,12 @@ type poolColumns struct {
|
|||
ID column
|
||||
SiteID column
|
||||
SiteVersion column
|
||||
Geometry column
|
||||
}
|
||||
|
||||
func (c poolColumns) AsSlice() []column {
|
||||
return []column{
|
||||
c.Condition, c.Created, c.CreatorID, c.ID, c.SiteID, c.SiteVersion,
|
||||
c.Condition, c.Created, c.CreatorID, c.ID, c.SiteID, c.SiteVersion, c.Geometry,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3623,6 +3623,7 @@ func (f *Factory) FromExistingPool(m *models.Pool) *PoolTemplate {
|
|||
o.ID = func() int32 { return m.ID }
|
||||
o.SiteID = func() int32 { return m.SiteID }
|
||||
o.SiteVersion = func() int32 { return m.SiteVersion }
|
||||
o.Geometry = func() null.Val[string] { return m.Geometry }
|
||||
|
||||
ctx := context.Background()
|
||||
if m.R.CreatorUser != nil {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@ import (
|
|||
"github.com/Gleipnir-Technology/bob"
|
||||
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||
models "github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/jaswdr/faker/v2"
|
||||
)
|
||||
|
||||
|
|
@ -42,6 +44,7 @@ type PoolTemplate struct {
|
|||
ID func() int32
|
||||
SiteID func() int32
|
||||
SiteVersion func() int32
|
||||
Geometry func() null.Val[string]
|
||||
|
||||
r poolR
|
||||
f *Factory
|
||||
|
|
@ -134,6 +137,10 @@ func (o PoolTemplate) BuildSetter() *models.PoolSetter {
|
|||
val := o.SiteVersion()
|
||||
m.SiteVersion = omit.From(val)
|
||||
}
|
||||
if o.Geometry != nil {
|
||||
val := o.Geometry()
|
||||
m.Geometry = omitnull.FromNull(val)
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
|
@ -174,6 +181,9 @@ func (o PoolTemplate) Build() *models.Pool {
|
|||
if o.SiteVersion != nil {
|
||||
m.SiteVersion = o.SiteVersion()
|
||||
}
|
||||
if o.Geometry != nil {
|
||||
m.Geometry = o.Geometry()
|
||||
}
|
||||
|
||||
o.setModelRels(m)
|
||||
|
||||
|
|
@ -358,6 +368,7 @@ func (m poolMods) RandomizeAllColumns(f *faker.Faker) PoolMod {
|
|||
PoolMods.RandomID(f),
|
||||
PoolMods.RandomSiteID(f),
|
||||
PoolMods.RandomSiteVersion(f),
|
||||
PoolMods.RandomGeometry(f),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -547,6 +558,59 @@ func (m poolMods) RandomSiteVersion(f *faker.Faker) PoolMod {
|
|||
})
|
||||
}
|
||||
|
||||
// Set the model columns to this value
|
||||
func (m poolMods) Geometry(val null.Val[string]) PoolMod {
|
||||
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
||||
o.Geometry = func() null.Val[string] { return val }
|
||||
})
|
||||
}
|
||||
|
||||
// Set the Column from the function
|
||||
func (m poolMods) GeometryFunc(f func() null.Val[string]) PoolMod {
|
||||
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
||||
o.Geometry = f
|
||||
})
|
||||
}
|
||||
|
||||
// Clear any values for the column
|
||||
func (m poolMods) UnsetGeometry() PoolMod {
|
||||
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
||||
o.Geometry = nil
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is sometimes null
|
||||
func (m poolMods) RandomGeometry(f *faker.Faker) PoolMod {
|
||||
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
||||
o.Geometry = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_string(f)
|
||||
return null.From(val)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Generates a random value for the column using the given faker
|
||||
// if faker is nil, a default faker is used
|
||||
// The generated value is never null
|
||||
func (m poolMods) RandomGeometryNotNull(f *faker.Faker) PoolMod {
|
||||
return PoolModFunc(func(_ context.Context, o *PoolTemplate) {
|
||||
o.Geometry = func() null.Val[string] {
|
||||
if f == nil {
|
||||
f = &defaultFaker
|
||||
}
|
||||
|
||||
val := random_string(f)
|
||||
return null.From(val)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m poolMods) WithParentsCascading() PoolMod {
|
||||
return PoolModFunc(func(ctx context.Context, o *PoolTemplate) {
|
||||
if isDone, _ := poolWithParentsCascadingCtx.Value(ctx); isDone {
|
||||
|
|
|
|||
4
db/migrations/00088_pool_geometry.sql
Normal file
4
db/migrations/00088_pool_geometry.sql
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
-- +goose Up
|
||||
ALTER TABLE pool ADD COLUMN geometry Geometry(Point, 4326);
|
||||
-- +goose Down
|
||||
ALTER TABLE pool DROP COLUMN geometry;
|
||||
|
|
@ -20,7 +20,9 @@ import (
|
|||
"github.com/Gleipnir-Technology/bob/orm"
|
||||
"github.com/Gleipnir-Technology/bob/types/pgtypes"
|
||||
enums "github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
||||
"github.com/aarondl/opt/null"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
)
|
||||
|
||||
// Pool is an object representing the database table.
|
||||
|
|
@ -31,6 +33,7 @@ type Pool struct {
|
|||
ID int32 `db:"id,pk" `
|
||||
SiteID int32 `db:"site_id" `
|
||||
SiteVersion int32 `db:"site_version" `
|
||||
Geometry null.Val[string] `db:"geometry" `
|
||||
|
||||
R poolR `db:"-" `
|
||||
|
||||
|
|
@ -57,7 +60,7 @@ type poolR struct {
|
|||
func buildPoolColumns(alias string) poolColumns {
|
||||
return poolColumns{
|
||||
ColumnsExpr: expr.NewColumnsExpr(
|
||||
"condition", "created", "creator_id", "id", "site_id", "site_version",
|
||||
"condition", "created", "creator_id", "id", "site_id", "site_version", "geometry",
|
||||
).WithParent("pool"),
|
||||
tableAlias: alias,
|
||||
Condition: psql.Quote(alias, "condition"),
|
||||
|
|
@ -66,6 +69,7 @@ func buildPoolColumns(alias string) poolColumns {
|
|||
ID: psql.Quote(alias, "id"),
|
||||
SiteID: psql.Quote(alias, "site_id"),
|
||||
SiteVersion: psql.Quote(alias, "site_version"),
|
||||
Geometry: psql.Quote(alias, "geometry"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -78,6 +82,7 @@ type poolColumns struct {
|
|||
ID psql.Expression
|
||||
SiteID psql.Expression
|
||||
SiteVersion psql.Expression
|
||||
Geometry psql.Expression
|
||||
}
|
||||
|
||||
func (c poolColumns) Alias() string {
|
||||
|
|
@ -98,10 +103,11 @@ type PoolSetter struct {
|
|||
ID omit.Val[int32] `db:"id,pk" `
|
||||
SiteID omit.Val[int32] `db:"site_id" `
|
||||
SiteVersion omit.Val[int32] `db:"site_version" `
|
||||
Geometry omitnull.Val[string] `db:"geometry" `
|
||||
}
|
||||
|
||||
func (s PoolSetter) SetColumns() []string {
|
||||
vals := make([]string, 0, 6)
|
||||
vals := make([]string, 0, 7)
|
||||
if s.Condition.IsValue() {
|
||||
vals = append(vals, "condition")
|
||||
}
|
||||
|
|
@ -120,6 +126,9 @@ func (s PoolSetter) SetColumns() []string {
|
|||
if s.SiteVersion.IsValue() {
|
||||
vals = append(vals, "site_version")
|
||||
}
|
||||
if !s.Geometry.IsUnset() {
|
||||
vals = append(vals, "geometry")
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
|
|
@ -142,6 +151,9 @@ func (s PoolSetter) Overwrite(t *Pool) {
|
|||
if s.SiteVersion.IsValue() {
|
||||
t.SiteVersion = s.SiteVersion.MustGet()
|
||||
}
|
||||
if !s.Geometry.IsUnset() {
|
||||
t.Geometry = s.Geometry.MustGetNull()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PoolSetter) Apply(q *dialect.InsertQuery) {
|
||||
|
|
@ -150,7 +162,7 @@ func (s *PoolSetter) Apply(q *dialect.InsertQuery) {
|
|||
})
|
||||
|
||||
q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
|
||||
vals := make([]bob.Expression, 6)
|
||||
vals := make([]bob.Expression, 7)
|
||||
if s.Condition.IsValue() {
|
||||
vals[0] = psql.Arg(s.Condition.MustGet())
|
||||
} else {
|
||||
|
|
@ -187,6 +199,12 @@ func (s *PoolSetter) Apply(q *dialect.InsertQuery) {
|
|||
vals[5] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
if !s.Geometry.IsUnset() {
|
||||
vals[6] = psql.Arg(s.Geometry.MustGetNull())
|
||||
} else {
|
||||
vals[6] = psql.Raw("DEFAULT")
|
||||
}
|
||||
|
||||
return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
|
||||
}))
|
||||
}
|
||||
|
|
@ -196,7 +214,7 @@ func (s PoolSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
|
|||
}
|
||||
|
||||
func (s PoolSetter) Expressions(prefix ...string) []bob.Expression {
|
||||
exprs := make([]bob.Expression, 0, 6)
|
||||
exprs := make([]bob.Expression, 0, 7)
|
||||
|
||||
if s.Condition.IsValue() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
|
|
@ -240,6 +258,13 @@ func (s PoolSetter) Expressions(prefix ...string) []bob.Expression {
|
|||
}})
|
||||
}
|
||||
|
||||
if !s.Geometry.IsUnset() {
|
||||
exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
|
||||
psql.Quote(append(prefix, "geometry")...),
|
||||
psql.Arg(s.Geometry),
|
||||
}})
|
||||
}
|
||||
|
||||
return exprs
|
||||
}
|
||||
|
||||
|
|
@ -646,6 +671,7 @@ type poolWhere[Q psql.Filterable] struct {
|
|||
ID psql.WhereMod[Q, int32]
|
||||
SiteID psql.WhereMod[Q, int32]
|
||||
SiteVersion psql.WhereMod[Q, int32]
|
||||
Geometry psql.WhereNullMod[Q, string]
|
||||
}
|
||||
|
||||
func (poolWhere[Q]) AliasedAs(alias string) poolWhere[Q] {
|
||||
|
|
@ -660,6 +686,7 @@ func buildPoolWhere[Q psql.Filterable](cols poolColumns) poolWhere[Q] {
|
|||
ID: psql.Where[Q, int32](cols.ID),
|
||||
SiteID: psql.Where[Q, int32](cols.SiteID),
|
||||
SiteVersion: psql.Where[Q, int32](cols.SiteVersion),
|
||||
Geometry: psql.WhereNull[Q, string](cols.Geometry),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue