Class: RubyVolt::DataType::Varbinary

Inherits:
Basic show all
Defined in:
lib/ruby_volt/data_type/basic/varbinary.rb

Direct Known Subclasses

String

Constant Summary collapse

DIRECTIVE =
'a'
NULL_INDICATOR =

SQL NULL indicator for object type serializations

-1 # SQL NULL indicator for object type serializations

Class Method Summary collapse

Methods inherited from Basic

bytesToInt, intToBytes

Methods inherited from RubyVolt::DataType

classifyDataType, testpacking, voltDataType

Class Method Details

.convert_input(val) ⇒ Object



27
28
29
# File 'lib/ruby_volt/data_type/basic/varbinary.rb', line 27

def convert_input(val)
  val.to_s.encode("ascii-8bit")
end

.pack(val) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/ruby_volt/data_type/basic/varbinary.rb', line 8

def pack(val)
  if val.nil?
    Integer.pack(self::NULL_INDICATOR)
  else
    val = convert_input(val)
    Integer.pack(val.bytesize) + val
  end
end

.unpack(bytes) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ruby_volt/data_type/basic/varbinary.rb', line 17

def unpack(bytes)
  length = Integer.unpack(bytes)
  case length
  when self::NULL_INDICATOR then nil
  when 0 then ::String.new
  else
    bytes.read(length).unpack1("#{self::DIRECTIVE}#{length}")
  end
end