tqbf / asymy

Trivial async MySQL driver for Ruby EventMachine

This URL has Read+Write access

asymy / messages.rb
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 1 require File.dirname(__FILE__) + '/asymy'
8d8cf684 » Thomas Ptacek 2008-06-11 import 2
3 module Asymy
4 class Framer < String
5 include StringX
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 6
8d8cf684 » Thomas Ptacek 2008-06-11 import 7 def complete?
8 return false if (sz = size()) < 4
9 return false if sz < (to_l24() + 4)
10 return true
11 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 12
8d8cf684 » Thomas Ptacek 2008-06-11 import 13 def next_buffer
14 sz = shift_l24()
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 15 num = slice!(0).ord
8d8cf684 » Thomas Ptacek 2008-06-11 import 16 return num, slice!(0, sz).extend(StringX)
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 17 end
18 end
8d8cf684 » Thomas Ptacek 2008-06-11 import 19
20 module Packets
21 class Packet
22 def self.field(n, t, default=nil)
23 @@fields ||= Hash.new {|h, k| h[k] = []}
24 @@fields[self] << [n, t, default]
25 attr_accessor n
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 26 end
27
28 def initialize(buf=nil)
8d8cf684 » Thomas Ptacek 2008-06-11 import 29 if buf
30 @@fields[self.class].each do |tup|
31 sym = "@#{ tup[0] }".intern
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 32 if (t = tup[1].to_s)[0].ord.chr == "r"
8d8cf684 » Thomas Ptacek 2008-06-11 import 33 instance_variable_set sym, buf.shift_r(t[1..-1].to_i)
34 else
35 instance_variable_set sym, buf.send("shift_#{ t }")
36 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 37 end
8d8cf684 » Thomas Ptacek 2008-06-11 import 38 else
39 @@fields[self.class].each do |tup|
40 if tup[2]
41 sym = "@#{ tup[0] }".intern
42 instance_variable_set sym, tup[2]
43 end
44 end
45 end
46 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 47
8d8cf684 » Thomas Ptacek 2008-06-11 import 48 def marshall(num=0)
49 m = @@fields[self.class].map do |tup|
50 sym = "@#{ tup[0] }".intern
51 t = tup[1].to_s
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 52 t = "r" if t[0].ord.chr == "r"
8d8cf684 » Thomas Ptacek 2008-06-11 import 53 instance_variable_get(sym).send("to_#{ t }")
54 end.join("")
55
56 (m.size.to_l24 + num.chr + m).extend(StringX)
57 end
58 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 59
8d8cf684 » Thomas Ptacek 2008-06-11 import 60 class Greeting < Packet
61 field :protocol_version, :l8
62 field :server_version, :asciiz
63 field :thread_id, :l32
64 field :challenge_head, :r8
65 field :zero, :l8
66 field :server_capabilities, :l16
67 field :server_language, :l8
68 field :server_status, :l16
69 field :padding, :r13
70 field :challenge_tail, :r12
71 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 72
8d8cf684 » Thomas Ptacek 2008-06-11 import 73 class Authenticate < Packet
74 field :client_flags, :l32
75 field :max_packet_size, :l32, 0x1000000
76 field :charset_number, :l8
77 field :filler, :r23, ("\x00" * 23).extend(StringX)
78 field :name, :asciiz
79 field :response, :lcstring
80 field :database, :asciiz
81 end
82
83 class OK < Packet
84 field :field_count, :lcb_int
85 field :affected_rows, :lcb_int
86 field :insert_id, :lcb_int
87 field :server_status, :l16
88 field :warning_count, :l16
89 field :message, :asciiz
90 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 91
8d8cf684 » Thomas Ptacek 2008-06-11 import 92 class Error < Packet
93 field :field_count, :l8
94 field :errno, :l16
95 field :sqlstate_market, :l8
96 field :sqlstate, :r5
97 field :message, :asciiz
98 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 99
8d8cf684 » Thomas Ptacek 2008-06-11 import 100 class ResultSet < Packet
101 field :field_count, :lcb_int
102 field :extra, :lcb_int
103 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 104
8d8cf684 » Thomas Ptacek 2008-06-11 import 105 class Field < Packet
106 field :catalog, :lcstring
107 field :db, :lcstring
108 field :table, :lcstring
109 field :org_table, :lcstring
110 field :name, :lcstring
111 field :org_name, :lcstring
112 field :zero, :l8
113 field :charsetnr, :l16
114 field :length, :l32
115 field :type, :l8
116 field :flags, :l16
117 field :decimals, :l8
118 field :z2, :l16
119 field :default, :lcstring
120 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 121
8d8cf684 » Thomas Ptacek 2008-06-11 import 122 class EOF < Packet
123 field :field_count, :l8
124 field :warning_count, :l16
125 field :status_flags, :l16
126 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 127
8d8cf684 » Thomas Ptacek 2008-06-11 import 128 class RowData < Packet
129 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 130
8d8cf684 » Thomas Ptacek 2008-06-11 import 131 class ChangeUser < Packet
132 field :command, :l8, Commands::CHANGE_USER
133 field :name, :asciiz
134 field :password, :lcstring
135 field :database, :asciiz
136 field :charset, :l16, 0x8
137 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 138
8d8cf684 » Thomas Ptacek 2008-06-11 import 139 class ProcessKill < Packet
140 field :command, :l8, Commands::PROCESS_KILL
141 field :process, :l32
142 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 143
8d8cf684 » Thomas Ptacek 2008-06-11 import 144 class Command < Packet
145 field :command, :l8
146 field :arg, :asciiz
147 end
687ee9dd » Thomas Ptacek 2008-06-13 a lazy swipe at prepared st... 148
149 class PrepareStatement < Packet
150 field :command, :l8, Commands::STMT_PREPARE
151 field :query, :asciiz
152 end
153
154 class PrepareOk < Packet
155 field :field_count, :l8
156 field :stmt_id, :l32
157 field :columns, :l16
158 field :parameters, :l16
159 end
ef447a76 » Thomas Ptacek 2008-06-12 Bunch of 1.9 fixes from Roger 160 end
161
8d8cf684 » Thomas Ptacek 2008-06-11 import 162 end