table_container
What is a table_container
The table_container
class is designed to manage table-like data structures and facilitate operations like selection, removal, filtering, and querying. It provides a flexible and type-safe interface for handling complex data queries.
Obtaining the table_container from a database
To perform a query you need to obtain the table_container object from your database, you can do it by using the container_execute method, this will return a shared pointer pointing to the table_container
Methods
select(std::string number)
Selects a specific record in the table or all records.
Parameters:
number
: Record index (string). Use"*"
for all records.
Returns: A pointer to the current instance.
remove(std::string number)
Marks specific records for removal based on the index.
Parameters:
number
: Record index (string). Use"*"
to mark all records.
Returns: A pointer to the current instance.
from(std::string table_to_query)
Specifies the table to query and loads its data.
Parameters:
table_to_query
: Name of the table to query.
Returns: A pointer to the current instance.
where(std::string value, std::any is, bool is_not = false)
Filters data rows based on a condition.
Parameters:
value
: Column name to filter on.is
: Value to compare against.is_not
: Whether to invert the condition (default:false
).
Returns: A pointer to the current instance.
get_output()
Retrieves filtered results or deletes records (depending on the operation).
Returns: A
table_vec
containing the filtered records.
Supported Data Types
The class supports the following data types:
INT
DOUBLE
FLOAT
STRING
BOOL
Example
#include "qic.h"
#include <any>
#include <chrono> // For time measurement
#include <iostream>
#include <unordered_map>
int main() {
data_base db;
// Open the database
db.open_database("mydatabase.db");
// Set the number of compiling threads
db.compiling_threads = 1;
// Add a table to the database
auto op = db.add_table(
"COMPANY",
std::unordered_map<std::string, data_type>{
{"ID", INT},
{"NAME", STRING},
{"AGE", INT},
{"ADDRESS", STRING},
{"SALARY", DOUBLE}
}
);
// If adding the table fails, load the existing table
if (op.stat == FAILED) {
db.load_table("COMPANY");
}
// Remove entries from the "COMPANY" table
db.container_execute()->remove("*")->from("COMPANY")->get_output();
// Query the "COMPANY" table
auto result = db.container_execute()->select("*")->from("COMPANY")->get_output();
// Display the query results
for (const auto& query_result : result) {
std::cout << "------------------------" << std::endl;
std::cout << "ID : " << std::any_cast<int>(query_result.at("ID")) << std::endl;
std::cout << "NAME : " << std::any_cast<std::string>(query_result.at("NAME")) << std::endl;
std::cout << "AGE : " << std::any_cast<int>(query_result.at("AGE")) << std::endl;
std::cout << "ADDRESS : " << std::any_cast<std::string>(query_result.at("ADDRESS")) << std::endl;
std::cout << "SALARY : " << std::any_cast<double>(query_result.at("SALARY")) << std::endl;
std::cout << "------------------------" << std::endl;
}
// Save the database
db.save();
// Pause execution until user input
char pause;
std::cin >> pause;
// Close the database
db.close();
return 0;
}
Last updated