Skip to content

Commit

Permalink
fix(database): Fixed column type check and handling for SQLite
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed May 29, 2023
1 parent 2dc1e91 commit b90cc72
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.georgev22.library.utilities.Utils.Assertions.notNull;

Expand Down Expand Up @@ -228,8 +230,8 @@ public int updateSQL(String query) throws SQLException, ClassNotFoundException {
* @throws SQLException if a database access error occurs
*/
public void checkColumn(@NotNull String tableName, @NotNull String column, @NotNull String type) throws SQLException, ClassNotFoundException {
ResultSet resultSet = querySQL("SELECT * FROM `" + notNull("tableName", tableName) + "`;");
ResultSetMetaData metaData = resultSet.getMetaData();
ResultSet set = querySQL("SELECT * FROM `" + notNull("tableName", tableName) + "`;");
ResultSetMetaData metaData = set.getMetaData();
int rowCount = metaData.getColumnCount();

boolean isMyColumnPresent = false;
Expand All @@ -238,7 +240,7 @@ public void checkColumn(@NotNull String tableName, @NotNull String column, @NotN
if (notNull("column", column).equals(metaData.getColumnName(i))) {
isMyColumnPresent = true;
}
if (type.equals(metaData.getColumnTypeName(i))) {
if (type.replaceAll("\\(.*\\)", "").equals(metaData.getColumnTypeName(i))) {
isMyColumnTypeCorrect = true;
}
}
Expand All @@ -247,7 +249,35 @@ public void checkColumn(@NotNull String tableName, @NotNull String column, @NotN
updateSQL("ALTER TABLE `" + notNull("tableName", tableName) + "` ADD COLUMN `" + column + "` " + notNull("type", type) + ";");
} else {
if (!isMyColumnTypeCorrect) {
updateSQL("ALTER TABLE `" + notNull("tableName", tableName) + "` MODIFY COLUMN `" + column + "` " + notNull("type", type) + ";");
if (this instanceof SQLite) {
String getTableQuery = "SELECT sql FROM sqlite_master WHERE type='table' AND name='" + tableName + "'";
ResultSet resultSet = querySQL(getTableQuery);
String tableCreationQuery = resultSet.getString("sql");

String oldColumnType = null;
Pattern pattern = Pattern.compile(column + "\\s+(\\w+)");
Matcher matcher = pattern.matcher(tableCreationQuery);
if (matcher.find()) {
oldColumnType = matcher.group(1);
}

if (oldColumnType != null) {
String modifiedTableCreationQuery = tableCreationQuery.replace(column + " " + oldColumnType, column + " " + type);

String renameTableQuery = "ALTER TABLE " + tableName + " RENAME TO temp_" + tableName;
updateSQL(renameTableQuery);

updateSQL(modifiedTableCreationQuery);

String migrateDataQuery = "INSERT INTO " + tableName + " SELECT * FROM temp_" + tableName;
updateSQL(migrateDataQuery);

String dropTempTableQuery = "DROP TABLE temp_" + tableName;
updateSQL(dropTempTableQuery);
}
} else {
updateSQL("ALTER TABLE `" + notNull("tableName", tableName) + "` MODIFY COLUMN `" + column + "` " + notNull("type", type) + ";");
}
}
}
}
Expand Down

0 comments on commit b90cc72

Please sign in to comment.