Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to setDefaultAutoDerivedColumnNames at global level. #321

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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