Skip to content

Commit

Permalink
1、新增接口:generateUpdateSql 和 generateDeleteSql
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdian committed Aug 17, 2023
1 parent 8a920d2 commit ae236c1
Show file tree
Hide file tree
Showing 35 changed files with 3,642 additions and 514 deletions.
500 changes: 500 additions & 0 deletions API_OVERVIEW.md

Large diffs are not rendered by default.

473 changes: 1 addition & 472 deletions README.md

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/main/java/com/dbmasker/api/DBDialectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,58 @@ public static String generateInsertSql(Connection connection, String dbType, Str
Dialect dialect = new DialectFactory().getDialect(dbType);
return dialect.generateInsertSql(connection, schemaName, tableName, data, dbType);
}

/**
* Generates an SQL UPDATE statement for the specified table with the provided conditions.
*
* @param connection The active database connection.
* @param dbType The type of the database (e.g., "MySQL", "PostgreSQL").
* @param schemaName The name of the schema.
* @param tableName The name of the table for which the SQL update statement is to be generated.
* @param setData The key-value pairs representing columns and their corresponding values to be updated.
* @param condition The conditions that determine which rows will be updated.
* @param filteredByUniqueKey Flag indicating whether to filter the conditions by unique keys.
* @return A string representing the SQL UPDATE statement.
* @throws SQLException If any SQL-related error occurs.
*/
public static String generateUpdateSql(Connection connection, String dbType, String schemaName,
String tableName, Map<String, Object> setData, Map<String, Object> condition,
boolean filteredByUniqueKey) throws SQLException {
if (connection == null || dbType == null) {
throw new IllegalArgumentException(ErrorMessages.NULL_CONNECTION_OR_DB_TYPE_ERROR);
}

if (tableName == null) {
throw new IllegalArgumentException(ErrorMessages.NULL_TABLE_OR_VIEW_NAME_ERROR);
}

Dialect dialect = new DialectFactory().getDialect(dbType);
return dialect.generateUpdateSql(connection, dbType, schemaName, tableName, setData, condition, filteredByUniqueKey);
}

/**
* Generates a SQL DELETE statement for the given table and conditions.
*
* @param connection The database connection.
* @param dbType The type of the database.
* @param schemaName The name of the schema.
* @param tableName The name of the table.
* @param condition A map representing the conditions for the DELETE operation.
* @param filteredByUniqueKey If true, the conditions are filtered by the unique keys.
* @return The SQL DELETE statement as a string.
* @throws SQLException If any SQL related error occurs.
*/
public static String generateDeleteSql(Connection connection, String dbType, String schemaName, String tableName,
Map<String, Object> condition, boolean filteredByUniqueKey) throws SQLException {
if (connection == null || dbType == null) {
throw new IllegalArgumentException(ErrorMessages.NULL_CONNECTION_OR_DB_TYPE_ERROR);
}

if (tableName == null) {
throw new IllegalArgumentException(ErrorMessages.NULL_TABLE_OR_VIEW_NAME_ERROR);
}

Dialect dialect = new DialectFactory().getDialect(dbType);
return dialect.generateDeleteSql(connection, dbType, schemaName, tableName, condition, filteredByUniqueKey);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/dbmasker/api/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ public static List<TableAttribute> getTableAttribute(Connection connection, Stri
* @param dbType the type of the database, such as "sqlite", "mysql", etc.
* @param schemaName The specified schema name.
* @param table The specified table name.
* @return Returns a list of primary key column names for the specified table.
* @return Returns a Set of primary key column names for the specified table.
* @throws SQLException if a database access error occurs
*/
public static List<String> getPrimaryKeys(Connection connection, String dbType,
public static Set<String> getPrimaryKeys(Connection connection, String dbType,
String schemaName, String table) throws SQLException {
if (connection == null || dbType == null) {
throw new IllegalArgumentException(ErrorMessages.NULL_CONNECTION_OR_DB_TYPE_ERROR);
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/dbmasker/database/BaseDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ public static List<TableAttribute> getTableAttribute(Connection connection, Stri
* @param connection A valid database connection.
* @param schemaName The specified schema name.
* @param table The specified table name.
* @return Returns a list of primary key column names for the specified table.
* @return Returns a Set of primary key column names for the specified table.
* @throws SQLException if a database access error occurs
*/
@Override
public List<String> getPrimaryKeys(Connection connection, String schemaName, String table) throws SQLException {
public Set<String> getPrimaryKeys(Connection connection, String schemaName, String table) throws SQLException {
return getPrimaryKeys(connection, null, schemaName, table);
}

Expand All @@ -228,10 +228,10 @@ public List<String> getPrimaryKeys(Connection connection, String schemaName, Str
* @param catalog a catalog name, must match the catalog name as it is stored in the database;
* @param schemaName The specified schema name.
* @param table The specified table name.
* @return Returns a list of primary key column names for the specified table.
* @return Returns a Set of primary key column names for the specified table.
* @throws SQLException if a database access error occurs
*/
public List<String> getPrimaryKeys(Connection connection, String catalog, String schemaName, String table) throws SQLException {
public Set<String> getPrimaryKeys(Connection connection, String catalog, String schemaName, String table) throws SQLException {
// Get metadata from the connection
DatabaseMetaData metaData = connection.getMetaData();

Expand All @@ -240,7 +240,7 @@ public List<String> getPrimaryKeys(Connection connection, String catalog, String
ResultSet primaryKeyResultSet = metaData.getPrimaryKeys(catalog, schemaName, table);

// Iterate through the ResultSet and add each primary key to the list
List<String> primaryKeys = new ArrayList<>();
Set<String> primaryKeys = new HashSet<>();
while (primaryKeyResultSet.next()) {
primaryKeys.add(primaryKeyResultSet.getString("COLUMN_NAME"));
}
Expand Down Expand Up @@ -475,7 +475,6 @@ public List<Map<String, Object>> execQuerySQLWithMask(Connection connection, Str
public List<List<Map<String, Object>>> executeQuerySQLBatch(Connection connection, List<String> sqlList) throws SQLException {
List<List<Map<String, Object>>> batchResult = new ArrayList<>();

// Use try-with-resources to ensure proper resource management
try {
// Iterate through the SQL query list
for (String sql : sqlList) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/dbmasker/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public interface Database {
* @param connection A valid database connection.
* @param schemaName The specified schema name.
* @param table The specified table name.
* @return Returns a list of primary key column names for the specified table.
* @return Returns a Set of primary key column names for the specified table.
* @throws SQLException if a database access error occurs
*/
List<String> getPrimaryKeys(Connection connection, String schemaName, String table) throws SQLException;
Set<String> getPrimaryKeys(Connection connection, String schemaName, String table) throws SQLException;

/**
* Retrieves the unique keys of a specified database table.
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/dbmasker/database/Phoenix.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Phoenix Database class implements the Database interface for Phoenix databases.
Expand Down Expand Up @@ -100,4 +102,44 @@ public List<DatabaseFunction> getFuncs(Connection connection, String schemaName)
throw new SQLFeatureNotSupportedException("Phoenix does not support functions");
}


/**
* Retrieves the primary keys of a specified table within the given schema.
*
* @param connection A valid database connection.
* @param schemaName The specified schema name.
* @param table The specified table name.
* @return Returns a Set of primary key column names for the specified table.
* @throws SQLException if a database access error occurs
*/
@Override
public Set<String> getPrimaryKeys(Connection connection, String schemaName, String table) throws SQLException {
Set<String> primaryKeys = new HashSet<>();

// Use a query to get the primary key column from the SYSTEM.CATALOG table for the given table and schema
String sql = "SELECT COLUMN_NAME " +
"FROM SYSTEM.CATALOG " +
"WHERE TABLE_SCHEM = ? " +
"AND TABLE_NAME = ? " +
"AND COLUMN_FAMILY IS NULL " + // Primary key columns have NULL COLUMN_FAMILY
"AND KEY_SEQ IS NOT NULL " + // Indicates the column is part of the primary key
"ORDER BY KEY_SEQ"; // Order by the key sequence to get the first part of compound primary key if any

// try-with-resources block ensures that both the PreparedStatement and ResultSet are closed automatically
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

// Set parameters for the PreparedStatement to prevent SQL injection
preparedStatement.setString(1, schemaName);
preparedStatement.setString(2, table);

try (ResultSet resultSet = preparedStatement.executeQuery()) {

// Loop over the resultSet to get all primary key columns
while (resultSet.next()) {
primaryKeys.add(resultSet.getString("COLUMN_NAME"));
}
}
}
return primaryKeys;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/dbmasker/database/gbase/GBase8a.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ public List<TableAttribute> getTableAttribute(Connection connection, String sche
* @param connection A valid database connection.
* @param schemaName The specified schema name.
* @param table The specified table name.
* @return Returns a list of primary key column names for the specified table.
* @return Returns a Set of primary key column names for the specified table.
* @throws SQLException if a database access error occurs
*/
@Override
public List<String> getPrimaryKeys(Connection connection, String schemaName, String table) throws SQLException {
public Set<String> getPrimaryKeys(Connection connection, String schemaName, String table) throws SQLException {
return getPrimaryKeys(connection, schemaName, "", table);
}

Expand Down
Loading

0 comments on commit ae236c1

Please sign in to comment.