-
Notifications
You must be signed in to change notification settings - Fork 25
/
TableMacro.scala
177 lines (160 loc) · 6.17 KB
/
TableMacro.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package scalasql.query
import scalasql.core.{DialectTypeMappers, Expr => SqlExpr, Queryable, Sc, TypeMapper}
import scala.compiletime.summonInline
import scala.quoted.*
object TableMacros {
def applyImpl[V[_[_]] <: Product](using Quotes, Type[V]): Expr[Table.Metadata[V]] = {
import quotes.reflect.*
val caseClassType = TypeRepr.of[V]
val constructor = caseClassType.typeSymbol.primaryConstructor
val constructorTypeParams = constructor.paramSymss(0)
val constructorValueParams = constructor.paramSymss(1)
def paramType(param: Symbol): TypeRepr = caseClassType.memberType(param)
def isTypeParamType(param: Symbol): Boolean =
paramType(param).typeSymbol.toString != constructorTypeParams.head.toString
def subParam(paramInfo: TypeRepr, tpe: TypeRepr): TypeRepr =
paramInfo.substituteTypes(List(constructorTypeParams.head), List(tpe))
val queryables = '{ (dialect: DialectTypeMappers, n: Int) =>
{
@annotation.nowarn("msg=unused")
given d: DialectTypeMappers = dialect
${
Expr.ofList(constructorValueParams.map { param =>
val paramTpe = paramType(param)
val tpe = subParam(paramTpe, TypeRepr.of[Sc])
val tpe2 = subParam(paramTpe, TypeRepr.of[SqlExpr])
(tpe.asType, tpe2.asType) match {
case ('[t], '[t2]) => '{ summonInline[Queryable.Row[t2, t]] }
}
})
}.apply(n)
}
}
val walkLabels0 = '{ () =>
${
Expr.ofList(constructorValueParams.map { param =>
if (isTypeParamType(param))
paramType(param) match {
case AppliedType(tpeCtor, _) =>
tpeCtor.asType match {
case '[
type t[_[_]]; t] =>
'{ summonInline[Table.ImplicitMetadata[t]].value.walkLabels0() }
}
}
else '{ Seq(${ Expr(param.name) }) }
})
}.flatten
}
val queryable = '{
(
walkLabels0: () => Seq[String],
dialect: DialectTypeMappers,
queryable: Table.Metadata.QueryableProxy
) =>
new Table.Internal.TableQueryable(
walkLabels0,
(table: V[SqlExpr]) =>
${
Expr.ofList(constructorValueParams.zipWithIndex.map { case (param, i) =>
val paramTpe = paramType(param)
val tpe = subParam(paramTpe, TypeRepr.of[Sc])
val tpe2 = subParam(paramTpe, TypeRepr.of[SqlExpr])
(tpe.asType, tpe2.asType) match {
case ('[t], '[t2]) =>
'{
queryable[t2, t](${ Expr(i) }).walkExprs(
${ Select.unique('table.asTerm, param.name).asExprOf[t2] }
)
}
}
})
}.flatten,
construct0 = (args: Queryable.ResultSetIterator) =>
${
Apply(
TypeApply(
Select.unique(New(TypeIdent(TypeRepr.of[V].typeSymbol)), "<init>"),
List(TypeTree.of[Sc])
),
constructorValueParams.zipWithIndex.map { case (param, i) =>
val paramTpe = paramType(param)
val tpe = subParam(paramTpe, TypeRepr.of[Sc]).asType
val tpe2 = subParam(paramTpe, TypeRepr.of[SqlExpr]).asType
(tpe, tpe2) match {
case ('[t], '[t2]) => '{ queryable[t2, t](${ Expr(i) }).construct(args) }.asTerm
}
}
).asExprOf[V[Sc]]
},
deconstruct0 = (r: V[Sc]) =>
${
Apply(
TypeApply(
Select.unique(New(TypeIdent(TypeRepr.of[V].typeSymbol)), "<init>"),
List(TypeTree.of[SqlExpr])
),
constructorValueParams.zipWithIndex.map { case (param, i) =>
val paramTpe = paramType(param)
val tpe = subParam(paramTpe, TypeRepr.of[Sc]).asType
val tpe2 = subParam(paramTpe, TypeRepr.of[SqlExpr]).asType
(tpe, tpe2) match {
case ('[t], '[t2]) =>
'{
queryable[t2, t](${ Expr(i) }).deconstruct(
${ Select.unique('r.asTerm, param.name).asExprOf[t] }
)
}.asTerm
}
}
).asExprOf[V[SqlExpr]]
}
)
}
val vExpr0 = '{
(tableRef: TableRef, dialect: DialectTypeMappers, queryable: Table.Metadata.QueryableProxy) =>
{
@annotation.nowarn("msg=unused")
given d: DialectTypeMappers = dialect
${
Apply(
TypeApply(
Select.unique(New(TypeIdent(TypeRepr.of[V].typeSymbol)), "<init>"),
List(TypeTree.of[Column])
),
constructorValueParams.map { param =>
val paramTpe = paramType(param)
if (isTypeParamType(param))
paramTpe match {
case AppliedType(tpeCtor, _) =>
tpeCtor.asType match {
case '[
type t[_[_]]; t] =>
'{
summonInline[Table.ImplicitMetadata[t]].value.vExpr(tableRef, dialect)
}.asTerm
}
}
else
paramTpe.typeArgs.head.asType match {
case '[t] =>
'{
new Column[t](
tableRef,
Table.columnNameOverride(tableRef.value)(${ Expr(param.name) })
)(using summonInline[TypeMapper[t]])
}.asTerm
}
}
).asExprOf[V[Column]]
}
}
}
'{ new Table.Metadata[V]($queryables, $walkLabels0, $queryable, $vExpr0) }
}
}
trait TableMacros {
inline given initTableMetadata[V[_[_]] <: Product]: Table.Metadata[V] = ${
TableMacros.applyImpl[V]
}
}