<commit_before># encoding: utf-8

#--
# MosesPG -- a Ruby library for accessing PostgreSQL
# Copyright (C) 2012 <NAME> (user: mosespg; domain: jcain.net)
#
# This file is part of the MosesPG library.  This Library is free software; you
# may redistribute it or modify it under the terms of the license contained in
# the file LICENCE.txt. If you did not receive a copy of the license, please
# contact the copyright holder.
#++

require 'state_machine'
require 'moses_pg/transaction'

module MosesPG

  #
  # Encapsulates a prepared statement
  #
  # Prepared statements can be created in one of two ways: +Statement::prepare+
  # or +Connection#prepare+.
  #
  class Statement

    state_machine :state, :initial => :prepared do
      event :describe_statement_sent do
        transition :prepared => :describe_statement_in_progress
      end
      event :bind_sent do
        transition [:statement_described, :bound, :executed] => :bind_in_progress
      end
      event :execute_sent do
        transition [:statement_described, :bound, :executed] => :execute_in_progress
      end
      event :close_portal_sent do
        transition [:bound, :executed] => :close_portal_in_progress
      end
      event :close_sent do
        transition [:statement_described, :bound, :executed] => :close_in_progress
      end
      event :action_completed do
        transition :describe_statement_in_progress => :statement_described
        transition :bind_in_progress => :bound
        transition :execute_in_progress => :executed
        transition :close_portal_in_progress => :statement_described
        transition :close_in_progress => :closed
      end
      event :action_failed do
        transition [:describe_statement_in_progress, :bind_in_progress, :execute_in_progress, :close_in_progress] => :prepared
      end

      state :prepared do
        def describe(tx = nil)
          deferrable = ::EM::DefaultDeferrable.new
          defer1 = @connection._describe_statement(self, tx)
          describe_statement_sent
          defer1.callback do |result|
            action_completed
            @parameters = result.parameters
            @columns = result.columns
            deferrable.succeed
          end
          defer1.errback do |err|
            action_failed
            deferrable.fail(err)
          end
          deferrable
        end
      end

      state :statement_described do
        def close_portal(tx = nil)
          deferrable = ::EM::DefaultDeferrable.new
          deferrable.succeed
          deferrable
        end
      end

      state :bound, :executed do
        def close_portal(tx = nil)
          deferrable = ::EM::DefaultDeferrable.new
          defer1 = @connection._close_portal(self, tx)
          close_portal_sent
          defer1.callback do
            action_completed
            deferrable.succeed
          end
          defer1.errback do |err|
            action_failed
            deferrable.fail(err)
          end
          deferrable
        end
      end

      state :statement_described, :bound, :executed do
        def _bind(tx, *bindvars)
          deferrable = ::EM::DefaultDeferrable.new
          # close the previous portal if there is one, to release the
          # resources on the backend
          defer1 = close_portal(tx)
          defer1.callback do
            @portal_name = generate_portal_name
            defer2 = @connection._bind(self, bindvars, tx)
            bind_sent
            defer2.callback do
              action_completed
              deferrable.succeed
            end
            defer2.errback do |err|
              action_failed
              deferrable.fail(err)
            end
          end
          defer1.errback { |err| deferrable.fail(err) }
          deferrable
        end

        def execute(*bindvars)<commit_msg>In #close_portal, only actually close it if the last #execute was not in a transaction<commit_after># encoding: utf-8

#--
# MosesPG -- a Ruby library for accessing PostgreSQL
# Copyright (C) 2012 <NAME> (user: mosespg; domain: jcain.net)
#
# This file is part of the MosesPG library.  This Library is free software; you
# may redistribute it or modify it under the terms of the license contained in
# the file LICENCE.txt. If you did not receive a copy of the license, please
# contact the copyright holder.
#++

require 'state_machine'
require 'moses_pg/transaction'

module MosesPG

  #
  # Encapsulates a prepared statement
  #
  # Prepared statements can be created in one of two ways: +Statement::prepare+
  # or +Connection#prepare+.
  #
  class Statement

    state_machine :state, :initial => :prepared do
      event :describe_statement_sent do
        transition :prepared => :describe_statement_in_progress
      end
      event :bind_sent do
        transition [:statement_described, :bound, :executed] => :bind_in_progress
      end
      event :execute_sent do
        transition [:statement_described, :bound, :executed] => :execute_in_progress
      end
      event :close_portal_sent do
        transition [:bound, :executed] => :close_portal_in_progress
      end
      event :close_sent do
        transition [:statement_described, :bound, :executed] => :close_in_progress
      end
      event :action_completed do
        transition :describe_statement_in_progress => :statement_described
        transition :bind_in_progress => :bound
        transition :execute_in_progress => :executed
        transition :close_portal_in_progress => :statement_described
        transition :close_in_progress => :closed
      end
      event :action_failed do
        transition [:describe_statement_in_progress, :bind_in_progress, :execute_in_progress, :close_in_progress] => :prepared
      end

      state :prepared do
        def describe(tx = nil)
          deferrable = ::EM::DefaultDeferrable.new
          defer1 = @connection._describe_statement(self, tx)
          describe_statement_sent
          defer1.callback do |result|
            action_completed
            @parameters = result.parameters
            @columns = result.columns
            deferrable.succeed
          end
          defer1.errback do |err|
            action_failed
            deferrable.fail(err)
          end
          deferrable
        end
      end

      state :statement_described do
        def close_portal(tx = nil)
          deferrable = ::EM::DefaultDeferrable.new
          deferrable.succeed
          deferrable
        end
      end

      state :bound, :executed do
        def close_portal(tx = nil)
          deferrable = ::EM::DefaultDeferrable.new
          if @last_tx
            # if the last bind was in a transaction, the portal was (or will
            # be) closed at the end of that transaction
            deferrable.succeed
          else
            defer1 = @connection._close_portal(self, tx)
            close_portal_sent
            defer1.callback do
              action_completed
              deferrable.succeed
            end
            defer1.errback do |err|
              action_failed
              deferrable.fail(err)
            end
          end
          deferrable
        end
      end

      state :statement_described, :bound, :executed do
        def _bind(tx, *bindvars)
          deferrable = ::EM::DefaultDeferrable.new
          # close the previous portal if there is one, to release the
          # resources on the backend
          defer1 = close_portal(tx)
          defer1.callback do
            @portal_name = generate_portal_name
            @last_tx = tx
            defer2 = @connection._bind(self, bindvars, tx)
            bind_sent
            defer2.callback do
              action_completed
              deferrable.succeed
            end
            defer2.errback do |err|
              action_failed
              deferrable.fail(err)
            end
          end
          defer1.errback { |err| deferrable.fail(err) }
          deferrable
        end

        def execute(*bindvars)
{'fraction_non_alphanumeric': 0.06726681670417604, 'fraction_numerical': 0.0042510627656914225, 'mean_word_length': 2.1015897634742147, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '7287845', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2140, 'n_words': 621}