-
Notifications
You must be signed in to change notification settings - Fork 215
Sql tool #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Sql tool #233
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8bc0967
add tool to run sql queries over postgres
srilaasya 5be3f58
fixed var names, assertion err
srilaasya 2d18ae3
fixed workflow and added tests for compile_llms_txt.py, converted con…
srilaasya da14a29
Err with accessing current working dir, fix updated
srilaasya 7f39402
Merge branch 'refs/heads/main' into sql_tool
bboynton97 3b799b7
global connection and docs
bboynton97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import os | ||
| import psycopg2 | ||
| from typing import Dict, Any | ||
|
|
||
| def get_connection(): | ||
| """Get PostgreSQL database connection""" | ||
| return psycopg2.connect( | ||
| dbname=os.getenv('POSTGRES_DB'), | ||
| user=os.getenv('POSTGRES_USER'), | ||
| password=os.getenv('POSTGRES_PASSWORD'), | ||
| host=os.getenv('POSTGRES_HOST', 'localhost'), | ||
| port=os.getenv('POSTGRES_PORT', '5432') | ||
| ) | ||
|
|
||
| def get_schema() -> Dict[str, Any]: | ||
| """ | ||
| Initialize connection and get database schema. | ||
| Returns a dictionary containing the database schema. | ||
| """ | ||
| try: | ||
| conn = get_connection() | ||
| cursor = conn.cursor() | ||
|
|
||
| # Query to get all tables in the current schema | ||
| schema_query = """ | ||
| SELECT table_name | ||
| FROM information_schema.tables | ||
| WHERE table_schema = 'public' | ||
| AND table_type = 'BASE TABLE'; | ||
| """ | ||
|
|
||
| cursor.execute(schema_query) | ||
| tables = cursor.fetchall() | ||
|
|
||
| # Create schema dictionary | ||
| schema = {} | ||
| for (table_name,) in tables: | ||
| # Get column information for each table | ||
| column_query = """ | ||
| SELECT column_name | ||
| FROM information_schema.columns | ||
| WHERE table_schema = 'public' | ||
| AND table_name = %s; | ||
| """ | ||
| cursor.execute(column_query, (table_name,)) | ||
| columns = [col[0] for col in cursor.fetchall()] | ||
| schema[table_name] = columns | ||
|
|
||
| cursor.close() | ||
| conn.close() | ||
| return schema | ||
|
|
||
| except Exception as e: | ||
| print(f"Error getting database schema: {str(e)}") | ||
| return {} | ||
|
|
||
| def execute_query(query: str) -> list: | ||
| """ | ||
| Execute a SQL query on the database. | ||
| Args: | ||
| query: SQL query to execute | ||
| Returns: | ||
| List of query results | ||
| """ | ||
| try: | ||
| conn = get_connection() | ||
| cursor = conn.cursor() | ||
|
|
||
| # Execute the query | ||
| cursor.execute(query) | ||
| results = cursor.fetchall() | ||
|
|
||
| cursor.close() | ||
| conn.close() | ||
| return results | ||
|
|
||
| except Exception as e: | ||
| print(f"Error executing query: {str(e)}") | ||
| return [] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "name": "py_sql", | ||
| "url": "https://pypi.org/project/psycopg2/", | ||
| "category": "database", | ||
| "env": { | ||
| "POSTGRES_DB": { | ||
|
bboynton97 marked this conversation as resolved.
Outdated
|
||
| "description": "PostgreSQL database name", | ||
| "required": true | ||
| }, | ||
| "POSTGRES_USER": { | ||
| "description": "PostgreSQL username", | ||
| "required": true | ||
| }, | ||
| "POSTGRES_PASSWORD": { | ||
| "description": "PostgreSQL password", | ||
| "required": true | ||
| }, | ||
| "POSTGRES_HOST": { | ||
| "description": "PostgreSQL host address", | ||
| "required": true, | ||
| "default": "localhost" | ||
| }, | ||
| "POSTGRES_PORT": { | ||
| "description": "PostgreSQL port number", | ||
| "required": true, | ||
| "default": "5432" | ||
| } | ||
| }, | ||
| "dependencies": [ | ||
| "psycopg2-binary>=2.9.9" | ||
| ], | ||
| "tools": [ | ||
| "get_schema", | ||
| "execute_query" | ||
| ], | ||
| "cta": "Set up your PostgreSQL connection variables in the environment file." | ||
| } | ||
|
bboynton97 marked this conversation as resolved.
Outdated
|
Binary file not shown.
|
bboynton97 marked this conversation as resolved.
Outdated
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from sql import Table | ||
| from __init__ import construct_sql_query | ||
|
|
||
| def test_query_construction(): | ||
|
bboynton97 marked this conversation as resolved.
Outdated
|
||
| """Test query construction without a real database""" | ||
|
|
||
| # Define our test table structure | ||
| users = Table('users') | ||
|
|
||
| print("\n=== Testing SELECT queries ===") | ||
| # Test basic select | ||
| query, params = construct_sql_query( | ||
| "select", | ||
| "users", | ||
| columns=[users.name, users.age], | ||
| where=users.age > 18 | ||
| ) | ||
| print("Select users over 18:") | ||
| print(f"Query: {query}") | ||
| print(f"Params: {params}") | ||
|
|
||
| # Test select with multiple conditions | ||
| query, params = construct_sql_query( | ||
| "select", | ||
| "users", | ||
| columns=[users.name, users.email], | ||
| where=(users.age > 18) & (users.active == True) | ||
| ) | ||
| print("\nSelect active users over 18:") | ||
| print(f"Query: {query}") | ||
| print(f"Params: {params}") | ||
|
|
||
| print("\n=== Testing INSERT queries ===") | ||
| query, params = construct_sql_query( | ||
| "insert", | ||
| "users", | ||
| values=[["John Doe", 25, "john@example.com", True]] | ||
| ) | ||
| print("Insert new user:") | ||
| print(f"Query: {query}") | ||
| print(f"Params: {params}") | ||
|
|
||
| print("\n=== Testing UPDATE queries ===") | ||
| query, params = construct_sql_query( | ||
| "update", | ||
| "users", | ||
| columns=[users.active], | ||
| values=[False], | ||
| where=users.age < 18 | ||
| ) | ||
| print("Deactivate users under 18:") | ||
| print(f"Query: {query}") | ||
| print(f"Params: {params}") | ||
|
|
||
| print("\n=== Testing DELETE queries ===") | ||
| query, params = construct_sql_query( | ||
| "delete", | ||
| "users", | ||
| where=users.active == False | ||
| ) | ||
| print("Delete inactive users:") | ||
| print(f"Query: {query}") | ||
| print(f"Params: {params}") | ||
|
|
||
| if __name__ == "__main__": | ||
| test_query_construction() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.