Sha256: 14d17d18ea07c2468428f7cc6292051e2f731eab4942f7127231764092505c26

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module MiniSql
  module Mysql
    class PreparedConnection < Connection

      attr_reader :unprepared

      def initialize(unprepared_connection)
        @unprepared         = unprepared_connection
        @raw_connection     = unprepared_connection.raw_connection
        @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

      def deserializer_cache
        @unprepared.deserializer_cache
      end

      private def run(sql, as, params)
        prepared_sql, binds, _bind_names = @param_binder.bind(sql, *params)
        statement = @prepared_cache.prepare_statement(prepared_sql)
        statement.execute(
          *binds,
          as: as,
          database_timezone: :utc,
          application_timezone: :utc,
          cast_booleans: true,
          cast: true,
          cache_rows: true,
          symbolize_keys: false
        )
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mini_sql-1.6.0 lib/mini_sql/mysql/prepared_connection.rb
mini_sql-1.5.0 lib/mini_sql/mysql/prepared_connection.rb
mini_sql-1.4.0 lib/mini_sql/mysql/prepared_connection.rb
mini_sql-1.3.0 lib/mini_sql/mysql/prepared_connection.rb
mini_sql-1.2.0 lib/mini_sql/mysql/prepared_connection.rb
mini_sql-1.1.3 lib/mini_sql/mysql/prepared_connection.rb