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

feat: EXPOSED-77 Support entity class for table with composite primary key #1987

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

bog-walk
Copy link
Member

@bog-walk bog-walk commented Feb 9, 2024

Phase 1:

  • Adds a new CompositeIdTable that allows id columns to be marked for EntityID inclusion using Column.compositeEntityid().
  • Adds all the necessary entity class objects, including a CompositeID that will be wrapped by EntityID for value comparison.
  • Uses a 'fake' column (not registered with the DB) to keep existing logic clean, with a few advantages:
    • CompositeIdTable.id can be used in DSL in the same way any IdTable.id can (for example, in WHERE or SELECT clauses). This means users can easily include all deconstructed PK columns in their queries.
    • ResultRow[CompositeIdTable.id] is also possible. Even though the result set returns each PK column value separately, using the previous accessor returns a new EntityID<CompositeID>, just as for a standard entity id column.
  • Conditional logic for the exact IdTable subtype has been extracted (in most places) to the lowest common point possible.

Phase 2 - About References:

This PR currently includes minimal changes to Reference logic (only for referencedOn). The better option seems to be to add a property to all reference classes. This new property would provide the full set of reference columns, while leaving the original single column reference property as an internal delegate (like an ambassador for the other columns), especially when it comes to managing the reference entity in the cache.

Ideally I'd split this PR into 2, but support isn't really complete until referencing is working, so I'd like for this to be an iterative process, if possible.
Once everything is agreed on with the first phase, I'll commit the improved reference changes for review.

@bog-walk bog-walk requested review from e5l and joc-a February 9, 2024 04:46
Copy link
Member

@e5l e5l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check the comparison and let's discuss merging

}

override fun equals(other: Any?): Boolean {
if (other !is CompositeID) return false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we compare CompositeId with one mapping with EntityId?

) : HashMap<Column<*>, Comparable<*>?>(idMapping), Comparable<CompositeID> {
override fun toString(): String = "CompositeID(${entries.joinToString { "${it.key.name}=${it.value}" }})"

override fun hashCode(): Int = entries.fold(0) { acc, entry ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single entry CompositeId will always have the same value as EntityId of same type

// SQLite excluded from all tests as it only allows auto-increment on single column PKs.
// SQL Server is sometimes excluded because it doesn't allow inserting explicit values for identity columns.
class CompositeIdTableEntityTest : DatabaseTestsBase() {
object Publishers : CompositeIdTable("publishers") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking if we can try make CompositeIdTable to IdTable by default, let's discuss

…y key

- Add base objects for new CompositeEntity and CompositeIdTable.
- Use existing implementation via EntityID to wrap new comparable type, CompositeID.
- Retain use of IdTable.id via a placeholder column that can deconstruct to its
component columns in DSL and ResultRow accessors.
- Add base unit tests.
…y key

- Extract logic between regular id columns and composite id columns to lower locations.
- Add KDocs and change some object locations.
- Flesh out unit tests
…y key

- Remove changes to reference logic.
- Small change to unit test.
- Rebase from main.
…y key

- Update KDocs to reflect merged changes
- Add CompositeID equality comparison for EntityID<*>
…y key

Clean up some logic

Add first example of supporting a simple referencedOn relation to a composite
primary key.
@bog-walk bog-walk force-pushed the bog-walk/support-composite-entityid branch from a0630fd to 54b8d89 Compare February 21, 2024 23:49
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

Successfully merging this pull request may close these issues.

None yet

2 participants