-
Notifications
You must be signed in to change notification settings - Fork 467
/
mongodb_adapter.rb
43 lines (35 loc) · 961 Bytes
/
mongodb_adapter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Blazer
module Adapters
class MongodbAdapter < BaseAdapter
def run_statement(statement, comment)
columns = []
rows = []
error = nil
begin
documents = db.command({:$eval => "#{statement.strip}.toArray()", nolock: true}).documents.first["retval"]
columns = documents.flat_map { |r| r.keys }.uniq
rows = documents.map { |r| columns.map { |c| r[c] } }
rescue => e
error = e.message
end
[columns, rows, error]
end
def tables
db.collection_names
end
def preview_statement
"db.{table}.find().limit(10)"
end
def quoting
:backslash_escape
end
protected
def client
@client ||= Mongo::Client.new(settings["url"], connect_timeout: 1, socket_timeout: 1, server_selection_timeout: 1)
end
def db
@db ||= client.database
end
end
end
end