Sha256: f0057dfb80f5d6e158771d749403e098884cc718d38d125ecc864d99b4c351b8

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module MiniSql
  module Mysql
    class Connection < MiniSql::Connection
      attr_reader :param_encoder, :raw_connection, :deserializer_cache

      def initialize(raw_connection, args = nil)
        @raw_connection = raw_connection
        @param_encoder = (args && args[:param_encoder]) || InlineParamEncoder.new(self)
        @deserializer_cache = (args && args[:deserializer_cache]) || DeserializerCache.new
      end

      def query_single(sql, *params)
        run(sql, :array, params).to_a.flatten!
      end

      def query_hash(sql, *params)
        result = run(sql, :hash, params)
        result.to_a
      end

      def exec(sql, *params)
        run(sql, :array, params)
        raw_connection.affected_rows
      end

      def query(sql, *params)
        result = run(sql, :array, params)
        @deserializer_cache.materialize(result)
      end

      def escape_string(str)
        raw_connection.escape(str)
      end

      def build(sql)
        Builder.new(self, sql)
      end

      private

      def run(sql, as, params)
        if params && params.length > 0
          sql = param_encoder.encode(sql, *params)
        end
        raw_connection.query(
          sql, 
          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

3 entries across 3 versions & 1 rubygems

Version Path
mini_sql-0.2.4 lib/mini_sql/mysql/connection.rb
mini_sql-0.2.3-java lib/mini_sql/mysql/connection.rb
mini_sql-0.2.3 lib/mini_sql/mysql/connection.rb