(ZInsert s, TransactionId tId)
| 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 |
no test coverage detected