Skip to content

Support ColumnAliasPrefix for AliasableSqlTable to simplify join query mappings #903

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

Closed
luckygc opened this issue Jan 24, 2025 · 2 comments

Comments

@luckygc
Copy link

luckygc commented Jan 24, 2025

<resultMap id="joinMap" type="xxx.Main"> 
    <id property="id" column="id"/>
    <collection property="subs" ofType="xxx.Sub" 
                columnPrefix="sub_"> 
        <id property="id" column="id"/>
    </collection>
</resultMap>
// current 
MainSqlSupport.Main mainTable = MainSqlSupport.Main.withAlias("main");
SubSqlSupport.Sub subTable = SubSqlSupport.Sub.withAlias("sub");

select(mainTable.allColumns(), subTable.id.as("sub_id"),subTable.foo.as("sub_foo"),subTable.bar.as("sub_bar"),subTable.xxx.as("sub_xxx"));


// expected
MainSqlSupport.Main mainTable = MainSqlSupport.Main.withAlias("main");
SubSqlSupport.Sub subTable = SubSqlSupport.Sub.withAlias("sub").withColumnAliasPrefix("sub_");

select(mainTable.allColumns(), subTable.id,subTable.foo,subTable.bar,subTable.xxx);
@luckygc
Copy link
Author

luckygc commented Jan 24, 2025

This feature is also helpful for modifying column names. Only need to update the column name in xxxSqlSupport.

@jeffgbutler
Copy link
Member

I understand your motivation here, but there are corner cases where it would break things (for example, an insert statement with a select).

I continue to believe that column aliases should be specified in the context of an individual select statement.

If you want a column to have a permanent alias, you can configure that currently in the xxxSqlSupport file. You would just need to test carefully to make sure it didn't break anything. Java makes this a little tricky because it doesn't propagate the generic type in chained method calls, but you can do it like this:

    SqlColumn<Integer> col = column("id", JDBCType.INTEGER);
    col = col.as("sub_id");

@luckygc luckygc closed this as completed Jan 25, 2025
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