Sha256: 1d24c47e7163c3079a2d2b40be33f7e6c7fa6ab3acd4c2359562fd10dbf44040

Contents?: true

Size: 1.67 KB

Versions: 38

Compression:

Stored size: 1.67 KB

Contents

require 'active_record/connection_adapters/abstract_mysql_adapter'

module ActiveRecord
  module ConnectionAdapters
    class AbstractMysqlAdapter

      def extract_schema_qualified_name_of_tables(table_names)
        table_names.map do |string|
          schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/)
          schema, name = nil, schema unless name
          [schema, name]
        end
      end

      def quoted_scope_of_tables(names = nil)
        extract_schema_qualified_name_of_tables(names).map do |(schema, name)|
          scope = {}
          scope[:schema] = schema ? quote(schema) : "database()"
          scope[:name] = quote(name) if name
          scope
        end
      end

      def primary_keys_of_tables(table_names) # :nodoc:
        raise ArgumentError unless table_names.present?

        scopes = quoted_scope_of_tables(table_names)

        res = query(<<~SQL, "SCHEMA")
        SELECT table_name, column_name
        FROM information_schema.statistics
        WHERE index_name = 'PRIMARY'
        AND (table_schema, table_name) in
        (#{scopes.map { |scope| "(#{scope[:schema]}, #{scope[:name]})" }.join(', ')})
          ORDER BY seq_in_index
        SQL

        res.group_by(&:first).map { |table, vlaues| [table, vlaues.map(&:last)] }.to_h
      end

      def table_comment_of_tables(table_names) # :nodoc:
        scopes = quoted_scope_of_tables(table_names)

        query(<<~SQL, "SCHEMA").presence.try(&:to_h)
        SELECT table_name, table_comment
        FROM information_schema.tables
        WHERE (table_schema, table_name) in
        (#{scopes.map { |scope| "(#{scope[:schema]}, #{scope[:name]})" }.join(', ')})
        SQL
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 2 rubygems

Version Path
arql-0.4.11 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.10 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.8 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.7 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.6 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.3 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.2 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.1 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.4.0 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.31 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.30 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.29 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.28 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.27 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.26 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.25 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.24 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.23 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.22 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb
arql-0.3.21 lib/arql/ext/active_record/connection_adapters/abstract_mysql_adapter.rb