Description
I have a lot of cases where I have to run string functions or stored procedures during a query. So far I've still been able to use sqlstruct by adding the function to the sql tag like this:
type Mystruct struct {
Id string `sql:"id" json:"id"`
Name string `sql:"UCASE(name)" json:"name"`
}
This isn't ideal but it works in most cases using sqlstruct.Columns() but now I'm trying to use sqlstruct.ColumnsAliased() with an additional struct:
type Mystruct2 struct {
Id string `sql:"id" json:"id"`
Name string `sql:"name" json:"name"`
NameSoundex string `sql:"SOUNDEX(name)" json:"name_soundex"`
}
If I create a query like this--
q := fmt.Sprintf(`SELECT %s, %s
FROM table1 ti
LEFT JOIN table2 t2 ON t2.id = t1.id`,
sqlstruct.ColumnsAliased(Mystruct{}, "t1"),
sqlstruct.ColumnsAliased(Mystruct2{}, "t2"))
I get something like--
SELECT t1.id as t1_id, t1.UCASE(name) as t1_UCASE(name),
t2.id as t2_id, t2.name as t2_name, t2.SOUNDEX(name) as t2_SOUNDEX(name)
FROM table1 t1
LEFT JOIN table2 t2 ON t2.id = t1.id
Is there any way around this? If there was a tag like "sql_func" a struct definition like
NameSoundex string `sql:"name" sql_func:"SOUNDEX(%s)" json:"name_soundex"`
could generate something like
SOUNDEX(t2.name) as t2_SOUNDEX_name
I'm probably going to just do this manually with rows.Scan() but I think this could be a useful feature for this package if there's a manageable way to do it.