Sha256: 631dbe8e21d6b285dded4c1f0a169f56879d2a3189743ac54ee9d900f0b68e84

Contents?: true

Size: 952 Bytes

Versions: 12

Compression:

Stored size: 952 Bytes

Contents

require 'mysql'
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)
      mysql = connection.instance_variable_get '@connection'
      stmt = mysql.prepare("SELECT blob_data FROM #{self.class.table_name} WHERE #{self.class.primary_key}=?")

      class << stmt
        include MysqlBlobStreaming

        attr_accessor :file
        attr_accessor :logger

        def handle_data(data)
          @file << data
          data = nil
          GC.start
        end
      end

      stmt.logger = ActiveRecord::Base.logger
      stmt.logger.debug "Streaming blob from MySQL into #{file.path}"
      stmt.file = file
      stmt.execute(id)
      stmt.stream(1.megabyte)
    ensure
      stmt.close if stmt
    end
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
infopark_fiona_connector-6.8.0.539.93842523 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.529.114081979 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.518.30999728 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.515.34928522 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.498.46559598 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.480.261594408 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.444.171626367 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.406.131718077 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.356.19698103 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.348.160665197 lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.328.4a753fc lib/rails_connector/blob_mysql.rb
infopark_fiona_connector-6.8.0.322.c003f11 lib/rails_connector/blob_mysql.rb