Sha256: a539b4be691271ffe31566afcb8296074ff9de27dde7c1a3d6660ad800263f5b

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 KB

Contents

module ArJdbc
  module MSSQL
    # @note Lot of code kindly borrowed from **activerecord-sqlserver-adapter**.
    module Utils

      module_function

      GET_TABLE_NAME_INSERT_UPDATE_RE =
        /^\s*(INSERT|EXEC sp_executesql N'INSERT)\s+INTO\s+([^\(\s,]+)\s*|^\s*update\s+([^\(\s,]+)\s*/i

      GET_TABLE_NAME_FROM_RE = /\bFROM\s+([^\(\)\s,]+)\s*/i

      def get_table_name(sql, qualified = nil)
        if sql =~ GET_TABLE_NAME_INSERT_UPDATE_RE
          tn = $2 || $3
          qualified ? tn : unqualify_table_name(tn)
        elsif sql =~ GET_TABLE_NAME_FROM_RE
          qualified ? $1 : unqualify_table_name($1)
        else
          nil
        end
      end

      def unquote_table_name(table_name)
        remove_identifier_delimiters(table_name)
      end

      def unquote_column_name(column_name)
        remove_identifier_delimiters(column_name)
      end

      def unquote_string(string)
        string.to_s.gsub("''", "'")
      end

      def unqualify_table_name(table_name)
        remove_identifier_delimiters(table_name.to_s.split('.').last)
      end

      def unqualify_table_schema(table_name)
        schema_name = table_name.to_s.split('.')[-2]
        schema_name.nil? ? nil : remove_identifier_delimiters(schema_name)
      end

      def unqualify_db_name(table_name)
        table_names = table_name.to_s.split('.')
        table_names.length == 3 ? remove_identifier_delimiters(table_names.first) : nil
      end

      # private

      # See "Delimited Identifiers": http://msdn.microsoft.com/en-us/library/ms176027.aspx
      def remove_identifier_delimiters(keyword)
        keyword.to_s.tr("\]\[\"", '')
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
activerecord-jdbc-adapter-1.3.17 lib/arjdbc/mssql/utils.rb
activerecord-jdbc-adapter-1.3.16 lib/arjdbc/mssql/utils.rb
activerecord-jdbc-adapter-1.3.15 lib/arjdbc/mssql/utils.rb
activerecord-jdbc-adapter-1.3.14 lib/arjdbc/mssql/utils.rb
activerecord-jdbc-adapter-1.3.13 lib/arjdbc/mssql/utils.rb
activerecord-jdbc-adapter-1.3.12 lib/arjdbc/mssql/utils.rb
activerecord-jdbc-adapter-1.3.11 lib/arjdbc/mssql/utils.rb
activerecord-jdbc-adapter-1.3.10 lib/arjdbc/mssql/utils.rb