Guest User

Untitled

a guest
Feb 5th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. module ActiveRecord
  2. class Base
  3. # Establishes a connection to the database that's used by all Active Record objects
  4. def self.influxdb_connection(config) # :nodoc:
  5. config = config.symbolize_keys
  6. host = config[:host]
  7. port = config[:port] || 5432
  8. username = config[:username].to_s if config[:username]
  9. password = config[:password].to_s if config[:password]
  10.  
  11. # The postgres drivers don't allow the creation of an unconnected PGconn object,
  12. # so just pass a nil connection object for the time being.
  13. ConnectionAdapters::InfluxdbAdapter.new(nil, logger, config)
  14. end
  15. end
  16.  
  17. module ConnectionAdapters
  18. class InfluxdbAdapter < AbstractAdapter
  19.  
  20. def adapter_name
  21. 'InfluxDB'
  22. end
  23.  
  24. def initialize(connection, logger, config)
  25. super(connection, logger)
  26.  
  27. @visitor = Arel::Visitors::InfluxDB.new self
  28.  
  29. connect(config)
  30. end
  31.  
  32. def connect(config)
  33. puts('connect')
  34. if ENV['INFLUX_DB_URL']
  35. @connection = InfluxDB::Client.new(url: ENV['INFLUX_DB_URL'])
  36. else
  37. @connection = InfluxDB::Client.new(config)
  38. end
  39. end
  40.  
  41. def columns(table)
  42. []
  43. end
  44.  
  45. def tables
  46. []
  47. end
  48.  
  49. def exec_query(sql, name, binds)
  50. puts("InfluxDB: #{sql} - #{name}")
  51. res = @connection.query(sql).first
  52. binding.pry
  53. ActiveRecord::Result.new(res['values'].first.keys, res['values'].map(&:values))
  54. end
  55.  
  56. def column_name_for_operation(operation, node) # :nodoc:
  57. {
  58. 'maximum' => 'max',
  59. 'minimum' => 'min',
  60. 'average' => 'avg',
  61. }.fetch(operation) { operation.downcase }
  62. end
  63. end
  64. end
  65. end
  66.  
  67. module Arel
  68. module Visitors
  69. class InfluxDB < ToSql
  70. private
  71. def visit_Arel_Attributes_Attribute o, collector
  72. collector << quote_column_name(o.name)
  73. end
  74. end
  75. end
  76. end
Add Comment
Please, Sign In to add comment