Skip to content

Instantly share code, notes, and snippets.

@glava
Created December 18, 2015 13:26
Show Gist options
  • Save glava/d723cff9d3c4d701f2c1 to your computer and use it in GitHub Desktop.
Save glava/d723cff9d3c4d701f2c1 to your computer and use it in GitHub Desktop.
Add save function to every jooq table
import org.jooq.impl.TableImpl
import org.jooq.Record
object InsertInto {
// This enables you to write inserts like this
// Tables.USER_TABLE.save(userRecord)
implicit class EnhancedTableImpl[T <: Record](val table: TableImpl[T]) extends AnyVal {
def save(record: T)(implicit context: DSLContext) = {
val fields = record.fields().toSeq
context
.insertInto(table, fields)
.values(seqAsJavaList(fields.zipWithIndex.map(x => record.getValue(x._2))))
.execute()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment