Skip to content

Commit a9b17c1

Browse files
committed
Better error message
1 parent dca29b2 commit a9b17c1

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

src/MiniSQL.CatalogManager/Controllers/Catalog.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,19 @@ public void CheckValidation(IStatement statement)
142142
//to create a table
143143
if (x.CreateType == CreateType.Table)
144144
{
145+
// primary key not exists
146+
if (x.PrimaryKey == null || x.PrimaryKey == "")
147+
throw new KeyNotExistsException($"Table \"{x.TableName}\" does not have a primary key");
145148
//to check whether the table has been created before
146149
Catalog_table a = new Catalog_table(_databaseName);
147150
a.AssertNotExist(x.TableName);
148151
}
149152
//to create an index
150153
else
151154
{
155+
// index key not exists
156+
if (x.AttributeName == null || x.AttributeName == "")
157+
throw new KeyNotExistsException($"Index \"{x.IndexName}\" does not have a index key");
152158
Catalog_table a = new Catalog_table(_databaseName);
153159
Catalog_index b = new Catalog_index(_databaseName);
154160
//to check whether the table exists
@@ -158,11 +164,11 @@ public void CheckValidation(IStatement statement)
158164
//to check whether the attribute is in the attribute list of the table
159165
bool condition3 = a.return_table(x.TableName).Has_attribute(x.AttributeName);
160166
if (!condition1)
161-
throw new TableOrIndexNotExistsException($"Table {x.TableName} not exists");
167+
throw new TableOrIndexNotExistsException($"Table \"\"{x.TableName}\"\" not exists");
162168
if (!condition2)
163-
throw new TableOrIndexAlreadyExistsException($"Index {x.IndexName} not exists");
169+
throw new TableOrIndexAlreadyExistsException($"Index \"\"{x.IndexName}\"\" not exists");
164170
if (!condition3)
165-
throw new AttributeNotExistsException($"Attribute {x.AttributeName} not exists in table {x.TableName}");
171+
throw new AttributeNotExistsException($"Attribute \"\"{x.AttributeName}\"\" not exists in table \"\"{x.TableName}\"\"");
166172
}
167173
}
168174
//check validation of a drop statement
@@ -184,7 +190,7 @@ public void CheckValidation(IStatement statement)
184190
b.AssertExist(x.IndexName);
185191

186192
if (b.Of_table(x.IndexName) != x.TableName)
187-
throw new AttributeNotExistsException($"Index {x.IndexName} is not associated with table {x.TableName}");
193+
throw new AttributeNotExistsException($"Index \"{x.IndexName}\" is not associated with table \"{x.TableName}\"");
188194
}
189195
else
190196
b.AssertExist(x.IndexName);
@@ -218,7 +224,7 @@ public void CheckValidation(IStatement statement)
218224
{
219225
if (!a.return_table(x.FromTable).Has_attribute(expression_piece.Key))
220226
{
221-
throw new AttributeNotExistsException($"Attribute {expression_piece.Key} not exists in table {x.FromTable}");
227+
throw new AttributeNotExistsException($"Attribute \"{expression_piece.Key}\" not exists in table \"{x.FromTable}\"");
222228
}
223229
}
224230
}
@@ -229,7 +235,7 @@ public void CheckValidation(IStatement statement)
229235
//check whether the only attribute is one of the table's attributes
230236
if (!a.return_table(x.FromTable).Has_attribute(x.Condition.AttributeName))
231237
{
232-
throw new AttributeNotExistsException($"Attribute {x.Condition.AttributeName} not exists in table {x.FromTable}");
238+
throw new AttributeNotExistsException($"Attribute \"{x.Condition.AttributeName}\" not exists in table \"{x.FromTable}\"");
233239
}
234240

235241
}
@@ -263,7 +269,7 @@ public void CheckValidation(IStatement statement)
263269
{
264270
if (!a.return_table(x.TableName).Has_attribute(expression_piece.Key))
265271
{
266-
throw new AttributeNotExistsException($"Attribute {expression_piece.Key} not exists in table {x.TableName}");
272+
throw new AttributeNotExistsException($"Attribute \"{expression_piece.Key}\" not exists in table \"{x.TableName}\"");
267273
}
268274
}
269275
}
@@ -274,7 +280,7 @@ public void CheckValidation(IStatement statement)
274280
//check whether the only attribute is one of the table's attributes
275281
if (!a.return_table(x.TableName).Has_attribute(x.Condition.AttributeName))
276282
{
277-
throw new AttributeNotExistsException($"Attribute {x.Condition.AttributeName} not exists in table {x.TableName}");
283+
throw new AttributeNotExistsException($"Attribute \"{x.Condition.AttributeName}\" not exists in table \"{x.TableName}\"");
278284
}
279285

280286
}
@@ -291,14 +297,14 @@ public void CheckValidation(IStatement statement)
291297
//check if the number of the attributes perfectly match the number of the values
292298
if (x.Values.Count != a.return_table(x.TableName).attribute_list.Count)
293299
{
294-
throw new NumberOfAttributesNotMatchsException($"Number of attributes {x.Values.Count} not matchs {a.return_table(x.TableName).attribute_list.Count}");
300+
throw new NumberOfAttributesNotMatchsException($"Number of attributes not matchs. Expected: \"{a.return_table(x.TableName).attribute_list.Count}\"; actual: \"{x.Values.Count}\"");
295301
}
296302
//check whether the type of the inserted data well suits the data definition of each attribute
297303
for (int i = 0; i < x.Values.Count; i++)
298304
{
299305
if (a.return_table(x.TableName).attribute_list[i].type != x.Values[i].Type)
300306
{
301-
throw new TypeOfAttributeNotMatchsException($"Type {x.Values[i].Type} of attribute {a.return_table(x.TableName).attribute_list[i].attribute_name} not matches {a.return_table(x.TableName).attribute_list[i].type}");
307+
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}\"");
302308
}
303309
}
304310
//if all data type suit, return true

src/MiniSQL.IndexManager/Controllers/BTreeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using MiniSQL.IndexManager.Interfaces;
77
using MiniSQL.Library.Models;
88
using MiniSQL.IndexManager.Utilities;
9+
using MiniSQL.Library.Interfaces;
910

1011
namespace MiniSQL.IndexManager.Controllers
1112
{
@@ -293,10 +294,9 @@ public BTreeNode Delete(DBRecord key, BTreeNode Root)
293294
BTreeNode NodeTobeDeleted = FindNode(key, Root);
294295
if (NodeTobeDeleted == null)
295296
{
296-
throw new KeyNotExistException("Cannot find the key!");
297+
throw new KeyNotExistsException("Cannot find the key!");
297298
}
298299
return Delete_entry(NodeTobeDeleted, key, Root);
299-
300300
}
301301

302302
public BTreeNode Delete(BTreeCell keyCell, BTreeNode Root)

src/MiniSQL.IndexManager/Interfaces/IIndexManager.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55

66
namespace MiniSQL.IndexManager.Interfaces
77
{
8-
public class KeyNotExistException : Exception
9-
{
10-
public KeyNotExistException() { }
11-
public KeyNotExistException(string message) : base(message) { }
12-
}
13-
148
public class RepeatedKeyException : Exception
159
{
1610
public RepeatedKeyException() { }

src/MiniSQL.Library/Interfaces/ICatalogManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public TypeOfAttributeNotMatchsException(string message, Exception inner)
5454
: base(message, inner) { }
5555
}
5656

57+
public class KeyNotExistsException : Exception
58+
{
59+
public KeyNotExistsException()
60+
{ }
61+
public KeyNotExistsException(string message)
62+
: base(message) { }
63+
public KeyNotExistsException(string message, Exception inner)
64+
: base(message, inner) { }
65+
}
66+
5767
public interface ICatalogManager
5868
{
5969
// try to save the create statement into file as a few schema records

src/MiniSQL.Startup/Controllers/View.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public void Interactive()
105105
{
106106
Console.WriteLine($"[Error] {ex.Message}");
107107
}
108-
catch (KeyNotExistException ex)
108+
catch (KeyNotExistsException ex)
109109
{
110110
Console.WriteLine($"[Error] {ex.Message}");
111111
}

0 commit comments

Comments
 (0)