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

Get rid of warnings when compiling under 1.5.x #60 #61

Merged
merged 2 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ elixir:
- 1.2.6
- 1.3.4
- 1.4.0
- 1.5.1
matrix:
include:
- elixir: 1.5.1
otp_release: 20.0
after_script:
- MIX_ENV=dev mix deps.get
- MIX_ENV=dev mix inch.report
Expand Down
18 changes: 14 additions & 4 deletions lib/sqlitex.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
defmodule Sqlitex do
if Version.compare(System.version, "1.3.0") == :lt do
@type charlist :: char_list
end

@type connection :: {:connection, reference, String.t}
@type string_or_charlist :: String.t | char_list
@type sqlite_error :: {:error, {:sqlite_error, char_list}}
@type string_or_charlist :: String.t | charlist
@type sqlite_error :: {:error, {:sqlite_error, charlist}}

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

@spec open(String.t) :: {:ok, connection}
@spec open(char_list) :: {:ok, connection} | {:error, {atom, char_list}}
def open(path) when is_binary(path), do: open(String.to_char_list(path))
@spec open(charlist) :: {:ok, connection} | {:error, {atom, charlist}}
def open(path) when is_binary(path), do: open(string_to_charlist(path))
def open(path) do
:esqlite3.open(path)
end
Expand Down Expand Up @@ -73,4 +77,10 @@ defmodule Sqlitex do
stmt = Sqlitex.SqlBuilder.create_table(name, table_opts, cols)
exec(db, stmt)
end

if Version.compare(System.version, "1.3.0") == :lt do
defp string_to_charlist(string), do: String.to_char_list(string)
else
defp string_to_charlist(string), do: String.to_charlist(string)
end
end
20 changes: 12 additions & 8 deletions lib/sqlitex/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ defmodule Sqlitex.Query do
* {:error, _} on failure.
"""

@spec query(Sqlitex.connection, String.t | char_list) :: [[]] | Sqlitex.sqlite_error
@spec query(Sqlitex.connection, String.t | char_list, [bind: [], into: Enum.t]) :: [Enum.t] | Sqlitex.sqlite_error
if Version.compare(System.version, "1.3.0") == :lt do
@type charlist :: char_list
end

@spec query(Sqlitex.connection, String.t | charlist) :: [[]] | Sqlitex.sqlite_error
@spec query(Sqlitex.connection, String.t | charlist, [bind: [], into: Enum.t]) :: [Enum.t] | Sqlitex.sqlite_error
def query(db, sql, opts \\ []) do
with {:ok, stmt} <- Statement.prepare(db, sql),
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
Expand All @@ -35,8 +39,8 @@ defmodule Sqlitex.Query do

Returns the results otherwise.
"""
@spec query!(Sqlitex.connection, String.t | char_list) :: [[]]
@spec query!(Sqlitex.connection, String.t | char_list, [bind: [], into: Enum.t]) :: [Enum.t]
@spec query!(Sqlitex.connection, String.t | charlist) :: [[]]
@spec query!(Sqlitex.connection, String.t | charlist, [bind: [], into: Enum.t]) :: [Enum.t]
def query!(db, sql, opts \\ []) do
case query(db, sql, opts) do
{:error, reason} -> raise Sqlitex.QueryError, reason: reason
Expand Down Expand Up @@ -64,8 +68,8 @@ defmodule Sqlitex.Query do
* {:error, _} on failure.
"""

@spec query_rows(Sqlitex.connection, String.t | char_list) :: {:ok, %{}} | Sqlitex.sqlite_error
@spec query_rows(Sqlitex.connection, String.t | char_list, [bind: []]) :: {:ok, %{}} | Sqlitex.sqlite_error
@spec query_rows(Sqlitex.connection, String.t | charlist) :: {:ok, %{}} | Sqlitex.sqlite_error
@spec query_rows(Sqlitex.connection, String.t | charlist, [bind: []]) :: {:ok, %{}} | Sqlitex.sqlite_error
def query_rows(db, sql, opts \\ []) do
with {:ok, stmt} <- Statement.prepare(db, sql),
{:ok, stmt} <- Statement.bind_values(stmt, Keyword.get(opts, :bind, [])),
Expand All @@ -78,8 +82,8 @@ defmodule Sqlitex.Query do

Returns the results otherwise.
"""
@spec query_rows!(Sqlitex.connection, String.t | char_list) :: [[]]
@spec query_rows!(Sqlitex.connection, String.t | char_list, [bind: []]) :: [Enum.t]
@spec query_rows!(Sqlitex.connection, String.t | charlist) :: [[]]
@spec query_rows!(Sqlitex.connection, String.t | charlist, [bind: []]) :: [Enum.t]
def query_rows!(db, sql, opts \\ []) do
case query_rows(db, sql, opts) do
{:error, reason} -> raise Sqlitex.QueryError, reason: reason
Expand Down
8 changes: 7 additions & 1 deletion lib/sqlitex/row.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Sqlitex.Row do
end
defp translate_value({float, "decimal"}), do: Decimal.new(float)
defp translate_value({float, "decimal(" <> rest}) do
[precision, scale] = rest |> String.rstrip(?)) |> String.split(",") |> Enum.map(&String.to_integer/1)
[precision, scale] = rest |> string_rstrip(?)) |> String.split(",") |> Enum.map(&String.to_integer/1)
Decimal.with_context %Decimal.Context{precision: precision, rounding: :down}, fn ->
float |> Float.round(scale) |> Decimal.new |> Decimal.plus
end
Expand All @@ -78,4 +78,10 @@ defmodule Sqlitex.Row do
fr = String.to_integer(fr <> String.duplicate("0", 6 - String.length(fr)))
{String.to_integer(hr), String.to_integer(mi), String.to_integer(se), fr}
end

if Version.compare(System.version, "1.5.0") == :lt do
defp string_rstrip(string, char), do: String.rstrip(string, char)
else
defp string_rstrip(string, char), do: String.trim_trailing(string, to_string([char]))
end
end