-
Notifications
You must be signed in to change notification settings - Fork 142
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
SQL scanner/valuer #44
Comments
Solution B +1 |
I’m also in favor of option B, it has more flexibility but will require a bit more work from the user. One can always write their own scanner/valuer on a wrapper type, but the other way around is more work. |
Solution B +1 |
I really like solution B, could you please create PR for this? |
Is there an update on this? Could really use this functionality. |
Money's Scan() function assumes that it receives a single column where the src value is a comma- separated string in the format "amount,currency_code" This can be done in SQLite like this: SELECT amount || "," || currency as 'amount' Because the driver.Valuer interface only returns a single value, it does not make sense to implement driver.Valuer on Money and it is left to the client to save the Amount and Currency into two separate fields.
strings.Split(src,,) will return a slice with length 2 even if one of the strings is empty
@Rhymond I have created a PR for this issue (#99). I first implemented the This led me to some research where I learned that the recommended way to implement I have tested #99 in my own project using golang's
and successfully deserialized two separate columns into a single |
This allows any Golang ORM which supports the sql.Scanner to serialize (via sql.Driver) and deserialize (via sql.Scanner) a money.Currency instance. money.Amount is now a type alias to int64 which is already supported by sql.Scanner as one of the core built-in data types
Money's Value() function enables compatible sql drivers to serialize a money.Money instance to a single comma-delimited string value of "amount,currency_code" Money's Scan() function assumes that it receives a single column where the src value is a comma- delimited string in the format "amount,currency_code" While the storage format is up to the client when the amount and currency are stored separately a compatible scanner value can be created in SQLite like this: SELECT amount || "," || currency as 'amount' It is left to the client to decide to use Money's Valuer implementation with a db annotation on a property of type Money or else to store the Amount and Currency values as two separate columns and return them as a single joined string field.
strings.Split(src,,) will return a slice with length 2 even if one of the strings is empty
This allows any Golang ORM which supports the sql.Scanner to serialize (via sql.Driver) and deserialize (via sql.Scanner) a money.Currency instance. money.Amount is now a type alias to int64 which is already supported by sql.Scanner as one of the core built-in data types
Money's Value() function enables compatible sql drivers to serialize a money.Money instance to a single comma-delimited string value of "amount,currency_code" Money's Scan() function assumes that it receives a single column where the src value is a comma- delimited string in the format "amount,currency_code" While the storage format is up to the client when the amount and currency are stored separately a compatible scanner value can be created in SQLite like this: SELECT amount || "," || currency as 'amount' It is left to the client to decide to use Money's Valuer implementation with a db annotation on a property of type Money or else to store the Amount and Currency values as two separate columns and return them as a single joined string field.
strings.Split(src,,) will return a slice with length 2 even if one of the strings is empty
any update?I'm looking forward it |
…#99) * GH-44 implement driver.Valuer and sql.Scanner for money.Currency This allows any Golang ORM which supports the sql.Scanner to serialize (via sql.Driver) and deserialize (via sql.Scanner) a money.Currency instance. money.Amount is now a type alias to int64 which is already supported by sql.Scanner as one of the core built-in data types * GH-44 implement driver.Value and sql.Scanner for money.Money Money's Value() function enables compatible sql drivers to serialize a money.Money instance to a single comma-delimited string value of "amount,currency_code" Money's Scan() function assumes that it receives a single column where the src value is a comma- delimited string in the format "amount,currency_code" While the storage format is up to the client when the amount and currency are stored separately a compatible scanner value can be created in SQLite like this: SELECT amount || "," || currency as 'amount' It is left to the client to decide to use Money's Valuer implementation with a db annotation on a property of type Money or else to store the Amount and Currency values as two separate columns and return them as a single joined string field. * GH-44 fix an edge case strings.Split(src,,) will return a slice with length 2 even if one of the strings is empty * fix: money.value tests * refactor out the currency separator and make it customizable clients can set money.DBMoneyValueSeparator to determine which separator (e.g. "," "|" ";" ":" "AS" etc) to use when creating a single driver.Value object to represent a money.Money instance as a single string database field. this allows the money package to support string values such as 10@USD 20;CAD 30|IRD 40 in GBP etc --------- Co-authored-by: Raymond <raima220@gmail.com>
@FlameMida it just got merged and released |
I'd like to do the implementations of scanner and valuer for
Money
:Consider this:
I see several ways to represent it in database.
Solution A
A string field in database holding both the amount and the currency.
Solution B
Represent two distinct fields in database, one for the amount and the other for the currency.
money.Amount
andmoney.Currency
.I prefer
solution B
, what do you think @Rhymond ?The text was updated successfully, but these errors were encountered: