Sha256: 9f98d5a84a5587680a04f89707be6316eaebd4d16408b44f76e5eb2b81a12127

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module MiniSql
  module Postgres
    class PreparedConnection < Connection

      attr_reader :unprepared

      def initialize(unprepared_connection, deserializer_cache)
        @unprepared         = unprepared_connection
        @raw_connection     = unprepared_connection.raw_connection
        @deserializer_cache = deserializer_cache
        @type_map           = unprepared_connection.type_map
        @param_encoder      = unprepared_connection.param_encoder

        @prepared_cache     = PreparedCache.new(@raw_connection)
        @param_binder       = PreparedBinds.new
      end

      def build(_)
        raise 'Builder can not be called on prepared connections, instead of `::MINI_SQL.prepared.build(sql).query` use `::MINI_SQL.build(sql).prepared.query`'
      end

      def prepared(condition = true)
        condition ? self : @unprepared
      end

      private def run(sql, params)
        prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)
        prepare_statement_key = @prepared_cache.prepare_statement(prepared_sql)
        raw_connection.exec_prepared(prepare_statement_key, binds)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mini_sql-1.1.2 lib/mini_sql/postgres/prepared_connection.rb
mini_sql-1.1.1 lib/mini_sql/postgres/prepared_connection.rb
mini_sql-1.1.0 lib/mini_sql/postgres/prepared_connection.rb