module SQLite3
  class Database
    def initialize(filename)
      @handle = Pointer.new(::Sqlite3.type)

      result = sqlite3_open(filename, @handle)
      raise SQLite3Error, sqlite3_errmsg(@handle.value) if result != SQLITE_OK
    end

    def execute(sql, params = nil, &block)
      raise ArgumentError if sql.nil?

      puts "*** #{sql}"
      puts "    #{params.inspect}" if params

      prepare(sql, params) do |statement|
        results = statement.execute

        if block_given?
          results.each do |result|
            yield result
          end
        end
      end
    end

    def execute_scalar(*args)
      result = {}

      execute(*args) do |row|
        result[:value] ||= row.values.first
      end

      result[:value]
    end

    private
    def prepare(sql, params, &block)
      statement = Statement.new(@handle, sql, params)

      begin
        yield statement

      ensure
        statement.finalize
      end
    end
  end
end

module SQLite3
  class Database
    def initialize(filename)
      @handle = Pointer.new(::Sqlite3.type)

      result = sqlite3_open(filename, @handle)
      raise SQLite3Error, sqlite3_errmsg(@handle.value) if result != SQLITE_OK
    end

    def execute(sql, params = nil, &block)
      raise ArgumentError if sql.nil?

      prepare(sql, params) do |statement|
        results = statement.execute

        if block_given?
          results.each do |result|
            yield result
          end
        end
      end
    end

    def execute_debug(sql, params = nil, &block)
      raise ArgumentError if sql.nil?

      puts "*** #{sql}"
      puts "    #{params.inspect}" if params

      prepare(sql, params) do |statement|
        results = statement.execute

        if block_given?
          results.each do |result|
            yield result
          end
        end
      end
    end

    def execute_scalar(*args)
      result = {}

      execute(*args) do |row|
        result[:value] ||= row.values.first
      end

      result[:value]
    end

    private
    def prepare(sql, params, &block)
      statement = Statement.new(@handle, sql, params)

      begin
        yield statement

      ensure
        statement.finalize
      end
    end
  end
end

Remove logging from execute and define new execute_debug method.
Remove logging from execute and define new execute_debug method.
