Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed docs/images/arch.png:Zone.Identifier
Binary file not shown.
Binary file removed logo.png:Zone.Identifier
Binary file not shown.
11 changes: 8 additions & 3 deletions src/connectors/redshift.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import psycopg2
import pandas as pd
from typing import List

from psycopg2 import sql
def connect(host: str, port: int, database: str, user: str, password: str):
"""Establish a connection to Amazon Redshift.

Expand Down Expand Up @@ -73,14 +73,19 @@ def fetch_table(host: str, port: int, database: str, user: str, password: str, t

Raises:
psycopg2.Error: If connection or query fails
ValueError: If table name is invalid
"""
if not table.replace('_', '').isalnum():
raise ValueError(f"Invalid table name: {table}")

conn = connect(host, port, database, user, password)
cursor = conn.cursor()
try:
cursor.execute(f"SELECT * FROM {table} LIMIT {limit}")
query = sql.SQL("SELECT * FROM {} LIMIT %s").format(sql.Identifier(table))
cursor.execute(query, (limit,))
columns = [desc[0] for desc in cursor.description]
rows = cursor.fetchall()
return pd.DataFrame(rows, columns=columns)
finally:
cursor.close()
conn.close()
conn.close()