Class: Sequel::Postgres::PGArray::Parser
- Defined in:
- lib/sequel/extensions/pg_array.rb
Overview
PostgreSQL array parser that handles PostgreSQL array output format. Note that does not handle all forms out input that PostgreSQL will accept, and it will not raise an error for all forms of invalid input.
Constant Summary collapse
- UNQUOTED_RE =
/[{}",]|[^{}",]+/
- QUOTED_RE =
/["\\]|[^"\\]+/
- NULL_RE =
/NULL",/
- OPEN_RE =
/((\[\d+:\d+\])+=)?\{/
Instance Method Summary collapse
-
#initialize(source, converter = nil) ⇒ Parser
constructor
A new instance of Parser.
- #new_entry(include_empty = false) ⇒ Object
- #parse ⇒ Object
Constructor Details
Instance Method Details
#new_entry(include_empty = false) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/sequel/extensions/pg_array.rb', line 360 def new_entry(include_empty=false) if !@recorded.empty? || include_empty entry = @recorded if entry == NULL && !include_empty entry = nil elsif @converter entry = @converter.call(entry) end @stack.last.push(entry) @recorded = String.new end end |
#parse ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/sequel/extensions/pg_array.rb', line 375 def parse raise Sequel::Error, "invalid array, empty string" if eos? raise Sequel::Error, "invalid array, doesn't start with {" unless scan(OPEN_RE) while !eos? char = scan(UNQUOTED_RE) if char == COMMA # Comma outside quoted string indicates end of current entry new_entry elsif char == QUOTE raise Sequel::Error, "invalid array, opening quote with existing recorded data" unless @recorded.empty? while true char = scan(QUOTED_RE) if char == BACKSLASH @recorded << getch elsif char == QUOTE n = peek(1) raise Sequel::Error, "invalid array, closing quote not followed by comma or closing brace" unless n == COMMA || n == CLOSE_BRACE break else @recorded << char end end new_entry(true) elsif char == OPEN_BRACE raise Sequel::Error, "invalid array, opening brace with existing recorded data" unless @recorded.empty? # Start of new array, add it to the stack new = [] @stack.last << new @stack << new elsif char == CLOSE_BRACE # End of current array, add current entry to the current array new_entry if @stack.length == 1 raise Sequel::Error, "array parsing finished without parsing entire string" unless eos? # Top level of array, parsing should be over. # Pop current array off stack and return it as result return @stack.pop else # Nested array, pop current array off stack @stack.pop end else # Add the character to the recorded character buffer. @recorded << char end end raise Sequel::Error, "array parsing finished with array unclosed" end |