Skip to content

Commit 4182ba1

Browse files
authored
Merge pull request #120 from almightycouch/debug_query
Add support for human-readable queries
2 parents aa30c2f + 93d902a commit 4182ba1

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

.travis.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
language: elixir
2+
23
elixir:
3-
- 1.1.1
4-
- 1.2.1
4+
- 1.3
5+
56
otp_release:
6-
- 18.0
7-
- 18.1
8-
sudo: required
9-
before_install:
10-
- source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
11-
- wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
12-
- sudo apt-get update -qq
13-
- sudo apt-get install rethinkdb -y --force-yes
14-
before_script: rethinkdb --daemon
7+
- 18.3
8+
- 19.1
9+
1510
install:
11+
- mix local.rebar --force
1612
- mix local.hex --force
1713
- mix deps.get --only test
14+
15+
addons:
16+
rethinkdb: '2.3'

lib/rethinkdb/q.ex

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
11
defmodule RethinkDB.Q do
22
@moduledoc false
3+
34
defstruct query: nil
45
end
6+
57
defimpl Poison.Encoder, for: RethinkDB.Q do
68
def encode(%{query: query}, options) do
79
Poison.Encoder.encode(query, options)
810
end
911
end
12+
13+
defimpl Inspect, for: RethinkDB.Q do
14+
@external_resource term_info = Path.join([__DIR__, "query", "term_info.json"])
15+
16+
@apidef term_info
17+
|> File.read!()
18+
|> Poison.decode!()
19+
|> Enum.into(%{}, fn {key, val} -> {val, key} end)
20+
21+
def inspect(%RethinkDB.Q{query: [index, params]}, _) do
22+
query =
23+
# Replaces references with a-z variable names,
24+
# using the same ref between fn head and body.
25+
params
26+
|> Enum.map_reduce(97, &resolve_param/2)
27+
|> elem(0)
28+
|> Enum.join(", ")
29+
"#{Map.get(@apidef, index, index)}(#{query})"
30+
end
31+
32+
defp resolve_param([2, [ref]], acc) when is_reference(ref) do
33+
# function head reference
34+
{to_string([acc]), acc + 1}
35+
end
36+
37+
defp resolve_param(ref, acc) when is_reference(ref) do
38+
# function body reference
39+
{to_string([acc]), acc + 1}
40+
end
41+
42+
defp resolve_param(val, acc) do
43+
{Kernel.inspect(val), acc}
44+
end
45+
end

lib/rethinkdb/query/term_info.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"has_fields":32,"db_list":59,"funcall":64,"random":151,"map":38,"to_geojson":158,"index_create":75,"ungroup":150,"get_intersecting":166,"get_all":78,"saturday":112,"error":12,"or":66,"reconfigure":176,"gt":21,"day":130,"september":122,"desc":74,"min":147,"nth":45,"splice_at":85,"make_array":2,"info":79,"mod":28,"set_insert":88,"thursday":110,"zip":72,"div":27,"db_drop":58,"skip":70,"insert":56,"may":118,"wait":177,"object":143,"range":173,"http":153,"july":120,"difference":95,"february":115,"outer_join":49,"without":34,"sub":25,"table_drop":61,"geojson":157,"fold":187,"minutes":134,"includes":164,"fill":167,"tuesday":108,"default":92,"date":106,"fn":69,"during":105,"august":121,"index_rename":156,"changes":152,"ceil":184,"binary":155,"limit":71,"offsets_of":87,"reduce":37,"time":136,"var":10,"get_nearest":168,"upcase":141,"type_of":52,"hours":133,"and":67,"polygon":161,"not":23,"asc":73,"match":97,"json":98,"distance":162,"inner_join":48,"filter":39,"minval":180,"set_difference":91,"slice":30,"status":175,"june":119,"round":185,"intersects":163,"sync":138,"monday":107,"make_obj":3,"prepend":80,"pluck":33,"table":15,"between":182,"merge":35,"order_by":41,"is_empty":86,"eq_join":50,"max":148,"day_of_year":132,"floor":183,"avg":146,"march":116,"delete_at":83,"rebalance":179,"seconds":135,"coerce_to":51,"delete":54,"update":53,"index_wait":140,"literal":137,"branch":65,"javascript":11,"sample":81,"epoch_time":101,"january":114,"sunday":113,"get":16,"le":20,"december":125,"db":14,"get_field":31,"april":117,"contains":93,"lt":19,"between_deprecated":36,"datum":1,"index_status":139,"set_intersection":89,"change_at":84,"mul":26,"replace":55,"october":123,"grant":188,"add":24,"table_create":60,"to_iso8601":100,"in_timezone":104,"day_of_week":131,"timezone":127,"maxval":181,"uuid":169,"concat_map":40,"split":149,"eq":17,"year":128,"downcase":142,"friday":111,"db_create":57,"count":43,"union":44,"bracket":170,"to_json_string":172,"insert_at":82,"month":129,"set_union":90,"ge":22,"point":159,"sum":145,"wednesday":109,"line":160,"now":103,"group":144,"november":124,"to_epoch_time":102,"append":29,"table_list":62,"time_of_day":126,"distinct":42,"index_drop":76,"index_list":77,"implicit_var":13,"config":174,"args":154,"values":186,"ne":18,"keys":94,"for_each":68,"circle":165,"iso8601":99,"with_fields":96,"polygon_sub":171}

0 commit comments

Comments
 (0)