Skip to content

Commit

Permalink
start adding support for boolean data types
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Black authored and Adam Black committed Aug 18, 2024
1 parent cb0ec92 commit b327541
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
10 changes: 5 additions & 5 deletions R/InsertTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

getSqlDataTypes <- function(column) {
getSqlDataTypes <- function(column) {
if (is.integer(column)) {
return("INTEGER")
} else if (is(column, "POSIXct") | is(column, "POSIXt")) {
Expand All @@ -27,6 +27,8 @@ getSqlDataTypes <- function(column) {
return("BIGINT")
} else if (is.numeric(column)) {
return("FLOAT")
} else if (is.logical(column)) {
return("BOOLEAN")
} else {
if (is.factor(column)) {
maxLength <-
Expand Down Expand Up @@ -294,10 +296,6 @@ insertTable.default <- function(connection,
inform("Attempting to use bulk loading...")
if (dbms == "redshift") {
bulkLoadRedshift(connection, sqlTableName, data)
} else if (dbms == "pdw") {
bulkLoadPdw(connection, sqlTableName, sqlDataTypes, data)
} else if (dbms == "hive") {
bulkLoadHive(connection, sqlTableName, sqlFieldNames, data)
} else if (dbms == "postgresql") {
bulkLoadPostgres(connection, sqlTableName, sqlFieldNames, sqlDataTypes, data)
}
Expand Down Expand Up @@ -359,6 +357,8 @@ insertTable.default <- function(connection,
rJava::.jcall(batchedInsert, "V", "setDateTime", i, format(column, format="%Y-%m-%d %H:%M:%S"))
} else if (is(column, "Date")) {
rJava::.jcall(batchedInsert, "V", "setDate", i, as.character(column))
} else if (is.logical(column)) {
rJava::.jcall(batchedInsert, "V", "setBoolean", i, column)
} else {
rJava::.jcall(batchedInsert, "V", "setString", i, as.character(column))
}
Expand Down
Binary file modified inst/java/DatabaseConnector.jar
Binary file not shown.
26 changes: 23 additions & 3 deletions java/org/ohdsi/databaseConnector/BatchedInsert.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public class BatchedInsert {
public static int DATE = 3;
public static int DATETIME = 4;
public static int BIGINT = 5;
private static String SPARK = "spark";
private static String SNOWFLAKE = "snowflake";
private static String BIGQUERY = "bigquery";
public static int BOOLEAN = 6;
private static String SPARK = "spark";
private static String SNOWFLAKE = "snowflake";
private static String BIGQUERY = "bigquery";

public static final int BIG_DATA_BATCH_INSERT_LIMIT = 1000;

Expand Down Expand Up @@ -61,6 +62,9 @@ private void checkColumns() {
if (columnTypes[i] == INTEGER) {
if (((int[]) columns[i]).length != rowCount)
throw new RuntimeException("Column " + (i + 1) + " data not of correct length");
} else if (columnTypes[i] == BOOLEAN) {
if (((Boolean[]) columns[i]).length != rowCount)
throw new RuntimeException("Column " + (i + 1) + " data not of correct length");
} else if (columnTypes[i] == NUMERIC) {
if (((double[]) columns[i]).length != rowCount)
throw new RuntimeException("Column " + (i + 1) + " data not of correct length");
Expand All @@ -81,6 +85,12 @@ private void setValue(PreparedStatement statement, int statementIndex, int rowIn
statement.setObject(statementIndex, null);
else
statement.setInt(statementIndex, value);
} else if (columnTypes[columnIndex] == BOOLEAN) {
Boolean value = ((Boolean[]) columns[columnIndex])[rowIndex];
if (value == null)
statement.setObject(statementIndex, null);
else
statement.setBoolean(statementIndex, value);
} else if (columnTypes[columnIndex] == NUMERIC) {
double value = ((double[]) columns[columnIndex])[rowIndex];
if (Double.isNaN(value))
Expand Down Expand Up @@ -212,6 +222,12 @@ public void setInteger(int columnIndex, int[] column) {
rowCount = column.length;
}

public void setBoolean(int columnIndex, Boolean[] column) {
columns[columnIndex - 1] = column;
columnTypes[columnIndex - 1] = BOOLEAN;
rowCount = column.length;
}

public void setNumeric(int columnIndex, double[] column) {
columns[columnIndex - 1] = column;
columnTypes[columnIndex - 1] = NUMERIC;
Expand Down Expand Up @@ -246,6 +262,10 @@ public void setInteger(int columnIndex, int column) {
setInteger(columnIndex, new int[] { column });
}

public void setBoolean(int columnIndex, Boolean column) {
setBoolean(columnIndex, new Boolean[] { column });
}

public void setNumeric(int columnIndex, double column) {
setNumeric(columnIndex, new double[] { column });
}
Expand Down

0 comments on commit b327541

Please sign in to comment.