Last active
August 10, 2019 11:06
-
-
Save frgomes/e5d928f0a77502bfc1975bbacaacfb74 to your computer and use it in GitHub Desktop.
Scala/Slick :: More than 22 fields using nested case classes
This file contains hidden or 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
//---------------------------------------------------------------------------------------------------------------------- | |
// This is an example of how more than 22 fields could be mapped with Slick 3.3.1 (other versions may work as well?). | |
// | |
// The general idea is pretty simple: | |
// 1. define a case class made of nested case classes. | |
// 2. define a projection made of nested projections. | |
// | |
// In addition, you can just get rid of noisy usages of ``GetResult`` generated by the Slick Code Generator. | |
// Just get rid of that, since usage of ``mapTo[T]`` makes definition of projections clean and hygienic. | |
//---------------------------------------------------------------------------------------------------------------------- | |
package my.model | |
trait MyRequestTable { | |
self:Tables => | |
import profile.api._ | |
/** Table description of table tb_api_call_request. Objects of this class serve as prototypes for rows in queries. */ | |
class MyRequest(_tableTag: Tag) extends profile.api.Table[MyRequestRow](_tableTag, Some("guided_repair"), "tb_api_call_request") { | |
def * = _projection | |
def _projection = (requestId, _header, _device, _order, _origin, receiveTimestamp, responseTimestamp).mapTo[MyRequestRow] | |
def _header = (version, id, timestamp, timezone).mapTo[RequestHeader] | |
def _device = (serialNumber, articleNumber, articleId, productModelId, productModelName, constructionYear, constructionWeek, installationDate).mapTo[RequestDevice] | |
def _order = (orderDescription, errorText, symptom1, symptom2, condition, failureType).mapTo[RequestOrder] | |
def _origin = (callCenterId, userProfile, country).mapTo[RequestOrigin] | |
/** Database column request_id SqlType(int identity), PrimaryKey */ | |
val requestId: Rep[Int] = column[Int]("request_id", O.PrimaryKey, O.AutoInc) | |
/** Database column version SqlType(nvarchar), Length(2147483647,true) */ | |
val version: Rep[Option[String]] = column[Option[String]]("version", O.Length(2147483647,varying=true)) | |
/** Database column id SqlType(nvarchar), Length(2147483647,true) */ | |
val id: Rep[Option[String]] = column[Option[String]]("id", O.Length(2147483647,varying=true)) | |
/** Database column timestamp SqlType(datetime) */ | |
val timestamp: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("timestamp") | |
/** Database column timezone SqlType(nvarchar), Length(2147483647,true) */ | |
val timezone: Rep[Option[String]] = column[Option[String]]("timezone", O.Length(2147483647,varying=true)) | |
/** Database column serial_number SqlType(nvarchar), Length(2147483647,true) */ | |
val serialNumber: Rep[Option[String]] = column[Option[String]]("serial_number", O.Length(2147483647,varying=true)) | |
/** Database column article_number SqlType(nvarchar), Length(2147483647,true) */ | |
val articleNumber: Rep[Option[String]] = column[Option[String]]("article_number", O.Length(2147483647,varying=true)) | |
/** Database column article_id SqlType(nvarchar), Length(2147483647,true) */ | |
val articleId: Rep[Option[String]] = column[Option[String]]("article_id", O.Length(2147483647,varying=true)) | |
/** Database column product_model_id SqlType(nvarchar), Length(2147483647,true) */ | |
val productModelId: Rep[Option[String]] = column[Option[String]]("product_model_id", O.Length(2147483647,varying=true)) | |
/** Database column product_model_name SqlType(nvarchar), Length(2147483647,true) */ | |
val productModelName: Rep[Option[String]] = column[Option[String]]("product_model_name", O.Length(2147483647,varying=true)) | |
/** Database column construction_year SqlType(int) */ | |
val constructionYear: Rep[Option[Int]] = column[Option[Int]]("construction_year") | |
/** Database column construction_week SqlType(int) */ | |
val constructionWeek: Rep[Option[Int]] = column[Option[Int]]("construction_week") | |
/** Database column installation_date SqlType(nvarchar), Length(2147483647,true) */ | |
val installationDate: Rep[Option[String]] = column[Option[String]]("installation_date", O.Length(2147483647,varying=true)) | |
/** Database column order_description SqlType(nvarchar), Length(2147483647,true) */ | |
val orderDescription: Rep[Option[String]] = column[Option[String]]("order_description", O.Length(2147483647,varying=true)) | |
/** Database column error_text SqlType(nvarchar), Length(2147483647,true) */ | |
val errorText: Rep[Option[String]] = column[Option[String]]("error_text", O.Length(2147483647,varying=true)) | |
/** Database column symptom1 SqlType(nvarchar), Length(2147483647,true) */ | |
val symptom1: Rep[Option[String]] = column[Option[String]]("symptom1", O.Length(2147483647,varying=true)) | |
/** Database column symptom2 SqlType(nvarchar), Length(2147483647,true) */ | |
val symptom2: Rep[Option[String]] = column[Option[String]]("symptom2", O.Length(2147483647,varying=true)) | |
/** Database column condition SqlType(nvarchar), Length(2147483647,true) */ | |
val condition: Rep[Option[String]] = column[Option[String]]("condition", O.Length(2147483647,varying=true)) | |
/** Database column failure_type SqlType(nvarchar), Length(2147483647,true) */ | |
val failureType: Rep[Option[String]] = column[Option[String]]("failure_type", O.Length(2147483647,varying=true)) | |
/** Database column call_center_id SqlType(nvarchar), Length(2147483647,true) */ | |
val callCenterId: Rep[Option[String]] = column[Option[String]]("call_center_id", O.Length(2147483647,varying=true)) | |
/** Database column user_profile SqlType(nvarchar), Length(2147483647,true) */ | |
val userProfile: Rep[Option[String]] = column[Option[String]]("user_profile", O.Length(2147483647,varying=true)) | |
/** Database column country SqlType(nvarchar), Length(2147483647,true) */ | |
val country: Rep[Option[String]] = column[Option[String]]("country", O.Length(2147483647,varying=true)) | |
/** Database column receive_timestamp SqlType(datetime) */ | |
val receiveTimestamp: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("receive_timestamp") | |
/** Database column response_timestamp SqlType(datetime) */ | |
val responseTimestamp: Rep[Option[java.sql.Timestamp]] = column[Option[java.sql.Timestamp]]("response_timestamp") | |
} | |
/** Collection-like TableQuery object for table MyRequest */ | |
val tbRequests = TableQuery[Tables.MyRequest] | |
} | |
/** Entity class storing rows of table MyRequest, employing nested case classes | |
* @param requestId Database column request_id SqlType(int identity), PrimaryKey | |
* @param header is a nested case class | |
* @param device is a nested case class | |
* @param order is a nested case class | |
* @param origin is a nested case class | |
* @param receiveTimestamp Database column receive_timestamp SqlType(datetime) | |
* @param responseTimestamp Database column response_timestamp SqlType(datetime) */ | |
case class MyRequestRow(requestId: Int, | |
header: RequestHeader, | |
device: RequestDevice, | |
order: RequestOrder, | |
origin: RequestOrigin, | |
receiveTimestamp: Option[java.sql.Timestamp], responseTimestamp: Option[java.sql.Timestamp]) | |
/** Nested case class RequestHeader | |
* @param version Database column version SqlType(nvarchar), Length(2147483647,true) | |
* @param id Database column id SqlType(nvarchar), Length(2147483647,true) | |
* @param timestamp Database column timestamp SqlType(datetime) | |
* @param timezone Database column timezone SqlType(nvarchar), Length(2147483647,true) */ | |
case class RequestHeader(version: Option[String], id: Option[String], timestamp: Option[java.sql.Timestamp], timezone: Option[String]) | |
/** Nested case class RequestDevice | |
* @param serialNumber Database column serial_number SqlType(nvarchar), Length(2147483647,true) | |
* @param articleNumber Database column article_number SqlType(nvarchar), Length(2147483647,true) | |
* @param articleId Database column article_id SqlType(nvarchar), Length(2147483647,true) | |
* @param productModelId Database column product_model_id SqlType(nvarchar), Length(2147483647,true) | |
* @param productModelName Database column product_model_name SqlType(nvarchar), Length(2147483647,true) | |
* @param constructionYear Database column construction_year SqlType(int) | |
* @param constructionWeek Database column construction_week SqlType(int) | |
* @param installationDate Database column installation_date SqlType(nvarchar), Length(2147483647,true) */ | |
case class RequestDevice(serialNumber: Option[String], articleNumber: Option[String], articleId: Option[String], productModelId: Option[String], productModelName: Option[String], constructionYear: Option[Int], constructionWeek: Option[Int], installationDate: Option[String]) | |
/** Nested case class NestedOrder | |
* @param orderDescription Database column order_description SqlType(nvarchar), Length(2147483647,true) | |
* @param errorText Database column error_text SqlType(nvarchar), Length(2147483647,true) | |
* @param symptom1 Database column symptom1 SqlType(nvarchar), Length(2147483647,true) | |
* @param symptom2 Database column symptom2 SqlType(nvarchar), Length(2147483647,true) | |
* @param condition Database column condition SqlType(nvarchar), Length(2147483647,true) | |
* @param failureType Database column failure_type SqlType(nvarchar), Length(2147483647,true) */ | |
case class RequestOrder(orderDescription: Option[String], errorText: Option[String], symptom1: Option[String], symptom2: Option[String], condition: Option[String], failureType: Option[String]) | |
/** Nested case class RequestOrigin | |
* @param callCenterId Database column call_center_id SqlType(nvarchar), Length(2147483647,true) | |
* @param userProfile Database column user_profile SqlType(nvarchar), Length(2147483647,true) | |
* @param country Database column country SqlType(nvarchar), Length(2147483647,true) */ | |
case class RequestOrigin(callCenterId: Option[String], userProfile: Option[String], country: Option[String]) |
This file contains hidden or 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
package my.model | |
object Tables extends { | |
val profile = slick.jdbc.SQLServerProfile | |
} with Tables | |
trait Tables extends MyRequestTable with MyResponseArticleTable with MyResponsePartTable { | |
val profile: slick.jdbc.JdbcProfile | |
import profile.api._ | |
val schema: profile.SchemaDescription = Array(tbMyRequests.schema, tbMyResponseArticles.schema, tbMyResponseParts.schema).reduceLeft(_ ++ _) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment