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" ) })
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 |
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.
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.
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 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