Skip to content

Instantly share code, notes, and snippets.

@leifwickland
Created June 15, 2017 22:45
Show Gist options
  • Save leifwickland/6a13bea7baa5d85a6a4c872b8fdcbd20 to your computer and use it in GitHub Desktop.
Save leifwickland/6a13bea7baa5d85a6a4c872b8fdcbd20 to your computer and use it in GitHub Desktop.
object QueryUtil {
/**
* Generates an SQL comment that indicates where in Scala source the query originated.
*/
def locationFragment(implicit file: sourcecode.File, line: sourcecode.Line): Fragment = {
Fragment.const(s"/* Query from ${file.value}:${line.value} */\n")
}
}
object SomeDao {
def getSomeData = QueryUtil.locationFragment ++ fr"SELECT foo FROM BAR".query[Foo].list
}
// Generates a query that looks like:
// /* Query from usage.scala:2 */
// SELECT foo FROM BAR
@tpolecat
Copy link

Neato! So, doobie already does some of this work with its Pos data type (which uses Haoyi's lib), and fragments already track their locations, so you should pass this information along and then the query checker can use it.

  import doobie.imports._
  import doobie.util.pos._

  def locationFragment(implicit pos: Pos): Fragment = {
    Fragment.const(s"/* Query from $pos */\n", Some(pos))
  }

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