forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompat.rb
269 lines (228 loc) · 5.16 KB
/
compat.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: binary -*-
# Copyright (C) 2008 TOMITA Masahiro
# mailto:[email protected]
# for compatibility
class RbMysql
class << self
def connect(*args)
my = self.allocate
my.instance_eval{initialize}
my.connect(*args)
my
end
alias new connect
alias real_connect connect
def init
my = self.allocate
my.instance_eval{initialize}
my
end
def client_version
50067
end
def client_info
"5.0.67"
end
alias get_client_info client_info
def escape_string(str)
str.gsub(/[\0\n\r\\\'\"\x1a]/n) do |s|
case s
when "\0" then "\\0"
when "\n" then "\\n"
when "\r" then "\\r"
when "\x1a" then "\\Z"
else "\\#{s}"
end
end
end
alias quote escape_string
end
attr_accessor :query_with_result, :reconnect
alias stmt_init statement
alias real_connect connect
alias initialize_orig initialize
def initialize(*args)
initialize_orig(*args)
@query_with_result = true
@reconnect = false
end
def query(str)
res = simple_query str
if res
res.each do |rec|
rec.map!{|v| v && v.to_s}
rec.each_index do |i|
@fields[i].max_length = [rec[i] ? rec[i].length : 0, @fields[i].max_length||0].max
end
end
res.data_seek 0
end
res
end
def client_version
self.class.client_version
end
def options(opt, val=nil)
case opt
when INIT_COMMAND
@init_command = val
when OPT_COMPRESS
raise ClientError, "not implemented"
when OPT_CONNECT_TIMEOUT
@connect_timeout = val
when OPT_GUESS_CONNECTION
raise ClientError, "not implemented"
when OPT_LOCAL_INFILE
@local_infile = val
when OPT_NAMED_PIPE
raise ClientError, "not implemented"
when OPT_PROTOCOL
raise ClientError, "not implemented"
when OPT_READ_TIMEOUT
@read_timeout = val
when OPT_USE_EMBEDDED_CONNECTION
raise ClientError, "not implemented"
when OPT_USE_REMOTE_CONNECTION
raise ClientError, "not implemented"
when OPT_WRITE_TIMEOUT
@write_timeout = val
when READ_DEFAULT_FILE
raise ClientError, "not implemented"
when READ_DEFAULT_GROUP
raise ClientError, "not implemented"
when SECURE_AUTH
raise ClientError, "not implemented"
when SET_CHARSET_DIR
raise ClientError, "not implemented"
when SET_CHARSET_NAME
self.charset = val
when SET_CLIENT_IP
raise ClientError, "not implemented"
when SHARED_MEMORY_BASE_NAME
raise ClientError, "not implemented"
else
raise ClientError, "unknown option: #{opt}"
end
self
end
def store_result
raise ClientError, "no result set" unless @fields
Result.new @fields, @stream
end
def use_result
raise ClientError, "no result set" unless @fields
Result.new @fields, @stream, false
end
class Result
alias initialize_orig initialize
def initialize(*args)
initialize_orig *args
@field_index = 0
end
def num_rows
@records.length
end
def data_seek(n)
@index = n
end
def row_tell
@index
end
def row_seek(n)
ret = @index
@index = n
ret
end
def free
# do nothing
end
alias fetch_row_orig fetch_row
def fetch_row
@fetched_record = fetch_row_orig
end
def fetch_field
return nil if @field_index >= @fields.length
ret = @fields[@field_index]
@field_index += 1
ret
end
def field_tell
@field_index
end
def field_seek(n)
@field_index = n
end
def fetch_field_direct(n)
raise ClientError, "invalid argument: #{n}" if n < 0 or n >= @fields.length
@fields[n]
end
def fetch_fields
@fields
end
def fetch_lengths
return nil unless @fetched_record
@fetched_record.map{|c|c.nil? ? 0 : c.length}
end
def num_fields
@fields.length
end
end
class Field
attr_accessor :max_length
def hash
{
"name" => @name,
"table" => @table,
"def" => @default,
"type" => @type,
"length" => @length,
"max_length" => @max_length,
"flags" => @flags,
"decimals" => @decimals
}
end
def inspect
"#<RbMysql::Field:#{@name}>"
end
end
class Statement
alias execute_orig execute
def execute(*args)
@res = execute_orig *args
end
def fetch
@res.fetch
end
alias fetch_row fetch
def each(*args, &block)
@res.each(*args, &block)
end
def num_rows
@res.num_rows
end
def data_seek(n)
@res.data_seek(n)
end
def row_tell
@res.row_tell
end
def row_seek(n)
@res.row_seek(n)
end
def field_count
@fields.length
end
def free_result
# do nothing
end
def result_metadata
return nil if @fields.empty?
res = Result.allocate
res.instance_variable_set :@mysql, @mysql
res.instance_variable_set :@fields, @fields
res.instance_variable_set :@records, []
res
end
end
Stmt = Statement
end