/
JdbcMappingCompilerComponent.scala
52 lines (45 loc) · 2.18 KB
/
JdbcMappingCompilerComponent.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package scala.slick.jdbc
import scala.slick.compiler.{InsertCompiler, CompilerState, CodeGen}
import scala.slick.ast._
import scala.slick.driver.JdbcDriver
import scala.slick.util.SQLBuilder
import scala.slick.SlickException
import scala.slick.profile.RelationalMappingCompilerComponent
/** JDBC driver component which contains the mapping compiler and insert compiler */
trait JdbcMappingCompilerComponent extends RelationalMappingCompilerComponent { driver: JdbcDriver =>
type RowReader = PositionedResult
type RowWriter = PositionedParameters
type RowUpdater = PositionedResult
trait MappingCompiler extends super.MappingCompiler {
def createColumnConverter(n: Node, path: Node, option: Boolean, column: Option[FieldSymbol]): ResultConverter = {
val ti = driver.typeInfoFor(n.nodeType)
val autoInc = column.map(_.options.contains(ColumnOption.AutoInc)).getOrElse(false)
new ResultConverter {
def read(pr: RowReader) =
if(option) ti.nextValue(pr)
else ti.nextValueOrElse(
if(ti.nullable) None
else throw new SlickException("Read NULL value for ResultSet column "+n),
pr
)
def update(value: Any, pr: RowUpdater) = ti.updateValue(value, pr)
def set(value: Any, pp: RowWriter, forced: Boolean) =
if(forced || !autoInc) ti.setValue(value, pp)
}
}
}
/** Code generator phase for JdbcProfile-based drivers. */
class JdbcCodeGen(f: QueryBuilder => SQLBuilder.Result) extends CodeGen with MappingCompiler {
def apply(state: CompilerState): CompilerState = state.map(n => apply(n, state))
def apply(node: Node, state: CompilerState): Node =
ClientSideOp.mapResultSetMapping(node, keepType = true) { rsm =>
val sbr = f(driver.createQueryBuilder(rsm.from, state))
val nfrom = CompiledStatement(sbr.sql, sbr, rsm.from.nodeType)
val nmap = CompiledMapping(compileMapping(rsm.map), rsm.map.nodeType)
rsm.copy(from = nfrom, map = nmap).nodeTyped(rsm.nodeType)
}
}
class JdbcInsertCompiler extends InsertCompiler with MappingCompiler {
def createMapping(ins: Insert) = CompiledMapping(compileMapping(ins.map), ins.map.nodeType)
}
}