Skip to content

Commit

Permalink
feat: Add SQL Server support to defog-python
Browse files Browse the repository at this point in the history
This commit enables SQL Server support in defog-python by adding the necessary code changes to handle SQL Server database credentials and connections. Now users can specify a SQL Server host, database name, user, and password when initializing defog-python. This allows defog-python to connect to SQL Server databases and perform data extraction and manipulation tasks.

The changes include:
- Adding validation for SQL Server database credentials in the `Defog` class.
- Modifying the `init` function in the CLI module to prompt users for SQL Server database information.
- Updating the `generate_sqlserver_schema` function to use the provided SQL Server credentials for establishing a connection.
- Modifying the `execute_query_once` function to use the SQL Server credentials for connecting to the database.

These changes were made in response to user requests and will enhance the functionality of defog-python by expanding its database compatibility.
  • Loading branch information
rishsriv committed Jun 25, 2024
1 parent 09f772c commit e857520
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
11 changes: 10 additions & 1 deletion defog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,16 @@ def check_db_creds(db_type: str, db_creds: dict):
raise KeyError("db_creds must contain a 'access_token' key.")
if "http_path" not in db_creds:
raise KeyError("db_creds must contain a 'http_path' key.")
elif db_type == "mongo" or db_type == "sqlserver":
elif db_type == "sqlserver":
if "server" not in db_creds:
raise KeyError("db_creds must contain a 'server' key.")
if "database" not in db_creds:
raise KeyError("db_creds must contain a 'database' key.")
if "user" not in db_creds:
raise KeyError("db_creds must contain a 'user' key.")
if "password" not in db_creds:
raise KeyError("db_creds must contain a 'password' key.")
elif db_type == "mongo":
if "connection_string" not in db_creds:
raise KeyError("db_creds must contain a 'connection_string' key.")
elif db_type == "bigquery":
Expand Down
12 changes: 12 additions & 0 deletions defog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ def init():
db_creds = {
"json_key_path": json_key_path,
}
elif db_type == "sqlserver":
server = prompt("Please enter your database server host").strip()
database = prompt("Please enter your database name").strip()
user = prompt("Please enter your database user").strip()
password = pwinput.pwinput("Please enter your database password")
db_creds = {
"server": server,
"database": database,
"user": user,
"password": password,
"schema": schema,
}

# write to filepath and print confirmation
with open(filepath, "w") as f:
Expand Down
3 changes: 2 additions & 1 deletion defog/generate_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ def generate_sqlserver_schema(
except:
raise Exception("pyodbc not installed.")

conn = pyodbc.connect(self.db_creds["connection_string"])
connection_string = f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={self.db_creds['server']};DATABASE={self.db_creds['database']};UID={self.db_creds['user']};PWD={self.db_creds['password']}"
conn = pyodbc.connect(connection_string)
cur = conn.cursor()
schemas = {}
schema = self.db_creds.get("schema", "dbo")
Expand Down
3 changes: 2 additions & 1 deletion defog/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def execute_query_once(db_type: str, db_creds, query: str):
import pyodbc
except:
raise Exception("pyodbc not installed.")
conn = pyodbc.connect(**db_creds)
connection_string = f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={db_creds['server']};DATABASE={db_creds['database']};UID={db_creds['user']};PWD={db_creds['password']}"
conn = pyodbc.connect(connection_string)
cur = conn.cursor()
cur.execute(query)
colnames = [desc[0] for desc in cur.description]
Expand Down

0 comments on commit e857520

Please sign in to comment.