def new_object(self, client_id, account): sql = "INSERT INTO accounts VALUES (DEFAULT,%s,%s) RETURNING *" cursor = connection.cursor() cursor.execute(sql, (client_id, account.value)) connection.commit() record = cursor.fetchone() return Account(record[0], record[1], record[2])
def update_object(self, change): sql = "UPDATE accounts SET value=%s WHERE account_id=%s RETURNING *" cursor = connection.cursor() cursor.execute(sql, (change.value, change.account_id)) connection.commit() return ""
def new_object(self, client, null=None): sql = "INSERT INTO clients VALUES (DEFAULT,%s,%s) RETURNING *" cursor = connection.cursor() cursor.execute(sql, (client.username, client.password)) connection.commit() record = cursor.fetchone() return Client(record[0], record[1], record[2])
def update_object(self, change): sql = "UPDATE clients SET username=%s,password=%s WHERE client_id=%s RETURNING *" cursor = connection.cursor() cursor.execute(sql, (change.username, change.password, change.client_id)) connection.commit() return ""
def get_object(self, client_id, account_id): sql = "SELECT * FROM accounts WHERE client_id = %s and account_id = %s" cursor = connection.cursor() cursor.execute(sql, (client_id, account_id)) record = cursor.fetchone() if record: return Account(record[0], record[1], record[2]) else: raise AccountNotFound(f"Account with ID {account_id} not found for Client with ID {client_id}.")
def delete_object(self, client_id, null=None): try: ClientDAO.get_object(self, client_id) except ClientNotFound as c: raise c sql = "DELETE FROM clients WHERE client_id = %s" cursor = connection.cursor() cursor.execute(sql, [client_id]) connection.commit()
def get_object(self, client_id, null=None): sql = "SELECT * FROM clients WHERE client_id = %s" cursor = connection.cursor() cursor.execute(sql, [client_id]) record = cursor.fetchone() if record: return Client(record[0], record[1], record[2]) else: raise ClientNotFound(f"Client with ID {client_id} not found.")
def delete_object(self, client_id, account_id): try: account = AccountDAO().get_object(client_id, account_id) except AccountNotFound as a: raise a sql = "DELETE FROM accounts WHERE client_id = %s and account_id = %s" cursor = connection.cursor() cursor.execute(sql, (client_id, account_id)) connection.commit() return ""
def get_all_objects_from_client(client_id): sql = "SELECT * FROM accounts WHERE client_id=%s" cursor = connection.cursor() cursor.execute(sql, [client_id]) records = cursor.fetchall() account_list = [] for record in records: account = Account(record[0], record[1], record[2]) account_list.append(account.json()) return account_list
def get_all_objects(self): sql = "SELECT * FROM accounts" cursor = connection.cursor() cursor.execute(sql) records = cursor.fetchall() account_list = [] for record in records: account = Account(record[0], record[1], record[2]) account_list.append(account.json()) return account_list