<commit_before>require 'data_mapper/adapters/abstract_adapter'
require 'data_mapper/adapters/sql/commands/load_command'
require 'data_mapper/adapters/sql/commands/advanced_load_command'
require 'data_mapper/adapters/sql/commands/save_command'
require 'data_mapper/adapters/sql/commands/delete_command'
require 'data_mapper/adapters/sql/commands/table_exists_command'
require 'data_mapper/adapters/sql/coersion'
require 'data_mapper/adapters/sql/quoting'
require 'data_mapper/adapters/sql/mappings/schema'
require 'data_mapper/support/connection_pool'

module DataMapper
  
  # An Adapter is really a Factory for three types of object,
  # so they can be selectively sub-classed where needed.
  # 
  # The first type is a Query. The Query is an object describing
  # the database-specific operations we wish to perform, in an
  # abstract manner. For example: While most if not all databases
  # support a mechanism for limiting the size of results returned,
  # some use a "LIMIT" keyword, while others use a "TOP" keyword.
  # We can set a SelectStatement#limit field then, and allow
  # the adapter to override the underlying SQL generated.
  # Refer to DataMapper::Queries.
  # 
  # The second type provided by the Adapter is a DataMapper::Connection.
  # This allows us to execute queries and return results in a clear and
  # uniform manner we can use throughout the DataMapper.
  #
  # The final type provided is a DataMapper::Transaction.
  # Transactions are duck-typed Connections that span multiple queries.
  #
  # Note: It is assumed that the Adapter implements it's own
  # ConnectionPool if any since some libraries implement their own at
  # a low-level, and it wouldn't make sense to pay a performance
  # cost twice by implementing a secondary pool in the DataMapper itself.
  # If the library being adapted does not provide such functionality,
  # DataMapper::Support::ConnectionPool can be used.
  module Adapters
      
    # You must inherit from the SqlAdapter, and implement the
    # required methods to adapt a database library for use with the DataMapper.
    #
    # NOTE: By inheriting from SqlAdapter, you get a copy of all the
    # standard sub-modules (Quoting, Coersion and Queries) in your own Adapter.
    # You can extend and overwrite these copies without affecting the originals.
    class SqlAdapter < AbstractAdapter
      
      FIND_OPTIONS = [
        :select, :limit, :class, :include, :shallow_include, :reload, :conditions, :order, :intercept_load
      ]
      
      def initialize(configuration)
        super
        
        unless @configuration.single_threaded?
          @connection_pool = Support::ConnectionPool.new { create_connection }
        end
      end
      
      def create_connection
        raise NotImplementedError.new
      end
      
      def close_connection(conn)
        raise NotImplementedError.new
      end
      
      # Yields an available connection. Flushes the connection-pool if
      # the connection returns an error.
      def connection
        begin
          if @configuration.single_threaded?
            yield(@active_connection || @active_connection = create_connection)
          else
            @connection_pool.hold { |active_connection| yield(active_connection) }
          end
        rescue => execution_error
          @configuration.log.fatal(execution_error)
          
          begin
            if @configuration.single_threaded?
              close_connection(@active_connection)
            else
              @connections.available_connections.each do |active_connection|
                close_connection(active_connection)
              end
            end
          rescue => close_connection_error
            @configuration.log.error(close_connection_error)
          end
          
          if @configuration.single_threaded?
            @active_connection = create_connection
          else
            @connections.available_connections.clear
          end
          
          raise execution_error
        end
      end
        
      def transaction(&block)
        raise NotImplementedError.new
      end
      
      def query(sql)
        raise NotImplementedError.new
      end
      
      def schema
        @schema || ( @schema = Mappings::Schema.new(self) )
      end<commit_msg>Added some documentation to #connection<commit_after>require 'data_mapper/adapters/abstract_adapter'
require 'data_mapper/adapters/sql/commands/load_command'
require 'data_mapper/adapters/sql/commands/advanced_load_command'
require 'data_mapper/adapters/sql/commands/save_command'
require 'data_mapper/adapters/sql/commands/delete_command'
require 'data_mapper/adapters/sql/commands/table_exists_command'
require 'data_mapper/adapters/sql/coersion'
require 'data_mapper/adapters/sql/quoting'
require 'data_mapper/adapters/sql/mappings/schema'
require 'data_mapper/support/connection_pool'

module DataMapper
  
  # An Adapter is really a Factory for three types of object,
  # so they can be selectively sub-classed where needed.
  # 
  # The first type is a Query. The Query is an object describing
  # the database-specific operations we wish to perform, in an
  # abstract manner. For example: While most if not all databases
  # support a mechanism for limiting the size of results returned,
  # some use a "LIMIT" keyword, while others use a "TOP" keyword.
  # We can set a SelectStatement#limit field then, and allow
  # the adapter to override the underlying SQL generated.
  # Refer to DataMapper::Queries.
  # 
  # The second type provided by the Adapter is a DataMapper::Connection.
  # This allows us to execute queries and return results in a clear and
  # uniform manner we can use throughout the DataMapper.
  #
  # The final type provided is a DataMapper::Transaction.
  # Transactions are duck-typed Connections that span multiple queries.
  #
  # Note: It is assumed that the Adapter implements it's own
  # ConnectionPool if any since some libraries implement their own at
  # a low-level, and it wouldn't make sense to pay a performance
  # cost twice by implementing a secondary pool in the DataMapper itself.
  # If the library being adapted does not provide such functionality,
  # DataMapper::Support::ConnectionPool can be used.
  module Adapters
      
    # You must inherit from the SqlAdapter, and implement the
    # required methods to adapt a database library for use with the DataMapper.
    #
    # NOTE: By inheriting from SqlAdapter, you get a copy of all the
    # standard sub-modules (Quoting, Coersion and Queries) in your own Adapter.
    # You can extend and overwrite these copies without affecting the originals.
    class SqlAdapter < AbstractAdapter
      
      FIND_OPTIONS = [
        :select, :limit, :class, :include, :shallow_include, :reload, :conditions, :order, :intercept_load
      ]
      
      def initialize(configuration)
        super
        
        unless @configuration.single_threaded?
          @connection_pool = Support::ConnectionPool.new { create_connection }
        end
      end
      
      def create_connection
        raise NotImplementedError.new
      end
      
      def close_connection(conn)
        raise NotImplementedError.new
      end
      
      # Yields an available connection. Flushes the connection-pool and reconnects
      # if the connection returns an error.
      def connection
        begin
          # Yield the appropriate connection
          if @configuration.single_threaded?
            yield(@active_connection || @active_connection = create_connection)
          else
            @connection_pool.hold { |active_connection| yield(active_connection) }
          end
        rescue => execution_error
          # Log error on failure
          @configuration.log.error(execution_error)
          
          # Close all open connections, assuming that if one
          # had an error, it's likely due to a lost connection,
          # in which case all connections are likely broken.
          begin
            if @configuration.single_threaded?
              close_connection(@active_connection)
            else
              @connections.available_connections.each do |active_connection|
                close_connection(active_connection)
              end
            end
          rescue => close_connection_error
            # An error on closing the connection is almost expected
            # if the socket is broken.
            @configuration.log.warn(close_connection_error)
          end
          
          # Reopen fresh connections.
          if @configuration.single_threaded?
            @active_connection = create_connection
          else
            @connections.available_connections.clear
          end
          
          raise execution_error
        end
      end
        
      def transaction(&block)
        raise NotImplementedError.new
      end
      
      def query(sql)
        raise NotImplementedError.new
      end
      
      def schema
        @schema || ( @schema = Mappings::Schema.new(self) )
      end
{'fraction_non_alphanumeric': 0.05779199294143598, 'fraction_numerical': 0.0, 'mean_word_length': 2.631557869443332, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7235149', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2283, 'n_words': 806}