Skip to content

Add the Ability to Specify an Alias in a Table Object #426

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

Merged
merged 6 commits into from
Dec 17, 2021

Conversation

jeffgbutler
Copy link
Member

This PR adds a new meta-model object AliasableSqlTable that can contain an alias to be used when a select statement is rendered.

Resolves #413

@jeffgbutler jeffgbutler added this to the 1.3.1 milestone Dec 17, 2021
@coveralls
Copy link

coveralls commented Dec 17, 2021

Coverage Status

Coverage remained the same at 100.0% when pulling 2a03903 on jeffgbutler:new-alias-strategy into 8fa2e76 on mybatis:master.

@jeffgbutler jeffgbutler merged commit d1a4207 into mybatis:master Dec 17, 2021
@jeffgbutler jeffgbutler deleted the new-alias-strategy branch December 17, 2021 20:02
@djechelon
Copy link

Hi, I got this in my project but looks like it's not working for EXISTS queries.

Here is an example

amlObjectMapper.selectDistinct(select -> select
                        .join(amlAttachment, "attachment", on(amlObject.objectid, equalTo(amlAttachment.attachmentid)), and(amlObject.objecttypeid, equalTo(amlAttachment.attachmenttypeid)))
                        .where(amlObject.objecttypeid, isEqualTo(AmlObjectType.ATTACHMENT.getValue()))
                        .and(exists(select(of("1"))
                                .from(taxonomyControlAttachment, "tca")
                                .where(taxonomyControlAttachment.attachmentcontrolid, isEqualTo(taxonomyControlIdVersion))
                                .and(taxonomyControlAttachment.attachmentid, isEqualTo(amlAttachment.attachmentid))
                                .and(taxonomyControlAttachment.attachmenttypeid, isEqualTo(amlAttachment.attachmenttypeid))
                                .applyWhere(limitToTypes != null && limitToTypes.length > 0
                                        ? where -> where.and(amlAttachment.attachmenttype, isIn(limitToTypes))
                                        : x -> {
                                })
                        ))

Resulting query

select distinct AML_Objects.ObjectId,
                AML_Objects.ObjectTypeId,
                AML_Objects.ObjectUpdatedBy,
                AML_Objects.ObjectCreatedBy,
                AML_Objects.ObjectUpdated,
                AML_Objects.ObjectCreated
from AML_Objects
         join AML_Attachments attachment on AML_Objects.ObjectId = attachment.AttachmentId and
                                            AML_Objects.ObjectTypeId = attachment.AttachmentTypeId
where AML_Objects.ObjectTypeId = ?
  and exists(select 1
             from TAXO_TaxonomyControlAttachments tca
             where tca.AttachmentControlId = ?
               and tca.AttachmentId = AttachmentId
               and tca.AttachmentTypeId = AttachmentTypeId)

Expected query (fragment)

               and tca.AttachmentId = attachment.AttachmentId
               and tca.AttachmentTypeId = attachment.AttachmentTypeId)

This is likely because the referenced object amlAttachment is aliased only in the outer query. As you can see, table TaxonomyControlAttachments is aliased tca.

I can confirm, however, this fixes the self-join alias problem. For the join queries, of course.

@djechelon
Copy link

Please note this is not critical to our team as we could change the query in order to make it work. For now, we are not stuck

@jeffgbutler
Copy link
Member Author

The aliases specified this way will not carry into to exists queries (or union queries either). You have a couple of options:

  1. You could use a hard alias on the Attachment table aliasedAttachment = attachment.withAlias("attachment") and then use the aliasedAttachment table and its fields in the query. The aliasedAttachment object should do exactly what you want
  2. You could explicitly qualify the columns in the exists query amlAttachment.attachmentid.qualifiedWith("attachment"). This is a bit wordier, but it will produce what you want without creating a separate table object

@jeffgbutler
Copy link
Member Author

@djechelon I've created a new issue for aliases in exists queries - I should be able to solve this. You can track progress here: #437

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

Successfully merging this pull request may close these issues.

Fluently set table alias at runtime with a new instance
3 participants