// drop statement private void HandleStatement(DropStatement statement) { _catalogManager.CheckValidation(statement); SchemaRecord schema; switch (statement.TargetType) { case DropTarget.Table: schema = _catalogManager.GetTableSchemaRecord(statement.TableName); List <SchemaRecord> indices = _catalogManager.GetIndicesSchemaRecord(statement.TableName); // drop table _catalogManager.DropStatement(statement); _recordManager.DropTable(schema.RootPage); // drop index trees foreach (SchemaRecord index in indices) { _recordManager.DropTable(index.RootPage); } break; case DropTarget.Index: schema = _catalogManager.GetIndexSchemaRecord(statement.IndexName); _catalogManager.DropStatement(statement); _recordManager.DropTable(schema.RootPage); break; } }
public void DropStatementForTable(DropStatement dropStatement) { bool if_find = false; if (this.If_in(dropStatement.TableName)) { for (int i = 0; i < this.tables.Count;) { if (this.tables[i].table_name == dropStatement.TableName) { tables.RemoveAt(i); Save_table(tables); if_find = true; } else { i++; } } } //delete all the indices that are related to the deleted table Catalog_index b = new Catalog_index(_databaseName); if (if_find) { b.DeleteIndicesOfTable(dropStatement.TableName); } else { throw new TableOrIndexNotExistsException($"Table {dropStatement.TableName} Not Exists"); } }
//try to drop one table and save the result into the file //if succeed, return true //if can't find table, return false public bool TryDropStatementForTable(DropStatement dropStatement) { bool if_find = false; if (this.If_in(dropStatement.TableName)) { for (int i = 0; i < this.tables.Count;) { if (this.tables[i].table_name == dropStatement.TableName) { tables.RemoveAt(i); Save_table(tables); if_find = true; } else { i++; } } } //delete all the indices that are related to the deleted table Catalog_index b = new Catalog_index(_databaseName); return(if_find && b.DeleteIndicesOfTable(dropStatement.TableName)); }
public override object VisitDropTable([NotNull] MiniSQLParser.DropTableContext context) { DropStatement obj = new DropStatement(); obj.TargetType = DropTarget.Table; obj.TableName = context.tableRef().GetText(); return(obj); }
// try to remove some schema records from file // if succeeded, return true. Vice versa public bool TryDropStatement(DropStatement dropStatement) { try { DropStatement(dropStatement); return(true); } catch (Exception) { return(false); } }
public override object VisitDropIndex([NotNull] MiniSQLParser.DropIndexContext context) { DropStatement obj = new DropStatement(); obj.TargetType = DropTarget.Index; if (context.tableRef() != null) { obj.TableName = context.tableRef().GetText(); } obj.IndexName = context.indexRef().GetText(); return(obj); }
public void DropStatement(DropStatement dropStatement) { if (dropStatement.TargetType == DropTarget.Table) { Catalog_table a = new Catalog_table(_databaseName);//load the table a.DropStatementForTable(dropStatement); } else { Catalog_index b = new Catalog_index(_databaseName);//load the index b.DropStatementForIndex(dropStatement); } }
public void DropStatementForIndex(DropStatement dropStatement) { //if the index hasn't been created before, return false if (!this.If_in(dropStatement.IndexName)) { throw new TableOrIndexNotExistsException($"Index {dropStatement.IndexName} Not Exists"); } for (int i = 0; i < index.Count;) { if (index[i].index_name == dropStatement.IndexName) { index.RemoveAt(i); break; } else { i++; } } Save_index(this.index); }
static bool CheckDrop() { //make a drop statement for a index //delete this index before delete the table, return true DropStatement test_drop_index = new DropStatement(); test_drop_index.TargetType = DropTarget.Index; test_drop_index.IndexName = "index_for_student_id"; bool test4 = icatalog.TryDropStatement(test_drop_index); // Console.WriteLine("Delete Index1, expecting true:"); // Console.WriteLine(test4); Debug.Assert(test4 == true); //make a drop statement for a table //delete this table and its assotiated indices, return true DropStatement test_drop_table = new DropStatement(); test_drop_table.TargetType = DropTarget.Table; test_drop_table.TableName = "Student"; bool test3 = icatalog.TryDropStatement(test_drop_table); // Console.WriteLine("Delete table, expecting true:"); // Console.WriteLine(test3); Debug.Assert(test3 == true); //make a drop statement for a index //delete this DropStatement test_drop_index2 = new DropStatement(); test_drop_index2.TargetType = DropTarget.Index; test_drop_index2.IndexName = "index2"; bool test5 = icatalog.TryDropStatement(test_drop_index2); // Console.WriteLine("Delete index2 after deleting table, expecting false:"); // Console.WriteLine(test5); Debug.Assert(test5 == false); return(true); }
public bool TryDropStatementForIndex(DropStatement dropStatement) { //if the index hasn't been created before, return false if (!this.If_in(dropStatement.IndexName)) { return(false); } for (int i = 0; i < index.Count;) { if (index[i].index_name == dropStatement.IndexName) { index.RemoveAt(i); break; } else { i++; } } Save_index(this.index); return(true); }
// check validation of the statement public void CheckValidation(IStatement statement) { //check validation of createStatement if (statement.Type == StatementType.CreateStatement) { CreateStatement x = (CreateStatement)statement; //to create a table if (x.CreateType == CreateType.Table) { // primary key not exists if (x.PrimaryKey == null || x.PrimaryKey == "") { throw new KeyNotExistsException($"Table \"{x.TableName}\" does not have a primary key"); } //to check whether the table has been created before Catalog_table a = new Catalog_table(_databaseName); a.AssertNotExist(x.TableName); } //to create an index else { // index key not exists if (x.AttributeName == null || x.AttributeName == "") { throw new KeyNotExistsException($"Index \"{x.IndexName}\" does not have a index key"); } Catalog_table a = new Catalog_table(_databaseName); Catalog_index b = new Catalog_index(_databaseName); //to check whether the table exists bool condition1 = a.If_in(x.TableName); //to check whether the index has been created before bool condition2 = !b.If_in(x.IndexName); //to check whether the attribute is in the attribute list of the table bool condition3 = a.return_table(x.TableName).Has_attribute(x.AttributeName); if (!condition1) { throw new TableOrIndexNotExistsException($"Table \"\"{x.TableName}\"\" not exists"); } if (!condition2) { throw new TableOrIndexAlreadyExistsException($"Index \"\"{x.IndexName}\"\" not exists"); } if (!condition3) { throw new AttributeNotExistsException($"Attribute \"\"{x.AttributeName}\"\" not exists in table \"\"{x.TableName}\"\""); } } } //check validation of a drop statement else if (statement.Type == StatementType.DropStatement) { DropStatement x = (DropStatement)statement; Catalog_table a = new Catalog_table(_databaseName); Catalog_index b = new Catalog_index(_databaseName); //to drop a table,we need to check whether the table exists if (x.TargetType == DropTarget.Table) { a.AssertExist(x.TableName); } //to drop a index,we need to check whether the index exists else { if (x.TableName != "") { b.AssertExist(x.IndexName); if (b.Of_table(x.IndexName) != x.TableName) { throw new AttributeNotExistsException($"Index \"{x.IndexName}\" is not associated with table \"{x.TableName}\""); } } else { b.AssertExist(x.IndexName); } } } //check validation of a select statement else if (statement.Type == StatementType.SelectStatement) { SelectStatement x = (SelectStatement)statement; //check whether the table is in the tables catalog Catalog_table a = new Catalog_table(_databaseName); a.AssertExist(x.FromTable); if (x.Condition == null) { return; } else if (x.Condition.AttributeName == "") { if (x.Condition.SimpleMinterms.Count == 0) { //if the ands is empty and attribute name is emply, the statement means select * from a table return; } else { //for each attribute in the egression list(named 'ands') //check whether the attribute is in the attribute list of this table foreach (KeyValuePair <string, Expression> expression_piece in x.Condition.SimpleMinterms) { if (!a.return_table(x.FromTable).Has_attribute(expression_piece.Key)) { throw new AttributeNotExistsException($"Attribute \"{expression_piece.Key}\" not exists in table \"{x.FromTable}\""); } } } } else { //check whether the only attribute is one of the table's attributes if (!a.return_table(x.FromTable).Has_attribute(x.Condition.AttributeName)) { throw new AttributeNotExistsException($"Attribute \"{x.Condition.AttributeName}\" not exists in table \"{x.FromTable}\""); } } return; } //check validation of a delete statement else if (statement.Type == StatementType.DeleteStatement) { DeleteStatement x = (DeleteStatement)statement; //check whether the table is in the tables catalog Catalog_table a = new Catalog_table(_databaseName); a.AssertExist(x.TableName); if (x.Condition == null) { return; } else if (x.Condition.AttributeName == "") { if (x.Condition.SimpleMinterms.Count == 0) { //if the ands is empty and attribute name is emply, the statement means select * from a table return; } else { //for each attribute in the epression list(named 'ands') //check whether the attribute is in the attribute list of this table foreach (KeyValuePair <string, Expression> expression_piece in x.Condition.SimpleMinterms) { if (!a.return_table(x.TableName).Has_attribute(expression_piece.Key)) { throw new AttributeNotExistsException($"Attribute \"{expression_piece.Key}\" not exists in table \"{x.TableName}\""); } } } } else { //check whether the only attribute is one of the table's attributes if (!a.return_table(x.TableName).Has_attribute(x.Condition.AttributeName)) { throw new AttributeNotExistsException($"Attribute \"{x.Condition.AttributeName}\" not exists in table \"{x.TableName}\""); } } return; } //check validation of an insert statement else if (statement.Type == StatementType.InsertStatement) { InsertStatement x = (InsertStatement)statement; //check whether the table is in the table list Catalog_table a = new Catalog_table(_databaseName); a.AssertExist(x.TableName); //check if the number of the attributes perfectly match the number of the values if (x.Values.Count != a.return_table(x.TableName).attribute_list.Count) { throw new NumberOfAttributesNotMatchsException($"Number of attributes not matchs. Expected: \"{a.return_table(x.TableName).attribute_list.Count}\"; actual: \"{x.Values.Count}\""); } //check whether the type of the inserted data well suits the data definition of each attribute for (int i = 0; i < x.Values.Count; i++) { if (a.return_table(x.TableName).attribute_list[i].type != x.Values[i].Type) { throw new TypeOfAttributeNotMatchsException($"Type for attribute \"{a.return_table(x.TableName).attribute_list[i].attribute_name}\" not matches. Expected: \"{a.return_table(x.TableName).attribute_list[i].type}\"; actual: \"{x.Values[i].Type}\""); } } //if all data type suit, return true return; } //check validation of a quit statement else if (statement.Type == StatementType.QuitStatement) { return; } //check validation of an exec file statement else { return; } }
//check if the program can successfully check the validation of each kind of input statement static bool CheckValidation() { //since quitstatement and execfile statement are too simple,we skip them here Catalog func = new Catalog("test"); //select statement //3 kinds, each with a true and a false version //select only from table without any attribute SelectStatement select_only_table_true = new SelectStatement(); // select_only_table_true.Condition = new Expression(); select_only_table_true.FromTable = "Student"; SelectStatement select_only_table_false = new SelectStatement(); // select_only_table_false.Condition = new Expression(); select_only_table_false.FromTable = "School"; bool test1 = func.IsValid(select_only_table_false); bool test2 = func.IsValid(select_only_table_true); // Console.WriteLine(" Wrong select statement that select only 1 table, expecting false:"); // Console.WriteLine(test1); Debug.Assert(test1 == false); // Console.WriteLine(" Right select statement that select only 1 table, expecting true:"); // Console.WriteLine(test2); Debug.Assert(test2 == true); //select from table with 1 attribute SelectStatement select_with_1_attribute_true = new SelectStatement(); select_with_1_attribute_true.FromTable = "Student"; select_with_1_attribute_true.Condition = new Expression(); select_with_1_attribute_true.Condition.Operator = Operator.AtomVariable; select_with_1_attribute_true.Condition.AttributeName = "ID"; SelectStatement select_with_1_attribute_false = new SelectStatement(); select_with_1_attribute_false.FromTable = "Student"; select_with_1_attribute_false.Condition = new Expression(); select_with_1_attribute_true.Condition.Operator = Operator.AtomVariable; select_with_1_attribute_false.Condition.AttributeName = "Gender"; bool test3 = icatalog.IsValid(select_with_1_attribute_true); bool test4 = icatalog.IsValid(select_with_1_attribute_false); // Console.WriteLine("Right select statement that select with 1 attribute, expecting true:"); // Console.WriteLine(test3); Debug.Assert(test3 == true); // Console.WriteLine("Wrong select statement that select with 1 attribute, expecting false:"); // Console.WriteLine(test4); Debug.Assert(test4 == false); // select from table with condition having 3 attribute SelectStatement select_with_3_attribute_true = new SelectStatement(); select_with_3_attribute_true.FromTable = "Student"; select_with_3_attribute_true.Condition = GetExpression("ID", "Class", "Score"); SelectStatement select_with_3_attribute_false = new SelectStatement(); select_with_3_attribute_false.FromTable = "Student"; select_with_3_attribute_false.Condition = GetExpression("Gender", "Class", "Score"); bool test5 = icatalog.IsValid(select_with_3_attribute_true); bool test6 = icatalog.IsValid(select_with_3_attribute_false); // Console.WriteLine("Right select statement that select with 3 attributes, expecting true:"); // Console.WriteLine(test5); Debug.Assert(test5 == true); // Console.WriteLine("Wrong select statement that select with 3 attribute, expecting false:"); // Console.WriteLine(test6); Debug.Assert(test6 == false); //delete statement //3 kinds, each with a true and a false version //almost the same as the select statement //delete table without any attribute DeleteStatement delete_only_table_true = new DeleteStatement(); delete_only_table_true.TableName = "Student"; DeleteStatement delete_only_table_false = new DeleteStatement(); delete_only_table_false.TableName = "School"; bool tst1 = func.IsValid(delete_only_table_false); bool tst2 = func.IsValid(delete_only_table_true); // Console.WriteLine("Wrong delete statement that delete only table, expecting false:"); // Console.WriteLine(tst1); Debug.Assert(tst1 == false); // Console.WriteLine("Right delete statement that delete only table, expecting true:"); // Console.WriteLine(tst2); Debug.Assert(tst2 == true); //delete from table with 1 attribute DeleteStatement delete_with_1_attribute_true = new DeleteStatement(); delete_with_1_attribute_true.TableName = "Student"; delete_with_1_attribute_true.Condition = new Expression() { Operator = Operator.AtomVariable }; delete_with_1_attribute_true.Condition.AttributeName = "ID"; DeleteStatement delete_with_1_attribute_false = new DeleteStatement(); delete_with_1_attribute_false.Condition = new Expression() { Operator = Operator.AtomVariable }; delete_with_1_attribute_false.TableName = "Student"; delete_with_1_attribute_false.Condition.AttributeName = "Gender"; bool tst3 = icatalog.IsValid(delete_with_1_attribute_true); bool tst4 = icatalog.IsValid(delete_with_1_attribute_false); // Console.WriteLine("Right delete statement that delete with 1 attribute, expecting true:"); // Console.WriteLine(tst3); Debug.Assert(tst3 == true); // Console.WriteLine("Wrong delete statement that delete with 1 attribute, expecting false:"); // Console.WriteLine(tst4); Debug.Assert(tst4 == false); //delete from table with 3 attribute DeleteStatement delete_with_3_attribute_true = new DeleteStatement(); delete_with_3_attribute_true.Condition = GetExpression("ID", "Class", "Score"); delete_with_3_attribute_true.TableName = "Student"; DeleteStatement delete_with_3_attribute_false = new DeleteStatement(); delete_with_3_attribute_false.TableName = "Student"; delete_with_3_attribute_false.Condition = GetExpression("ID", "Gender", "Score"); bool tst5 = icatalog.IsValid(delete_with_3_attribute_true); bool tst6 = icatalog.IsValid(delete_with_3_attribute_false); // Console.WriteLine("Right delete statement that delete with 3 attribute, expecting true:"); // Console.WriteLine(tst5); Debug.Assert(tst5 == true); // Console.WriteLine("Wrong delete statement that delete with 3 attribute, expecting false:"); // Console.WriteLine(tst6); Debug.Assert(tst6 == false); //insert statement //3 kinds, only the first is true, others are false //all attributes suit InsertStatement insert_all_suit = new InsertStatement(); insert_all_suit.TableName = "Student"; //make all the values into a list List <AtomValue> insert_tmp1 = new List <AtomValue>(); AtomValue attribute_value1 = new AtomValue(); attribute_value1.Type = AttributeTypes.Char; attribute_value1.StringValue = "111"; AtomValue attribute_value2 = new AtomValue(); attribute_value2.Type = AttributeTypes.Char; attribute_value2.StringValue = "Lily"; AtomValue attribute_value3 = new AtomValue(); attribute_value3.Type = AttributeTypes.Float; attribute_value3.FloatValue = 80.5; AtomValue attribute_value4 = new AtomValue(); attribute_value4.Type = AttributeTypes.Int; attribute_value4.IntegerValue = 5; insert_tmp1.Add(attribute_value1); insert_tmp1.Add(attribute_value2); insert_tmp1.Add(attribute_value3); insert_tmp1.Add(attribute_value4); insert_all_suit.Values = insert_tmp1; bool insert_test1 = icatalog.IsValid(insert_all_suit); // Console.WriteLine("Right insert statement, expecting true:"); // Console.WriteLine(insert_test1); Debug.Assert(insert_test1 == true); //doesn't match the number of attributes InsertStatement insert_wrong_number = new InsertStatement(); insert_wrong_number.TableName = "Student"; //get the value list of only 3 attributes when 4 is the right number insert_tmp1.RemoveAt(3); insert_wrong_number.Values = insert_tmp1; bool insert_test2 = icatalog.IsValid(insert_wrong_number); // Console.WriteLine("Wrong insert statement for wrong number of attributes, expecting false:"); // Console.WriteLine(insert_test2); Debug.Assert(insert_test2 == false); //doesn't match the type of attributes InsertStatement insert_wrong_type = new InsertStatement(); insert_wrong_type.TableName = "Student"; //give attribute 4 type string instead of int AtomValue attribute_value5 = new AtomValue(); attribute_value5.Type = AttributeTypes.Char; attribute_value5.StringValue = "hhhh"; insert_tmp1.Add(attribute_value5); insert_wrong_type.Values = insert_tmp1; bool insert_test3 = icatalog.IsValid(insert_wrong_type); // Console.WriteLine("Wrong insert statement for wrong type of attributes, expecting false:"); // Console.WriteLine(insert_test3); Debug.Assert(insert_test3 == false); //drop statement //2 kind, try to drop a table or an index, each with a true and a false version //drop an existing table, true DropStatement drop_table_true = new DropStatement(); drop_table_true.TargetType = DropTarget.Table; drop_table_true.TableName = "Student"; bool drop_test1 = icatalog.IsValid(drop_table_true); // Console.WriteLine("Right drop table statement, expecting true:"); // Console.WriteLine(drop_test1); Debug.Assert(drop_test1 == true); //drop a table that doesn't exist, false DropStatement drop_table_false = new DropStatement(); drop_table_false.TargetType = DropTarget.Table; drop_table_false.TableName = "School"; bool drop_test2 = icatalog.IsValid(drop_table_false); // Console.WriteLine("Wrong drop table statement, expecting false:"); // Console.WriteLine(drop_test2); Debug.Assert(drop_test2 == false); //drop an existing index from the table that doesn't exist, false DropStatement drop_index_false1 = new DropStatement(); drop_index_false1.TargetType = DropTarget.Index; drop_index_false1.IndexName = "index_for_student_id"; drop_index_false1.TableName = "School"; bool drop_test3 = icatalog.IsValid(drop_index_false1); // Console.WriteLine("Wrong drop index statement that drop from the table that doesn't exist, expecting false:"); // Console.WriteLine(drop_test3); Debug.Assert(drop_test3 == false); //drop an index that doesn't exist, false DropStatement drop_index_false2 = new DropStatement(); drop_index_false2.TargetType = DropTarget.Index; drop_index_false2.IndexName = "index_wrong"; drop_index_false2.TableName = "Student"; bool drop_test4 = icatalog.IsValid(drop_index_false2); // Console.WriteLine("Wrong drop index statement that drop an unknown index, expecting false:"); // Console.WriteLine(drop_test4); Debug.Assert(drop_test4 == false); //drop an index that exits, true DropStatement drop_index_true = new DropStatement(); drop_index_true.TargetType = DropTarget.Index; drop_index_true.IndexName = "index_for_student_id"; drop_index_true.TableName = "Student"; bool drop_test5 = icatalog.IsValid(drop_index_true); // Console.WriteLine("Right drop index statement, expecting true:"); // Console.WriteLine(drop_test5); Debug.Assert(drop_test5 == true); //create statement //2 kinds, for index and for table, each with both true and wrong version //create a table CreateStatement create_table_true = new CreateStatement(); create_table_true.CreateType = CreateType.Table; create_table_true.TableName = "Home"; bool create_test1 = icatalog.IsValid(create_table_true); // Console.WriteLine("Right create table statement, expecting true:"); // Console.WriteLine(create_test1); Debug.Assert(create_test1 == true); //create an index, true CreateStatement create_index_true = new CreateStatement(); create_index_true.CreateType = CreateType.Index; create_index_true.TableName = "Student"; create_index_true.IndexName = "second_index_for_student_id"; create_index_true.AttributeName = "ID"; bool create_test2 = icatalog.IsValid(create_index_true); // Console.WriteLine("Right create index statement, expecting true:"); // Console.WriteLine(create_test2); Debug.Assert(create_test2 == true); //create an index that already exists, false CreateStatement create_index_false1 = new CreateStatement(); create_index_false1.CreateType = CreateType.Index; create_index_false1.TableName = "Student"; create_index_false1.IndexName = "index_for_student_id"; create_index_false1.AttributeName = "ID"; bool create_test3 = icatalog.IsValid(create_index_false1); // Console.WriteLine("Wrong create index statement, expecting false:"); // Console.WriteLine(create_test3); Debug.Assert(create_test3 == false); //create an index for an attribute that doesn't eixst CreateStatement create_index_false2 = new CreateStatement(); create_index_false2.CreateType = CreateType.Index; create_index_false2.TableName = "Student"; create_index_false2.IndexName = "index_for_student_home"; create_index_false2.AttributeName = "Home"; bool create_test4 = icatalog.IsValid(create_index_false2); // Console.WriteLine("Wrong create index statement that is created on an attribute that does not exist, expecting false:"); // Console.WriteLine(create_test4); Debug.Assert(create_test4 == false); //create an index for a table that doesn't eixst CreateStatement create_index_false3 = new CreateStatement(); create_index_false3.CreateType = CreateType.Index; create_index_false3.TableName = "Home"; create_index_false3.IndexName = "index_for_student_house"; create_index_false3.AttributeName = "Street"; bool create_test5 = icatalog.IsValid(create_index_false3); // Console.WriteLine("Wrong create index statement that is created on a table that doesn't exist, expecting false:"); // Console.WriteLine(create_test5); Debug.Assert(create_test5 == false); return(true); }