Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ActiveRecord
- class Base
- # Establishes a connection to the database that's used by all Active Record objects
- def self.influxdb_connection(config) # :nodoc:
- config = config.symbolize_keys
- host = config[:host]
- port = config[:port] || 5432
- username = config[:username].to_s if config[:username]
- password = config[:password].to_s if config[:password]
- # The postgres drivers don't allow the creation of an unconnected PGconn object,
- # so just pass a nil connection object for the time being.
- ConnectionAdapters::InfluxdbAdapter.new(nil, logger, config)
- end
- end
- module ConnectionAdapters
- class InfluxdbAdapter < AbstractAdapter
- def adapter_name
- 'InfluxDB'
- end
- def initialize(connection, logger, config)
- super(connection, logger)
- @visitor = Arel::Visitors::InfluxDB.new self
- connect(config)
- end
- def connect(config)
- puts('connect')
- if ENV['INFLUX_DB_URL']
- @connection = InfluxDB::Client.new(url: ENV['INFLUX_DB_URL'])
- else
- @connection = InfluxDB::Client.new(config)
- end
- end
- def columns(table)
- []
- end
- def tables
- []
- end
- def exec_query(sql, name, binds)
- puts("InfluxDB: #{sql} - #{name}")
- res = @connection.query(sql).first
- binding.pry
- ActiveRecord::Result.new(res['values'].first.keys, res['values'].map(&:values))
- end
- def column_name_for_operation(operation, node) # :nodoc:
- {
- 'maximum' => 'max',
- 'minimum' => 'min',
- 'average' => 'avg',
- }.fetch(operation) { operation.downcase }
- end
- end
- end
- end
- module Arel
- module Visitors
- class InfluxDB < ToSql
- private
- def visit_Arel_Attributes_Attribute o, collector
- collector << quote_column_name(o.name)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment