Skip to content

Commit

Permalink
Bugfixes in dna_recastVariable, dna_renameVariable, and dna_removeVar…
Browse files Browse the repository at this point in the history
…iable
  • Loading branch information
Philip Leifeld committed Feb 4, 2019
1 parent 36e1968 commit 55232fd
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
23 changes: 14 additions & 9 deletions DNA/src/dna/SqlConnection.java
Expand Up @@ -1299,16 +1299,21 @@ public void recastVariable(int statementTypeId, String variable) throws Exceptio
throw new Exception("Data type in database for variable '" + variable + "' was not recognized.");
}

// copy data from sourceTable to targetTable (and look up Variable ID),
// then delete in sourceTable,
// then change variable data type in VARIABLES
String string = "INSERT INTO " + targetTable + " (StatementID, VariableID, StatementTypeId, Value) VALUES "
+ "(SELECT StatementID, VariableID, StatementTypeId, Value FROM " + sourceTable + " WHERE "
// copy data from sourceTable to targetTable (and look up Variable ID)
String string = "INSERT INTO " + targetTable + " (StatementID, VariableID, StatementTypeId, Value) "
+ "SELECT StatementId, VariableId, StatementTypeId, Value FROM " + sourceTable + " WHERE "
+ "StatementTypeId = " + statementTypeId + " AND VariableID = (SELECT ID FROM VARIABLES WHERE StatementTypeId = "
+ statementTypeId + " AND Variable = '" + variable + "'));"
+ "DELETE FROM " + sourceTable + " * WHERE StatementTypeId = " + statementTypeId + " AND Variable = '" + variable + "';"
+ "UPDATE VARIABLES SET DataType = '" + newDataType + "' WHERE StatementTypeId = " + statementTypeId
+ "AND Variable = '" + variable + "';";
+ statementTypeId + " AND Variable = '" + variable + "');";
executeStatement(string);

// delete in sourceTable
string = " DELETE FROM " + sourceTable + " WHERE StatementTypeId = " + statementTypeId
+ " AND VariableId = (SELECT ID FROM VARIABLES WHERE StatementTypeId = " + statementTypeId + " AND Variable = '" + variable + "');";
executeStatement(string);

// change variable data type in VARIABLES
string = " UPDATE VARIABLES SET DataType = '" + newDataType + "' WHERE StatementTypeId = " + statementTypeId
+ " AND Variable = '" + variable + "';";
executeStatement(string);
}

Expand Down
37 changes: 26 additions & 11 deletions DNA/src/dna/export/ExporterR.java
Expand Up @@ -2566,6 +2566,16 @@ public void removeVariable(int statementTypeId, String variable, boolean simulat
if (!this.data.getStatementTypeById(statementTypeId).getVariables().containsKey(variable)) {
throw new Exception("Variable '" + variable + "' does not exist in statement type " + statementTypeId + ".");
}

// report simulation mode
if (verbose == true) {
if (simulate == true) {
System.out.println("Simulation mode: no actual changes are made to the database!");
} else {
System.out.println("Changes will be written both in memory and to the SQL database!");
}
}

int removeFromStatementCounter = 0;
int removeAttributeCounter = 0;
for (int i = this.data.getAttributes().size() - 1; i > -1 ; i--) {
Expand All @@ -2576,14 +2586,17 @@ public void removeVariable(int statementTypeId, String variable, boolean simulat
removeAttributeCounter++;
}
}
for (int i = 0; i < this.data.getStatements().size(); i++) {
if (this.data.getStatements().get(i).getStatementTypeId() == statementTypeId) {
if (simulate == false) {
this.data.getStatements().get(i).getValues().remove(variable);
if (this.data.getStatements().size() > 0) {
for (int i = 0; i < this.data.getStatements().size(); i++) {
if (this.data.getStatements().get(i).getStatementTypeId() == statementTypeId) {
if (simulate == false) {
this.data.getStatements().get(i).getValues().remove(variable);
}
removeFromStatementCounter++;
}
removeFromStatementCounter++;
}
}

if (simulate == false) {
this.data.getStatementTypeById(statementTypeId).getVariables().remove(variable);
this.sql.removeVariable(statementTypeId, variable);
Expand Down Expand Up @@ -2652,13 +2665,15 @@ public void renameVariable(int statementTypeId, String variable, String newLabel

// update statements
int updateStatementCounter = 0;
for (int i = 0; i < this.data.getStatements().size(); i++) {
if (this.data.getStatements().get(i).getStatementTypeId() == statementTypeId) {
if (simulate == false) {
this.data.getStatements().get(i).getValues().put(newLabel, this.data.getStatements().get(i).getValues().get(variable));
this.data.getStatements().get(i).getValues().remove(variable);
if (this.data.getStatements().size() > 0) {
for (int i = 0; i < this.data.getStatements().size(); i++) {
if (this.data.getStatements().get(i).getStatementTypeId() == statementTypeId) {
if (simulate == false) {
this.data.getStatements().get(i).getValues().put(newLabel, this.data.getStatements().get(i).getValues().get(variable));
this.data.getStatements().get(i).getValues().remove(variable);
}
updateStatementCounter++;
}
updateStatementCounter++;
}
}

Expand Down
4 changes: 2 additions & 2 deletions rDNA/DESCRIPTION
@@ -1,6 +1,6 @@
Package: rDNA
Version: 2.1.12
Date: 2019-01-27
Version: 2.1.13
Date: 2019-02-04
Title: Discourse Network Analysis in R
Authors@R:
c(person("Philip", "Leifeld", email = "Philip.Leifeld@glasgow.ac.uk",
Expand Down
10 changes: 8 additions & 2 deletions rDNA/R/rDNA.R
Expand Up @@ -1459,7 +1459,7 @@ dna_renameStatementType <- function(connection, statementType, label) {
#' \code{"actor" or "intensity"}. The label must not contain spaces.
#'
#' @export
dna_renameVariable <- function(connection, statementType, variable, label) {
dna_renameVariable <- function(connection, statementType, variable, label, simulate = TRUE, verbose = TRUE) {
if (is.null(statementType) || is.na(statementType) || length(statementType) != 1
|| (!is.numeric(statementType) && !is.character(statementType))) {
stop("'statementType' must be an integer or character object of length 1.")
Expand All @@ -1479,7 +1479,13 @@ dna_renameVariable <- function(connection, statementType, variable, label) {
if (grepl("\\W", label)) {
stop("'label' must not contain any spaces. Only characters and numbers are allowed.")
}
.jcall(connection$dna_connection, "V", "renameVariable", statementType, variable, label)
if (is.null(simulate) || is.na(simulate) || !is.logical(simulate) || length(simulate) != 1) {
stop("'simulate' must be a logical value of length 1")
}
if (is.null(verbose) || is.na(verbose) || !is.logical(verbose) || length(verbose) != 1) {
stop("'verbose' must be a logical value of length 1")
}
.jcall(connection$dna_connection, "V", "renameVariable", statementType, variable, label, simulate, verbose)
}

#' Recode attributes in the DNA database
Expand Down
3 changes: 2 additions & 1 deletion rDNA/man/dna_renameVariable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 55232fd

Please sign in to comment.