/src/bql_client.erl

https://github.com/rabbitmq/rabbitmq-bql · Erlang · 64 lines · 28 code · 7 blank · 29 comment · 0 complexity · 1db06b03094df995910e90b2513c98f3 MD5 · raw file

  1. %% The contents of this file are subject to the Mozilla Public License
  2. %% Version 1.1 (the "License"); you may not use this file except in
  3. %% compliance with the License. You may obtain a copy of the License at
  4. %% http://www.mozilla.org/MPL/
  5. %%
  6. %% Software distributed under the License is distributed on an "AS IS"
  7. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  8. %% License for the specific language governing rights and limitations
  9. %% under the License.
  10. %%
  11. %% The Original Code is RabbitMQ BQL Plugin.
  12. %%
  13. %% The Initial Developers of the Original Code are LShift Ltd.
  14. %%
  15. %% Copyright (C) 2009 LShift Ltd.
  16. %%
  17. %% All Rights Reserved.
  18. %%
  19. %% Contributor(s): ______________________________________.
  20. %%
  21. -module(bql_client).
  22. -export([connect/0, connect/5, close/1, execute/2]).
  23. -include_lib("amqp_client/include/amqp_client.hrl").
  24. % Record defining the context in which BQL commands are executed
  25. -record(client_ctx, {username, password, vhost, connection, rpc_client}).
  26. %% Creates a connection to the Rabbit server that can subsequently be used
  27. %% to issue BQL requests. Uses init arguments to determine connection
  28. %% parameters.
  29. connect() ->
  30. Username = list_to_binary(bql_utils:argument_or_default(username, "guest")),
  31. Password = list_to_binary(bql_utils:argument_or_default(password, "guest")),
  32. VHost = list_to_binary(bql_utils:argument_or_default(vhost, "/")),
  33. Host = bql_utils:argument_or_default(host, "localhost"),
  34. Port = list_to_integer(bql_utils:argument_or_default(port, integer_to_list(?PROTOCOL_PORT))),
  35. connect(Host, Port, Username, Password, VHost).
  36. %% Creates a connection to the Rabbit server that can subsequently be used
  37. %% to issue BQL requests.
  38. connect(Host, Port, Username, Password, VHost) ->
  39. % Open a conenction and wire a RPC client to it
  40. {ok, Connection} = amqp_connection:start(network, #amqp_params{
  41. username = Username,
  42. password = Password,
  43. virtual_host = <<"/">>, %% bql.query is in the default vhost
  44. host = Host,
  45. port = Port}),
  46. Client = bql_amqp_rpc_client:start(Connection, <<"bql.query">>),
  47. #client_ctx{username = Username, password = Password, vhost = VHost,
  48. connection = Connection, rpc_client = Client}.
  49. %% Disconnects the BQL client and frees up any resources associated with it
  50. close(#client_ctx { connection = Connection, rpc_client = Client }) ->
  51. bql_amqp_rpc_client:stop(Client),
  52. amqp_connection:close(Connection).
  53. %% Executes the given BQL request on the connected server
  54. execute(#client_ctx { username = User, password = Password, vhost = VHost, rpc_client = Client }, Contents) ->
  55. Request = [{user, User}, {password, Password}, {vhost, VHost}, {query_text, Contents}],
  56. Res = bql_amqp_rpc_client:call(Client, <<"application/bert">>, term_to_binary(Request), 500),
  57. binary_to_term(Res).