Skip to content

Commit a6500f3

Browse files
committed
ensure inspect follows the standard representation
# Conflicts: # CHANGELOG.md # lib/sql.ex
1 parent 530fc0b commit a6500f3

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- MySQL adapter [#5](https://github.com/elixir-dbvisor/sql/pull/5).
1616
- PostgreSQL adapter [#5](https://github.com/elixir-dbvisor/sql/pull/5).
1717
- TDS adapter [#5](https://github.com/elixir-dbvisor/sql/pull/5).
18+
- Ensure inspect follows the standard [representation](https://hexdocs.pm/elixir/Inspect.html#module-inspect-representation) [#4](https://github.com/elixir-dbvisor/sql/pull/4)
1819

1920
### Deprecation
2021
- token_to_sql/2 is deprecated in favor of SQL.Token behaviour token_to_string/2 [#5](https://github.com/elixir-dbvisor/sql/pull/5).

lib/sql.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ defmodule SQL do
9999

100100
defimpl Inspect, for: SQL do
101101
def inspect(sql, _opts) do
102+
string = "#SQL<#{to_string(sql)}"
103+
string = String.pad_trailing(string, byte_size(string)+2, ";>")
102104
if Kernel.function_exported?(sql.module, :config, 0) do
103-
Enum.reduce(0..length(sql.params), to_string(sql), &String.replace(&2, sql.module.config()[:adapter].token_to_string({:binding, [], [&1]}), sql.module.config()[:adapter].token_to_string(Enum.at(sql.params, &1)), global: false))
105+
Enum.reduce(0..length(sql.params), string, &String.replace(&2, sql.module.config()[:adapter].token_to_string({:binding, [], [&1]}), sql.module.config()[:adapter].token_to_string(Enum.at(sql.params, &1)), global: false))
104106
else
105-
Enum.reduce(0..length(sql.params), to_string(sql), &String.replace(&2, SQL.String.token_to_sql({:binding, [], [&1]}), SQL.String.token_to_sql(Enum.at(sql.params, &1))))
107+
Enum.reduce(0..length(sql.params), string, &String.replace(&2, SQL.String.token_to_sql({:binding, [], [&1]}), SQL.String.token_to_sql(Enum.at(sql.params, &1))))
106108
end
107109
end
108110
end

test/sql_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule SQLTest do
3737
end
3838

3939
test "inspect/1" do
40-
assert "select +1000" == inspect(~SQL[select +1000])
40+
assert "#SQL<select +1000;>" == inspect(~SQL[select +1000])
4141
end
4242

4343
test "to_sql/1" do

0 commit comments

Comments
 (0)