From a4e5b62bdb95ed4fc29552e49099408ad93266a5 Mon Sep 17 00:00:00 2001 From: Konstantinos Despoinidis Date: Tue, 26 Sep 2023 00:49:06 +0300 Subject: [PATCH] added execute for DDLs --- .../connections/DatabaseConnection.java | 8 +++++- .../connections/MSAccessConnection.java | 28 +++++++++++++++++-- .../connections/SQLiteConnection.java | 23 +++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/main/java/kdesp73/databridge/connections/DatabaseConnection.java b/src/main/java/kdesp73/databridge/connections/DatabaseConnection.java index 7a719a8..1c8f31c 100644 --- a/src/main/java/kdesp73/databridge/connections/DatabaseConnection.java +++ b/src/main/java/kdesp73/databridge/connections/DatabaseConnection.java @@ -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 */ diff --git a/src/main/java/kdesp73/databridge/connections/MSAccessConnection.java b/src/main/java/kdesp73/databridge/connections/MSAccessConnection.java index f0cb47c..b04f378 100644 --- a/src/main/java/kdesp73/databridge/connections/MSAccessConnection.java +++ b/src/main/java/kdesp73/databridge/connections/MSAccessConnection.java @@ -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; @@ -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) { @@ -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 @@ -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 */ diff --git a/src/main/java/kdesp73/databridge/connections/SQLiteConnection.java b/src/main/java/kdesp73/databridge/connections/SQLiteConnection.java index af6e088..a7f4235 100644 --- a/src/main/java/kdesp73/databridge/connections/SQLiteConnection.java +++ b/src/main/java/kdesp73/databridge/connections/SQLiteConnection.java @@ -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; @@ -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 */