-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to map multi-column values? #65
Comments
Do you mean something like this? val transactionExtractor = extract[Transaction](
time = TransactionsTable.time
amount = monetaryValueExtractor
)
val monetaryValueExtractor = extract[MonetaryValue](
amount = TransactionsTable.amount
currencyCode = TransactionsTable.currencyCode
)
insert
.into(TransactionsTable)
.values(transactionExtractor.settersFor(
Transaction(DateTime.now, MonetaryValue(BigDecimal(13.00), "GBP")
))
|
Maybe, only if I can accomplish the following: Say we have a REST API for transactions like so:
Now we want to be able to allow callers only asking for certain fields, say:
How would your approach allow me to select only Also, looking at the code I can't tell how I would be able to filter rows by Thanks, Drew |
sqlest determines at compile time what the result of the select statement running will be. Therefore if you want the fields within case class Transaction(time: Option[Date], amount: Option[MonetaryValue])
def transactionExtractor(includeTime: Boolean, includeAmount: Boolean) = extract[Transaction](
time = if (includeTime) TransactionsTable.time.asOption else extractConstant(Option.empty[Date])
amount = if (includeAmount) monetaryValueExtractor else extractConstant(Option.empty[MonetaryValue])
) Regarding filtering by amount: unfortunately there's nothing built in for filtering by composite values at the moment. I think it should be easy enough to implement this: monetaryValueExtractor ==== amount
implicit class ExtractorComparisionOps[A](extractor: sqlest.extractor.Extractor[ResultSet, A]) {
def ====(value: A): Column[Boolean] =
extractor.settersFor(value).map {
case Setter(column, value) => sqlest.ast.InfixFunctionColumn[Boolean]("=", column, value)
}.reduce(_ && _)
} I will look into adding this |
How can I map a single Scala value to multiple columns in the database? For example:
Consider mapping class
Transaction
as defined below:Where MonetaryValue is defined as:
to a table like so:
Looking at the code,
ColumnType
issealed
so not sure how something like this would be possible.The text was updated successfully, but these errors were encountered: