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

Do not rename columns in generated JOINs unless necessary #20

Open
Alexander-- opened this issue Sep 29, 2015 · 3 comments
Open

Do not rename columns in generated JOINs unless necessary #20

Alexander-- opened this issue Sep 29, 2015 · 3 comments

Comments

@Alexander--
Copy link

Right now AnnotatedSQL mangles column names during joins by appending "as table_name___column_name" to each column in generated SELECT statement, e.g.

CREATE VIEW nearest as SELECT locations._id as _id, locations.coslat as locations_coslat, locations.sinlat as locations_sinlat...

While occasionally useful and more mistake-proof, this prevents hardcoding column names in really complex statements. Instead the generator should check for duplicate names and rename columns only when necessary. For most purposes it should be enough to maintain a single global list of names and rename columns when any two tables have column with the same name. Otherwise just keep old names.

Hopefully, this won't not introduce any breakage, but just in case you can leave old behavior by default and make it toggleable with apt option.

@hamsterksu
Copy link
Owner

Library generates class with all new columns names grouped by tables from view

Class name like ClassNameSchema2

@Alexander--
Copy link
Author

I know, but sometimes one really have to hardcode them in several places. E.g. I have a following method in my code (taken from this SO answer):

public static String buildDistanceQuery(double latitude, double longitude) {
final double coslat = Math.cos(Math.toRadians(latitude));
final double sinlat = Math.sin(Math.toRadians(latitude));
final double coslng = Math.cos(Math.toRadians(longitude));
final double sinlng = Math.sin(Math.toRadians(longitude));

return "(" + coslat + "*" + LocationColumns.COSLAT
        + "*(" + LocationColumns.COSLNG + "*" + coslng
        + "+" + LocationColumns.SINLNG + "*" + sinlng
        + ")+" + sinlat + "*" + LocationColumns.SINLAT 
        + ") AS " + LocationColumns.DISTANCE;
}

It is used in queries to several tables/views, so I have to either write lots of SQL manually or do complex manipulations with column names to make it work with AnnotatedSQL.

@Alexander--
Copy link
Author

Ok, I have notices that @SimpleView does use a projection passed to it. So I tried to create a quick fix by renaming a column twice (in hopes, that new name would be usable in the rest of SELECT) but... apparently SQL "AS" is not supposed to work this way. I guess, I will just hardcode these queries for now.

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

2 participants