Andrew Kane пре 8 година
родитељ
комит
9652872d6c

+ 4 - 0
app/controllers/blazer/queries_controller.rb

@@ -173,6 +173,10 @@ module Blazer
       render partial: "tables", layout: false
     end
 
+    def schema
+      @schema = Blazer.data_sources[params[:data_source]].schema
+    end
+
     private
 
     def continue_run

+ 18 - 0
app/views/blazer/queries/schema.html.erb

@@ -0,0 +1,18 @@
+<% @schema.each do |table| %>
+  <h4>
+    <%= table[:table] %>
+    <% if table[:schema] != "public" %>
+      <small><%= table[:schema] %></small>
+    <% end %>
+  </h4>
+  <table class="table" style="max-width: 500px;">
+    <tbody>
+      <% table[:columns].each do |column| %>
+        <tr>
+          <td style="width: 60%;"><%= column[:name] %></td>
+          <td class="text-muted"><%= column[:data_type] %></td>
+        </tr>
+      <% end %>
+    </tbody>
+  </table>
+<% end %>

+ 1 - 0
config/routes.rb

@@ -3,6 +3,7 @@ Blazer::Engine.routes.draw do
     post :run, on: :collection # err on the side of caution
     post :refresh, on: :member
     get :tables, on: :collection
+    get :schema, on: :collection
   end
   resources :checks, except: [:show] do
     get :run, on: :member

+ 4 - 0
lib/blazer/adapters/base_adapter.rb

@@ -15,6 +15,10 @@ module Blazer
         [] # optional, but nice to have
       end
 
+      def schema
+        [] # optional, but nice to have
+      end
+
       def preview_statement
         "" # also optional, but nice to have
       end

+ 5 - 0
lib/blazer/adapters/sql_adapter.rb

@@ -44,6 +44,11 @@ module Blazer
         result.rows.map(&:first)
       end
 
+      def schema
+        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]))
+        result.rows.group_by { |r| [r[0], r[1]] }.map { |k, vs| {schema: k[0], table: k[1], columns: vs.sort_by { |v| v[4].to_i }.map { |v| {name: v[2], data_type: v[3]} }} }
+      end
+
       def preview_statement
         "SELECT * FROM {table} LIMIT 10"
       end