private bool ProcessCreateTableStatement(List <string> tokens, ref QueryResult queryResult) { // assuming only the following rule is accepted // <Create Stm> ::= CREATE TABLE Id '(' <ID List> ')' ------ NO SUPPORT for <Constraint Opt> string newTableName = tokens[2]; foreach (IntSchTable tbl in tables) { if (tbl.Name == newTableName) { string errorType = "SQL"; string errorMsg = "table " + tbl.Name + " already exists"; queryResult.Error = new InputError(errorType, errorMsg); return(false); } } List <string> columnNames = new List <string>(); List <string> columnTypes = new List <string>(); int idCount = 2; for (int i = 4; i < tokens.Count; ++i) { if (tokens[i] == ")") { break; } else if (tokens[i] == ",") { continue; } else { if (idCount == 2) { columnNames.Add(tokens[i]); --idCount; } else if (idCount == 1) { columnTypes.Add(tokens[i]); idCount = 2; } } } IntSchTable newTable = new IntSchTable(newTableName); for (int i = 0; i < columnNames.Count; ++i) { newTable.AddColumn(columnNames[i], columnTypes[i]); } tables.Add(newTable); return(true); }
private bool ProcessCreateTableStatement(List <string> tokens) { // assuming only the following rule is accepted // <Create Stm> ::= CREATE TABLE Id '(' <ID List> ')' ------ NO SUPPORT for <Constraint Opt> string newTableName = tokens[2]; foreach (IntSchTable tbl in tables) { if (tbl.Name == newTableName) { //cannot create a new table with the same name return(false); } } List <string> columnNames = new List <string>(); List <string> columnTypes = new List <string>(); int idCount = 2; for (int i = 4; i < tokens.Count; ++i) { if (tokens[i] == ")") { break; } else if (tokens[i] == ",") { continue; } else { if (idCount == 2) { columnNames.Add(tokens[i]); --idCount; } else if (idCount == 1) { columnTypes.Add(tokens[i]); idCount = 2; } } } IntSchTable newTable = new IntSchTable(newTableName); for (int i = 0; i < columnNames.Count; ++i) { newTable.AddColumn(columnNames[i], columnTypes[i]); } tables.Add(newTable); return(true); }
// create table test(col1 whatever, col2 whatever, col3 whatever) private string[,] ProcessCreateTableStatement(List <string> tokens) { // assuming only the following rule is accepted // <Create Stm> ::= CREATE TABLE Id '(' <ID List> ')' ------ NO SUPPORT for <Constraint Opt> string newTableName = tokens[2]; if (tables.ContainsKey(newTableName)) { //cannot create a new table with the same name return(new string[, ] { { "Table " + newTableName + " already exists" } }); } List <string> columnNames = new List <string>(); List <TypeEnum> columnTypes = new List <TypeEnum>(); int idCount = 2; for (int i = 4; i < tokens.Count; ++i) { if (tokens[i] == ")") { break; } else if (tokens[i] == ",") { continue; } else { if (idCount == 2) { columnNames.Add(tokens[i]); --idCount; } else if (idCount == 1) { TypeEnum type; switch (tokens[i]) { case "varchar": type = TypeEnum.String; break; case "int": type = TypeEnum.Integer; break; case "float": type = TypeEnum.Float; break; default: return(new string[, ] { { "Type is not supported: " + tokens[i] } }); } columnTypes.Add(type); idCount = 2; } } } IntSchTable newTable = new IntSchTable(newTableName); for (int i = 0; i < columnNames.Count; ++i) { newTable.AddColumn(columnNames[i], columnTypes[i]); } tables.Add(newTableName, newTable); return(new string[, ] { { "Succesfully created table " + newTableName } }); }