data_source.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. require "digest/md5"
  2. module Blazer
  3. class DataSource
  4. attr_reader :id, :settings, :connection_model
  5. def initialize(id, settings)
  6. @id = id
  7. @settings = settings
  8. unless settings["url"] || Rails.env.development?
  9. raise Blazer::Error, "Empty url"
  10. end
  11. @connection_model =
  12. Class.new(Blazer::Connection) do
  13. def self.name
  14. "Blazer::Connection::#{object_id}"
  15. end
  16. establish_connection(settings["url"]) if settings["url"]
  17. end
  18. end
  19. def name
  20. settings["name"] || @id
  21. end
  22. def linked_columns
  23. settings["linked_columns"] || {}
  24. end
  25. def smart_columns
  26. settings["smart_columns"] || {}
  27. end
  28. def smart_variables
  29. settings["smart_variables"] || {}
  30. end
  31. def variable_defaults
  32. settings["variable_defaults"] || {}
  33. end
  34. def timeout
  35. settings["timeout"]
  36. end
  37. def cache
  38. settings["cache"]
  39. end
  40. def local_time_suffix
  41. @local_time_suffix ||= Array(settings["local_time_suffix"])
  42. end
  43. def use_transaction?
  44. settings.key?("use_transaction") ? settings["use_transaction"] : true
  45. end
  46. def run_statement(statement, options = {})
  47. columns = nil
  48. rows = nil
  49. error = nil
  50. cached_at = nil
  51. cache_key = self.cache_key(statement) if cache
  52. if cache && !options[:refresh_cache]
  53. value = Blazer.cache.read(cache_key)
  54. columns, rows, cached_at = Marshal.load(value) if value
  55. end
  56. unless rows
  57. columns = []
  58. rows = []
  59. comment = "blazer"
  60. if options[:user].respond_to?(:id)
  61. comment << ",user_id:#{options[:user].id}"
  62. end
  63. if options[:user].respond_to?(Blazer.user_name)
  64. # only include letters, numbers, and spaces to prevent injection
  65. comment << ",user_name:#{options[:user].send(Blazer.user_name).to_s.gsub(/[^a-zA-Z0-9 ]/, "")}"
  66. end
  67. if options[:query].respond_to?(:id)
  68. comment << ",query_id:#{options[:query].id}"
  69. end
  70. in_transaction do
  71. begin
  72. if timeout
  73. if postgresql? || redshift?
  74. connection_model.connection.execute("SET statement_timeout = #{timeout.to_i * 1000}")
  75. elsif mysql?
  76. connection_model.connection.execute("SET max_execution_time = #{timeout.to_i * 1000}")
  77. else
  78. raise Blazer::TimeoutNotSupported, "Timeout not supported for #{adapter_name} adapter"
  79. end
  80. end
  81. result = connection_model.connection.select_all("#{statement} /*#{comment}*/")
  82. columns = result.columns
  83. cast_method = Rails::VERSION::MAJOR < 5 ? :type_cast : :cast_value
  84. result.rows.each do |untyped_row|
  85. rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| result.column_types[c].send(cast_method, untyped_row[i]) })
  86. end
  87. rescue ActiveRecord::StatementInvalid => e
  88. error = e.message.sub(/.+ERROR: /, "")
  89. error = Blazer::TIMEOUT_MESSAGE if Blazer::TIMEOUT_ERRORS.any? { |e| error.include?(e) }
  90. end
  91. end
  92. Blazer.cache.write(cache_key, Marshal.dump([columns, rows, Time.now]), expires_in: cache.to_f * 60) if !error && cache
  93. end
  94. [columns, rows, error, cached_at]
  95. end
  96. def clear_cache(statement)
  97. Blazer.cache.delete(cache_key(statement))
  98. end
  99. def cache_key(statement)
  100. ["blazer", "v3", id, Digest::MD5.hexdigest(statement)].join("/")
  101. end
  102. def schemas
  103. default_schema = (postgresql? || redshift?) ? "public" : connection_model.connection_config[:database]
  104. settings["schemas"] || [connection_model.connection_config[:schema] || default_schema]
  105. end
  106. def tables
  107. columns, rows, error, cached_at = run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema IN (?)", schemas]))
  108. rows.map(&:first)
  109. end
  110. def postgresql?
  111. ["PostgreSQL", "PostGIS"].include?(adapter_name)
  112. end
  113. def redshift?
  114. ["Redshift"].include?(adapter_name)
  115. end
  116. def mysql?
  117. ["MySQL", "Mysql2", "Mysql2Spatial"].include?(adapter_name)
  118. end
  119. def reconnect
  120. connection_model.establish_connection(settings["url"])
  121. end
  122. protected
  123. def adapter_name
  124. connection_model.connection.adapter_name
  125. end
  126. def in_transaction
  127. if use_transaction?
  128. connection_model.transaction do
  129. yield
  130. raise ActiveRecord::Rollback
  131. end
  132. else
  133. yield
  134. end
  135. end
  136. end
  137. end