Skip to content

Instantly share code, notes, and snippets.

View cvogt's full-sized avatar

Jan Christopher Vogt cvogt

  • Symbiont.io
View GitHub Profile
@cvogt
cvogt / gist:5924001
Last active December 19, 2015 08:09
generate slick release changelog
git log --no-merges '--format=format:* [``%h``](https://github.com/slick/slick/commit/%H) %s' 2.0.0-RC1..head|gsed 's/\*/\\\*/2g'|gsed 's/\[/\\\[/2g'|gsed 's/\]/\\\]/2g'
@cvogt
cvogt / gist:5941501
Created July 6, 2013 22:21
tuple matching helper methods
package util
/**
* tuple helpers for safely adding and matching elements
*/
package object tuples{
// first element extractor
object +:{
def unapply[T1,T2](t: Tuple2[T1,T2]) = Some( t._1, Tuple1(t._2) )
def unapply[T1,T2,T3](t: Tuple3[T1,T2,T3]) = Some( t._1, Tuple2(t._2,t._3) )
def unapply[T1,T2,T3,T4](t: Tuple4[T1,T2,T3,T4]) = Some( t._1, Tuple3(t._2,t._3,t._4) )
import scala.slick.driver.JdbcProfile
import scala.slick.ast.BaseTypedType
trait Profile {
val profile: JdbcProfile
}
trait TableComponent extends Profile {
import profile.simple._
trait TypedId{ def id:Long }
trait Entity[ID]{
def id : Option[ID]
@cvogt
cvogt / gist:8054159
Created December 20, 2013 12:31
This code implements a facility for Slick 1.0.1 to inject custom SQL instead of the Slick produced SQL to be used when running a query
import scala.slick.lifted._
import scala.slick.ast._
import scala.slick.driver._
/** Extends QueryInvoker to allow overriding used SQL statement when executing a query */
trait OverridingInvoker extends BasicDriver{
// 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 {
@cvogt
cvogt / trac2md.rb
Last active January 2, 2016 09:29 — forked from tjdett/trac2md.rb
trac to github flavored markdown
#!/usr/bin/env ruby
body = STDIN.read
body.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '`\1`')
body.gsub!(/\{\{\{(.+?)\}\}\}/m){|m| m.each_line.map{|x| "\t#{x}".gsub(/[\{\}]{3}/,'')}.join}
body.gsub!(/\=\=\=\=\s(.+?)\s\=\=\=\=/, '### \1')
body.gsub!(/\=\=\=\s(.+?)\s\=\=\=/, '## \1')
body.gsub!(/\=\=\s(.+?)\s\=\=/, '# \1')
body.gsub!(/\=\s(.+?)\s\=[\s\n]*/, '')
@cvogt
cvogt / gist:9193220
Last active February 13, 2022 13:50 — forked from ruescasd/gist:7911033
Slick: Dynamic query conditions using the **MaybeFilter** (Updated to support nullable columns)
import scala.slick.lifted.CanBeQueryCondition
// optionally filter on a column with a supplied predicate
case class MaybeFilter[X, Y](val query: scala.slick.lifted.Query[X, Y]) {
def filter[T,R:CanBeQueryCondition](data: Option[T])(f: T => X => R) = {
data.map(v => MaybeFilter(query.filter(f(v)))).getOrElse(this)
}
}
// example use case
import java.sql.Date
@cvogt
cvogt / gist:9239494
Last active September 9, 2019 01:30
Slick app architecture cheat sheet
// Please comment in case of typos or bugs
import scala.slick.driver.H2Driver._
val db = Database.for...(...)
case class Record( ... )
class Records(tag: Tag) extends Table[Record](tag,"RECORDS"){
...
def * = ... <> (Record.tupled,Record.unapply)
// place additional methods here which return values of type Column
@cvogt
cvogt / gist:9519186
Created March 12, 2014 23:53
Nested entity mapping with Slick 1.x
case class Part1(i1: Int, i2: String)
case class Part2(i1: String, i2: Int)
case class Whole(p1: Part1, p2: Part2)
class Tuple2Mapper[Entity,Component1,Tuple1,Component2,Tuple2](
entityFactory: (Component1,Component2) => Entity,
entityExtractor: Entity => Option[(Component1,Component2)],
component1factory: Tuple1 => Component1,
component1extractor: Component1 => Option[Tuple1],
component2factory: Tuple2 => Component2,
component2extractor: Component2 => Option[Tuple2]
trait Monoid[M] {
def zero: M
def add(m1: M, m2: M): M
}
trait Foldable[F[_]] {
def foldl[A, B](as: F[A], z: B, f: (B, A) => B): B
}
def mapReduce[F[_], A, B](as: F[A], m: Monoid[A])
@cvogt
cvogt / gist:d9049c63fc395654c4b4
Created May 17, 2014 20:04
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