You can add an extra level of data integrity when creating a new table by placing the code to create a table inside a transaction. If the transaction is aborted, the table entry in the database dictionary file is removed, and the table data and index files are deleted from disk.
When a table is created inside a transaction, and until the transaction is committed or aborted, the newly created table must be open with CTOPEN_EXCLUSIVE mode, otherwise the table open operation will fail. After the transaction is committed the table can be open in non exclusive mode.
The code fragment below creates a new table under transaction control. Again no error checking code is included in the example:
// create a new table object
CTTable ATable(ADatabase);
// begin a transaction
ATable.Begin();
try
{
// add a field
ATable.AddField("Field1", CT_INTEGER, 4);
// add another field
ATable.AddField("Field1", CT_CHAR, 30);
// create the table
ATable.Create("MyTable", CTCREATE_NORMAL);
// commit the transaction
ATable.Commit();
}
catch (CTException &err)
{
// abort the transaction
ATable.Abort();
printf("Create table failed with error %d\n", err.GetErrorCode());
}
Note: It is important to note that if you open a table that was created inside the current transaction, i.e. the transaction has not yet been committed or aborted, you must open the table in exclusive mode by specifying the CTOPEN_EXCLUSIVE mode to CTTable::Open().