SelectParser { public static Select parse(String expr) { List<Select.Item> items = new ArrayList<>(); Select.Item item; String[] names = Strings.split(expr, ','); for (String name : names) { String[] parts = Strings.splitWhitespaces(name); if (parts.length <= 2) { String[] join = Strings.split(parts[0], "."); if (join.length == 1) { if (parts.length == 1) { item = new Select.Item(parts[0]); } else { item = new Select.Item(parts[0], parts[1]); } } else if (join.length == 2) { if (parts.length == 1) { item = new Select.Item(join[1], null, join[0]); } else { item = new Select.Item(join[1], parts[1], join[0]); } } else { throw new BadRequestException("Invalid join item '" + parts[0] + "'"); } } else { throw new BadRequestException("Invalid select item '" + name + "'"); } items.add(item); } return new Select(items.toArray(new Select.Item[items.size()])); }  static Select parse(String expr); static final String SELETE; }
@Test public void testSingle() { Select select = SelectParser.parse("name"); assertSingleSelect(select, "name", null, null); select = SelectParser.parse("name n"); assertSingleSelect(select, "name", "n", null); select = SelectParser.parse("t.name"); assertSingleSelect(select, "name", null, "t"); select = SelectParser.parse("t.name n"); assertSingleSelect(select, "name", "n", "t"); }
@Test public void testMulti() { Select select = SelectParser.parse("id,name"); assertEquals(2, select.items().length); assertSelect(select.items()[0], "id", null, null); assertSelect(select.items()[1], "name", null, null); select = SelectParser.parse("id i,name n"); assertSelect(select.items()[0], "id", "i", null); assertSelect(select.items()[1], "name", "n", null); select = SelectParser.parse("t.id i,t.name n"); assertSelect(select.items()[0], "id", "i", "t"); assertSelect(select.items()[1], "name", "n", "t"); }