📄 sqltoappdata.java
字号:
/** * Parses UNIQUE (NAME,FOO,BAR) statement * * @throws ParseException error parsing the input file */ private void create_Table_Column_Unique(Table tbl) throws ParseException { next(); if (!token.getStr().toUpperCase().equals("(")) { err("( expected"); } next(); while (!token.getStr().equals(")")) { if (!token.getStr().equals(",")) { String colName = token.getStr(); Column c = tbl.getColumn(colName); if (c == null) { err("Invalid column name: " + colName); } c.setUnique(true); } next(); } if (!token.getStr().toUpperCase().equals(")")) { err(") expected got: " + token.getStr()); } next(); // skip the ) } /** * Parses FOREIGN KEY (BAR) REFERENCES TABLE (BAR) statement * * @throws ParseException error parsing the input file */ private void create_Table_Column_Foreign(Table tbl) throws ParseException { next(); if (!token.getStr().toUpperCase().equals("KEY")) { err("KEY expected"); } next(); if (!token.getStr().toUpperCase().equals("(")) { err("( expected"); } next(); ForeignKey fk = new ForeignKey(); List localColumns = new ArrayList(); tbl.addForeignKey(fk); String colName = token.getStr(); localColumns.add(colName); next(); while (token.getStr().equals(",")) { next(); colName = token.getStr(); localColumns.add(colName); next(); } if (!token.getStr().toUpperCase().equals(")")) { err(") expected"); } next(); if (!token.getStr().toUpperCase().equals("REFERENCES")) { err("REFERENCES expected"); } next(); fk.setForeignTableName(token.getStr()); next(); if (token.getStr().toUpperCase().equals("(")) { next(); int i = 0; fk.addReference((String) localColumns.get(i++), token.getStr()); next(); while (token.getStr().equals(",")) { next(); fk.addReference((String) localColumns.get(i++), token.getStr()); next(); } if (!token.getStr().toUpperCase().equals(")")) { err(") expected"); } next(); } } /** * Parse the data definition of the column statement. * * @throws ParseException error parsing the input file */ private void create_Table_Column_Data(Table tbl) throws ParseException { String columnSize = null; String columnPrecision = null; String columnDefault = null; boolean inEnum = false; String columnName = token.getStr(); next(); String columnType = token.getStr(); if (columnName.equals(")") && columnType.equals(";")) { return; } next(); // special case for MySQL ENUM's which are stupid anyway // and not properly handled by Torque. if (columnType.toUpperCase().equals("ENUM")) { inEnum = true; next(); // skip ( while (!token.getStr().equals(")")) { // skip until ) next(); } while (!token.getStr().equals(",")) { if (token.getStr().toUpperCase().equals("DEFAULT")) { next(); if (token.getStr().equals("'")) { next(); } columnDefault = token.getStr(); next(); if (token.getStr().equals("'")) { next(); } } // skip until , next(); } next(); // skip , columnType = "VARCHAR"; } else if (token.getStr().toUpperCase().equals("(")) { next(); columnSize = token.getStr(); next(); if (token.getStr().equals(",")) { next(); columnPrecision = token.getStr(); next(); } if (!token.getStr().equals(")")) { err(") expected"); } next(); } Column col = new Column(columnName); if (columnPrecision != null) { columnSize = columnSize + columnPrecision; } col.setTypeFromString(columnType, columnSize); tbl.addColumn(col); if (inEnum) { col.setNotNull(true); if (columnDefault != null) { col.setDefaultValue(columnDefault); } } else { while (!token.getStr().equals(",") && !token.getStr().equals(")")) { if (token.getStr().toUpperCase().equals("NOT")) { next(); if (!token.getStr().toUpperCase().equals("NULL")) { err("NULL expected after NOT"); } col.setNotNull(true); next(); } else if (token.getStr().toUpperCase().equals("PRIMARY")) { next(); if (!token.getStr().toUpperCase().equals("KEY")) { err("KEY expected after PRIMARY"); } col.setPrimaryKey(true); next(); } else if (token.getStr().toUpperCase().equals("UNIQUE")) { col.setUnique(true); next(); } else if (token.getStr().toUpperCase().equals("NULL")) { col.setNotNull(false); next(); } else if (token.getStr().toUpperCase().equals("AUTO_INCREMENT")) { col.setAutoIncrement(true); next(); } else if (token.getStr().toUpperCase().equals("DEFAULT")) { next(); if (token.getStr().equals("'")) { next(); } col.setDefaultValue(token.getStr()); next(); if (token.getStr().equals("'")) { next(); } } } next(); // eat the , } } /** * Execute the parser. * * @throws IOException If an I/O error occurs * @throws ParseException error parsing the input file */ public AppData execute() throws IOException, ParseException { count = 0; appData = new AppData(databaseType, basePropsFilePath); appDataDB = new Database(); appData.addDatabase(appDataDB); FileReader fr = new FileReader(sqlFile); BufferedReader br = new BufferedReader(fr); SQLScanner scanner = new SQLScanner(br); tokens = scanner.scan(); br.close(); while (hasTokens()) { if (token == null) { next(); } if (token.getStr().toUpperCase().equals("CREATE")) { create(); } if (hasTokens()) { next(); } } return appData; } /** * Just 4 testing. * * @param args commandline args * @throws Exception an exception */ public static void main(String args[]) throws Exception { SQLToAppData s2a = new SQLToAppData(args[0]); AppData ad = s2a.execute(); System.out.println(ad); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -