Sha256: 7bd8b024a22426f9a0fd95a32e6be139e3f47d8ddee1f0c76fa916e09c51b74f

Contents?: true

Size: 1.62 KB

Versions: 6

Compression:

Stored size: 1.62 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 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 escape_string(str)
        str.gsub("'","''")
      end

      private

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

6 entries across 6 versions & 1 rubygems

Version Path
mini_sql-0.2.4 lib/mini_sql/sqlite/connection.rb
mini_sql-0.2.2-java lib/mini_sql/sqlite/connection.rb
mini_sql-0.2.3-java lib/mini_sql/sqlite/connection.rb
mini_sql-0.2.3 lib/mini_sql/sqlite/connection.rb
mini_sql-0.2.2 lib/mini_sql/sqlite/connection.rb
mini_sql-0.2.1 lib/mini_sql/sqlite/connection.rb