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

Fix NamingStrategy override of querySchema #1560

Merged
merged 2 commits into from
Aug 15, 2019

Conversation

deusaquilus
Copy link
Collaborator

@deusaquilus deusaquilus commented Aug 13, 2019

Fixes #766 and #1562.

Fix naming strategy override of querySchema by introducing Opinions into the AST.

An Opinion represents a piece of data that needs to be propagated through AST transformations but is not directly related to how ASTs are transformed in most stages. The Opinion, Renameable controls how columns are named (i.e. whether to use a NamingStrategy or not) after most of the SQL transformations are done. Some transformations (e.g. RenameProperties will use Opinions or even modify them so that the correct kind of query comes out at the end of the normalizations. That said, Opinions should be transparent in most steps of the normalization. In some cases e.g. BetaReduction, AST elements need to be neutralized (i.e. set back to defaults in the entire tree) so that this works correctly.

Properties now also have an Opinion about how the NamingStrategy affects their name. For example something like Property.Opinionated(Ident(p), "s_name", Fixed) will become p.s_name even if the NamingStrategy is UpperCase (whereas Property(Ident(p), "s_name") would become p.S_NAME). When Property is constructed without Opinionated being used, the default opinion ByStrategy is used.

Entities also have the renameable opinion. Entity has an Opinion called renameable which will be the value Fixed when a querySchema is specified. That means that even if the NamingSchema is UpperCase, the resulting query will select t_person as opposed to T_PERSON or Person.

  • Unit test all changes
  • Update README.md if applicable
  • Add [WIP] to the pull request title if it's work in progress
  • Squash commits that aren't meaningful changes
  • Run sbt scalariformFormat test:scalariformFormat to make sure that the source files are formatted

@getquill/maintainers

@@ -60,7 +64,9 @@ trait DynamicQueryDsl {
implicit def toQuoted[T <: Action[_]](q: DynamicAction[T]): Quoted[T] = q.q

def dynamicQuery[T](implicit t: ClassTag[T]): DynamicEntityQuery[T] =
dynamicQuerySchema(t.runtimeClass.getName.split('.').last.split('$').last)
Copy link
Collaborator Author

@deusaquilus deusaquilus Aug 13, 2019

Choose a reason for hiding this comment

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

The dynamicQuerySchema method needs to use Fixed naming strategy since it's the equivalent of querySchema. Need to have a separate invocation of Entity here.

@@ -58,7 +58,7 @@ class MetaDslMacro(val c: MacroContext) extends ValueComputation {
q"""
new ${c.prefix}.SchemaMeta[$t] {
private[this] val _entity =
${c.prefix}.quote(${c.prefix}.querySchema[$t](${t.tpe.typeSymbol.name.decodedName.toString}))
${c.prefix}.quote(${c.prefix}.impliedQuerySchema[$t](${t.tpe.typeSymbol.name.decodedName.toString}))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Need to have a separatae impliedQuerySchema method so that the parser knows to not to set the resulting Entity class's renamable field to Fixed. Basically, now all querySchema elements will have their Entity.renameable set to Fixed and impliedQuerySchema elements will have their Entity.renameable set to ByStrategy.

@@ -13,6 +13,9 @@ private[getquill] trait QueryDsl {
@compileTimeOnly(NonQuotedException.message)
def querySchema[T](entity: String, columns: (T => (Any, String))*): EntityQuery[T] = NonQuotedException()

@compileTimeOnly(NonQuotedException.message)
def impliedQuerySchema[T](entity: String, columns: (T => (Any, String))*): EntityQuery[T] = NonQuotedException()
Copy link
Collaborator Author

@deusaquilus deusaquilus Aug 13, 2019

Choose a reason for hiding this comment

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

We can't use querySchema for places where a schema needs to be implicitly inferred (e.g. insert/update/delete) because when querySchema goes into the parser, the parser thinks it's an entity with Entity.renameable = Fixed which is wrong (when insert/update/delete are used).

@@ -104,17 +104,17 @@ trait Unliftables {
}

implicit val queryUnliftable: Unliftable[Query] = Unliftable[Query] {
case q"$pack.Entity.apply(${ a: String }, ${ b: List[PropertyAlias] })" => Entity(a, b)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not sure why the formatter decided to do this but it did

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.

schemaMeta column name case is overridden by NamingStrategy
1 participant