remove_table

Function: remove_table

The remove_table function is used to delete an existing table from the currently opened database. It removes the table's file from the filesystem and updates the database's metadata to reflect the change.


Declaration

operation remove_table(std::string name);

Parameters

  • name (std::string) The name of the table to be removed. The function will look for a file named <name>.table in the database directory.

Return Value

  • operation An object containing:

    • stat: Indicates whether the operation was SUCCESS or FAILED.

    • error: Provides a description of the error if the operation fails.

Functionality

  1. Checks if the Database is Open The function verifies if the database is currently open. If the database is not open, the operation fails with an error message stating "Database not open."

  2. Validates Table Existence The function checks if the table file exists in the database directory. If the file is not found, the operation fails with an error message stating "Table does not exist."

  3. Removes the Table File If the table file exists, it is deleted from the filesystem using std::filesystem::remove.

  4. Updates Database Metadata After successfully removing the file, the table is removed from the tables map, ensuring the database's in-memory metadata is consistent.

  5. Returns the Result If the table is successfully removed, the operation status is set to SUCCESS. Otherwise, an error message is returned describing the failure.

Example

Here’s a simple example of how to use the remove_table function:

#include "qic.h"
#include <iostream>

int main() {
    data_base db;

    // Open the database
    db.open_database("mydatabase.db");

    // Remove the table named "users"
    operation op = db.remove_table("users");

    // Check if the operation was successful
    if (op.stat == SUCCESS) {
        std::cout << "Table 'users' removed successfully!" << std::endl;
    } else {
        std::cout << "Failed to remove table: " << op.error << std::endl;
    }

    // Close the database
    db.close();

    return 0;
}

Last updated