Skip to content
Closed
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
82 changes: 82 additions & 0 deletions sql2graph/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import mgclient


# UIs:
# * CLI -> here (mgconsole)
# * MCP -> https://github.com/memgraph/ai-toolkit
# * WEB -> Memgraph Lab


def connect_to_memgraph(
host="127.0.0.1", port=7687, user="memgraph", password="memgraph"
):
conn = mgclient.connect(
host=host,
port=port,
username=user,
password=password,
)
return conn


def fetch_sql_metadata(sql_system_name):
conn = connect_to_memgraph()
cursor = conn.cursor()
cursor.execute(f"CALL {sql_system_name}.get_metadata() YIELD *")
results = cursor.fetchall()
cursor.close()
conn.close()
return results


def fetch_sql_table(sql_system_name, table):
conn = connect_to_memgraph()
cursor = conn.cursor()
cursor.execute(f"CALL {sql_system_name}.migrate('{table}') YIELD *")
results = cursor.fetchall()
cursor.close()
conn.close()
return results


def fetch_sql_join_data(sql_system_name, generate_sql_query):
conn = connect_to_memgraph()
cursor = conn.cursor()
cursor.execute(f"CALL {sql_system_name}.migrate('{generate_sql_query}') YIELD *")
results = cursor.fetchall()
cursor.close()
conn.close()
return results


def run_generated_cypher_migration_query(row, generate_migration_query):
conn = connect_to_memgraph()
cursor = conn.cursor()
# TODO(gitbuda): Inject row into the query.
cursor.execute(f"{generate_migration_query}")
results = cursor.fetchall()
cursor.close()
conn.close()
return results


if __name__ == "__main__":
sql_system = "mysql" # Replace with your SQL system name
metadata = fetch_sql_metadata(sql_system)
# TODO(gitbuda): Ask LLM to determine what's the right table / graph format.
print("Fetched metadata:")
for row in metadata:
print(row)
data = fetch_sql_table(sql_system, "users")
print("Fetched data:")
for row in data:
print(row)


# CALL metadata() YIELD tables
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take metadata/schema from SQL, but also the arrows-like schema from the user

# -> LLM decideds what are relevant tables
# WITH relevant_metadata # NOTE: it has to be a single variable (a map) -> inject YES/NO.
# CALL migrate(relevant_metadata["table1"]) AS row # this is going to be called N times...
# custom code 0 -> generated by LLM
# CALL migrate(relevant_metadata["tableN"]) AS row
# custom code N -> generated by LLM
1 change: 1 addition & 0 deletions sql2graph/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pymgclient==1.4.0
Loading