Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit f4c2b36

Browse files
authored
Merge pull request #67 from TokiTori/master
fix decimal scale and precision parsing
2 parents 577bf1e + 1a7f619 commit f4c2b36

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/sqlitex/row.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ defmodule Sqlitex.Row do
5353
end
5454
defp translate_value({float, "decimal"}), do: Decimal.new(float)
5555
defp translate_value({float, "decimal(" <> rest}) do
56-
[precision, scale] = rest |> string_rstrip(?)) |> String.split(",") |> Enum.map(&String.to_integer/1)
56+
[precision, scale] = rest |> string_rstrip(?)) |> String.split(",") |> Enum.map(&(&1 |> String.trim() |> String.to_integer))
5757
Decimal.with_context %Decimal.Context{precision: precision, rounding: :down}, fn ->
5858
float |> Float.round(scale) |> Float.to_string |> Decimal.new |> Decimal.plus
5959
end

test/sqlitex_test.exs

+8
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ defmodule SqlitexTest do
213213
|> Enum.each(fn {res, ans} -> assert Decimal.equal?(res, ans) end)
214214
end
215215

216+
test "decimal types with spaces near scale and precision" do
217+
{:ok, db} = Sqlitex.open(":memory:")
218+
:ok = Sqlitex.exec(db, "CREATE TABLE t (f1 DECIMAL(3,2), f2 DECIMAL(3, 2), f3 DECIMAL( 3 ,2 ))")
219+
{:ok, []} = Sqlitex.query(db, "INSERT INTO t VALUES (?,?,?)", bind: [Decimal.new("1.23"), Decimal.new("4.56"), Decimal.new("7.89")])
220+
[row] = Sqlitex.query!(db, "SELECT f1, f2, f3 FROM t")
221+
assert row == [f1: Decimal.new("1.23"), f2: Decimal.new("4.56"), f3: Decimal.new("7.89")]
222+
end
223+
216224
test "it handles datetime, date, time with empty string" do
217225
{:ok, db} = Sqlitex.open(":memory:")
218226
:ok = Sqlitex.exec(db, "CREATE TABLE t (a datetime NOT NULL, b date NOT NULL, c time NOT NULL)")

0 commit comments

Comments
 (0)