|
@@ -41,12 +41,11 @@ import org.eclipse.persistence.sessions.DatabaseLogin;
|
|
|
import org.eclipse.persistence.sessions.DatabaseSession;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-
|
|
|
+
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.FileReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
-import java.nio.charset.Charset;
|
|
|
import java.sql.Blob;
|
|
|
import java.sql.Connection;
|
|
|
import java.sql.DatabaseMetaData;
|
|
@@ -71,7 +70,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
private static final String dbURLPatternString = "jdbc:(.*?):.*";
|
|
|
private Pattern dbURLPattern = Pattern.compile(dbURLPatternString, Pattern.CASE_INSENSITIVE);
|
|
|
private DbType dbType;
|
|
|
-
|
|
|
+
|
|
|
@Inject
|
|
|
public DBAccessorImpl(Configuration configuration) {
|
|
|
this.configuration = configuration;
|
|
@@ -80,14 +79,14 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
Class.forName(configuration.getDatabaseDriver());
|
|
|
|
|
|
connection = DriverManager.getConnection(configuration.getDatabaseUrl(),
|
|
|
- configuration.getDatabaseUser(),
|
|
|
- configuration.getDatabasePassword());
|
|
|
+ configuration.getDatabaseUser(),
|
|
|
+ configuration.getDatabasePassword());
|
|
|
|
|
|
connection.setAutoCommit(true); //enable autocommit
|
|
|
|
|
|
//TODO create own mapping and platform classes for supported databases
|
|
|
- String vendorName = connection.getMetaData().getDatabaseProductName()
|
|
|
- + connection.getMetaData().getDatabaseMajorVersion();
|
|
|
+ String vendorName = connection.getMetaData().getDatabaseProductName() +
|
|
|
+ connection.getMetaData().getDatabaseMajorVersion();
|
|
|
String dbPlatform = DBPlatformHelper.getDBPlatform(vendorName, new AbstractSessionLog() {
|
|
|
@Override
|
|
|
public void log(SessionLogEntry sessionLogEntry) {
|
|
@@ -107,13 +106,13 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
if (databasePlatform instanceof OraclePlatform) {
|
|
|
dbType = DbType.ORACLE;
|
|
|
return new OracleHelper(databasePlatform);
|
|
|
- } else if (databasePlatform instanceof MySQLPlatform) {
|
|
|
+ }else if (databasePlatform instanceof MySQLPlatform) {
|
|
|
dbType = DbType.MYSQL;
|
|
|
return new MySqlHelper(databasePlatform);
|
|
|
- } else if (databasePlatform instanceof PostgreSQLPlatform) {
|
|
|
+ }else if (databasePlatform instanceof PostgreSQLPlatform) {
|
|
|
dbType = DbType.POSTGRES;
|
|
|
return new PostgresHelper(databasePlatform);
|
|
|
- } else if (databasePlatform instanceof DerbyPlatform) {
|
|
|
+ }else if (databasePlatform instanceof DerbyPlatform) {
|
|
|
dbType = DbType.DERBY;
|
|
|
return new DerbyHelper(databasePlatform);
|
|
|
} else {
|
|
@@ -130,8 +129,8 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
public Connection getNewConnection() {
|
|
|
try {
|
|
|
return DriverManager.getConnection(configuration.getDatabaseUrl(),
|
|
|
- configuration.getDatabaseUser(),
|
|
|
- configuration.getDatabasePassword());
|
|
|
+ configuration.getDatabaseUser(),
|
|
|
+ configuration.getDatabasePassword());
|
|
|
} catch (SQLException e) {
|
|
|
throw new RuntimeException("Unable to connect to database", e);
|
|
|
}
|
|
@@ -144,7 +143,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
@Override
|
|
|
public void createTable(String tableName, List<DBColumnInfo> columnInfo,
|
|
|
- String... primaryKeyColumns) throws SQLException {
|
|
|
+ String... primaryKeyColumns) throws SQLException {
|
|
|
if (!tableExists(tableName)) {
|
|
|
String query = dbmsHelper.getCreateTableStatement(tableName, columnInfo, Arrays.asList(primaryKeyColumns));
|
|
|
|
|
@@ -168,24 +167,27 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
DatabaseMetaData metaData = getDatabaseMetaData();
|
|
|
if (metaData.storesLowerCaseIdentifiers()) {
|
|
|
return objectName.toLowerCase();
|
|
|
- } else if (metaData.storesUpperCaseIdentifiers()) {
|
|
|
+ }else if (metaData.storesUpperCaseIdentifiers()) {
|
|
|
return objectName.toUpperCase();
|
|
|
}
|
|
|
|
|
|
return objectName;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public boolean tableExists(String tableName) throws SQLException {
|
|
|
boolean result = false;
|
|
|
DatabaseMetaData metaData = getDatabaseMetaData();
|
|
|
|
|
|
- ResultSet res = metaData.getTables(null, null, convertObjectName(tableName), new String[]{"TABLE"});
|
|
|
+ ResultSet res = metaData.getTables(null, null, convertObjectName(tableName), new String[] { "TABLE" });
|
|
|
|
|
|
if (res != null) {
|
|
|
try {
|
|
|
if (res.next()) {
|
|
|
- return res.getString("TABLE_NAME") != null && res.getString("TABLE_NAME").equalsIgnoreCase(tableName);
|
|
|
+ return res.getString("TABLE_NAME") != null && res.getString
|
|
|
+ ("TABLE_NAME").equalsIgnoreCase(tableName);
|
|
|
}
|
|
|
} finally {
|
|
|
res.close();
|
|
@@ -206,7 +208,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
boolean retVal = false;
|
|
|
ResultSet rs = null;
|
|
|
try {
|
|
|
- rs = statement.executeQuery(query);
|
|
|
+ rs = statement.executeQuery(query);
|
|
|
if (rs != null) {
|
|
|
if (rs.next()) {
|
|
|
return rs.getInt(1) > 0;
|
|
@@ -215,9 +217,6 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
} catch (Exception e) {
|
|
|
LOG.error("Unable to check if table " + tableName + " has any data. Exception: " + e.getMessage());
|
|
|
} finally {
|
|
|
- if (statement != null) {
|
|
|
- statement.close();
|
|
|
- }
|
|
|
if (rs != null) {
|
|
|
rs.close();
|
|
|
}
|
|
@@ -234,7 +233,8 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
if (rs != null) {
|
|
|
try {
|
|
|
if (rs.next()) {
|
|
|
- return rs.getString("COLUMN_NAME") != null && rs.getString("COLUMN_NAME").equalsIgnoreCase(columnName);
|
|
|
+ return rs.getString("COLUMN_NAME") != null && rs.getString
|
|
|
+ ("COLUMN_NAME").equalsIgnoreCase(columnName);
|
|
|
}
|
|
|
} finally {
|
|
|
rs.close();
|
|
@@ -245,7 +245,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean tableHasColumn(String tableName, String... columnName) throws SQLException {
|
|
|
+ public boolean tableHasColumn(String tableName, String... columnName) throws SQLException{
|
|
|
List<String> columnsList = new ArrayList<String>(Arrays.asList(columnName));
|
|
|
DatabaseMetaData metaData = getDatabaseMetaData();
|
|
|
|
|
@@ -292,18 +292,19 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
@Override
|
|
|
public boolean tableHasForeignKey(String tableName, String refTableName,
|
|
|
- String columnName, String refColumnName) throws SQLException {
|
|
|
+ String columnName, String refColumnName) throws SQLException {
|
|
|
return tableHasForeignKey(tableName, refTableName, new String[]{columnName}, new String[]{refColumnName});
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean tableHasForeignKey(String tableName, String referenceTableName, String[] keyColumns,
|
|
|
- String[] referenceColumns) throws SQLException {
|
|
|
+ String[] referenceColumns) throws SQLException {
|
|
|
DatabaseMetaData metaData = getDatabaseMetaData();
|
|
|
|
|
|
//NB: reference table contains pk columns while key table contains fk columns
|
|
|
+
|
|
|
ResultSet rs = metaData.getCrossReference(null, null, convertObjectName(referenceTableName),
|
|
|
- null, null, convertObjectName(tableName));
|
|
|
+ null, null, convertObjectName(tableName));
|
|
|
|
|
|
List<String> pkColumns = new ArrayList<String>(referenceColumns.length);
|
|
|
for (String referenceColumn : referenceColumns) {
|
|
@@ -331,10 +332,12 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
fkColumns.remove(fkIndex);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
} else {
|
|
|
LOG.debug("pkCol={}, fkCol={} not found in provided column names, skipping", pkColumn, fkColumn); //TODO debug
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|
|
|
if (pkColumns.isEmpty() && fkColumns.isEmpty()) {
|
|
|
return true;
|
|
@@ -345,13 +348,14 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void createIndex(String indexName, String tableName,
|
|
|
- String... columnNames) throws SQLException {
|
|
|
+ String... columnNames) throws SQLException {
|
|
|
String query = dbmsHelper.getCreateIndexStatement(indexName, tableName, columnNames);
|
|
|
|
|
|
executeQuery(query);
|
|
@@ -359,49 +363,48 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
@Override
|
|
|
public void addFKConstraint(String tableName, String constraintName,
|
|
|
- String keyColumn, String referenceTableName,
|
|
|
- String referenceColumn, boolean ignoreFailure) throws SQLException {
|
|
|
+ String keyColumn, String referenceTableName,
|
|
|
+ String referenceColumn, boolean ignoreFailure) throws SQLException {
|
|
|
|
|
|
addFKConstraint(tableName, constraintName, new String[]{keyColumn}, referenceTableName,
|
|
|
- new String[]{referenceColumn}, false, ignoreFailure);
|
|
|
+ new String[]{referenceColumn}, false, ignoreFailure);
|
|
|
}
|
|
|
-
|
|
|
@Override
|
|
|
public void addFKConstraint(String tableName, String constraintName,
|
|
|
- String keyColumn, String referenceTableName,
|
|
|
- String referenceColumn, boolean shouldCascadeOnDelete,
|
|
|
- boolean ignoreFailure) throws SQLException {
|
|
|
+ String keyColumn, String referenceTableName,
|
|
|
+ String referenceColumn, boolean shouldCascadeOnDelete,
|
|
|
+ boolean ignoreFailure) throws SQLException {
|
|
|
|
|
|
addFKConstraint(tableName, constraintName, new String[]{keyColumn}, referenceTableName,
|
|
|
- new String[]{referenceColumn}, shouldCascadeOnDelete, ignoreFailure);
|
|
|
+ new String[]{referenceColumn}, shouldCascadeOnDelete, ignoreFailure);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void addFKConstraint(String tableName, String constraintName,
|
|
|
- String[] keyColumns, String referenceTableName,
|
|
|
- String[] referenceColumns,
|
|
|
- boolean ignoreFailure) throws SQLException {
|
|
|
+ String[] keyColumns, String referenceTableName,
|
|
|
+ String[] referenceColumns,
|
|
|
+ boolean ignoreFailure) throws SQLException {
|
|
|
addFKConstraint(tableName, constraintName, keyColumns, referenceTableName, referenceColumns, false, ignoreFailure);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void addFKConstraint(String tableName, String constraintName,
|
|
|
- String[] keyColumns, String referenceTableName,
|
|
|
- String[] referenceColumns, boolean shouldCascadeOnDelete,
|
|
|
- boolean ignoreFailure) throws SQLException {
|
|
|
+ String[] keyColumns, String referenceTableName,
|
|
|
+ String[] referenceColumns, boolean shouldCascadeOnDelete,
|
|
|
+ boolean ignoreFailure) throws SQLException {
|
|
|
if (!tableHasForeignKey(tableName, referenceTableName, keyColumns, referenceColumns)) {
|
|
|
String query = dbmsHelper.getAddForeignKeyStatement(tableName, constraintName,
|
|
|
- Arrays.asList(keyColumns),
|
|
|
- referenceTableName,
|
|
|
- Arrays.asList(referenceColumns),
|
|
|
- shouldCascadeOnDelete);
|
|
|
+ Arrays.asList(keyColumns),
|
|
|
+ referenceTableName,
|
|
|
+ Arrays.asList(referenceColumns),
|
|
|
+ shouldCascadeOnDelete);
|
|
|
|
|
|
try {
|
|
|
executeQuery(query, ignoreFailure);
|
|
|
} catch (SQLException e) {
|
|
|
- LOG.warn("Add FK constraint failed"
|
|
|
- + ", constraintName = " + constraintName
|
|
|
- + ", tableName = " + tableName, e.getMessage());
|
|
|
+ LOG.warn("Add FK constraint failed" +
|
|
|
+ ", constraintName = " + constraintName +
|
|
|
+ ", tableName = " + tableName, e.getMessage());
|
|
|
if (!ignoreFailure) {
|
|
|
throw e;
|
|
|
}
|
|
@@ -411,13 +414,13 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public boolean tableHasConstraint(String tableName, String constraintName) throws SQLException {
|
|
|
+ public boolean tableHasConstraint(String tableName, String constraintName) throws SQLException{
|
|
|
// this kind of request is well lower level as we querying system tables, due that we need for some the name of catalog.
|
|
|
String query = dbmsHelper.getTableConstraintsStatement(connection.getCatalog(), tableName);
|
|
|
ResultSet rs = executeSelect(query);
|
|
|
- if (rs != null) {
|
|
|
+ if (rs != null){
|
|
|
while (rs.next()) {
|
|
|
- if (rs.getString("CONSTRAINT_NAME").equalsIgnoreCase(constraintName)) {
|
|
|
+ if (rs.getString("CONSTRAINT_NAME").equalsIgnoreCase(constraintName)){
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
@@ -427,7 +430,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
@Override
|
|
|
public void addUniqueConstraint(String tableName, String constraintName, String... columnNames)
|
|
|
- throws SQLException {
|
|
|
+ throws SQLException{
|
|
|
if (!tableHasConstraint(tableName, constraintName)) {
|
|
|
String query = dbmsHelper.getAddUniqueConstraintStatement(tableName, constraintName, columnNames);
|
|
|
try {
|
|
@@ -442,25 +445,25 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void addPKConstraint(String tableName, String constraintName, boolean ignoreErrors, String... columnName) throws SQLException {
|
|
|
+ public void addPKConstraint(String tableName, String constraintName, boolean ignoreErrors, String... columnName) throws SQLException{
|
|
|
if (!tableHasPrimaryKey(tableName, null) && tableHasColumn(tableName, columnName)) {
|
|
|
String query = dbmsHelper.getAddPrimaryKeyConstraintStatement(tableName, constraintName, columnName);
|
|
|
|
|
|
executeQuery(query, ignoreErrors);
|
|
|
} else {
|
|
|
LOG.warn("Primary constraint {} not altered to table {} as column {} not present or constraint already exists",
|
|
|
- constraintName, tableName, columnName);
|
|
|
+ constraintName, tableName, columnName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void addPKConstraint(String tableName, String constraintName, String... columnName) throws SQLException {
|
|
|
+ public void addPKConstraint(String tableName, String constraintName, String... columnName) throws SQLException{
|
|
|
addPKConstraint(tableName, constraintName, false, columnName);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void renameColumn(String tableName, String oldColumnName,
|
|
|
- DBColumnInfo columnInfo) throws SQLException {
|
|
|
+ DBColumnInfo columnInfo) throws SQLException {
|
|
|
//it is mandatory to specify type in column change clause for mysql
|
|
|
String renameColumnStatement = dbmsHelper.getRenameColumnStatement(tableName, oldColumnName, columnInfo);
|
|
|
executeQuery(renameColumnStatement);
|
|
@@ -485,7 +488,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
@Override
|
|
|
public void alterColumn(String tableName, DBColumnInfo columnInfo)
|
|
|
- throws SQLException {
|
|
|
+ throws SQLException {
|
|
|
//varchar extension only (derby limitation, but not too much for others),
|
|
|
if (dbmsHelper.supportsColumnTypeChange()) {
|
|
|
String statement = dbmsHelper.getAlterColumnStatement(tableName,
|
|
@@ -494,9 +497,9 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
} else {
|
|
|
//use addColumn: add_tmp-update-drop-rename for Derby
|
|
|
DBColumnInfo columnInfoTmp = new DBColumnInfo(
|
|
|
- columnInfo.getName() + "_TMP",
|
|
|
- columnInfo.getType(),
|
|
|
- columnInfo.getLength());
|
|
|
+ columnInfo.getName() + "_TMP",
|
|
|
+ columnInfo.getType(),
|
|
|
+ columnInfo.getLength());
|
|
|
String statement = dbmsHelper.getAddColumnStatement(tableName, columnInfoTmp);
|
|
|
executeQuery(statement);
|
|
|
updateTable(tableName, columnInfo, columnInfoTmp);
|
|
@@ -507,43 +510,32 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
@Override
|
|
|
public void updateTable(String tableName, DBColumnInfo columnNameFrom,
|
|
|
- DBColumnInfo columnNameTo) throws SQLException {
|
|
|
+ DBColumnInfo columnNameTo) throws SQLException {
|
|
|
LOG.info("Executing query: UPDATE TABLE " + tableName + " SET "
|
|
|
- + columnNameTo.getName() + "=" + columnNameFrom.getName());
|
|
|
+ + columnNameTo.getName() + "=" + columnNameFrom.getName());
|
|
|
|
|
|
String statement = "SELECT * FROM " + tableName;
|
|
|
int typeFrom = getColumnType(tableName, columnNameFrom.getName());
|
|
|
int typeTo = getColumnType(tableName, columnNameTo.getName());
|
|
|
- Statement dbStatement = null;
|
|
|
- ResultSet rs = null;
|
|
|
- try {
|
|
|
- dbStatement = getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
|
|
|
- ResultSet.CONCUR_UPDATABLE);
|
|
|
- rs = dbStatement.executeQuery(statement);
|
|
|
+ ResultSet rs = executeSelect(statement, ResultSet.TYPE_SCROLL_SENSITIVE,
|
|
|
+ ResultSet.CONCUR_UPDATABLE);
|
|
|
|
|
|
while (rs.next()) {
|
|
|
convertUpdateData(rs, columnNameFrom, typeFrom, columnNameTo, typeTo);
|
|
|
rs.updateRow();
|
|
|
}
|
|
|
- } finally {
|
|
|
- if (rs != null) {
|
|
|
- rs.close();
|
|
|
- }
|
|
|
- if (dbStatement != null) {
|
|
|
- dbStatement.close();
|
|
|
- }
|
|
|
- }
|
|
|
+ rs.close();
|
|
|
}
|
|
|
|
|
|
private void convertUpdateData(ResultSet rs, DBColumnInfo columnNameFrom,
|
|
|
- int typeFrom,
|
|
|
- DBColumnInfo columnNameTo, int typeTo) throws SQLException {
|
|
|
+ int typeFrom,
|
|
|
+ DBColumnInfo columnNameTo, int typeTo) throws SQLException {
|
|
|
if (typeFrom == Types.BLOB && typeTo == Types.CLOB) {
|
|
|
//BLOB-->CLOB
|
|
|
Blob data = rs.getBlob(columnNameFrom.getName());
|
|
|
if (data != null) {
|
|
|
rs.updateClob(columnNameTo.getName(),
|
|
|
- new BufferedReader(new InputStreamReader(data.getBinaryStream(), Charset.defaultCharset())));
|
|
|
+ new BufferedReader(new InputStreamReader(data.getBinaryStream())));
|
|
|
}
|
|
|
} else {
|
|
|
Object data = rs.getObject(columnNameFrom.getName());
|
|
@@ -562,7 +554,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
for (int i = 0; i < columnNames.length; i++) {
|
|
|
builder.append(columnNames[i]);
|
|
|
- if (i != columnNames.length - 1) {
|
|
|
+ if(i!=columnNames.length-1){
|
|
|
builder.append(",");
|
|
|
}
|
|
|
}
|
|
@@ -571,7 +563,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
builder.append(values[i]);
|
|
|
- if (i != values.length - 1) {
|
|
|
+ if(i!=values.length-1){
|
|
|
builder.append(",");
|
|
|
}
|
|
|
}
|
|
@@ -588,20 +580,18 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
if (!ignoreFailure) {
|
|
|
throw e;
|
|
|
}
|
|
|
- } finally {
|
|
|
- if (statement != null) {
|
|
|
- statement.close();
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
return rowsUpdated != 0;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
public int updateTable(String tableName, String columnName, Object value,
|
|
|
- String whereClause) throws SQLException {
|
|
|
+ String whereClause) throws SQLException {
|
|
|
|
|
|
- StringBuilder query = new StringBuilder(String.format("UPDATE %s SET %s = ", tableName, columnName));
|
|
|
+ StringBuilder query = new StringBuilder
|
|
|
+ (String.format("UPDATE %s SET %s = ", tableName, columnName));
|
|
|
|
|
|
// Only String and number supported.
|
|
|
// Taken from: org.eclipse.persistence.internal.databaseaccess.appendParameterInternal
|
|
@@ -616,31 +606,24 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
query.append(whereClause);
|
|
|
|
|
|
Statement statement = getConnection().createStatement();
|
|
|
- int res = -1;
|
|
|
- try {
|
|
|
- res = statement.executeUpdate(query.toString());
|
|
|
- } finally {
|
|
|
- if (statement != null) {
|
|
|
- statement.close();
|
|
|
- }
|
|
|
- }
|
|
|
- return res;
|
|
|
+
|
|
|
+ return statement.executeUpdate(query.toString());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public int executeUpdate(String query) throws SQLException {
|
|
|
- return executeUpdate(query, false);
|
|
|
+ public int executeUpdate(String query) throws SQLException{
|
|
|
+ return executeUpdate(query, false);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public int executeUpdate(String query, boolean ignoreErrors) throws SQLException {
|
|
|
+ public int executeUpdate(String query, boolean ignoreErrors) throws SQLException{
|
|
|
Statement statement = getConnection().createStatement();
|
|
|
try {
|
|
|
return statement.executeUpdate(query);
|
|
|
- } catch (SQLException e) {
|
|
|
- LOG.warn("Error executing query: " + query + ", "
|
|
|
- + "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
|
|
|
- if (!ignoreErrors) {
|
|
|
+ } catch (SQLException e){
|
|
|
+ LOG.warn("Error executing query: " + query + ", " +
|
|
|
+ "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
|
|
|
+ if (!ignoreErrors){
|
|
|
throw e;
|
|
|
}
|
|
|
}
|
|
@@ -648,8 +631,8 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void executeQuery(String query, String tableName, String hasColumnName) throws SQLException {
|
|
|
- if (tableHasColumn(tableName, hasColumnName)) {
|
|
|
+ public void executeQuery(String query, String tableName, String hasColumnName) throws SQLException{
|
|
|
+ if (tableHasColumn(tableName, hasColumnName)){
|
|
|
executeQuery(query);
|
|
|
}
|
|
|
}
|
|
@@ -662,19 +645,9 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
@Override
|
|
|
public ResultSet executeSelect(String query) throws SQLException {
|
|
|
Statement statement = getConnection().createStatement();
|
|
|
- ResultSet rs = statement.executeQuery(query);
|
|
|
- statement.closeOnCompletion();
|
|
|
- return rs;
|
|
|
+ return statement.executeQuery(query);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public ResultSet executeSelect(String query, int resultSetType, int resultSetConcur) throws SQLException {
|
|
|
- Statement statement = getConnection().createStatement(resultSetType, resultSetConcur);
|
|
|
- ResultSet rs = statement.executeQuery(query);
|
|
|
- statement.closeOnCompletion();
|
|
|
- return rs;
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void executeQuery(String query, boolean ignoreFailure) throws SQLException {
|
|
|
LOG.info("Executing query: {}", query);
|
|
@@ -686,12 +659,8 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
LOG.error("Error executing query: " + query, e);
|
|
|
throw e;
|
|
|
} else {
|
|
|
- LOG.warn("Error executing query: " + query + ", "
|
|
|
- + "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
|
|
|
- }
|
|
|
- } finally {
|
|
|
- if (statement != null) {
|
|
|
- statement.close();
|
|
|
+ LOG.warn("Error executing query: " + query + ", " +
|
|
|
+ "errorCode = " + e.getErrorCode() + ", message = " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -702,8 +671,12 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
executeQuery(query);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@Override
|
|
|
+ public ResultSet executeSelect(String query, int resultSetType, int resultSetConcur) throws SQLException {
|
|
|
+ Statement statement = getConnection().createStatement(resultSetType, resultSetConcur);
|
|
|
+ return statement.executeQuery(query);
|
|
|
+ }
|
|
|
+
|
|
|
public void truncateTable(String tableName) throws SQLException {
|
|
|
String query = "DELETE FROM " + tableName;
|
|
|
executeQuery(query);
|
|
@@ -739,8 +712,8 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void dropUniqueConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException {
|
|
|
- if (tableHasConstraint(convertObjectName(tableName), convertObjectName(constraintName))) {
|
|
|
+ public void dropUniqueConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException{
|
|
|
+ if (tableHasConstraint(convertObjectName(tableName), convertObjectName(constraintName))){
|
|
|
String query = dbmsHelper.getDropUniqueConstraintStatement(tableName, constraintName);
|
|
|
executeQuery(query, ignoreFailure);
|
|
|
} else {
|
|
@@ -749,22 +722,22 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void dropUniqueConstraint(String tableName, String constraintName) throws SQLException {
|
|
|
+ public void dropUniqueConstraint(String tableName, String constraintName) throws SQLException{
|
|
|
dropUniqueConstraint(tableName, constraintName, false);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void dropPKConstraint(String tableName, String constraintName, String columnName) throws SQLException {
|
|
|
- if (tableHasPrimaryKey(tableName, columnName)) {
|
|
|
- String query = dbmsHelper.getDropPrimaryKeyStatement(convertObjectName(tableName), constraintName);
|
|
|
- executeQuery(query, false);
|
|
|
- } else {
|
|
|
- LOG.warn("Primary key doesn't exists for {} table, skipping", tableName);
|
|
|
+ public void dropPKConstraint(String tableName, String constraintName, String columnName) throws SQLException{
|
|
|
+ if (tableHasPrimaryKey(tableName, columnName)){
|
|
|
+ String query = dbmsHelper.getDropPrimaryKeyStatement(convertObjectName(tableName), constraintName);
|
|
|
+ executeQuery(query, false);
|
|
|
+ } else{
|
|
|
+ LOG.warn("Primary key doesn't exists for {} table, skipping", tableName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void dropPKConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException {
|
|
|
+ public void dropPKConstraint(String tableName, String constraintName, boolean ignoreFailure) throws SQLException{
|
|
|
/*
|
|
|
* Note, this is un-safe implementation as constraint name checking will work only for PostgresSQL,
|
|
|
* MySQL and Oracle doesn't use constraint name for drop primary key
|
|
@@ -773,31 +746,24 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
if (tableHasPrimaryKey(tableName, null)) {
|
|
|
String query = dbmsHelper.getDropPrimaryKeyStatement(convertObjectName(tableName), constraintName);
|
|
|
executeQuery(query, ignoreFailure);
|
|
|
- } else {
|
|
|
+ } else{
|
|
|
LOG.warn("Primary key doesn't exists for {} table, skipping", tableName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void dropPKConstraint(String tableName, String constraintName) throws SQLException {
|
|
|
+ public void dropPKConstraint(String tableName, String constraintName) throws SQLException{
|
|
|
dropPKConstraint(tableName, constraintName, false);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
/**
|
|
|
- * Execute script with autocommit and error tolerance, like psql and sqlplus
|
|
|
- * do by default
|
|
|
+ * Execute script with autocommit and error tolerance, like psql and sqlplus do by default
|
|
|
*/
|
|
|
public void executeScript(String filePath) throws SQLException, IOException {
|
|
|
BufferedReader br = new BufferedReader(new FileReader(filePath));
|
|
|
- try {
|
|
|
- ScriptRunner scriptRunner = new ScriptRunner(getConnection(), false, false);
|
|
|
- scriptRunner.runScript(br);
|
|
|
- } finally {
|
|
|
- if (br != null) {
|
|
|
- br.close();
|
|
|
- }
|
|
|
- }
|
|
|
+ ScriptRunner scriptRunner = new ScriptRunner(getConnection(), false, false);
|
|
|
+ scriptRunner.runScript(br);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -813,55 +779,31 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean tableHasPrimaryKey(String tableName, String columnName) throws SQLException {
|
|
|
+ public boolean tableHasPrimaryKey(String tableName, String columnName) throws SQLException{
|
|
|
ResultSet rs = getDatabaseMetaData().getPrimaryKeys(null, null, convertObjectName(tableName));
|
|
|
- boolean res = false;
|
|
|
- try {
|
|
|
- if (rs != null && columnName != null) {
|
|
|
- while (rs.next()) {
|
|
|
- if (rs.getString("COLUMN_NAME").equalsIgnoreCase(columnName)) {
|
|
|
- res = true;
|
|
|
- break;
|
|
|
- }
|
|
|
+ if (rs != null && columnName != null){
|
|
|
+ while (rs.next()){
|
|
|
+ if (rs.getString("COLUMN_NAME").equalsIgnoreCase(columnName)) {
|
|
|
+ return true;
|
|
|
}
|
|
|
- } else if (rs != null) {
|
|
|
- res = rs.next();
|
|
|
- }
|
|
|
- } finally {
|
|
|
- if (rs != null) {
|
|
|
- rs.close();
|
|
|
}
|
|
|
+ } else if (rs != null){
|
|
|
+ return rs.next();
|
|
|
}
|
|
|
- return res;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getColumnType(String tableName, String columnName)
|
|
|
- throws SQLException {
|
|
|
+ throws SQLException {
|
|
|
// We doesn't require any actual result except metadata, so WHERE clause shouldn't match
|
|
|
- int res;
|
|
|
- String query;
|
|
|
- Statement statement = null;
|
|
|
- ResultSet rs = null;
|
|
|
- ResultSetMetaData rsmd = null;
|
|
|
- try {
|
|
|
- query = String.format("SELECT %s FROM %s WHERE 1=2", columnName, convertObjectName(tableName));
|
|
|
- statement = getConnection().createStatement();
|
|
|
- rs = statement.executeQuery(query);
|
|
|
- rsmd = rs.getMetaData();
|
|
|
- res = rsmd.getColumnType(1);
|
|
|
- } finally {
|
|
|
- if (rs != null){
|
|
|
- rs.close();
|
|
|
- }
|
|
|
- if (statement != null) {
|
|
|
- statement.close();
|
|
|
- }
|
|
|
- }
|
|
|
- return res;
|
|
|
+ String query = String.format("SELECT %s FROM %s WHERE 1=2", columnName, convertObjectName(tableName));
|
|
|
+ ResultSet rs = executeSelect(query);
|
|
|
+ ResultSetMetaData rsmd = rs.getMetaData();
|
|
|
+ return rsmd.getColumnType(1);
|
|
|
}
|
|
|
|
|
|
- private ResultSetMetaData getColumnMetadata(String tableName, String columnName) throws SQLException {
|
|
|
+ private ResultSetMetaData getColumnMetadata(String tableName, String columnName) throws SQLException{
|
|
|
// We doesn't require any actual result except metadata, so WHERE clause shouldn't match
|
|
|
String query = String.format("SELECT %s FROM %s WHERE 1=2", convertObjectName(columnName), convertObjectName(tableName));
|
|
|
ResultSet rs = executeSelect(query);
|
|
@@ -870,20 +812,20 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
|
|
|
@Override
|
|
|
public Class getColumnClass(String tableName, String columnName)
|
|
|
- throws SQLException, ClassNotFoundException {
|
|
|
- ResultSetMetaData rsmd = getColumnMetadata(tableName, columnName);
|
|
|
- return Class.forName(rsmd.getColumnClassName(1));
|
|
|
+ throws SQLException, ClassNotFoundException{
|
|
|
+ ResultSetMetaData rsmd = getColumnMetadata(tableName, columnName);
|
|
|
+ return Class.forName(rsmd.getColumnClassName(1));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean isColumnNullable(String tableName, String columnName) throws SQLException {
|
|
|
+ public boolean isColumnNullable(String tableName, String columnName) throws SQLException{
|
|
|
ResultSetMetaData rsmd = getColumnMetadata(tableName, columnName);
|
|
|
return !(rsmd.isNullable(1) == ResultSetMetaData.columnNoNulls);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setColumnNullable(String tableName, DBAccessor.DBColumnInfo columnInfo, boolean nullable)
|
|
|
- throws SQLException {
|
|
|
+ throws SQLException {
|
|
|
|
|
|
String statement = dbmsHelper.getSetNullableStatement(tableName, columnInfo, nullable);
|
|
|
executeQuery(statement);
|
|
@@ -900,7 +842,7 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
executeQuery(query);
|
|
|
} else {
|
|
|
LOG.info("Column nullability property is not changed due to {} column from {} table is already in {} state, skipping",
|
|
|
- columnName, tableName, (nullable) ? "nullable" : "not nullable");
|
|
|
+ columnName, tableName, (nullable)?"nullable":"not nullable");
|
|
|
}
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
LOG.error("Could not modify table=[], column={}, error={}", tableName, columnName, e.getMessage());
|
|
@@ -912,22 +854,27 @@ public class DBAccessorImpl implements DBAccessor {
|
|
|
// ToDo: create column with more random name
|
|
|
String tempColumnName = columnName + "_temp";
|
|
|
|
|
|
- switch (configuration.getDatabaseType()) {
|
|
|
+ switch (configuration.getDatabaseType()){
|
|
|
case ORACLE:
|
|
|
- if (String.class.equals(fromType)
|
|
|
- && (toType.equals(Character[].class))
|
|
|
- || toType.equals(char[].class)) {
|
|
|
+ // ToDo: add check, if target column is a part of constraint.
|
|
|
+ // oracle doesn't support direct type change from varchar2 -> clob
|
|
|
+ if (String.class.equals(fromType) && (Character[].class.equals(toType) || char[].class.equals(toType))){
|
|
|
addColumn(tableName, new DBColumnInfo(tempColumnName, toType));
|
|
|
executeUpdate(String.format("UPDATE %s SET %s = %s", convertObjectName(tableName),
|
|
|
- convertObjectName(tempColumnName), convertObjectName(columnName)));
|
|
|
+ convertObjectName(tempColumnName), convertObjectName(columnName)));
|
|
|
dropColumn(tableName, columnName);
|
|
|
- renameColumn(tableName, tempColumnName, new DBColumnInfo(columnName, toType));
|
|
|
+ renameColumn(tableName,tempColumnName, new DBColumnInfo(columnName, toType));
|
|
|
return;
|
|
|
}
|
|
|
- break;
|
|
|
}
|
|
|
|
|
|
alterColumn(tableName, new DBColumnInfo(columnName, toType, null));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|