From d91ada5c896073fd159dc47f4f84fca7d4a48f94 Mon Sep 17 00:00:00 2001 From: neilgrover Date: Wed, 3 Apr 2019 18:44:31 -0600 Subject: [PATCH] Add ability to setDefaultAutoDerivedColumnNames at global level. --- core/src/main/java/org/sql2o/Query.java | 1 + core/src/main/java/org/sql2o/Sql2o.java | 19 +++++++++++++++++++ core/src/test/java/org/sql2o/Sql2oTest.java | 15 +++++++++++---- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/sql2o/Query.java b/core/src/main/java/org/sql2o/Query.java index 98f4b978..a6512633 100644 --- a/core/src/main/java/org/sql2o/Query.java +++ b/core/src/main/java/org/sql2o/Query.java @@ -76,6 +76,7 @@ private Query(Connection connection, String queryText, boolean returnGeneratedKe this.columnNames = columnNames; this.setColumnMappings(connection.getSql2o().getDefaultColumnMappings()); this.caseSensitive = connection.getSql2o().isDefaultCaseSensitive(); + this.autoDeriveColumnNames = connection.getSql2o().isDefaultAutoDeriveColumnNames(); paramNameToIdxMap = new HashMap<>(); parameters = new HashMap<>(); diff --git a/core/src/main/java/org/sql2o/Sql2o.java b/core/src/main/java/org/sql2o/Sql2o.java index 9202c8f8..5d044ffa 100644 --- a/core/src/main/java/org/sql2o/Sql2o.java +++ b/core/src/main/java/org/sql2o/Sql2o.java @@ -30,6 +30,7 @@ public class Sql2o { final Quirks quirks; private Map defaultColumnMappings; private boolean defaultCaseSensitive; + private boolean defaultAutoDeriveColumnNames; private ConnectionSource connectionSource; @@ -151,6 +152,24 @@ public void setDefaultCaseSensitive(boolean defaultCaseSensitive) { this.defaultCaseSensitive = defaultCaseSensitive; } + /** + * Gets value indicating if this instance of Sql2o will automatically derive column names + * @see #setDefaultAutoDeriveColumnNames + * @return defaultAutoDeriveColumnNames + */ + public boolean isDefaultAutoDeriveColumnNames() { + return defaultAutoDeriveColumnNames; + } + + /** + * Sets a value indicating if this instance of Sql2o will auto derive column names by default. If your database uses + * underscores in column names and your properties do not, then this will handle the conversion. + * @param defaultAutoDeriveColumnNames + */ + public void setDefaultAutoDeriveColumnNames(boolean defaultAutoDeriveColumnNames) { + this.defaultAutoDeriveColumnNames = defaultAutoDeriveColumnNames; + } + /** * Creates a {@link Query} * @param query the sql query string diff --git a/core/src/test/java/org/sql2o/Sql2oTest.java b/core/src/test/java/org/sql2o/Sql2oTest.java index d59f7467..e0e2874c 100644 --- a/core/src/test/java/org/sql2o/Sql2oTest.java +++ b/core/src/test/java/org/sql2o/Sql2oTest.java @@ -1364,14 +1364,21 @@ public void setAnotherVeryExcitingValue(String anotherVeryExcitingValue) { assertNotNull(ex); - LocalPojo p = con.createQuery(selectSql) + LocalPojo localPojo1 = con.createQuery(selectSql) .setAutoDeriveColumnNames(true) .executeAndFetchFirst(LocalPojo.class); - assertNotNull(p); - assertEquals(1, p.getIdVal()); - assertEquals("test1", p.getAnotherVeryExcitingValue()); + assertNotNull(localPojo1); + assertEquals(1, localPojo1.getIdVal()); + assertEquals("test1", localPojo1.getAnotherVeryExcitingValue()); + sql2o.setDefaultAutoDeriveColumnNames(true); + LocalPojo localPojo2 = con.createQuery(selectSql) + .executeAndFetchFirst(LocalPojo.class); + + assertNotNull(localPojo2); + assertEquals(1, localPojo2.getIdVal()); + assertEquals("test1", localPojo2.getAnotherVeryExcitingValue()); } }