Skip to content

Commit

Permalink
added execute for DDLs
Browse files Browse the repository at this point in the history
  • Loading branch information
KDesp73 committed Sep 25, 2023
1 parent 2ecb536 commit a4e5b62
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ public interface DatabaseConnection {
ResultSet executeQuery(String query);

/**
* Executes the SQL query if it's valid (For INSERT, UPDATE, DELETE etc)
* Executes the SQL query if it's valid (For DMLs)
* @param query
* @return
*/
int executeUpdate(String query);

/**
* Executes the SQL query if it's valid (For DDLs)
* @param query
*/
void execute(String query);

/**
* Close the connection with the database
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MSAccessConnection implements DatabaseConnection {
private Connection connection;
Expand All @@ -31,9 +32,8 @@ public void connect(String url, String username, String password) {
}
}


/**
* Executes the SQL query if it's valid (For INSERT, UPDATE, DELETE etc)
* Executes the SQL query if it's valid (For DMLs)
*/
@Override
public int executeUpdate(String query) {
Expand All @@ -52,7 +52,7 @@ public int executeUpdate(String query) {
}

/**
* Executes the SQL query if it's valid
* Executes the SQL query if it's valid (For SELECT)
*
* @param query
* @return ResultSet
Expand All @@ -72,6 +72,28 @@ public ResultSet executeQuery(String query) {
}
}

/**
* Executes the SQL query if it's valid (For DDLs)
* @param query
*/
@Override
public void execute(String query) {
try (Statement statement = connection.createStatement()) {
if (query.toLowerCase().contains("create") ||
query.toLowerCase().contains("alter") ||
query.toLowerCase().contains("drop")) {
statement.execute(query);
System.out.println("DDL statement executed successfully.");
} else {
throw new IllegalArgumentException("Only CREATE, ALTER, and DROP statements are allowed.");
}
} catch (SQLException e) {
e.printStackTrace();
// You can handle SQLException differently based on your needs.
throw new RuntimeException("Error executing DDL statement: " + e.getMessage());
}
}

/**
* Close the connection with the database
*/
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/kdesp73/databridge/connections/SQLiteConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLiteConnection implements DatabaseConnection {
private Connection connection;
Expand Down Expand Up @@ -65,6 +66,28 @@ public ResultSet executeQuery(String query) {
}
}

/**
* Executes the SQL query if it's valid (For DDLs)
* @param query
*/
@Override
public void execute(String query) {
try (Statement statement = connection.createStatement()) {
if (query.toLowerCase().contains("create") ||
query.toLowerCase().contains("alter") ||
query.toLowerCase().contains("drop")) {
statement.execute(query);
System.out.println("DDL statement executed successfully.");
} else {
throw new IllegalArgumentException("Only CREATE, ALTER, and DROP statements are allowed.");
}
} catch (SQLException e) {
e.printStackTrace();
// You can handle SQLException differently based on your needs.
throw new RuntimeException("Error executing DDL statement: " + e.getMessage());
}
}

/**
* Close the connection with the database
*/
Expand Down

0 comments on commit a4e5b62

Please sign in to comment.