Created
December 18, 2015 13:26
-
-
Save glava/d723cff9d3c4d701f2c1 to your computer and use it in GitHub Desktop.
Add save function to every jooq table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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