Class: Ferret::Expression_Parser
- Inherits:
-
Object
- Object
- Ferret::Expression_Parser
- Defined in:
- lib/sql-ferret.rb
Instance Attribute Summary collapse
-
#expr ⇒ Object
readonly
Returns the value of attribute expr.
Instance Method Summary collapse
-
#initialize(raw_expr, schema) ⇒ Expression_Parser
constructor
A new instance of Expression_Parser.
- #parse_fetch_verb ⇒ Object
- #parse_optional_join_arrow ⇒ Object
- #parse_optional_output_name_override ⇒ Object
- #parse_stage(stage, parens: false) ⇒ Object
- #parse_update_verb ⇒ Object
- #start_subsequent_stage(parent, stalk, join_type) ⇒ Object
Constructor Details
#initialize(raw_expr, schema) ⇒ Expression_Parser
Returns a new instance of Expression_Parser.
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
# File 'lib/sql-ferret.rb', line 1061 def initialize raw_expr, schema super() @raw_expr = raw_expr @schema = schema @expr = Ferret::Expression.new @scanner = Ferret::Scanner.new @raw_expr @first_star_offset = nil first_table_name = @scanner.get_id 'table-name' @expr.stages[0].table = @schema[first_table_name] or ugh 'unknown-table', table: first_table_name, offset: @scanner.last_token_offset, expr: @raw_expr @scanner.pass :':' parenthesised = @scanner.pass? :'(' loop do exemplar_escaped, exemplar_column_name = @scanner.get_optional_escaped_id 'column-expected' if exemplar_column_name then exemplar_column = @expr.stages[0].table[exemplar_column_name] or ugh 'unknown-field', field: exemplar_column_name, table: @expr.stages[0].table.name, role: 'key-field', offset: @scanner.last_token_offset, expr: @raw_expr # the key column must be a column, not a ghost field unless exemplar_column.column? then ugh 'not-a-column', field: exemplar_column.name, table: @expr.stages[0].table.name, offset: @scanner.last_token_offset, expr: @raw_expr end exemplar_interpretation = exemplar_escaped ? nil : exemplar_column.interpretation key_output_name = parse_optional_output_name_override || exemplar_column_name @expr.exemplars.push Ferret::Exemplar.new( exemplar_column, exemplar_interpretation) @expr.selectees.push Ferret::Selectee.new( @expr.stages[0], exemplar_column, key_output_name, exemplar_interpretation) end break unless parenthesised and @scanner.pass? :',' end @scanner.pass :')' if parenthesised if @scanner.pass? :':' then # Colon without dereference: we should expect a fetch # verb. @expr.type = parse_fetch_verb else @scanner.pass :'->' if @scanner.pass? :':' then # Colon past dereference: we should expect an update # verb. @expr.type = parse_update_verb else # Note that [[parse_stage]] can change [[@expr.type]] # if it meets the [[-> :]]. parse_stage @expr.stages.last, parens: false end end if @expr.modification? then if @first_star_offset then ugh 'star-in-modification', offset: @first_star_offset, expr: @raw_expr end end @scanner.expected_eof! if @expr.modification? then ugh 'multiple-columns-selected-in-modification' \ if @expr.multicolumn end unless @expr.multicolumn then # In single-column expressions, only the very last # selectee is actually selected. @expr.selectees[0 ... -1] = [] end @expr.assign_stage_qualifiers @schema.alias_generator return end |
Instance Attribute Details
#expr ⇒ Object (readonly)
Returns the value of attribute expr.
1059 1060 1061 |
# File 'lib/sql-ferret.rb', line 1059 def expr @expr end |
Instance Method Details
#parse_fetch_verb ⇒ Object
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 |
# File 'lib/sql-ferret.rb', line 1314 def parse_fetch_verb verb = @scanner.get_id 'fetch-verb' case verb when 'select' then return @scanner.pass?('distinct') ? :select_distinct : :select when 'distinct' then return :select_distinct else ugh 'unknown-fetch-verb', got: verb, input: @expr, offset: @scanner.last_token_offset end end |
#parse_optional_join_arrow ⇒ Object
1308 1309 1310 1311 1312 |
# File 'lib/sql-ferret.rb', line 1308 def parse_optional_join_arrow return :left if @scanner.pass? :'->' return :inner if @scanner.pass? :'<->' return nil end |
#parse_optional_output_name_override ⇒ Object
1298 1299 1300 1301 1302 1303 1304 1305 1306 |
# File 'lib/sql-ferret.rb', line 1298 def parse_optional_output_name_override if @scanner.pass? :"'" then override = @scanner.get_id 'output-name-override' @scanner.pass :"'" return override else return nil end end |
#parse_stage(stage, parens: false) ⇒ Object
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 |
# File 'lib/sql-ferret.rb', line 1178 def parse_stage stage, parens: false starred = false stage_empty = true loop do field_escaped, field_name = @scanner.get_optional_escaped_id 'field-expected' if field_name then field_offset = @scanner.last_token_offset raise 'assertion failed' unless stage.table field = stage.table[field_name] or ugh 'unknown-field', field: field_name, expr: @raw_expr, offset: field_offset field_output_name = parse_optional_output_name_override || field_name # Has this column, or its name, been used already? (0 ... @expr.selectees.length).reverse_each do |i| selectee = @expr.selectees[i] if (selectee.stage == stage and selectee.field == field) or selectee.output_name == field_output_name then # Possible conflict detected. if selectee.star? then # The previous selectee was implicit, added due # to star expansion. We'll just discard it, for # explicit fields take precedence. @expr.selectees.delete_at i else ugh 'duplicate-field-in-stage', field: field.name, output_name: field_output_name, expr: @raw_expr, offset: field_offset end end end @expr.selectees.push Ferret::Selectee.new( stage, field, field_output_name, field_escaped ? nil : field.interpretation) stage_empty = false if @scanner.pass? :'(' then join_type = parse_optional_join_arrow or expected!('join-arrow', candidates: '-> <->') # If something goes wrong trying to start a new # stage, it must be the last field's fault. # ([[start_subsequent_stage]] won't attach the # offset to the ugh on its own just because it # doesn't _have_ the offset.) ugh? offset: field_offset do start_subsequent_stage stage, field, join_type end parse_stage @expr.stages.last, parens: true @scanner.pass :')' else if !parens and @scanner.pass? :':' then @expr.type = parse_fetch_verb break end if join_type = parse_optional_join_arrow then ugh? offset: field_offset do start_subsequent_stage stage, field, join_type end if !parens and @scanner.pass? :':' then @expr.type = parse_update_verb else parse_stage @expr.stages.last, parens: parens end break end end elsif @scanner.pass? :'*' then @first_star_offset ||= @scanner.last_token_offset # only one star per stage @scanner.expected! 'field-name' if starred starred = true @expr.multicolumn = true stage.table.columns.each do |column| # We'll skip columns that have been selected (at # this stage) already, or columns whose names have # already been used. next if @expr.selectees.any? do |selectee| (selectee.stage == stage and selectee.column == column) or selectee.output_name == column.name end @expr.selectees.push Ferret::Selectee.new( stage, column, column.name, column.interpretation, true) end # Note that [[->]] can not appear immediately # following a [[*]]. break else if stage_empty then @scanner.expected! 'field-name' end break end if @scanner.pass? :',' then @expr.multicolumn = true else break end end return end |
#parse_update_verb ⇒ Object
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 |
# File 'lib/sql-ferret.rb', line 1330 def parse_update_verb verb = @scanner.get_id 'update-verb' case verb when 'update', 'set' then return :update when 'delete' then return :delete else ugh 'unknown-update-verb', got: verb, input: @expr, offset: @scanner.last_token_offset end end |
#start_subsequent_stage(parent, stalk, join_type) ⇒ Object
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 |
# File 'lib/sql-ferret.rb', line 1159 def start_subsequent_stage parent, stalk, join_type raise 'type mismatch' \ unless parent.is_a? Ferret::Stage raise 'type mismatch' \ unless stalk.is_a? Ferret::Field raise 'assertion failed' \ unless [:left, :inner].include? join_type # Note that we don't have the field's offset. But the # caller might. unless stalk.ref then ugh 'unable-to-dereference', field: field.name end @expr.stages.push Ferret::Stage.new( parent, stalk, join_type) return end |