sql_adapter.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. module Blazer
  2. module Adapters
  3. class SqlAdapter < BaseAdapter
  4. attr_reader :connection_model
  5. def initialize(data_source)
  6. super
  7. @connection_model =
  8. Class.new(Blazer::Connection) do
  9. def self.name
  10. "Blazer::Connection::Adapter#{object_id}"
  11. end
  12. establish_connection(data_source.settings["url"]) if data_source.settings["url"]
  13. end
  14. end
  15. def run_statement(statement, comment)
  16. columns = []
  17. rows = []
  18. error = nil
  19. begin
  20. in_transaction do
  21. set_timeout(data_source.timeout) if data_source.timeout
  22. result = select_all("#{statement} /*#{comment}*/")
  23. columns = result.columns
  24. cast_method = Rails::VERSION::MAJOR < 5 ? :type_cast : :cast_value
  25. result.rows.each do |untyped_row|
  26. rows << (result.column_types.empty? ? untyped_row : columns.each_with_index.map { |c, i| untyped_row[i] ? result.column_types[c].send(cast_method, untyped_row[i]) : untyped_row[i] })
  27. end
  28. end
  29. rescue => e
  30. error = e.message.sub(/.+ERROR: /, "")
  31. error = Blazer::TIMEOUT_MESSAGE if Blazer::TIMEOUT_ERRORS.any? { |e| error.include?(e) }
  32. reconnect if error.include?("PG::ConnectionBad")
  33. end
  34. [columns, rows, error]
  35. end
  36. def tables
  37. result = data_source.run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_name FROM information_schema.tables WHERE table_schema IN (?) ORDER BY table_name", schemas]), refresh_cache: true)
  38. result.rows.map(&:first)
  39. end
  40. def schema
  41. result = data_source.run_statement(connection_model.send(:sanitize_sql_array, ["SELECT table_schema, table_name, column_name, data_type, ordinal_position FROM information_schema.columns WHERE table_schema IN (?) ORDER BY 1, 2", schemas]))
  42. result.rows.group_by { |r| [r[0], r[1]] }.map { |k, vs| {schema: k[0], table: k[1], columns: vs.sort_by { |v| v[2] }.map { |v| {name: v[2], data_type: v[3]} }} }
  43. end
  44. def preview_statement
  45. if postgresql?
  46. "SELECT * FROM \"{table}\" LIMIT 10"
  47. elsif sqlserver?
  48. "SELECT TOP (10) * FROM {table}"
  49. else
  50. "SELECT * FROM {table} LIMIT 10"
  51. end
  52. end
  53. def reconnect
  54. connection_model.establish_connection(settings["url"])
  55. end
  56. def cost(statement)
  57. result = explain(statement)
  58. if sqlserver?
  59. result["TotalSubtreeCost"]
  60. else
  61. match = /cost=\d+\.\d+..(\d+\.\d+) /.match(result)
  62. match[1] if match
  63. end
  64. end
  65. def explain(statement)
  66. if postgresql? || redshift?
  67. select_all("EXPLAIN #{statement}").rows.first.first
  68. elsif sqlserver?
  69. begin
  70. execute("SET SHOWPLAN_ALL ON")
  71. result = select_all(statement).each.first
  72. ensure
  73. execute("SET SHOWPLAN_ALL OFF")
  74. end
  75. result
  76. end
  77. rescue
  78. nil
  79. end
  80. def cancel(run_id)
  81. if postgresql?
  82. select_all("SELECT pg_cancel_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND query LIKE '%,run_id:#{run_id}%'")
  83. elsif redshift?
  84. first_row = select_all("SELECT pid FROM stv_recents WHERE status = 'Running' AND query LIKE '%,run_id:#{run_id}%'").first
  85. if first_row
  86. select_all("CANCEL #{first_row["pid"].to_i}")
  87. end
  88. end
  89. end
  90. def cachable?(statement)
  91. !%w[CREATE ALTER UPDATE INSERT DELETE].include?(statement.split.first.to_s.upcase)
  92. end
  93. protected
  94. def select_all(statement)
  95. connection_model.connection.select_all(statement)
  96. end
  97. # seperate from select_all to prevent mysql error
  98. def execute(statement)
  99. connection_model.connection.execute(statement)
  100. end
  101. def postgresql?
  102. ["PostgreSQL", "PostGIS"].include?(adapter_name)
  103. end
  104. def redshift?
  105. ["Redshift"].include?(adapter_name)
  106. end
  107. def mysql?
  108. ["MySQL", "Mysql2", "Mysql2Spatial"].include?(adapter_name)
  109. end
  110. def sqlserver?
  111. ["SQLServer", "tinytds", "mssql"].include?(adapter_name)
  112. end
  113. def adapter_name
  114. # prevent bad data source from taking down queries/new
  115. connection_model.connection.adapter_name rescue nil
  116. end
  117. def schemas
  118. settings["schemas"] || [connection_model.connection_config[:schema] || default_schema]
  119. end
  120. def default_schema
  121. if postgresql? || redshift?
  122. "public"
  123. elsif sqlserver?
  124. "dbo"
  125. else
  126. connection_model.connection_config[:database]
  127. end
  128. end
  129. def set_timeout(timeout)
  130. if postgresql? || redshift?
  131. execute("SET #{use_transaction? ? "LOCAL " : ""}statement_timeout = #{timeout.to_i * 1000}")
  132. elsif mysql?
  133. execute("SET max_execution_time = #{timeout.to_i * 1000}")
  134. else
  135. raise Blazer::TimeoutNotSupported, "Timeout not supported for #{adapter_name} adapter"
  136. end
  137. end
  138. def use_transaction?
  139. settings.key?("use_transaction") ? settings["use_transaction"] : true
  140. end
  141. def in_transaction
  142. connection_model.connection_pool.with_connection do
  143. if use_transaction?
  144. connection_model.transaction do
  145. yield
  146. raise ActiveRecord::Rollback
  147. end
  148. else
  149. yield
  150. end
  151. end
  152. end
  153. end
  154. end
  155. end