Class: Cybertrain::DB::Blob

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/db/blob.rb

Overview

A bind value that goes to SQLite as a BLOB: Connection#bind sends its bytes with sqlite3_bind_blob and their byte length, so a NUL byte or a byte above 127 is stored as it is. Every other String binds as TEXT, which is why a binary column’s value is wrapped in one (the generated models’ to_row does it for t.binary columns).

Reading needs no wrapper: a BLOB cell comes back from Connection#execute as a String holding exactly the stored bytes (Cast.binary narrows such a cell to a String, “” for NULL).

Examples:

bytes = File.binread("logo.png")
Cybertrain::DB.with do |c|
  c.execute("INSERT INTO images (data) VALUES (?)", [Cybertrain::DB::Blob.new(bytes)])
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Blob

Returns a new instance of Blob.

Parameters:

  • bytes (String) —

    the raw bytes (any encoding; only the bytes count)



27
28
29
# File 'cybertrain/db/blob.rb', line 27

def initialize(bytes)
  @blob_bytes = bytes
end

Instance Attribute Details

#blob_bytes ⇒ String (readonly)

The bytes to store. A distinctive name, not bytes or data (spikes/NOTES.md rules 10 and 34).

Returns:

  • (String)


23
24
25
# File 'cybertrain/db/blob.rb', line 23

def blob_bytes
  @blob_bytes
end