Class | Amalgalite::Blob
In: | lib/amalgalite/blob.rb lib/amalgalite/blob.rb
Parent: | Object

This is the interface to allow Blob objects to be written to and read from the SQLite database. When using statements, use a Blob object as the wrapper around the source to be written to the row, and a Blob object is returned if the the type mapping warrents during select queries.

For instance during an insert:

      blob_column = db.schema.tables['blobs'].columns['data']
      db.execute("INSERT INTO blobs(name, data) VALUES ( $name, $blob )",
                { "$name" => "/path/to/file",
                  "$blob" => Amalgalite::Blob.new( :file => '/path/to/file',
                                                   :column => blob_column) } )

      db.execute("INSERT INTO blobs(id, data) VALUES ($id, $blob )",
                { "$name" => 'blobname',
                  "$blob" => Amalgalite::Blob.new( :io => "something with .read and .length methods",
                                                   :column => blob_column) } )

On select the blob data needs to be read into an IO object

      all_rows = db.execute("SELECT name, blob FROM blobs WHERE name = '/path/to/file' ")
      blob_row = all_rows.first
      blob_row['blob'].write_to_file( blob_row['name'] )

Or write to an IO object

      blob_results = {}
      db.execute("SELECT name, blob FROM blobs") do |row|
        io = StringIO.new
        row['blob'].write_to_io( io )
        blob_results[row['name']] = io
        # or use a shortcut
        # blob_results[row['name']] = row['blob'].to_string_io
      end

If using a Blob as a conditional, for instance in a WHERE clause then the Blob must resolvable to a String.

      db.execute("SELECT FROM blobs(name, data) WHERE data = $blob",
                { "$blob' => Amalgalite::Blob.new( :string => "A string of data" ) })

### Methods

close_source_after_read? close_source_after_read? default_block_size default_block_size incremental? incremental? new new to_s to_s to_string_io to_string_io valid_source_params valid_source_params write_to_column! write_to_column! write_to_file write_to_file write_to_io write_to_io

### Classes and Modules

Class Amalgalite::Blob::Error

### Attributes

block_size | [R] | the size in bytes of the blocks of data to move from the source
block_size | [R] | the size in bytes of the blocks of data to move from the source
column | [R] | the column the blob is associated with
column | [R] | the column the blob is associated with
length | [R] | the size in bytes of the of the blob
length | [R] | the size in bytes of the of the blob
source | [R] | the object representing the source of the blob
source | [R] | the object representing the source of the blob

### Public Class methods

default_block_size()
default_block_size()
new( params )

Initialize a new blob, it takes a single parameter, a hash which describes the source of the blob. The keys of the hash are one of:

      :file    : the value is the path to a file on the file system
      :io      : the value is an object that responds to the the methods +read+
                 and +length+.  +read+ should have the behavior of IO#read
      :db_blob : not normally used by an end user, used to initialize a blob
                 object that is returned from an SQL query.
      :string  : used when a Blob is part of a WHERE clause or result

And additional key of :block_size may be used to indicate the maximum size of a single block of data to move from the source to the destination, this defaults ot 8192.

new( params )

Initialize a new blob, it takes a single parameter, a hash which describes the source of the blob. The keys of the hash are one of:

      :file    : the value is the path to a file on the file system
      :io      : the value is an object that responds to the the methods +read+
                 and +length+.  +read+ should have the behavior of IO#read
      :db_blob : not normally used by an end user, used to initialize a blob
                 object that is returned from an SQL query.
      :string  : used when a Blob is part of a WHERE clause or result

And additional key of :block_size may be used to indicate the maximum size of a single block of data to move from the source to the destination, this defaults ot 8192.

valid_source_params()
valid_source_params()

### Public Instance methods

close_source_after_read?()

close the source when done reading from it

close_source_after_read?()

close the source when done reading from it

incremental?()

is this an incremental Blob or not

incremental?()

is this an incremental Blob or not

to_s()

conver the blob to a string

to_s()

conver the blob to a string

to_string_io()

write the Blob contents to a StringIO

to_string_io()

write the Blob contents to a StringIO

write_to_column!()

Write the Blob contents to the column. This assumes that the row_id to insert into is the last row that was inserted into the db

write_to_column!()

Write the Blob contents to the column. This assumes that the row_id to insert into is the last row that was inserted into the db

write_to_file( filename, modestring="w" )

Write the Blob contents to a File.

write_to_file( filename, modestring="w" )

Write the Blob contents to a File.

write_to_io( io )

Write the Blob to an IO object

write_to_io( io )

Write the Blob to an IO object

[Validate]