Mercurial > hg > monetdb-ruby
comparison lib/MonetDBData.rb @ 18:8bf57d01c887
Further improvement, parse header strings between double quotes and with spaces
author | Pedro Ferreira <pedro.ferreira@monetdbsolutions.com> |
---|---|
date | Tue, 03 Dec 2019 17:06:45 +0100 (2019-12-03) |
parents | 6abcfcdb4924 |
children | 139897304835 |
comparison
equal
deleted
inserted
replaced
17:6abcfcdb4924 | 18:8bf57d01c887 |
---|---|
295 end | 295 end |
296 | 296 |
297 return header.freeze | 297 return header.freeze |
298 end | 298 end |
299 | 299 |
300 # Parse escaped strings between double quotes | |
301 def parse_header_table_values(line, start, stop, results) | |
302 i = start | |
303 inString = false | |
304 escaped = false | |
305 | |
306 until i == stop do | |
307 case line[i] | |
308 when '\\' | |
309 escaped = !escaped | |
310 when '"' | |
311 if !inString | |
312 inString = true | |
313 else | |
314 inString = false | |
315 end | |
316 escaped = false | |
317 when ',' | |
318 if !inString # && line[i + 1] == '\t' | |
319 if line[start] == '"' # Don't include the leading " in the column atribute | |
320 start += 1 | |
321 end | |
322 if line[i - 1] == '"' | |
323 next_end = 2 | |
324 else | |
325 next_end = 1 | |
326 end | |
327 results.push(line[start..i - next_end]) | |
328 i += 1 | |
329 start = i + 1 | |
330 end | |
331 escaped = false | |
332 else | |
333 escaped = false | |
334 end | |
335 i += 1 | |
336 end | |
337 if line[start] == '"' # Don't include the leading " in the column atribute | |
338 start += 1 | |
339 end | |
340 if line[stop] == '"' | |
341 next_end = 1 | |
342 else | |
343 next_end = 0 | |
344 end | |
345 results.push(line[start..stop - next_end]) | |
346 end | |
347 | |
300 # Parses a Q_TABLE header and returns information about the schema. | 348 # Parses a Q_TABLE header and returns information about the schema. |
301 def parse_header_table(header_t) | 349 def parse_header_table(header_t) |
302 if @query["type"] == MonetDBConnection::Q_TABLE | 350 if @query["type"] == MonetDBConnection::Q_TABLE |
303 if header_t != nil | 351 if header_t != nil |
304 | 352 |
305 name_t = header_t[0][2..-15].split(' ') # remove # table_name | 353 name_t = Array.new |
306 name_t.each_with_index do |col, i| | 354 parse_header_table_values(header_t[0], 2, header_t[0].length - 15, name_t) |
307 if i != name_t.length - 1 | 355 |
308 name_t[i] = col[0..-2] # remove trailing comma | 356 name_cols = Array.new |
309 end | 357 parse_header_table_values(header_t[1], 2, header_t[1].length - 9, name_cols) |
358 | |
359 type_cols = { } | |
360 type_cols_array = Array.new | |
361 parse_header_table_values(header_t[2], 2, header_t[2].length - 9, type_cols_array) | |
362 type_cols_array.each_with_index do |col, i| | |
363 type_cols[name_cols[i]] = col | |
310 end | 364 end |
311 | 365 |
312 name_cols = header_t[1][2..-9].split(' ') # remove # name | |
313 name_cols.each_with_index do |col, i| | |
314 if i != name_cols.length - 1 | |
315 name_cols[i] = col[0..-2] # remove trailing comma | |
316 end | |
317 end | |
318 | |
319 type_cols = { } | |
320 type_cols_array = header_t[2][2..-9].split(' ') # remove # type | |
321 type_cols_array.each_with_index do |col, i| | |
322 if i != type_cols_array.length - 1 | |
323 type_cols[name_cols[i]] = col[0..-2] # remove trailing comma | |
324 else | |
325 type_cols[name_cols[i]] = col | |
326 end | |
327 end | |
328 | |
329 length_cols = { } | 366 length_cols = { } |
330 length_cols_array = header_t[3][2..-11].split(' ') # remove # length | 367 length_cols_array = Array.new |
368 parse_header_table_values(header_t[3], 2, header_t[3].length - 11, length_cols_array) | |
331 length_cols_array.each_with_index do |col, i| | 369 length_cols_array.each_with_index do |col, i| |
332 if i != length_cols_array.length - 1 | |
333 length_cols[name_cols[i]] = col[0..-2] # remove trailing comma | |
334 else | |
335 length_cols[name_cols[i]] = col | 370 length_cols[name_cols[i]] = col |
336 end | |
337 end | 371 end |
338 | 372 |
339 columns_order = {} | 373 columns_order = {} |
340 name_cols.each_with_index do |col, i| | 374 name_cols.each_with_index do |col, i| |
341 columns_order[col] = i | 375 columns_order[col] = i |