Sha256: 2c4b7893f1534a36c8dc175e619eaa19eaa359695be4cf2d0d70f4ccfb2f7442

Contents?: true

Size: 961 Bytes

Versions: 6

Compression:

Stored size: 961 Bytes

Contents

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
blazer-2.6.5 lib/blazer/adapters/mongodb_adapter.rb
blazer-2.6.4 lib/blazer/adapters/mongodb_adapter.rb
blazer-2.6.3 lib/blazer/adapters/mongodb_adapter.rb
blazer-2.6.2 lib/blazer/adapters/mongodb_adapter.rb
blazer-2.6.1 lib/blazer/adapters/mongodb_adapter.rb
blazer-2.6.0 lib/blazer/adapters/mongodb_adapter.rb