Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This code implements a facility for Slick 2.0 to inject custom SQL instead of the Slick produced SQL to be used when running a query
import scala.slick.lifted.{TableQuery => _}
import scala.slick.ast._
import scala.slick.driver._
import scala.language.implicitConversions
/** Extends QueryInvoker to allow overriding used SQL statement when executing a query */
trait OverridingInvoker extends JdbcDriver{
// get the extended QueryInvoker into the .simple._ implicits
override val Implicit: Implicits = new Implicits
override val simple: Implicits with SimpleQL = new Implicits with SimpleQL
class Implicits extends super.Implicits {
override implicit def queryToAppliedQueryInvoker[U](q: Query[_,U])
= super.queryToAppliedQueryInvoker(q: Query[_,U]).asInstanceOf[UnitQueryInvoker[U]]
}
override def createUnitQueryInvoker[R](tree: Node): UnitQueryInvoker[R] = new UnitQueryInvoker[R](tree)
// extended QueryInvoker
class UnitQueryInvoker[R](n: Node) extends super.UnitQueryInvoker[R](n) {
def overrideSql(sql: String) = new PatchedUnitQueryInvoker[R](sql,n)
}
// QueryInvokers that patch the used SQL
class PatchedUnitQueryInvoker[U](sql:String, n: Node) extends UnitQueryInvoker[U](n){
override def getStatement = sql
}
}
// Create a custom MySQL driver patching in the Overriding invoker
object CustomMySQLDriver extends MySQLDriver with OverridingInvoker
object Main extends App{
// Import stuff from the patched driver instead of the default
import CustomMySQLDriver.simple._
// please fill in jdbc connection url and driver
Database.forURL("...", driver = "...") withSession {
// tamper with SQL statement
val munged = Query(Suppliers).selectStatement + " where SUP_ID = 49"
// inject munged version
println(Query(Suppliers).overrideSql(munged).list)
}
}
@cvogt

This comment has been minimized.

Copy link
Owner Author

commented May 17, 2014

There is a PR to add this feature natively to Slick 2.1 slick/slick#810

@killdashnine

This comment has been minimized.

Copy link

commented Feb 13, 2015

Any hints to make this work for 2.1? As the PR is currently not in the 2.1 release but the 3.0 milestone

@carlosnagac1

This comment has been minimized.

Copy link

commented Aug 31, 2016

help me
hack on assault fire plsss.......

@masoy143

This comment has been minimized.

Copy link

commented Sep 1, 2016

how to insert

@xjboybaldomero

This comment has been minimized.

Copy link

commented Sep 11, 2016

How to download Slick Inject ?? on assault fire

@PhatoomImBa

This comment has been minimized.

Copy link

commented Sep 12, 2016

help me in aasult fire please in wall hack

@albatrosrony

This comment has been minimized.

Copy link

commented Sep 28, 2016

pa pasa po ng slick inject sa acc ko sa fb ito po ang name ko > aqu afwaller < please po pasahan niyo po ako

@aldub123x

This comment has been minimized.

Copy link

commented Oct 18, 2016

hahahahahahaha what the fuck

@Maiakov

This comment has been minimized.

Copy link

commented Jul 9, 2018

why not present in slick 3+ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.