Class: SQLTree::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/turntable/sql_tree_patch.rb

Instance Method Summary collapse

Instance Method Details

#each_token(&block) ⇒ Object Also known as: each

Note:

Override to handle x’..‘ binary string

rubocop:disable Lint/EmptyWhen:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 42

def each_token(&block) # :yields: SQLTree::Token
  while next_char
    case current_char
    when /^\s?$/ then # whitespace, go to next character
    when "(" then            handle_token(SQLTree::Token::LPAREN, &block)
    when ")" then            handle_token(SQLTree::Token::RPAREN, &block)
    when "." then            handle_token(SQLTree::Token::DOT, &block)
    when "," then            handle_token(SQLTree::Token::COMMA, &block)
    when /\d/ then           tokenize_number(&block)
    when "'" then            tokenize_quoted_string(&block)
    when "E", "x", "X" then  tokenize_possible_escaped_string(&block)
    when /\w/ then           tokenize_keyword(&block)
    when OPERATOR_CHARS then tokenize_operator(&block)
    when SQLTree.identifier_quote_char then tokenize_quoted_identifier(&block)
    end
  end

  # Make sure to yield any tokens that are still stashed on the queue.
  empty_keyword_queue!(&block)
end

#tokenize_possible_escaped_string(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 65

def tokenize_possible_escaped_string(&block)
  if peek_char == "'"
    token = case current_char
            when "E"
              SQLTree::Token::STRING_ESCAPE
            when "x", "X"
              SQLTree::Token::BINARY_ESCAPE
            end
    handle_token(token, &block)
  else
    tokenize_keyword(&block)
  end
end

#tokenize_quoted_string(&block) ⇒ Object

:yields: SQLTree::Token::String



32
33
34
35
36
37
38
# File 'lib/active_record/turntable/sql_tree_patch.rb', line 32

def tokenize_quoted_string(&block) # :yields: SQLTree::Token::String
  string = ""
  until next_char.nil? || current_char == "'"
    string << (current_char == "\\" ? instance_eval("%@\\#{next_char.gsub('@', '\@')}@") : current_char)
  end
  handle_token(SQLTree::Token::String.new(string), &block)
end