Module: Sparkql::ParserCompatibility
- Included in:
- Parser
- Defined in:
- lib/sparkql/parser_compatibility.rb
Overview
Required interface for existing parser implementations
Constant Summary collapse
- MAXIMUM_MULTIPLE_VALUES =
200
- MAXIMUM_EXPRESSIONS =
75
- MAXIMUM_LEVEL_DEPTH =
2
- FILTER_VALUES =
TODO I Really don’t think this is required anymore Ordered by precedence.
[ { :type => :datetime, :regex => /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}\:[0-9]{2}\:[0-9]{2}\.[0-9]{6}$/, :operators => Sparkql::Token::OPERATORS + [Sparkql::Token::RANGE_OPERATOR] }, { :type => :date, :regex => /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/, :operators => Sparkql::Token::OPERATORS + [Sparkql::Token::RANGE_OPERATOR] }, { :type => :time, :regex => /^[0-9]{2}\:[0-9]{2}(\:[0-9]{2})?(\.[0-9]{6)$/, :operators => Sparkql::Token::OPERATORS + [Sparkql::Token::RANGE_OPERATOR] }, { :type => :character, :regex => /^'([^'\\]*(\\.[^'\\]*)*)'$/, # Strings must be single quoted. Any inside single quotes must be escaped. :multiple => /^'([^'\\]*(\\.[^'\\]*)*)'/, :operators => Sparkql::Token::EQUALITY_OPERATORS }, { :type => :integer, :regex => /^\-?[0-9]+$/, :multiple => /^\-?[0-9]+/, :operators => Sparkql::Token::OPERATORS + [Sparkql::Token::RANGE_OPERATOR] }, { :type => :decimal, :regex => /^\-?[0-9]+\.[0-9]+$/, :multiple => /^\-?[0-9]+\.[0-9]+/, :operators => Sparkql::Token::OPERATORS + [Sparkql::Token::RANGE_OPERATOR] }, { :type => :shape, # This type is not parseable, so no regex :operators => Sparkql::Token::EQUALITY_OPERATORS }, { :type => :boolean, :regex => /^true|false$/, :operators => Sparkql::Token::EQUALITY_OPERATORS }, { :type => :null, :regex => /^NULL|Null|null$/, :operators => Sparkql::Token::EQUALITY_OPERATORS }, { :type => :function, # This type is not parseable, so no regex :operators => Sparkql::Token::OPERATORS + [Sparkql::Token::RANGE_OPERATOR] } ]
- OPERATORS_SUPPORTING_MULTIPLES =
["Eq","Ne"]
Instance Method Summary collapse
- #boolean_escape(string) ⇒ Object
-
#character_escape(string) ⇒ Object
processes escape characters for a given string.
-
#compile(source, mapper) ⇒ Object
To be implemented by child class.
- #date_escape(string) ⇒ Object
- #datetime_escape(string) ⇒ Object
- #decimal_escape(string) ⇒ Object
- #dropped_errors? ⇒ Boolean
-
#errors ⇒ Object
Returns an array of errors.
-
#errors? ⇒ Boolean
delegate :errors?, :fatal_errors?, :dropped_errors?, :recovered_errors?, :to => :process_errors Since I don’t have rails delegate…
- #escape_value(expression) ⇒ Object
- #escape_value_list(expression) ⇒ Object
- #fatal_errors? ⇒ Boolean
- #integer_escape(string) ⇒ Object
- #max_expressions ⇒ Object
-
#max_level_depth ⇒ Object
Maximum supported nesting level for the parser filters.
- #max_values ⇒ Object
-
#process_errors ⇒ Object
Delegator for methods to process the error list.
- #recovered_errors? ⇒ Boolean
-
#rules_for_type(type) ⇒ Object
Returns the rule hash for a given type.
-
#supports_multiple?(type) ⇒ Boolean
true if a given type supports multiple values.
- #time_escape(string) ⇒ Object
-
#tokenize(source) ⇒ Object
Returns a list of expressions tokenized in the following format: [{ :field => IdentifierName, :operator => “Eq”, :value => “‘Fargo’”, :type => :character, :conjunction => “And” }] This step will set errors if source is not syntactically correct.
Instance Method Details
#boolean_escape(string) ⇒ Object
179 180 181 |
# File 'lib/sparkql/parser_compatibility.rb', line 179 def boolean_escape(string) "true" == string end |
#character_escape(string) ⇒ Object
processes escape characters for a given string. May be overridden by child classes.
155 156 157 |
# File 'lib/sparkql/parser_compatibility.rb', line 155 def character_escape( string ) string.gsub(/^\'/,'').gsub(/\'$/,'').gsub(/\\'/, "'") end |
#compile(source, mapper) ⇒ Object
To be implemented by child class. Shall return a valid query string for the respective database, or nil if the source could not be processed. It may be possible to return a valid SQL string AND have errors ( as checked by errors? ), but this will be left to the discretion of the child class.
73 74 75 |
# File 'lib/sparkql/parser_compatibility.rb', line 73 def compile( source, mapper ) raise NotImplementedError end |
#date_escape(string) ⇒ Object
167 168 169 |
# File 'lib/sparkql/parser_compatibility.rb', line 167 def date_escape(string) Date.parse(string) end |
#datetime_escape(string) ⇒ Object
171 172 173 |
# File 'lib/sparkql/parser_compatibility.rb', line 171 def datetime_escape(string) DateTime.parse(string) end |
#decimal_escape(string) ⇒ Object
163 164 165 |
# File 'lib/sparkql/parser_compatibility.rb', line 163 def decimal_escape( string ) string.to_f end |
#dropped_errors? ⇒ Boolean
109 110 111 |
# File 'lib/sparkql/parser_compatibility.rb', line 109 def dropped_errors? process_errors.dropped_errors? end |
#errors ⇒ Object
Returns an array of errors. This is an array of ParserError objects
91 92 93 94 |
# File 'lib/sparkql/parser_compatibility.rb', line 91 def errors @errors = [] unless defined?(@errors) @errors end |
#errors? ⇒ Boolean
delegate :errors?, :fatal_errors?, :dropped_errors?, :recovered_errors?, :to => :process_errors Since I don’t have rails delegate…
103 104 105 |
# File 'lib/sparkql/parser_compatibility.rb', line 103 def errors? process_errors.errors? end |
#escape_value(expression) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/sparkql/parser_compatibility.rb', line 128 def escape_value( expression ) if expression[:value].is_a? Array return escape_value_list( expression ) end case expression[:type] when :character return character_escape(expression[:value]) when :integer return integer_escape(expression[:value]) when :decimal return decimal_escape(expression[:value]) when :date return date_escape(expression[:value]) when :datetime return datetime_escape(expression[:value]) when :time return time_escape(expression[:value]) when :boolean return boolean_escape(expression[:value]) when :null return nil end expression[:value] end |
#escape_value_list(expression) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sparkql/parser_compatibility.rb', line 116 def escape_value_list( expression ) final_list = [] expression[:value].each do | value | new_exp = { :value => value, :type => expression[:type] } final_list << escape_value(new_exp) end expression[:value] = final_list end |
#fatal_errors? ⇒ Boolean
106 107 108 |
# File 'lib/sparkql/parser_compatibility.rb', line 106 def fatal_errors? process_errors.fatal_errors? end |
#integer_escape(string) ⇒ Object
159 160 161 |
# File 'lib/sparkql/parser_compatibility.rb', line 159 def integer_escape( string ) string.to_i end |
#max_expressions ⇒ Object
201 202 203 |
# File 'lib/sparkql/parser_compatibility.rb', line 201 def max_expressions MAXIMUM_EXPRESSIONS end |
#max_level_depth ⇒ Object
Maximum supported nesting level for the parser filters
197 198 199 |
# File 'lib/sparkql/parser_compatibility.rb', line 197 def max_level_depth MAXIMUM_LEVEL_DEPTH end |
#max_values ⇒ Object
205 206 207 |
# File 'lib/sparkql/parser_compatibility.rb', line 205 def max_values MAXIMUM_MULTIPLE_VALUES end |
#process_errors ⇒ Object
Delegator for methods to process the error list.
97 98 99 |
# File 'lib/sparkql/parser_compatibility.rb', line 97 def process_errors Sparkql::ErrorsProcessor.new(@errors) end |
#recovered_errors? ⇒ Boolean
112 113 114 |
# File 'lib/sparkql/parser_compatibility.rb', line 112 def recovered_errors? process_errors.recovered_errors? end |
#rules_for_type(type) ⇒ Object
Returns the rule hash for a given type
184 185 186 187 188 189 |
# File 'lib/sparkql/parser_compatibility.rb', line 184 def rules_for_type( type ) FILTER_VALUES.each do |rule| return rule if rule[:type] == type end nil end |
#supports_multiple?(type) ⇒ Boolean
true if a given type supports multiple values
192 193 194 |
# File 'lib/sparkql/parser_compatibility.rb', line 192 def supports_multiple?( type ) rules_for_type(type).include?( :multiple ) end |
#time_escape(string) ⇒ Object
175 176 177 |
# File 'lib/sparkql/parser_compatibility.rb', line 175 def time_escape(string) DateTime.parse(string) end |
#tokenize(source) ⇒ Object
Returns a list of expressions tokenized in the following format:
- { :field => IdentifierName, :operator => “Eq”, :value => “‘Fargo’”, :type => :character, :conjunction => “And” }
-
This step will set errors if source is not syntactically correct.
80 81 82 83 84 85 86 87 88 |
# File 'lib/sparkql/parser_compatibility.rb', line 80 def tokenize( source ) raise ArgumentError, "You must supply a source string to tokenize!" unless source.is_a?(String) # Reset the parser error stack @errors = [] expressions = self.parse(source) expressions end |