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

Commit 8b3be7d

Browse files
committed
Get rid of warnings when compiling under 1.5.x
These changes quiet the warnings emitted when compiling under Elixir 1.5.1, they also work under 1.2.6, 1.3.2, and 1.4.0.
1 parent a4e7fd5 commit 8b3be7d

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

lib/sqlitex.ex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
defmodule Sqlitex do
2+
if Version.compare(System.version, "1.3.0") == :lt do
3+
@type charlist :: char_list
4+
end
5+
26
@type connection :: {:connection, reference, String.t}
3-
@type string_or_charlist :: String.t | char_list
4-
@type sqlite_error :: {:error, {:sqlite_error, char_list}}
7+
@type string_or_charlist :: String.t | charlist
8+
@type sqlite_error :: {:error, {:sqlite_error, charlist}}
59

610
@moduledoc """
711
Sqlitex gives you a way to create and query sqlite databases.
@@ -28,8 +32,8 @@ defmodule Sqlitex do
2832
end
2933

3034
@spec open(String.t) :: {:ok, connection}
31-
@spec open(char_list) :: {:ok, connection} | {:error, {atom, char_list}}
32-
def open(path) when is_binary(path), do: open(String.to_char_list(path))
35+
@spec open(charlist) :: {:ok, connection} | {:error, {atom, charlist}}
36+
def open(path) when is_binary(path), do: open(string_to_charlist(path))
3337
def open(path) do
3438
:esqlite3.open(path)
3539
end
@@ -73,4 +77,10 @@ defmodule Sqlitex do
7377
stmt = Sqlitex.SqlBuilder.create_table(name, table_opts, cols)
7478
exec(db, stmt)
7579
end
80+
81+
if Version.compare(System.version, "1.3.0") == :lt do
82+
defp string_to_charlist(string), do: String.to_char_list(string)
83+
else
84+
defp string_to_charlist(string), do: String.to_charlist(string)
85+
end
7686
end

lib/sqlitex/query.ex

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ defmodule Sqlitex.Query do
2121
* {:error, _} on failure.
2222
"""
2323

24-
@spec query(Sqlitex.connection, String.t | char_list) :: [[]] | Sqlitex.sqlite_error
25-
@spec query(Sqlitex.connection, String.t | char_list, [bind: [], into: Enum.t]) :: [Enum.t] | Sqlitex.sqlite_error
24+
if Version.compare(System.version, "1.3.0") == :lt do
25+
@type charlist :: char_list
26+
end
27+
28+
@spec query(Sqlitex.connection, String.t | charlist) :: [[]] | Sqlitex.sqlite_error
29+
@spec query(Sqlitex.connection, String.t | charlist, [bind: [], into: Enum.t]) :: [Enum.t] | Sqlitex.sqlite_error
2630
def query(db, sql, opts \\ []) do
2731
with {:ok, stmt} <- Statement.prepare(db, sql),
2832
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
@@ -35,8 +39,8 @@ defmodule Sqlitex.Query do
3539
3640
Returns the results otherwise.
3741
"""
38-
@spec query!(Sqlitex.connection, String.t | char_list) :: [[]]
39-
@spec query!(Sqlitex.connection, String.t | char_list, [bind: [], into: Enum.t]) :: [Enum.t]
42+
@spec query!(Sqlitex.connection, String.t | charlist) :: [[]]
43+
@spec query!(Sqlitex.connection, String.t | charlist, [bind: [], into: Enum.t]) :: [Enum.t]
4044
def query!(db, sql, opts \\ []) do
4145
case query(db, sql, opts) do
4246
{:error, reason} -> raise Sqlitex.QueryError, reason: reason
@@ -64,8 +68,8 @@ defmodule Sqlitex.Query do
6468
* {:error, _} on failure.
6569
"""
6670

67-
@spec query_rows(Sqlitex.connection, String.t | char_list) :: {:ok, %{}} | Sqlitex.sqlite_error
68-
@spec query_rows(Sqlitex.connection, String.t | char_list, [bind: []]) :: {:ok, %{}} | Sqlitex.sqlite_error
71+
@spec query_rows(Sqlitex.connection, String.t | charlist) :: {:ok, %{}} | Sqlitex.sqlite_error
72+
@spec query_rows(Sqlitex.connection, String.t | charlist, [bind: []]) :: {:ok, %{}} | Sqlitex.sqlite_error
6973
def query_rows(db, sql, opts \\ []) do
7074
with {:ok, stmt} <- Statement.prepare(db, sql),
7175
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
@@ -78,8 +82,8 @@ defmodule Sqlitex.Query do
7882
7983
Returns the results otherwise.
8084
"""
81-
@spec query_rows!(Sqlitex.connection, String.t | char_list) :: [[]]
82-
@spec query_rows!(Sqlitex.connection, String.t | char_list, [bind: []]) :: [Enum.t]
85+
@spec query_rows!(Sqlitex.connection, String.t | charlist) :: [[]]
86+
@spec query_rows!(Sqlitex.connection, String.t | charlist, [bind: []]) :: [Enum.t]
8387
def query_rows!(db, sql, opts \\ []) do
8488
case query_rows(db, sql, opts) do
8589
{:error, reason} -> raise Sqlitex.QueryError, reason: reason

lib/sqlitex/row.ex

Lines changed: 7 additions & 1 deletion
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(&String.to_integer/1)
5757
Decimal.with_context %Decimal.Context{precision: precision, rounding: :down}, fn ->
5858
float |> Float.round(scale) |> Decimal.new |> Decimal.plus
5959
end
@@ -78,4 +78,10 @@ defmodule Sqlitex.Row do
7878
fr = String.to_integer(fr <> String.duplicate("0", 6 - String.length(fr)))
7979
{String.to_integer(hr), String.to_integer(mi), String.to_integer(se), fr}
8080
end
81+
82+
if Version.compare(System.version, "1.5.0") == :lt do
83+
defp string_rstrip(string, char), do: String.rstrip(string, char)
84+
else
85+
defp string_rstrip(string, char), do: String.trim_trailing(string, to_string([char]))
86+
end
8187
end

0 commit comments

Comments
 (0)