Class: Mysql::Packet
- Inherits:
-
Object
- Object
- Mysql::Packet
- Defined in:
- lib/mysql/packet.rb
Overview
Mysql::Packet
Class Method Summary collapse
-
.lcb(num) ⇒ Object
convert Numeric to LengthCodedBinary.
-
.lcs(str) ⇒ Object
convert String to LengthCodedString.
Instance Method Summary collapse
- #eof? ⇒ Boolean
-
#initialize(data) ⇒ Packet
constructor
A new instance of Packet.
- #lcb ⇒ Object
- #lcs ⇒ Object
- #read(len) ⇒ Object
- #string ⇒ Object
- #to_s ⇒ Object
- #ulong ⇒ Object
- #ushort ⇒ Object
- #utiny ⇒ Object
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 |
Instance Method Details
#eof? ⇒ Boolean
71 72 73 |
# File 'lib/mysql/packet.rb', line 71 def eof? @data[0] == ?\xfe && @data.length == 5 end |
#lcb ⇒ Object
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 |
#lcs ⇒ Object
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 |
#string ⇒ Object
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_s ⇒ Object
75 76 77 |
# File 'lib/mysql/packet.rb', line 75 def to_s @data end |
#ulong ⇒ Object
67 68 69 |
# File 'lib/mysql/packet.rb', line 67 def ulong @data.slice!(0, 4).unpack1('V') end |
#ushort ⇒ Object
63 64 65 |
# File 'lib/mysql/packet.rb', line 63 def ushort @data.slice!(0, 2).unpack1('v') end |
#utiny ⇒ Object
59 60 61 |
# File 'lib/mysql/packet.rb', line 59 def utiny @data.slice!(0, 1).unpack1('C') end |