Skip to content

Commit

Permalink
Add ability to setDefaultAutoDerivedColumnNames at global level.
Browse files Browse the repository at this point in the history
  • Loading branch information
neilgrover committed Apr 4, 2019
1 parent 01d3490 commit d91ada5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/org/sql2o/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/org/sql2o/Sql2o.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Sql2o {
final Quirks quirks;
private Map<String, String> defaultColumnMappings;
private boolean defaultCaseSensitive;
private boolean defaultAutoDeriveColumnNames;

private ConnectionSource connectionSource;

Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions core/src/test/java/org/sql2o/Sql2oTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down

0 comments on commit d91ada5

Please sign in to comment.