Sha256: b11f09118f7031295e00af911858941b896f97c293c06d43c6c9ce79c341b2fb

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module MiniSql
  module Sqlite
    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)
        # a bit lazy can be optimized
        run(sql, *params).flatten!
      end

      def query_hash(sql, *params)
        r = []
        run(sql, *params) do |set|
          set.each_hash do |h|
            r << h
          end
        end
        r
      end

      def query_array(sql, *params)
        run(sql, *params)
      end

      def exec(sql, *params)

        start = raw_connection.total_changes

        r = run(sql, *params)
        # this is not safe for multithreading, also for DELETE from TABLE will return
        # incorrect data
        if r.length > 0
          r.length
        else
          raw_connection.total_changes - start
        end
      end

      def query(sql, *params)
        run(sql, *params) do |set|
          deserializer_cache.materialize(set)
        end
      end

      def query_decorator(decorator, sql, *params)
        run(sql, *params) do |set|
          deserializer_cache.materialize(set, decorator)
        end
      end

      def escape_string(str)
        str.gsub("'", "''")
      end

      private

      def run(sql, *params)
        sql = param_encoder.encode(sql, *params)
        if block_given?
          stmt = SQLite3::Statement.new(raw_connection, sql)
          yield stmt.execute
        else
          raw_connection.execute(sql)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mini_sql-1.0 lib/mini_sql/sqlite/connection.rb