|
| 1 | +import firebase_admin |
| 2 | +from firebase_admin import credentials, db |
| 3 | +import pyrebase |
| 4 | +import pandas as pd |
| 5 | +from tabulate import tabulate |
| 6 | + |
| 7 | + |
| 8 | +class Firebase: |
| 9 | + CREDENTIALS_FILE = "db.json" |
| 10 | + DATABASE_URL = "https://dbdeskapp-e4d13-default-rtdb.europe-west1.firebasedatabase.app/" |
| 11 | + API_KEY = "AIzaSyD-0p1f8b34D_dyke74CdobrQDcJCAfSsE" |
| 12 | + PROJECT_ID = "dbdeskapp-e4d13" |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + self.cred = credentials.Certificate(self.CREDENTIALS_FILE) |
| 16 | + firebase_admin.initialize_app(self.cred, { |
| 17 | + "databaseURL": self.DATABASE_URL |
| 18 | + }) |
| 19 | + self.ref = db.reference("/") |
| 20 | + |
| 21 | + self.config = { |
| 22 | + "apiKey": self.API_KEY, |
| 23 | + "authDomain": f"{self.PROJECT_ID}.firebaseapp.com", |
| 24 | + "databaseURL": self.DATABASE_URL, |
| 25 | + "storageBucket": f"{self.PROJECT_ID}.appspot.com", |
| 26 | + } |
| 27 | + self.firebase = pyrebase.initialize_app(self.config) |
| 28 | + self.auth = self.firebase.auth() |
| 29 | + self.db = self.firebase.database() |
| 30 | + |
| 31 | + def login(self, email, password): |
| 32 | + user = self.auth.sign_in_with_email_and_password(email, password) |
| 33 | + return user |
| 34 | + |
| 35 | + def register(self, email, password): |
| 36 | + user = self.auth.create_user_with_email_and_password(email, password) |
| 37 | + return user |
| 38 | + |
| 39 | + def get_user(self): |
| 40 | + return self.auth.current_user |
| 41 | + |
| 42 | + def create_new_database(self, database_name): |
| 43 | + user = self.get_user() |
| 44 | + if user: |
| 45 | + user_uid = user['localId'] |
| 46 | + self.db.child("users").child(user_uid).child("databases").child(database_name).set({"name": database_name}) |
| 47 | + return database_name |
| 48 | + else: |
| 49 | + print("User is not authenticated.") |
| 50 | + return None |
| 51 | + |
| 52 | + def create_table(self, user_id, database_name, table_name, table_id, table_info): |
| 53 | + try: |
| 54 | + table_ref = self.db.child("users").child(user_id).child("databases").child(database_name).child("tables").child(table_name) |
| 55 | + table_ref.child("table_id").set(table_id) |
| 56 | + table_ref.child("table_name").set(table_name) |
| 57 | + table_ref.child("info").set(table_info) |
| 58 | + print(f"Table '{table_name}' created successfully in database '{database_name}'.") |
| 59 | + except Exception as e: |
| 60 | + print(f"Error creating table: {e}") |
| 61 | + |
| 62 | + def create_column(self, user_id, selected_table_name, col_name, data_type, primary_key, foreign_key): |
| 63 | + try: |
| 64 | + # Debug print statements for tracking values |
| 65 | + print(f"User ID: {user_id}") |
| 66 | + print(f"Table Name: {selected_table_name}") |
| 67 | + print(f"Column Name: {col_name}, Data Type: {data_type}, Primary Key: {primary_key}, Foreign Key: {foreign_key}") |
| 68 | + |
| 69 | + columns_ref = self.db.child("user_tables").child(user_id).child(selected_table_name).child("columns").child(col_name) |
| 70 | + columns_ref.set({ |
| 71 | + "column_name": col_name, |
| 72 | + "data_type": data_type, |
| 73 | + "primary_key": primary_key, |
| 74 | + "foreign_key": foreign_key |
| 75 | + }) |
| 76 | + print(f"Column '{col_name}' created successfully in table '{selected_table_name}'.") |
| 77 | + except Exception as e: |
| 78 | + print(f"Error creating column: {e}") |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + def get_user_databases(self, user_id): |
| 84 | + try: |
| 85 | + return self.db.child("users").child(user_id).get().val() |
| 86 | + except Exception as e: |
| 87 | + print(f"Error fetching user databases: {e}") |
| 88 | + return None |
| 89 | + |
| 90 | + def save_table_info(self, database_name, table_name, table_id): |
| 91 | + try: |
| 92 | + table_info = {"table_name": table_name, "table_id": table_id} |
| 93 | + user = self.get_user() |
| 94 | + if user: |
| 95 | + user_uid = user['localId'] |
| 96 | + user_tables_ref = self.db.child("users").child(user_uid).child("databases").child(database_name).child("tables").child(table_name) |
| 97 | + user_tables_ref.set(table_info) |
| 98 | + print(f"Table '{table_name}' info saved successfully in database '{database_name}'.") |
| 99 | + return True |
| 100 | + else: |
| 101 | + print("User is not authenticated.") |
| 102 | + return False |
| 103 | + except Exception as e: |
| 104 | + print(f"Error saving table info: {e}") |
| 105 | + return False |
| 106 | + |
| 107 | + def get_table_names(self, database_name): |
| 108 | + try: |
| 109 | + table_names = [] |
| 110 | + user = self.get_user() |
| 111 | + if user: |
| 112 | + user_uid = user['localId'] |
| 113 | + tables_ref = self.db.child("users").child(user_uid).child("databases").child(database_name).child("tables") |
| 114 | + table_snapshots = tables_ref.get() |
| 115 | + if table_snapshots: |
| 116 | + for table_snapshot in table_snapshots.each(): |
| 117 | + table_info = table_snapshot.val() |
| 118 | + if "table_name" in table_info: |
| 119 | + table_names.append(table_info["table_name"]) |
| 120 | + else: |
| 121 | + print(f"Warning: 'table_name' key not found in table info: {table_info}") |
| 122 | + else: |
| 123 | + print("No tables found for the selected database.") |
| 124 | + else: |
| 125 | + print("User is not authenticated.") |
| 126 | + print(f"Retrieved table names: {table_names}") # Debugging line |
| 127 | + return table_names |
| 128 | + except Exception as e: |
| 129 | + print(f"Error getting table names: {e}") |
| 130 | + return [] |
| 131 | + |
| 132 | + def get_database_names(self): |
| 133 | + user = self.get_user() |
| 134 | + if user: |
| 135 | + user_uid = user['localId'] |
| 136 | + databases = self.db.child("users").child(user_uid).child("databases").get().val() |
| 137 | + return [db['name'] for db in databases.values()] if databases else [] |
| 138 | + else: |
| 139 | + print("User is not authenticated.") |
| 140 | + return [] |
| 141 | + |
| 142 | + def get_dbtable_names(self, database_name): |
| 143 | + try: |
| 144 | + user = self.get_user() |
| 145 | + if user: |
| 146 | + user_uid = user['localId'] |
| 147 | + db_ref = self.db.child("users").child(user_uid).child("databases").child(database_name) |
| 148 | + db_info = db_ref.get().val() |
| 149 | + if db_info: |
| 150 | + table_names = [] |
| 151 | + tables_ref = db_ref.child("tables") |
| 152 | + tables = tables_ref.get() |
| 153 | + if tables: |
| 154 | + for table_name, _ in tables.items(): |
| 155 | + table_names.append(table_name) |
| 156 | + return table_names |
| 157 | + else: |
| 158 | + print("Database not found.") |
| 159 | + return [] |
| 160 | + else: |
| 161 | + print("User is not authenticated.") |
| 162 | + return [] |
| 163 | + except Exception as e: |
| 164 | + print(f"Error getting table names: {e}") |
| 165 | + return [] |
| 166 | + |
| 167 | + def get_columns(self, user_id, selected_table_name): |
| 168 | + try: |
| 169 | + print(f"Retrieving columns for user ID: {user_id}, table: {selected_table_name}") |
| 170 | + columns_ref = self.db.child("user_tables").child(user_id).child(selected_table_name).child("columns") |
| 171 | + columns = [] |
| 172 | + column_snapshots = columns_ref.get() |
| 173 | + if column_snapshots.each() is not None: |
| 174 | + for column_snapshot in column_snapshots.each(): |
| 175 | + column_info = column_snapshot.val() |
| 176 | + column_name = column_snapshot.key() # column name is the key |
| 177 | + column_info["name"] = column_name # add the column name to the column info |
| 178 | + columns.append(column_info) |
| 179 | + print(f"Columns retrieved: {columns}") |
| 180 | + else: |
| 181 | + print("No columns found.") |
| 182 | + return columns |
| 183 | + except Exception as e: |
| 184 | + print(f"Error getting columns: {e}") |
| 185 | + return [] |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + def add_data_to_table(self, user_id, database_name, table_name, data): |
| 191 | + try: |
| 192 | + table_ref = self.db.child("users").child(user_id).child("databases").child(database_name).child("tables").child(table_name).child("data") |
| 193 | + table_ref.push(data) |
| 194 | + print(f"Data added to table '{table_name}' in database '{database_name}'.") |
| 195 | + except Exception as e: |
| 196 | + print(f"Error adding data to table: {e}") |
| 197 | + |
| 198 | + def get_table_data(self, user_id, database_name, table_name): |
| 199 | + try: |
| 200 | + data_ref = self.db.child("users").child(user_id).child("databases").child(database_name).child("tables").child(table_name).child("data") |
| 201 | + data = data_ref.get().val() |
| 202 | + if data: |
| 203 | + df = pd.DataFrame(data.values()) |
| 204 | + return df |
| 205 | + else: |
| 206 | + print(f"No data found in table '{table_name}'.") |
| 207 | + return pd.DataFrame() |
| 208 | + except Exception as e: |
| 209 | + print(f"Error getting table data: {e}") |
| 210 | + return pd.DataFrame() |
| 211 | + |
| 212 | + def display_table_data(self, user_id, database_name, table_name): |
| 213 | + df = self.get_table_data(user_id, database_name, table_name) |
| 214 | + if not df.empty: |
| 215 | + print(tabulate(df, headers='keys', tablefmt='psql')) |
| 216 | + else: |
| 217 | + print(f"No data to display for table '{table_name}'.") |
| 218 | + |
| 219 | + def delete_database(self, user_id, database_name): |
| 220 | + try: |
| 221 | + self.db.child("users").child(user_id).child("databases").child(database_name).remove() |
| 222 | + print(f"Database '{database_name}' deleted successfully.") |
| 223 | + except Exception as e: |
| 224 | + print(f"Error deleting database: {e}") |
| 225 | + |
| 226 | + def delete_table(self, user_id, database_name, table_name): |
| 227 | + try: |
| 228 | + self.db.child("users").child(user_id).child("databases").child(database_name).child("tables").child(table_name).remove() |
| 229 | + print(f"Table '{table_name}' in database '{database_name}' deleted successfully.") |
| 230 | + except Exception as e: |
| 231 | + print(f"Error deleting table: {e}") |
| 232 | + |
| 233 | + |
| 234 | + def delete_column(self, user_id, database_name, table_name, column_name): |
| 235 | + try: |
| 236 | + # Delete column from user_tables |
| 237 | + self.db.child("user_tables").child(user_id).child(table_name).child("columns").child(column_name).remove() |
| 238 | + print(f"Column '{column_name}' in table '{table_name}' deleted successfully from user_tables.") |
| 239 | + |
| 240 | + # Delete column from users |
| 241 | + user_ref = self.db.child("users").child(user_id).child("databases").child(database_name).child("tables") |
| 242 | + tables = user_ref.get() |
| 243 | + |
| 244 | + if tables.each() is not None: |
| 245 | + for table_snapshot in tables.each(): |
| 246 | + table_data = table_snapshot.val() |
| 247 | + if table_data.get("table_name") == table_name: |
| 248 | + data_snapshots = table_data.get("data", {}) |
| 249 | + for data_id, data in data_snapshots.items(): |
| 250 | + if isinstance(data, dict) and column_name in data: |
| 251 | + self.db.child("users").child(user_id).child("databases").child(database_name).child("tables").child(table_snapshot.key()).child("data").child(data_id).child(column_name).remove() |
| 252 | + print(f"Column '{column_name}' in table '{table_name}' deleted successfully from users data.") |
| 253 | + except Exception as e: |
| 254 | + print(f"Error deleting column: {e}") |
| 255 | + |
| 256 | + |
0 commit comments