From 55232fd2571e56ea72d7bb6611727d287b046c9f Mon Sep 17 00:00:00 2001 From: Philip Leifeld Date: Mon, 4 Feb 2019 02:11:29 +0000 Subject: [PATCH] Bugfixes in dna_recastVariable, dna_renameVariable, and dna_removeVariable --- DNA/src/dna/SqlConnection.java | 23 +++++++++++-------- DNA/src/dna/export/ExporterR.java | 37 ++++++++++++++++++++++--------- rDNA/DESCRIPTION | 4 ++-- rDNA/R/rDNA.R | 10 +++++++-- rDNA/man/dna_renameVariable.Rd | 3 ++- 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/DNA/src/dna/SqlConnection.java b/DNA/src/dna/SqlConnection.java index f3117ce9..b00d236a 100644 --- a/DNA/src/dna/SqlConnection.java +++ b/DNA/src/dna/SqlConnection.java @@ -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); } diff --git a/DNA/src/dna/export/ExporterR.java b/DNA/src/dna/export/ExporterR.java index f75256e1..236db795 100644 --- a/DNA/src/dna/export/ExporterR.java +++ b/DNA/src/dna/export/ExporterR.java @@ -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--) { @@ -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); @@ -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++; } } diff --git a/rDNA/DESCRIPTION b/rDNA/DESCRIPTION index cef46ac8..a256e224 100755 --- a/rDNA/DESCRIPTION +++ b/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", diff --git a/rDNA/R/rDNA.R b/rDNA/R/rDNA.R index d203fe7a..2901383b 100644 --- a/rDNA/R/rDNA.R +++ b/rDNA/R/rDNA.R @@ -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.") @@ -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 diff --git a/rDNA/man/dna_renameVariable.Rd b/rDNA/man/dna_renameVariable.Rd index 1f8acf0d..267a48a7 100644 --- a/rDNA/man/dna_renameVariable.Rd +++ b/rDNA/man/dna_renameVariable.Rd @@ -4,7 +4,8 @@ \alias{dna_renameVariable} \title{Rename a variable} \usage{ -dna_renameVariable(connection, statementType, variable, label) +dna_renameVariable(connection, statementType, variable, label, + simulate = TRUE, verbose = TRUE) } \arguments{ \item{connection}{A \code{dna_connection} object created by the