volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Geometric types for Postgres #259

Closed saulortega closed 6 years ago

saulortega commented 6 years ago

It was not easy to try it in v3. It's very broken! ("postgres" changes to "psql", missing ALL imports in main_test.go, and so on...)

But in the end I succeeded. :)

Well, before doing PR, I want to show here the changes that would be added to the code, and to know your opinions. For now, I did tests with the point type. These same changes should be made for the other geometric types.

OK, the point type: https://github.com/saulortega/pgeo/blob/master/point.go

randomize/randomize.go:

import (
        //...
    "github.com/volatiletech/sqlboilerv3/strmangle"
    "github.com/volatiletech/sqlboilerv3/types"
    "github.com/saulortega/pgeo" // -------------- New import added --------------
)

//...

var (
        //...
    typeStringArray  = reflect.TypeOf(types.StringArray{})
    typeHStore       = reflect.TypeOf(types.HStore{})
    typePoint        = reflect.TypeOf(pgeo.Point{}) // -------------- New added --------------
)

//...

func randomizeField(s *Seed, field reflect.Value, fieldType string, canBeNull bool) error {
//...
            case typeHStore:
                value := types.HStore{}
                value[randStr(s, 3)] = sql.NullString{String: randStr(s, 3), Valid: s.nextInt()%3 == 0}
                value[randStr(s, 3)] = sql.NullString{String: randStr(s, 3), Valid: s.nextInt()%3 == 0}
                field.Set(reflect.ValueOf(value))
                return nil
                // --------------------- BEGIN ADDED LINES (none random yet, really :) ) -----------------
            case typePoint:
                value = pgeo.Point{}
                field.Set(reflect.ValueOf(value))
                return nil
                // ---------------------- END ADDED LINES -------------------
            }
//...
}

importers/imports.go:

func NewDefaultImports() Collection {
        //...
        "types.Hstore": {
            ThirdParty: List{`"github.com/volatiletech/sqlboilerv3/types"`},
        },
        // --------------------- BEGIN ADDED LINES -----------------
        "pgeo.Point": {
            ThirdParty: List{`"github.com/saulortega/pgeo"`},
        },
        // ---------------------- END ADDED LINES -------------------
        //...
}

drivers/sqlboiler-psql/driver/psql.go:

func (p *PostgresDriver) TranslateColumnType(c drivers.Column) drivers.Column {
        //...
        case "date", "time", "timestamp without time zone", "timestamp with time zone":
            c.Type = "time.Time"
        // --------------------- BEGIN ADDED LINES -----------------
        case "point":
            c.Type = "pgeo.Point"
        // ---------------------- END ADDED LINES -------------------
        case "ARRAY":
        //...
}

That is all.

What do you think, Aaron?

aarondl commented 6 years ago

Hmm. v3 shouldn't be that broken... There were some fixes contributed recently so it should be working? There shouldn't be any imports missing for tests. Though yes, postgres -> psql was a breaking change.

I'd hope that we could put the Point type into the sqlboiler types package. No real extra comments except for that until I see the real code :)

saulortega commented 6 years ago

Sure. But, should we create a subpackage for these types? Because other databases with the same types work differently, so foreseeing a future implementation for Mysql (or other) it must be taken into account that the Point type of Mysql is different than the Point type of Postgres.

aarondl commented 6 years ago

Sure thing.

aarondl commented 6 years ago

Closing this in favor of the PR #263