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

Adds initial aliasing support. #346

Closed
wants to merge 3 commits into from
Closed

Conversation

telser
Copy link
Contributor

@telser telser commented Feb 27, 2024

The highlights:

  • Moving to use 'Qualified ColumnName' in many/most places. This allows for the aliasing to be used naturally in those places.

  • Add an alias type and function to create a 'Qualified ColumnName' with it. This signals that 'Alias' is in fact different than 'ColumnName' or 'TableName', and reusing them for aliasing is not always appropriate.

  • Adding a 'MarshallAlias' constructor to the 'SqlMarshaller' so that an alias can be attached to all of the natural fields in a marshaller in a single function call from users. Synthetic fields could be refactored with this facility, but that was left out here to keep the already big changeset smaller.

  • Many of the fieldDefinition helpers now have a version to support generating various subexpressions with an alias along with the fieldDefinition. This is a choice to allow the simple case to not deal with the complexity of the alias, but make it easier for when aliases are needed.

  • A small number of helpers are added to make generation of bits of SQL, such as a 'TableReferenceList' that includes the 'AS alias' piece.

The highlights:

- Moving to use 'Qualified ColumnName' in many/most places. This
allows for the aliasing to be used naturally in those places.

- Add an alias type and function to create a 'Qualified ColumnName'
with it. This signals that 'Alias' is in fact different than
'ColumnName' or 'TableName', and reusing them for aliasing is not
always appropriate.

- Adding a 'MarshallAlias' constructor to the 'SqlMarshaller' so that
an alias can be attached to all of the natural fields in a marshaller
in a single function call from users. Synthetic fields could be
refactored with this facility, but that was left out here to keep the
already big changeset smaller.

- Many of the fieldDefinition helpers now have a version to support
generating various subexpressions with an alias along with the
fieldDefinition. This is a choice to allow the simple case to not deal
with the complexity of the alias, but make it easier for when aliases
are needed.

- A small number of helpers are added to make generation of bits of
SQL, such as a 'TableReferenceList' that includes the 'AS alias' piece.
'TableDefinition'. This is the safest way to build a 'Select', because table
name and columns are all read from the 'TableDefinition'. If the table is
being managed with Orville auto-migrations, this will match the schema in the
database.
Copy link
Member

Choose a reason for hiding this comment

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

This should probably say something about how the alias is used

@@ -49,7 +49,7 @@ newtype ColumnDefinition
-}
columnDefinition ::
-- | The name the resulting column should have.
ColumnName ->
Qualified ColumnName ->
Copy link
Member

Choose a reason for hiding this comment

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

It seems like there are a number of replace like this one where ColumnName got replaced with Qualified ColumnName in DDL related expressions. In a good number of these I don't think qualified columns names are actually permitted by PostgreSQL. For instance PostgreSQL rejects the following:

create table qxjit (qxjit.foo int);

I don't think that our Haskell interface should use Qualified ColumnName in places where PostgreSQL will reject qualified syntax.

fieldNotEqualsWithAlias :: Maybe Expr.Alias -> FieldDefinition nullability a -> a -> Expr.BooleanExpr
fieldNotEqualsWithAlias mbAlias =
whereColumnComparisonWithAlias mbAlias Expr.notEquals

Copy link
Member

Choose a reason for hiding this comment

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

I think we should revisit adding WithAlias versions of all these functions. I don't have an immediate suggestion, but having 2 versions of each function seems like a pretty inconvenient API for users

Removing the Qualified in a handful of places where it really should
have never been in the first place.
@telser
Copy link
Contributor Author

telser commented Apr 6, 2024

Closing this in favor of #352.

@telser telser closed this Apr 6, 2024
@telser telser deleted the telser/add-alias-support branch April 6, 2024 01:38
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.

2 participants