> For the complete documentation index, see [llms.txt](https://hrodebert.gitbook.io/qic-database-ver-1.0.0/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hrodebert.gitbook.io/qic-database-ver-1.0.0/database/data_base/save.md).

# save

## Function: `save`

The `save` function ensures that all in-memory changes made to the database are persisted to disk. It updates the `.table` files for each table with the current state of their records and schema, making the changes permanent.

***

### Declaration

```cpp
void save();
```

### Functionality

1. **Iterates Through Tables** The function loops through all tables registered in the `tables` map.
2. **Retrieves Table Schema** It uses the `get_table_content_header` method to extract the schema (header) of each table, which defines the column names and their corresponding data types.
3. **Appends Records to the Table File** For each table:
   * Existing file content is read and preserved up to the header.
   * New records are appended to the file in a structured format, enclosed by square brackets (`[ ... ]`).
4. **Type-Safe Serialization** Each record's values are serialized into the correct format based on their data type (`INT`, `DOUBLE`, `FLOAT`, `BOOL`, `STRING`). Sensitive data (e.g., strings) is compressed before being written.
5. **Writes Back to the File** After constructing the updated content, the function writes it back to the `.table` file in the database directory.

### Notes

* The database must be open for this function to work. If the database is not open, the function will not execute.
* This function modifies the `.table` files directly in the temporary working directory.
* Compression is applied to `STRING` data for security purposes, reducing the risk of database compromise.
* The `save` function does not affect other database files; it strictly operates on `.table` files.

### Example

Here’s a simple demonstration of how to use the `save` function:

```cpp
#include "qic.h"
#include <iostream>
#include <unordered_map>
#include <any>

int main() {
    data_base db;

    // Open the database
    std::filesystem::path dbPath = "mydatabase.db";
    db.open_database(dbPath);

    // Create a table and add some records
    db.add_table("COMPANY", std::unordered_map<std::string, data_type>{
                                 {"ID", INT},
                                 {"NAME", STRING},
                                 {"AGE", INT},
                                 {"ADDRESS", STRING},
                                 {"SALARY", DOUBLE}});

    db.add_value("COMPANY", std::unordered_map<std::string, std::any>{
                                 {"ID", std::any(1)},
                                 {"NAME", std::any(std::string("Alice"))},
                                 {"AGE", std::any(30)},
                                 {"ADDRESS", std::any(std::string("123 Elm St"))},
                                 {"SALARY", std::any(5000.0)}});

    db.add_value("COMPANY", std::unordered_map<std::string, std::any>{
                                 {"ID", std::any(2)},
                                 {"NAME", std::any(std::string("Bob"))},
                                 {"AGE", std::any(40)},
                                 {"ADDRESS", std::any(std::string("456 Oak Ave"))},
                                 {"SALARY", std::any(7000.0)}});

    // Save all changes to the database
    db.save();

    // Close the database
    db.close();

    std::cout << "Changes saved and database closed successfully!" << std::endl;

    return 0;
}
```
