Skip to content

Commit

Permalink
Adding table relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysena committed Sep 27, 2024
1 parent 0aa5ffb commit 717bf8b
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 71 deletions.
92 changes: 92 additions & 0 deletions extras/rdms/SqlJoinRelationshipExtractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import java.io.*;
import java.util.*;
import java.util.regex.*;

public class SqlJoinRelationshipExtractor {

// Method to analyze SQL queries and extract the FROM and JOIN relationships, including multiple conditions (AND/OR)
public static void extractSqlJoins(String inputCsvFilePath, String outputCsvFilePath) {
try {
BufferedReader csvReader = new BufferedReader(new FileReader(inputCsvFilePath));
FileWriter csvWriter = new FileWriter(outputCsvFilePath);

String row;
boolean isFirstRow = true;

// Write the header of the output CSV
csvWriter.append("fileName,startLine,endLine,fromClause,joinRelationship\n");

// Pattern to match FROM clause
Pattern fromPattern = Pattern.compile("from\\s+([\\w.@]+)\\s+([\\w@]+)", Pattern.CASE_INSENSITIVE);
// Pattern to match JOIN clauses, including AND/OR conditions
Pattern joinPattern = Pattern.compile("join\\s+([\\w.@]+)\\s+([\\w@]+)\\s+on\\s+(.+?)(?=\\s+join|\\s+where|;|$)", Pattern.CASE_INSENSITIVE);

// Process each row of the CSV (from SqlExtractor)
while ((row = csvReader.readLine()) != null) {
if (isFirstRow) {
isFirstRow = false; // Skip the header
continue;
}

// Split the row into columns (fileName, startLine, endLine, sqlQuery)
String[] columns = row.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
if (columns.length < 4) continue; // Ensure row has the necessary columns

String fileName = columns[0].replace("\"", ""); // Extract fileName
String startLine = columns[1].replace("\"", ""); // Extract startLine
String endLine = columns[2].replace("\"", ""); // Extract endLine
String sqlQuery = columns[3].replace("\"", ""); // Extract the SQL query

// Capture the FROM clause
String fromClause = "";
Matcher fromMatcher = fromPattern.matcher(sqlQuery);
if (fromMatcher.find()) {
fromClause = "FROM " + fromMatcher.group(1) + " " + fromMatcher.group(2);
}

// Extract JOIN clauses including AND/OR conditions
Matcher joinMatcher = joinPattern.matcher(sqlQuery);
while (joinMatcher.find()) {
// Capture the full JOIN clause
String fullJoinClause = "JOIN " + joinMatcher.group(1) + " " + joinMatcher.group(2) + " ON " + joinMatcher.group(3).trim();

// Split the join conditions by AND/OR (but keep AND/OR in the condition for clarity)
String[] joinConditions = joinMatcher.group(3).split("(?<=\\))\\s+(AND|OR)\\s+");

// For each condition in the JOIN clause, create a new row
for (String condition : joinConditions) {
String joinRelationship = "JOIN " + joinMatcher.group(1) + " " + joinMatcher.group(2) + " ON " + condition.trim();

// Write fileName, startLine, endLine, fromClause, and each joinRelationship to the CSV
csvWriter.append(fileName).append(",")
.append(startLine).append(",")
.append(endLine).append(",")
.append("\"").append(fromClause).append("\",")
.append("\"").append(joinRelationship).append("\"\n");
}
}
}

csvReader.close();
csvWriter.flush();
csvWriter.close();

System.out.println("SQL FROM and JOIN relationships extraction complete. Results saved to: " + outputCsvFilePath);

} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java SqlJoinRelationshipExtractor <input_csv_file_path> <output_csv_file_path>");
return;
}

String inputCsvFilePath = args[0];
String outputCsvFilePath = args[1];

extractSqlJoins(inputCsvFilePath, outputCsvFilePath);
}
}
71 changes: 0 additions & 71 deletions extras/rdms/SqlTokenAnalyzer.java

This file was deleted.

Loading

0 comments on commit 717bf8b

Please sign in to comment.