-
Notifications
You must be signed in to change notification settings - Fork 8
[WIP]: Database Templates #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nbesimi
wants to merge
1
commit into
develop
Choose a base branch
from
feature/database-templates
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package com.adaptivescale.rosetta.common; | ||
|
|
||
| import com.adaptivescale.rosetta.common.models.DatabaseTemplateModel; | ||
| import com.adaptivescale.rosetta.common.models.TranslationAttributeModel; | ||
| import com.adaptivescale.rosetta.common.models.TranslationModel; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
@@ -20,6 +21,11 @@ public class TranslationMatrix { | |
| private static final String TRANSLATION_TABLE_NAME = "TRANSLATION"; | ||
| private static final String TRANSLATION_ATTRIBUTE_TABLE_NAME = "TRANSLATION_ATTRIBUTE"; | ||
|
|
||
| private static final String DATABASE_TEMPLATE_TABLE_NAME = "DATABASE_TEMPLATE"; | ||
|
|
||
| private static final String DEFAULT_DATABASE_TEMPLATE_FILE = "translation_matrix/database_template.csv"; | ||
|
|
||
|
|
||
| private static TranslationMatrix instance = null; | ||
|
|
||
| public TranslationMatrix() { | ||
|
|
@@ -58,6 +64,14 @@ void initTables() throws IOException { | |
| "ON "+ TRANSLATION_TABLE_NAME +" (source_type, source_column_type, target_type)"; | ||
| execute(index); | ||
| execute(uniqueIndex); | ||
|
|
||
| String databaseTemplateTable = "CREATE TABLE "+ DATABASE_TEMPLATE_TABLE_NAME +"(id INT PRIMARY KEY AUTO_INCREMENT, " + | ||
| "database_type VARCHAR(255) not null, " + | ||
| "template_name VARCHAR(255) not null, " + | ||
| "template_file VARCHAR(255) not null " + | ||
| ");"; | ||
| execute(databaseTemplateTable); | ||
|
|
||
| loadCSVData(); | ||
| } | ||
|
|
||
|
|
@@ -66,7 +80,9 @@ void loadCSVData() throws IOException { | |
|
|
||
| StringBuilder dataInsertQuery = new StringBuilder(); | ||
| StringBuilder attributesInsertQuery = new StringBuilder(); | ||
| StringBuilder databaseTemplateInsertQuery = new StringBuilder(); | ||
|
|
||
| readDatabaseTemplate(databaseTemplateInsertQuery); | ||
| Map<Integer, List<TranslationAttributeModel>> translationAttributesMappedByTranslationId = readTranslationAttributes(attributesInsertQuery); | ||
| BufferedReader br = readTranslationMatrixFile(); | ||
|
|
||
|
|
@@ -86,6 +102,7 @@ void loadCSVData() throws IOException { | |
| } | ||
| execute(dataInsertQuery.toString()); | ||
| execute(attributesInsertQuery.toString()); | ||
| execute(databaseTemplateInsertQuery.toString()); | ||
| } | ||
|
|
||
| void execute(String sql) { | ||
|
|
@@ -144,6 +161,36 @@ private List<TranslationAttributeModel> getTranslationAttributeRecords(String qu | |
| } | ||
| } | ||
|
|
||
| private Map<String, Map<String, String>> getDatabaseTemplates(String query) { | ||
| try { | ||
| Connection connection = DriverManager.getConnection(URL); | ||
| Statement statement = connection.createStatement(); | ||
| ResultSet resultSet = statement.executeQuery(query); | ||
| Map<String, Map<String, String>> result = new HashMap<>(); | ||
|
|
||
| while (resultSet.next()) { | ||
| DatabaseTemplateModel databaseTemplateModel = new DatabaseTemplateModel(); | ||
| databaseTemplateModel.setId(resultSet.getInt("id")); | ||
| databaseTemplateModel.setDatabaseType(resultSet.getString("database_type")); | ||
| databaseTemplateModel.setTemplateName(resultSet.getString("template_name")); | ||
| databaseTemplateModel.setTemplateFile(resultSet.getString("template_file")); | ||
|
|
||
| if (!result.containsKey(databaseTemplateModel.getDatabaseType())) { | ||
| result.put(databaseTemplateModel.getDatabaseType(), new HashMap<>()); | ||
| } | ||
|
|
||
| result.get(databaseTemplateModel.getDatabaseType()) | ||
| .put(databaseTemplateModel.getTemplateName(), databaseTemplateModel.getTemplateFile()); | ||
| } | ||
|
|
||
| connection.close(); | ||
|
|
||
| return result; | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public TranslationModel findById(Integer id) { | ||
| String query = String.format("SELECT TOP 1 * from %s where id=%s", TRANSLATION_TABLE_NAME, id); | ||
| return getSingleRecord(query); | ||
|
|
@@ -167,6 +214,13 @@ public List<TranslationAttributeModel> findByTranslationAttributesByTranslationI | |
| return getTranslationAttributeRecords(query); | ||
| } | ||
|
|
||
| public Map<String, Map<String, String>> findDatabaseTemplates() { | ||
| String query = String.format("SELECT * from %s", | ||
| DATABASE_TEMPLATE_TABLE_NAME); | ||
|
|
||
| return getDatabaseTemplates(query); | ||
| } | ||
|
|
||
| private Map<Integer, List<TranslationAttributeModel>> readTranslationAttributes(StringBuilder attributesInsertQuery) throws IOException { | ||
| Map<Integer, List<TranslationAttributeModel>> translationAttributesMappedByTranslationId = new HashMap<>(); | ||
| BufferedReader br = readTranslationAttributesFile(); | ||
|
|
@@ -190,6 +244,23 @@ private Map<Integer, List<TranslationAttributeModel>> readTranslationAttributes( | |
| return translationAttributesMappedByTranslationId; | ||
| } | ||
|
|
||
| private void readDatabaseTemplate(StringBuilder databaseTemplateInsertQuery) throws IOException { | ||
| BufferedReader br = readDatabaseTemplateFile(); | ||
| String line = ""; | ||
| while ((line = br.readLine()) != null) | ||
| { | ||
| String[] translation = line.split(DELIMITER); | ||
| DatabaseTemplateModel databaseTemplateModel = new DatabaseTemplateModel(); | ||
| databaseTemplateModel.setId(Integer.valueOf(translation[0])); | ||
| databaseTemplateModel.setDatabaseType(translation[1]); | ||
| databaseTemplateModel.setTemplateName(translation[2]); | ||
| databaseTemplateModel.setTemplateFile(translation[3]); | ||
|
|
||
| String insertStatement = databaseTemplateModel.generateInsertStatement(DATABASE_TEMPLATE_TABLE_NAME); | ||
| databaseTemplateInsertQuery.append(insertStatement); | ||
| } | ||
| } | ||
|
|
||
| private BufferedReader readTranslationMatrixFile() throws FileNotFoundException { | ||
| //Check for the translation file from the ENV variable EXTERNAL_TRANSLATION_FILE | ||
| String externalTranslationFile = System.getenv(EXTERNAL_TRANSLATION_FILE_ENV); | ||
|
|
@@ -217,4 +288,10 @@ private BufferedReader readTranslationAttributesFile() throws FileNotFoundExcept | |
| InputStream resourceAsStream = TranslationMatrix.class.getClassLoader().getResourceAsStream(DEFAULT_TRANSLATION_ATTRIBUTE_FILE); | ||
| return new BufferedReader(new InputStreamReader(resourceAsStream)); | ||
| } | ||
|
|
||
| private BufferedReader readDatabaseTemplateFile() throws FileNotFoundException { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ailegion for this I did not add an ENV variable. Should we allow users to override the database template file? |
||
| //If the file is not provided in the project read it from the resources | ||
| InputStream resourceAsStream = TranslationMatrix.class.getClassLoader().getResourceAsStream(DEFAULT_DATABASE_TEMPLATE_FILE); | ||
| return new BufferedReader(new InputStreamReader(resourceAsStream)); | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
common/src/main/java/com/adaptivescale/rosetta/common/models/DatabaseTemplateModel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.adaptivescale.rosetta.common.models; | ||
|
|
||
| public class DatabaseTemplateModel { | ||
|
|
||
| private Integer id; | ||
|
|
||
| private String databaseType; | ||
|
|
||
| private String templateName; | ||
|
|
||
| private String templateFile; | ||
|
|
||
| public Integer getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Integer id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getDatabaseType() { | ||
| return databaseType; | ||
| } | ||
|
|
||
| public void setDatabaseType(String databaseType) { | ||
| this.databaseType = databaseType; | ||
| } | ||
|
|
||
| public String getTemplateName() { | ||
| return templateName; | ||
| } | ||
|
|
||
| public void setTemplateName(String templateName) { | ||
| this.templateName = templateName; | ||
| } | ||
|
|
||
| public String getTemplateFile() { | ||
| return templateFile; | ||
| } | ||
|
|
||
| public void setTemplateFile(String templateFile) { | ||
| this.templateFile = templateFile; | ||
| } | ||
|
|
||
| public String generateInsertStatement(String tableName) { | ||
| StringBuilder builder = new StringBuilder(); | ||
| builder.append("insert into ").append(tableName) | ||
| .append(" values (") | ||
| .append(id) | ||
| .append(", '").append(databaseType).append("' ") | ||
| .append(", '").append(templateName).append("' ") | ||
| .append(", '").append(templateFile).append("' ") | ||
| .append(");"); | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "DatabaseTemplateModel{" + | ||
| "id=" + id + | ||
| ", databaseType='" + databaseType + '\'' + | ||
| ", templateName='" + templateName + '\'' + | ||
| ", templateFile='" + templateFile + '\'' + | ||
| '}'; | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
ddl/src/main/java/com/adaptivescale/rosetta/ddl/DatabaseTemplateEnum.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.adaptivescale.rosetta.ddl; | ||
|
|
||
| public enum DatabaseTemplateEnum { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| SCHEMA_CREATE("schema_create"), | ||
| TABLE_CREATE("table_create"), | ||
| TABLE_ALTER("table_alter"), | ||
| TABLE_ALTER_DROP_PRIMARY_KEY("table_alter_drop_primary_key"), | ||
| TABLE_ALTER_ADD_PRIMARY_KEY("table_alter_add_primary_key"), | ||
| TABLE_DROP("table_drop"), | ||
| FOREIGNKEY_CREATE("foreignkey_create"), | ||
| FOREIGNKEY_DROP("foreignkey_drop"), | ||
| COLUMN_ADD("column_add"), | ||
| COLUMN_ALTER_TYPE("column_alter_type"), | ||
| COLUMN_ALTER_NULL("column_alter_null"), | ||
| COLUMN_DROP("column_drop"); | ||
|
|
||
| private String name; | ||
|
|
||
| DatabaseTemplateEnum(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ailegion @jetroni we can put this implementation in a new class or rename the TranslationMatrix to a something more general.