MCPcopy Create free account
hub / github.com/1345414527/MIT6.830 / handleInsertStatement

Method handleInsertStatement

src/java/simpledb/Parser.java:319–391  ·  view source on GitHub ↗
(ZInsert s, TransactionId tId)

Source from the content-addressed store, hash-verified

317 }
318
319 public Query handleInsertStatement(ZInsert s, TransactionId tId)
320 throws DbException, IOException,
321 simpledb.ParsingException, Zql.ParseException {
322 int tableId;
323 try {
324 tableId = Database.getCatalog().getTableId(s.getTable()); // will
325 // fall
326 // through if
327 // table
328 // doesn't
329 // exist
330 } catch (NoSuchElementException e) {
331 throw new simpledb.ParsingException("Unknown table : "
332 + s.getTable());
333 }
334
335 TupleDesc td = Database.getCatalog().getTupleDesc(tableId);
336
337 Tuple t = new Tuple(td);
338 int i = 0;
339 OpIterator newTups;
340
341 if (s.getValues() != null) {
342 @SuppressWarnings("unchecked")
343 List<ZExp> values = s.getValues();
344 if (td.numFields() != values.size()) {
345 throw new simpledb.ParsingException(
346 "INSERT statement does not contain same number of fields as table "
347 + s.getTable());
348 }
349 for (ZExp e : values) {
350
351 if (!(e instanceof ZConstant))
352 throw new simpledb.ParsingException(
353 "Complex expressions not allowed in INSERT statements.");
354 ZConstant zc = (ZConstant) e;
355 if (zc.getType() == ZConstant.NUMBER) {
356 if (td.getFieldType(i) != Type.INT_TYPE) {
357 throw new simpledb.ParsingException("Value "
358 + zc.getValue()
359 + " is not an integer, expected a string.");
360 }
361 IntField f = new IntField(new Integer(zc.getValue()));
362 t.setField(i, f);
363 } else if (zc.getType() == ZConstant.STRING) {
364 if (td.getFieldType(i) != Type.STRING_TYPE) {
365 throw new simpledb.ParsingException("Value "
366 + zc.getValue()
367 + " is a string, expected an integer.");
368 }
369 StringField f = new StringField(zc.getValue(),
370 Type.STRING_LEN);
371 t.setField(i, f);
372 } else {
373 throw new simpledb.ParsingException(
374 "Only string or int fields are supported.");
375 }
376

Callers 1

processNextStatementMethod · 0.95

Calls 13

getCatalogMethod · 0.95
numFieldsMethod · 0.95
getFieldTypeMethod · 0.95
setFieldMethod · 0.95
parseQueryLogicalPlanMethod · 0.95
physicalPlanMethod · 0.95
getStatsMapMethod · 0.95
setPhysicalPlanMethod · 0.95
getQueryMethod · 0.80
getTableIdMethod · 0.65
getTupleDescMethod · 0.65
getTypeMethod · 0.65

Tested by

no test coverage detected