Class: TriCoreDB::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/tricoredb/params.rb

Overview

Explicit marker for bytes bound to a BLOB column, whatever the String's encoding.

Examples:

db.execute("INSERT INTO files VALUES (?, ?)", [1, TriCoreDB::Binary.new(File.binread("a.png"))])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ Binary

Returns a new instance of Binary.

Parameters:

  • bytes (String, Array<Integer>)


21
22
23
24
# File 'lib/tricoredb/params.rb', line 21

def initialize(bytes)
  @bytes = bytes.is_a?(Array) ? bytes.pack("C*") : String(bytes).b
  @bytes.freeze
end

Instance Attribute Details

#bytes ⇒ String (readonly)

Returns the bytes, as a binary (ASCII-8BIT) String.

Returns:

  • (String) —

    the bytes, as a binary (ASCII-8BIT) String



18
19
20
# File 'lib/tricoredb/params.rb', line 18

def bytes
  @bytes
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



31
32
33
# File 'lib/tricoredb/params.rb', line 31

def ==(other)
  other.is_a?(Binary) && other.bytes == bytes
end

#hash ⇒ Object



36
37
38
# File 'lib/tricoredb/params.rb', line 36

def hash
  bytes.hash
end

#to_param ⇒ String

Returns the 0x… hex text a BLOB column parses.

Returns:

  • (String) —

    the 0x… hex text a BLOB column parses



27
28
29
# File 'lib/tricoredb/params.rb', line 27

def to_param
  "0x#{@bytes.unpack1('H*')}"
end