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

Provide comparison operators for supported data types #677

Open
mxl opened this issue Dec 21, 2016 · 11 comments · Fixed by #2598
Open

Provide comparison operators for supported data types #677

mxl opened this issue Dec 21, 2016 · 11 comments · Fixed by #2598
Assignees

Comments

@mxl
Copy link
Contributor

mxl commented Dec 21, 2016

Version: 1.0.2-SNAPSHOT

E. g. for java.util.Date:

trait Operators {
  this: SqlContext[_, _] =>
  implicit class DateComparison(left: Date) {
    def >(right: Date) = quote(infix"$left > $right".as[Boolean])

    def <(right: Date) = quote(infix"$left < $right".as[Boolean])

    def >=(right: Date) = quote(infix"$left >= $right".as[Boolean])

    def <=(right: Date) = quote(infix"$left <= $right".as[Boolean])
  }
}

val ctx = new MirrorContext[MirrorSqlDialect,SnakeCase] with Operators

Also:

  • string
  • UUID
  • org.joda.time types

@getquill/maintainers WDYT?

@fwbrasil
Copy link
Collaborator

@mxl is this fixed by #678

@mxl mxl added the enhancement label May 9, 2017
@mxl
Copy link
Contributor Author

mxl commented Jul 7, 2017

@fwbrasil Sorry for late response 😄 No, #678 is just documentation update, but I want to integrate this in Quill.

@fwbrasil
Copy link
Collaborator

fwbrasil commented Jul 7, 2017

@mxl sounds good

@mosyp
Copy link
Collaborator

mosyp commented Sep 14, 2017

@mxl What if we could introduce some new dsl, something like this

trait ComparisonOperatorsDsl {
  this: Context[_, _] =>
  trait ComparisonOperators[T] {
    def left: T
    def >(right: T) = quote(infix"$left > $right".as[Boolean])
      ...
  }

  implicit class StringComparison(left: String) extends ComparisonOperators[String]
}

trait SqlComparisonOperators {
  this: SqlContext[_, _] =>

  implicit class DateComparison(left: Date) extends ComparisonOperators[Date]
  implicit class ContextSpecificComparison(left: ContextSpecific) extends ComparisonOperators[ContextSpecific]
}

@mxl
Copy link
Contributor Author

mxl commented Sep 14, 2017

@mentegy Cool! 👍

@mxl
Copy link
Contributor Author

mxl commented Sep 14, 2017

Also it would be nice to provide other operators, for example, string concatenation:

implicit class StringConcatenation(left: String) {
   def +(right: String) = quote(infix"CONCAT($left, $right)".as[String])
}

I suggest to change title and description of this issue and not create another one.
WDYT @getquill/maintainers?

@mosyp
Copy link
Collaborator

mosyp commented Sep 14, 2017

@mxl for string concat we can just enhance this class https://github.com/getquill/quill/blob/master/quill-sql/src/main/scala/io/getquill/context/sql/dsl/SqlDsl.scala#L8-L10, since it only applicable for sql. But this issue leave only for comparisons

@mosyp
Copy link
Collaborator

mosyp commented Sep 14, 2017

also i'm not sure that + will work for strings since default method from scala will take a place first. Changes to parsing ast required

@mxl
Copy link
Contributor Author

mxl commented Sep 14, 2017

@mentegy Ok!

@deusaquilus
Copy link
Collaborator

For things like Date comparison, I'd like to have operators that I can use both inside-of and outside-of Quill contexts. Otherwise my coworkers are going to try to use these wonderful > and < operators outside of quoted contexts and lots of WTFs will result. I'd like something like this:

implicit class DateOps(d:java.util.Date /* Or LocalDateTime etc...*/ ) {
  def >(o:java.util.Date /* Or LocalDateTime etc...*/):Boolean = d.getTime > o.getTime
  def <(o:java.util.Date /* Or LocalDateTime etc...*/):Boolean = d.getTime < o.getTime
  // etc...
}

Then have Parsing.scala figure this all out e.g:

val dateContextParser: Parser[Ast] = Parser[Ast] {
  case q"$pack.DateOps($d).>($o) =>
    BinaryOperation(astParser(d), NumericOperator.`>`, astParser(o))
  // etc...
}

Then people can use these operators both inside and outside of Quill contexts:

// It works here:
val q = quote {
  query[Record].filter(_.createdAt > lift(runtimeDateValue))
}
// It also works here:
val r:Record = ...
if (r.createdAt > runtimeDateValue) doSomething()

WDYT? @mentegy @mxl @fwbrasil

@slgobinath
Copy link

slgobinath commented May 19, 2021

Hi, Any updates on this feature? I am looking for a way to compare String using quill. I tried to extend quill using infix function as shown below (code from #862 ), but the operators are conflicting with the default Scala comparison operators.

implicit class StringOperators(left: String) {
  def >(right: String) = quote(infix"STRCMP($left, $right) > 0".as[Boolean])
}

If I change > to something like >> or >>>, it works but looks odd.

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

Successfully merging a pull request may close this issue.

5 participants