Class: Mysql::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/packet.rb

Overview

Mysql::Packet

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Packet

Returns a new instance of Packet.



21
22
23
# File 'lib/mysql/packet.rb', line 21

def initialize(data)
  @data = data
end

Class Method Details

.lcb(num) ⇒ Object

convert Numeric to LengthCodedBinary



7
8
9
10
11
12
13
# File 'lib/mysql/packet.rb', line 7

def self.lcb(num)
  return "\xfb" if num.nil?
  return [num].pack("C") if num < 251
  return [252, num].pack("Cv") if num < 65536
  return [253, num&0xffff, num>>16].pack("CvC") if num < 16777216
  return [254, num&0xffffffff, num>>32].pack("CVV")
end

.lcs(str) ⇒ Object

convert String to LengthCodedString



16
17
18
19
# File 'lib/mysql/packet.rb', line 16

def self.lcs(str)
  str = Charset.to_binary str.dup
  lcb(str.length)+str
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/mysql/packet.rb', line 71

def eof?
  @data[0] == ?\xfe && @data.length == 5
end

#lcbObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mysql/packet.rb', line 25

def lcb
  return nil if @data.empty?
  case v = utiny
  when 0xfb
    return nil
  when 0xfc
    return ushort
  when 0xfd
    c, v = utiny, ushort
    return (v << 8)+c
  when 0xfe
    v1, v2 = ulong, ulong
    return (v2 << 32)+v1
  else
    return v
  end
end

#lcsObject



43
44
45
46
47
# File 'lib/mysql/packet.rb', line 43

def lcs
  len = self.lcb
  return nil unless len
  @data.slice!(0, len)
end

#read(len) ⇒ Object



49
50
51
# File 'lib/mysql/packet.rb', line 49

def read(len)
  @data.slice!(0, len)
end

#stringObject



53
54
55
56
57
# File 'lib/mysql/packet.rb', line 53

def string
  str = @data.unpack1('Z*')
  @data.slice!(0, str.length+1)
  str
end

#to_sObject



75
76
77
# File 'lib/mysql/packet.rb', line 75

def to_s
  @data
end

#ulongObject



67
68
69
# File 'lib/mysql/packet.rb', line 67

def ulong
  @data.slice!(0, 4).unpack1('V')
end

#ushortObject



63
64
65
# File 'lib/mysql/packet.rb', line 63

def ushort
  @data.slice!(0, 2).unpack1('v')
end

#utinyObject



59
60
61
# File 'lib/mysql/packet.rb', line 59

def utiny
  @data.slice!(0, 1).unpack1('C')
end