segadb.database

# Imports: Standard Library

 
Modules
       
bcrypt
csv
inspect
logging
multiprocessing
os
random
uuid

 
Classes
       
builtins.object
Database

 
class Database(builtins.object)
    Database(name, db_logging=False, table_logging=False)
 

 
  Methods defined here:
__init__(self, name, db_logging=False, table_logging=False)
Initializes a new instance of the database with the given name.
Args:
    name (str): The name of the database.
    db_logging (bool, optional): If True, enables logging for the database. Defaults to False.
    table_logging (bool, optional): If True, enables logging for tables. Defaults to False.
add_constraint = wrapper(self, *args, **kwargs) from log_method_call.
add_role = wrapper(self, *args, **kwargs) from log_method_call.
add_stored_procedure = wrapper(self, *args, **kwargs) from log_method_call.
add_trigger = wrapper(self, *args, **kwargs) from log_method_call.
aggregate_table(self, table_name, group_column, agg_column, agg_func)
Perform an aggregation on a specified column in a table using the provided aggregation function.
Args:
    table_name (str): The name of the table.
    group_column (str): The column to group by.
    agg_column (str): The column to aggregate.
    agg_func (str): The aggregation function to apply. Supported values are 'MIN', 'MAX', 'COUNT', 'SUM', 'AVG', 'COUNT_DISTINCT'.
Returns:
    Table: A new table containing the result of the aggregation.
authenticate_user(self, username, password)
Authenticates a user by their username and password.
Args:
    username (str): The username of the user.
    password (str): The password of the user.
Returns:
    dict or None: The user dictionary if authentication is successful, None otherwise.
check_permission(self, username, permission)
Check if a user has a specific permission.
Args:
    username (str): The username of the user.
    permission (str): The permission to check for.
Returns:
    bool: True if the user has the permission, False otherwise.
check_role(self, username, role)
Check if a user has a specific role.
Args:
    username (str): The username of the user to check.
    role (str): The role to check for.
Returns:
    bool: True if the user has the specified role, False otherwise.
copy(self)
Create a deep copy of the database state.  
This method uses the `copy` module to create a deep copy of the current
database instance, ensuring that all nested objects are also copied.
Returns:
    A new instance of the database with the same state as the original.
create_authorization(self)
Create a new instance of the Authorization class.
Returns:
    Authorization: A new instance of the Authorization class.
create_materialized_view = wrapper(self, *args, **kwargs) from log_method_call.
create_session = wrapper(self, *args, **kwargs) from log_method_call.
create_table = wrapper(self, *args, **kwargs) from log_method_call.
create_table_from_csv = wrapper(self, *args, **kwargs) from log_method_call.
create_user_manager(self)
Create a new instance of the UserManager class.
Returns:
    UserManager: A new instance of the UserManager class.
create_view = wrapper(self, *args, **kwargs) from log_method_call.
delete_materialized_view = wrapper(self, *args, **kwargs) from log_method_call.
delete_session = wrapper(self, *args, **kwargs) from log_method_call.
delete_stored_procedure = wrapper(self, *args, **kwargs) from log_method_call.
delete_trigger = wrapper(self, *args, **kwargs) from log_method_call.
delete_view = wrapper(self, *args, **kwargs) from log_method_call.
drop_table = wrapper(self, *args, **kwargs) from log_method_call.
execute_stored_procedure = wrapper(self, *args, **kwargs) from log_method_call.
execute_triggers = wrapper(self, *args, **kwargs) from log_method_call.
filter_table(self, table_name, condition)
Filter records in a table based on a condition.
Args:
    table_name (str): The name of the table.
    condition (function): A function that takes a record as input and returns True if the record satisfies the condition, False otherwise.
                          Example: lambda record: record.data["product"] == "Laptop"
Returns:
    Table: A new table containing the filtered records.
get_db_size(self)
Calculate the size of the database in bytes.
Returns:
    int: The size of the database in bytes.
get_materialized_view(self, view_name)
Retrieve a materialized view by name.
Args:
    view_name (str): The name of the materialized view.
Returns:
    MaterializedView: The materialized view object.
get_stored_procedure(self, name)
Retrieve a stored procedure by name.
Args:
    name (str): The name of the stored procedure.
Returns:
    function: The stored procedure function.
get_table(self, table_name)
Retrieve a table from the database by its name.
Args:
    table_name (str): The name of the table to retrieve.
Returns:
    Table: The table object.
get_user(self, username)
Retrieve user data from the Users table based on the provided username.
Args:
    username (str): The username of the user to retrieve.
Returns:
    dict: A dictionary containing the user's data if found, otherwise None.
get_username_by_session = wrapper(self, *args, **kwargs) from log_method_call.
get_view(self, view_name)
Retrieve a view by name.
Args:
    view_name (str): The name of the view.
Returns:
    View: The view object.
join_tables(self, table_name1, table_name2, on_column, other_column)
Perform an inner join between two tables on specified columns.
Args:
    table_name1 (str): The name of the first table.
    table_name2 (str): The name of the second table.
    on_column (str): The column in the first table to join on.
    other_column (str): The column in the second table to join on.
Returns:
    Table: A new table containing the joined records.
print_db(self, index=False, limit=None, tables=True, views=False, materialized_views=False, stored_procedures=False, triggers=False)
Print the database tables, including their names, columns, constraints, and records.
Args:
    index (bool, optional): Whether to print the index of each record. Defaults to False.
    limit (int, optional): The maximum number of records to print for each table. Defaults to None.
    tables (bool, optional): Whether to print the tables. Defaults to True.
    views (bool, optional): Whether to print the views. Defaults to False.
    materialized_views (bool, optional): Whether to print the materialized views. Defaults to False.
refresh_materialized_view = wrapper(self, *args, **kwargs) from log_method_call.
register_user = wrapper(self, *args, **kwargs) from log_method_call.
remove_user = wrapper(self, *args, **kwargs) from log_method_call.
restore(self, state)
Restore the database state from a shadow copy.
Args:
    state (object): An object containing the state to restore, including tables and name attributes.
Returns:
    self: The instance of the database with the restored state.
show_db_with_curses(self)
# Utility Methods
# ---------------------------------------------------------------------------------------------
update_table = wrapper(self, *args, **kwargs) from log_method_call.

Static methods defined here:
load_sample_database(name='SampleDB', n_users=10, n_orders=100, n_products=50, n_reviews=200, n_categories=10, n_suppliers=20, db_logging=False, table_logging=False)
Create a sample database with predefined tables and data for testing and demonstration purposes.
Args:
    name (str): The name of the sample database.
Returns:
    Database: An instance of the Database class populated with sample data.

Data descriptors defined here:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
Functions
       
log_method_call(func)
Decorator to log method calls in the Database class.
Logs the method name, arguments, and return value.
 
Args:
    func (function): The function to decorate.
Returns:
    function: The decorated function.

 
Data
        PRESET_ROLES = {'admin': ['create_table', 'delete_table', 'update_table', 'read_table'], 'editor': ['update_table', 'read_table'], 'read_only': ['read_table']}
inf = inf