/
CRUDFeature.scala
220 lines (183 loc) · 7.91 KB
/
CRUDFeature.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package skinny.orm.feature
import scalikejdbc._, SQLInterpolation._
import skinny._
import skinny.orm._
import skinny.orm.feature.associations._
/**
* Provides auto-generated CRUD feature.
*
* @tparam Entity entity
*/
trait CRUDFeature[Entity]
extends CRUDFeatureWithId[Long, Entity]
trait CRUDFeatureWithId[Id, Entity]
extends SkinnyMapperBase[Entity]
with SkinnyModel[Id, Entity]
with ConnectionPoolFeature
with AutoSessionFeature
with AssociationsWithIdFeature[Id, Entity]
with JoinsFeature[Entity]
with IdFeature[Id]
with NoIdCUDFeature[Entity]
with IncludesFeatureWithId[Id, Entity]
with QueryingFeatureWithId[Id, Entity]
with FinderFeatureWithId[Id, Entity]
with DynamicTableNameFeatureWithId[Id, Entity]
with StrongParametersFeature {
override def joins[Id](associations: Association[_]*): CRUDFeatureWithId[Id, Entity] = {
val _self = this
val _associations = associations
// creates new instance but ideally this should be more DRY & safe implementation
new CRUDFeatureWithId[Id, Entity] {
override protected val underlying = _self
override def defaultAlias = _self.defaultAlias
override def rawValueToId(value: Any) = _self.rawValueToId(value).asInstanceOf[Id]
// override def idToRawValue(id: Id) = _self.idToRawValue(id)
override def idToRawValue(id: Id) = id
override def associations = _self.associations ++ _associations
override val defaultJoinDefinitions = _self.defaultJoinDefinitions
override val defaultBelongsToExtractors = _self.defaultBelongsToExtractors
override val defaultHasOneExtractors = _self.defaultHasOneExtractors
override val defaultOneToManyExtractors = _self.defaultOneToManyExtractors
override def autoSession = underlying.autoSession
override def connectionPoolName = underlying.connectionPoolName
override def connectionPool = underlying.connectionPool
override def defaultScope(alias: Alias[Entity]): Option[SQLSyntax] = _self.defaultScope(alias)
def extract(rs: WrappedResultSet, n: SQLInterpolation.ResultName[Entity]) = underlying.extract(rs, n)
}
}
/**
* Replaces table name on runtime.
*
* @param tableName table name
* @return self
*/
override def withTableName(tableName: String): CRUDFeatureWithId[Id, Entity] = {
val _self = this
val dynamicTableName = tableName
// creates new instance but ideally this should be more DRY & safe implementation
new CRUDFeatureWithId[Id, Entity] {
// overwritten table name
override val tableName = dynamicTableName
override def defaultAlias = _self.defaultAlias
override def rawValueToId(value: Any) = _self.rawValueToId(value)
override def idToRawValue(id: Id) = _self.idToRawValue(id)
override protected val underlying = _self
override def associations = _self.associations
override val defaultJoinDefinitions = _self.defaultJoinDefinitions
override val defaultBelongsToExtractors = _self.defaultBelongsToExtractors
override val defaultHasOneExtractors = _self.defaultHasOneExtractors
override val defaultOneToManyExtractors = _self.defaultOneToManyExtractors
override def autoSession = underlying.autoSession
override def connectionPoolName = underlying.connectionPoolName
override def connectionPool = underlying.connectionPool
override def defaultScope(alias: Alias[Entity]): Option[SQLSyntax] = _self.defaultScope(alias)
def extract(rs: WrappedResultSet, n: SQLInterpolation.ResultName[Entity]) = underlying.extract(rs, n)
}
}
/**
* #createWithNamedValues pre-execution.
*
* @param namedValues named values
*/
protected def beforeCreate(namedValues: Seq[(SQLSyntax, Any)])(implicit s: DBSession = autoSession): Unit = {}
/**
* #createWithNamedValues post-execution.
*
* @param namedValues named values
* @param generatedId generated id
*/
protected def afterCreate(namedValues: Seq[(SQLSyntax, Any)], generatedId: Option[Id])(
implicit s: DBSession = autoSession): Unit = {}
/**
* Creates a new entity with named values.
*
* @param namedValues named values
* @param s db session
* @return created count
*/
override def createWithNamedValues(namedValues: (SQLSyntax, Any)*)(implicit s: DBSession = autoSession): Id = {
val (allNamedValues, generatedIdOpt) = {
val primaryKey = column.field(primaryKeyFieldName)
if (namedValues.exists(_._1 == primaryKey)) {
// already primary key is set
val passedIdOpt: Option[Id] = namedValues.find(_._1 == primaryKey).map {
case (k, v) =>
try v.asInstanceOf[Id]
catch { case e: ClassCastException => rawValueToId(v) }
}
val namedValuesWithRawId: Seq[(SQLSyntax, Any)] = namedValues.map {
case (k, v) if k == primaryKey => {
try k -> idToRawValue(v.asInstanceOf[Id])
catch { case e: ClassCastException => k -> v }
}
case (k, v) => k -> v
}
(mergeNamedValuesForCreation(namedValuesWithRawId), passedIdOpt)
} else if (useExternalIdGenerator) {
// generate new primary key value using external key generator
val newId = generateId
(mergeNamedValuesForCreation(namedValues) :+ (primaryKey -> idToRawValue(newId)), Some(newId))
} else {
// no generated key
(mergeNamedValuesForCreation(namedValues), None)
}
}
beforeCreate(allNamedValues)
if (!useExternalIdGenerator && useAutoIncrementPrimaryKey) {
val id = withSQL { insert.into(this).namedValues(allNamedValues: _*) }.updateAndReturnGeneratedKey.apply()
afterCreate(allNamedValues, Some(id).map(_.asInstanceOf[Id]))
convertAutoGeneratedIdToId(id).getOrElse(
throw new IllegalStateException(s"Failed to retrieve auto-generated primary key value from ${tableName} when insertion."))
} else {
withSQL { insert.into(this).namedValues(allNamedValues: _*) }.update.apply()
afterCreate(allNamedValues, generatedIdOpt)
generatedIdOpt.getOrElse(null.asInstanceOf[Id])
}
}
/**
* Returns update query builder with primary key.
*
* @param id primary key
* @return update query builder
*/
def updateById(id: Id): UpdateOperationBuilder = updateBy(byId(id))
/**
* Updates entities with parameters.
*
* @param id primary key
* @param parameters parameters
* @return updated count
*/
def updateById(id: Id, parameters: PermittedStrongParameters): Int = updateById(id).withPermittedAttributes(parameters)
/**
* Returns a query part which represents primary key search condition.
*
* @param id primary key
* @return query part
*/
protected def byId(id: Id): SQLSyntax = sqls.eq(column.field(primaryKeyFieldName), idToRawValue(id))
/**
* Deletes a single entity by primary key.
*
* @param id primary key
* @param s db session
* @return deleted count
*/
def deleteById(id: Id)(implicit s: DBSession = autoSession): Int = deleteBy(byId(id))
override def createWithPermittedAttributes(strongParameters: PermittedStrongParameters)(implicit s: DBSession): Id = {
super.createWithPermittedAttributes(strongParameters).asInstanceOf[Id]
}
override def createWithAttributes(parameters: (Symbol, Any)*)(implicit s: DBSession): Id = {
super.createWithAttributes(parameters: _*).asInstanceOf[Id]
}
// ---------------------
// for SkinnyModel
override def createNewModel(parameters: PermittedStrongParameters): Id = createWithPermittedAttributes(parameters)
override def findAllModels() = findAll()
override def countAllModels(): Long = count()
override def findModels(pageSize: Int, pageNo: Int) = findAllWithLimitOffset(pageSize, pageSize * (pageNo - 1))
override def findModel(id: Id) = findById(id)
override def updateModelById(id: Id, parameters: PermittedStrongParameters) = updateById(id, parameters)
override def deleteModelById(id: Id) = deleteById(id)
}