Sha256: 23d669c348aaecbe232462195707f7bf450c9ca4f562e3cab515d85299461e2d

Contents?: true

Size: 880 Bytes

Versions: 5

Compression:

Stored size: 880 Bytes

Contents

require 'mysql2'
require 'mysql_blob_streaming'

module RailsConnector

  module BlobMysql
    def self.included(base)
      base.instance_eval do
        define_method :store_data do
          store_data_atomically {|f| fetch_into_file(f)}
        end
      end
    end

    def fetch_into_file(file)
      mysql2_client = self.class.connection.raw_connection
      ActiveRecord::Base.logger.debug "Streaming blob from MySQL2 into #{file.path}"

      table_name = mysql2_client.escape(self.class.table_name)
      primary_key = mysql2_client.escape(self.class.primary_key)
      escaped_id = mysql2_client.escape(id)
      query = "SELECT `blob_data` FROM `#{table_name}` WHERE `#{primary_key}`='#{escaped_id}'"

      MysqlBlobStreaming.stream(mysql2_client, query, 1.megabyte) do |chunk|
        file << chunk
        chunk = nil
        GC.start
      end
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
infopark_fiona_connector-7.0.1.5.2.3.rc4 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-7.0.1 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-7.0.1.beta2 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-7.0.0 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.10.0.beta1 lib/rails_connector/blob_mysql.rb