|
| 1 | +import duckdb |
| 2 | +import pandas as pd |
| 3 | +from typing import List, Optional, Union, Tuple |
| 4 | +from . import exceptions |
| 5 | + |
| 6 | + |
| 7 | +class Memory: |
| 8 | + def __init__(self, dfs: Optional[List[Tuple[pd.DataFrame, str]]] = None, database_path: Optional[str] = None) -> None: |
| 9 | + """ |
| 10 | + Initialize Memory class with optional database file. |
| 11 | + |
| 12 | + Args: |
| 13 | + dfs: List of (dataframe, table_name) tuples to cache |
| 14 | + database_path: Path to database file. If None, uses in-memory database. |
| 15 | + """ |
| 16 | + self.database_path = database_path |
| 17 | + self._is_memory_mode = database_path is None |
| 18 | + |
| 19 | + # Create read-write connection for data loading |
| 20 | + if self._is_memory_mode: |
| 21 | + self.conn = duckdb.connect(database=':memory:', read_only=False) |
| 22 | + else: |
| 23 | + self.conn = duckdb.connect(database=database_path, read_only=False) |
| 24 | + |
| 25 | + if dfs: |
| 26 | + self.cache_dataframes(dfs) |
| 27 | + |
| 28 | + def cache_dataframes(self, dfs: List[Tuple[pd.DataFrame, str]]) -> None: |
| 29 | + """Cache multiple dataframes to the database. |
| 30 | +
|
| 31 | + This method takes a list of (dataframe, table_name) tuples and adds each |
| 32 | + dataframe to the database with the specified table name. |
| 33 | +
|
| 34 | + Args: |
| 35 | + dfs: List of tuples containing (dataframe, table_name) pairs to cache. |
| 36 | +
|
| 37 | + Example: |
| 38 | + >>> memory = Memory() |
| 39 | + >>> df1 = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) |
| 40 | + >>> df2 = pd.DataFrame({"x": [5, 6], "y": [7, 8]}) |
| 41 | + >>> memory.cache_dataframes([(df1, "table1"), (df2, "table2")]) |
| 42 | + """ |
| 43 | + for df, table_name in dfs: |
| 44 | + self.add(table_name, df) |
| 45 | + |
| 46 | + def add(self, table_name: str, df: pd.DataFrame) -> None: |
| 47 | + """Add or replace a table in the database. |
| 48 | +
|
| 49 | + This method creates a new table or replaces an existing one with the |
| 50 | + provided dataframe. If a table with the same name already exists, it |
| 51 | + will be dropped and recreated with the new data. |
| 52 | +
|
| 53 | + Args: |
| 54 | + table_name: Name of the table to create or replace. |
| 55 | + df: Pandas DataFrame containing the data to store. |
| 56 | +
|
| 57 | + Example: |
| 58 | + >>> memory = Memory() |
| 59 | + >>> df = pd.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]}) |
| 60 | + >>> memory.add("users", df) |
| 61 | + """ |
| 62 | + self.conn.execute(f"DROP TABLE IF EXISTS {table_name}") |
| 63 | + self.conn.execute(f"CREATE TABLE {table_name} AS SELECT * FROM df") |
| 64 | + |
| 65 | + def delete(self, table_name: str) -> None: |
| 66 | + """remove a table in the database. |
| 67 | +
|
| 68 | + This method removes a table from the database if the table exists. |
| 69 | +
|
| 70 | + Args: |
| 71 | + table_name: Name of the table to remove. |
| 72 | +
|
| 73 | + Example: |
| 74 | + >>> memory = Memory() |
| 75 | + >>> df = pd.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]}) |
| 76 | + >>> memory.add("users", df) |
| 77 | + >>> memory.remove("users") |
| 78 | + """ |
| 79 | + self.conn.execute(f"DROP TABLE IF EXISTS {table_name}") |
| 80 | + |
| 81 | + def append(self, table_name: str, df: pd.DataFrame) -> None: |
| 82 | + """Append data to an existing table in the database. |
| 83 | +
|
| 84 | + This method adds new data to an existing table. The incoming dataframe |
| 85 | + will be automatically reordered to match the existing table's column |
| 86 | + order to ensure compatibility. The table must already exist. |
| 87 | +
|
| 88 | + Args: |
| 89 | + table_name: Name of the existing table to append data to. |
| 90 | + df: Pandas DataFrame containing the data to append. |
| 91 | +
|
| 92 | + Raises: |
| 93 | + exceptions.TableNotFoundError: If the specified table does not exist. |
| 94 | + exceptions.InsertOperationError: If the insert operation fails. |
| 95 | +
|
| 96 | + Example: |
| 97 | + >>> memory = Memory() |
| 98 | + >>> # First create a table |
| 99 | + >>> initial_df = pd.DataFrame({"id": [1, 2], "name": ["Alice", "Bob"]}) |
| 100 | + >>> memory.add("users", initial_df) |
| 101 | + >>> # Then append more data |
| 102 | + >>> new_df = pd.DataFrame({"id": [3, 4], "name": ["Charlie", "Diana"]}) |
| 103 | + >>> memory.append("users", new_df) |
| 104 | + """ |
| 105 | + # Check if table exists |
| 106 | + try: |
| 107 | + table_info = self.conn.execute(f"DESCRIBE {table_name}").fetchdf() |
| 108 | + except Exception as e: |
| 109 | + raise exceptions.TableNotFoundError(f"Table '{table_name}' does not exist on Memory. Error: {str(e)}") |
| 110 | + |
| 111 | + existing_columns = table_info['column_name'].tolist() |
| 112 | + |
| 113 | + # Reorder dataframe columns to match existing table |
| 114 | + df_reordered = df[existing_columns] |
| 115 | + |
| 116 | + # Attempt to insert data |
| 117 | + try: |
| 118 | + self.conn.execute(f"INSERT INTO {table_name} SELECT * FROM df_reordered") |
| 119 | + except Exception as e: |
| 120 | + raise exceptions.InsertOperationError(f"Failed to insert data into table '{table_name}' on Memory. Error: {str(e)}") |
| 121 | + |
| 122 | + def query(self, query: str) -> pd.DataFrame: |
| 123 | + """Execute a SELECT query on the database. |
| 124 | +
|
| 125 | + This method executes SQL SELECT queries on the database. Only SELECT |
| 126 | + statements and Common Table Expressions (CTEs) are allowed for security. |
| 127 | + All queries are validated to prevent SQL injection attacks. |
| 128 | +
|
| 129 | + Args: |
| 130 | + query: SQL SELECT query string to execute. |
| 131 | +
|
| 132 | + Returns: |
| 133 | + pd.DataFrame: Query results as a pandas DataFrame. |
| 134 | +
|
| 135 | + Raises: |
| 136 | + ValueError: If the query is not a valid SELECT statement or contains |
| 137 | + multiple statements with non-SELECT operations. |
| 138 | +
|
| 139 | + Example: |
| 140 | + >>> memory = Memory() |
| 141 | + >>> memory.add("users", df) |
| 142 | + >>> result = memory.query("SELECT * FROM users WHERE id > 1") |
| 143 | + >>> result = memory.query("WITH cte AS (SELECT * FROM users) SELECT * FROM cte") |
| 144 | + """ |
| 145 | + if not self._is_select_query(query): |
| 146 | + raise ValueError("Only SELECT queries are allowed. Other operations are not permitted.") |
| 147 | + return self.conn.execute(query).fetchdf() |
| 148 | + |
| 149 | + def _is_select_query(self, query: str) -> bool: |
| 150 | + """Check if the query contains only SELECT statements, preventing SQL injection. |
| 151 | +
|
| 152 | + This private method validates that the provided query contains only SELECT |
| 153 | + statements or Common Table Expressions (CTEs). It prevents SQL injection |
| 154 | + by ensuring no other SQL operations (INSERT, UPDATE, DELETE, DROP, etc.) |
| 155 | + are present in the query. |
| 156 | +
|
| 157 | + Args: |
| 158 | + query: SQL query string to validate. |
| 159 | +
|
| 160 | + Returns: |
| 161 | + bool: True if the query contains only SELECT/WITH statements, |
| 162 | + False otherwise. |
| 163 | + """ |
| 164 | + normalized_query = query.strip() |
| 165 | + |
| 166 | + statements = [stmt.strip() for stmt in normalized_query.split(';') if stmt.strip()] |
| 167 | + |
| 168 | + for statement in statements: |
| 169 | + statement_upper = statement.upper() |
| 170 | + if not statement_upper.startswith(('SELECT', 'WITH')): |
| 171 | + return False |
| 172 | + |
| 173 | + return len(statements) > 0 |
| 174 | + |
| 175 | + def is_memory_mode(self) -> bool: |
| 176 | + """Check if the database is running in memory mode. |
| 177 | +
|
| 178 | + Returns: |
| 179 | + bool: True if using in-memory database, False if using file database. |
| 180 | + """ |
| 181 | + return self._is_memory_mode |
| 182 | + |
| 183 | + def get_database_path(self) -> Optional[str]: |
| 184 | + """Get the database file path. |
| 185 | +
|
| 186 | + Returns: |
| 187 | + Optional[str]: Path to the database file if using file mode, |
| 188 | + None if using memory mode. |
| 189 | + """ |
| 190 | + return self.database_path |
| 191 | + |
| 192 | + def close(self) -> None: |
| 193 | + """Close database connections. |
| 194 | +
|
| 195 | + This method properly closes all database connections. It should be |
| 196 | + called when the Memory instance is no longer needed to free up |
| 197 | + resources. |
| 198 | + """ |
| 199 | + if hasattr(self, 'conn'): |
| 200 | + self.conn.close() |
| 201 | + |
| 202 | + def __enter__(self) -> 'Memory': |
| 203 | + """Context manager entry. |
| 204 | +
|
| 205 | + Returns: |
| 206 | + Memory: The Memory instance for use in a with statement. |
| 207 | + """ |
| 208 | + return self |
| 209 | + |
| 210 | + def __exit__(self, exc_type: Optional[type], exc_val: Optional[Exception], exc_tb: Optional[object]) -> None: |
| 211 | + """Context manager exit. |
| 212 | +
|
| 213 | + Automatically closes database connections when exiting the with block. |
| 214 | +
|
| 215 | + Args: |
| 216 | + exc_type: Exception type if an exception occurred. |
| 217 | + exc_val: Exception value if an exception occurred. |
| 218 | + exc_tb: Exception traceback if an exception occurred. |
| 219 | + """ |
| 220 | + self.close() |
0 commit comments