Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database functions & stored procedures #13

Open
oraknabo opened this issue Sep 6, 2017 · 0 comments
Open

Database functions & stored procedures #13

oraknabo opened this issue Sep 6, 2017 · 0 comments

Comments

@oraknabo
Copy link

oraknabo commented Sep 6, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant