private void ProcessDeleteStatement(List <string> tokens, ref QueryResult queryResult) { // <Delete Stm> ::= DELETE <From Clause> TABLE <Where Clause> IntSchTable tableToDeleteFrom = null; int tableOffset = 0; for (int i = 1; i < tokens.Count; ++i) { if (tokens[i] == "from") { tableOffset = i + 1; break; } } tableToDeleteFrom = GetTable(tokens[tableOffset]); SQLConditional sqlConditional = ParseWhereClause(tokens); if (!ValidateColumns(ref queryResult, sqlConditional.GetColumns(), tableToDeleteFrom, allowWildCard: false)) { return; } tableToDeleteFrom.Delete(sqlConditional); }
private void ProcessUpdateStatement(List <string> tokens, ref QueryResult queryResult) { string nameOfTableToUpdate = tokens[1]; List <string> columnsToUpdate = new List <string>(); List <string> updatedValues = new List <string>(); ParseUpdates(tokens, ref columnsToUpdate, ref updatedValues); SQLConditional conditional = ParseWhereClause(tokens); IntSchTable tableToUpdate = GetTable(nameOfTableToUpdate); // Validate update columns if (!ValidateColumns(ref queryResult, columnsToUpdate, tableToUpdate, /*allowWildCard=*/ false)) { return; } // Validate conditional columns List <string> conditionalColumns = conditional.GetColumns(); if (!ValidateColumns(ref queryResult, conditionalColumns, tableToUpdate, /*allowWildCard=*/ false)) { return; } tableToUpdate.Update(columnsToUpdate, updatedValues, conditional); }
private void ProcessSelectStatement(List <string> tokens, ref QueryResult queryResult) { // <Select Stm> ::= SELECT <Columns> <From Clause> <Where Clause> <Group Clause> <Having Clause> <Order Clause>\ IntSchTable tableToSelectFrom = null; IntSchTable results = null; List <string> colsToSelect = new List <string>(); List <string> colsToValidate = new List <string>(); int tableOffset = 0; for (int i = 1; i < tokens.Count; ++i) { if (tokens[i] == "from") { tableOffset = i + 1; break; } else if (tokens[i] == ",") { continue; } else if (i + 3 < tokens.Count && (tokens[i].ToLower() == "avg" || tokens[i].ToLower() == "max" || tokens[i].ToLower() == "min" || tokens[i].ToLower() == "sum")) { if (tokens[i + 2] == "*") { string errorMessage = "Invalid arguments for function " + tokens[i] + "()"; string errorType = "SQL"; queryResult.Error = new InputError(errorType, errorMessage); return; } colsToSelect.Add(tokens[i] + tokens[i + 1] + tokens[i + 2] + tokens[i + 3]); colsToValidate.Add(tokens[i + 2]); i += 3; } else if (i + 3 < tokens.Count && tokens[i].ToLower() == "count") { colsToSelect.Add(tokens[i] + tokens[i + 1] + tokens[i + 2] + tokens[i + 3]); colsToValidate.Add(tokens[i + 2]); i += 3; } else { colsToSelect.Add(tokens[i]); colsToValidate.Add(tokens[i]); } } string nameOfTableToSelectFrom = tokens[tableOffset]; // Validate Table's existence for (int i = 0; i < tables.Count; ++i) { if (tables[i].Name == nameOfTableToSelectFrom) { tableToSelectFrom = tables[i]; break; } } if (tableToSelectFrom == null) { string errorMessage = "Could not find table: " + nameOfTableToSelectFrom; string errorType = "SQL"; queryResult.Error = new InputError(errorType, errorMessage); return; } if (!ValidateColumns(ref queryResult, colsToValidate, tableToSelectFrom, /*allowWildcard=*/ true)) { return; } SQLConditional sqlConditional = ParseWhereClause(tokens); // Validate conditional List <string> conditionalColumns = sqlConditional.GetColumns(); if (!ValidateColumns(ref queryResult, conditionalColumns, tableToSelectFrom, /*allowWildCard=*/ false)) { return; } // Execute Selection results = tableToSelectFrom.Select(colsToSelect, sqlConditional); queryResult.Results = results; return; }