forked from altran/Valuereporter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 Building dynamic sql for ObservedActivity.
- Loading branch information
Showing
6 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/org/valuereporter/activity/ActivitiesDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.valuereporter.activity; | ||
|
||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.BatchPreparedStatementSetter; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
/** | ||
* Created by t-blind5-01 on 02.03.2016. | ||
*/ | ||
@Component | ||
public class ActivitiesDao { | ||
private static final Logger log = getLogger(ActivitiesDao.class); | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
|
||
@Autowired | ||
public ActivitiesDao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
protected long insertActivities(String tableName, final List<String> columnNames, final List<ObservedActivity> activities) { | ||
if (tableName == null || tableName.isEmpty()) { | ||
throw new IllegalArgumentException("tableName must have a value"); | ||
} | ||
if (columnNames == null) { | ||
throw new IllegalArgumentException("columntNames must not be null"); | ||
} | ||
String sql = buildSql(tableName, columnNames); | ||
|
||
int [] updatePrStatement = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { | ||
|
||
@Override | ||
public void setValues(PreparedStatement ps, int i) throws SQLException { | ||
ObservedActivity activity = activities.get(i); | ||
int paramNum = 1; | ||
for (String columnName : columnNames) { | ||
ps.setObject(paramNum, activity.getValue(columnName)); | ||
} | ||
// ps.setObject(1, activity.getValue(columnNames.get(0))); | ||
// ps.setLong(1, customer.getCustId()); | ||
// ps.setString(2, customer.getName()); | ||
// ps.setInt(3, customer.getAge() ); | ||
} | ||
|
||
@Override | ||
public int getBatchSize() { | ||
return activities.size(); | ||
} | ||
}); | ||
|
||
int sum = 0; | ||
if (updatePrStatement != null) { | ||
sum = IntStream.of(updatePrStatement).sum(); | ||
} | ||
return sum; | ||
} | ||
|
||
protected String buildSql(String tableName, List<String> columnNames) { | ||
String sql = "INSERT INTO " + tableName + | ||
"("; | ||
for (String columnName : columnNames) { | ||
sql += columnName + ", "; | ||
} | ||
sql = sql.substring(0,sql.length() - 2); | ||
sql += ") VALUES ("; | ||
for (int i = 0; i < columnNames.size(); i++) { | ||
if (i < (columnNames.size() - 1)) { | ||
sql += "?,"; | ||
} else { | ||
sql += "?"; | ||
} | ||
} | ||
sql += ")"; | ||
return sql; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/org/valuereporter/activity/ActivitiesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.valuereporter.activity; | ||
|
||
/** | ||
* Created by t-blind5-01 on 02.03.2016. | ||
*/ | ||
public class ActivitiesService { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/java/org/valuereporter/activity/ActivitiesDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.valuereporter.activity; | ||
|
||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
|
||
/** | ||
* Created by t-blind5-01 on 02.03.2016. | ||
*/ | ||
public class ActivitiesDaoTest { | ||
|
||
@BeforeMethod | ||
public void setUp() throws Exception { | ||
|
||
} | ||
|
||
@Test | ||
public void testBuildSql() throws Exception { | ||
ActivitiesDao activitiesDao = new ActivitiesDao(null); | ||
List<String> activities = new ArrayList<>(); | ||
activities.add("userId"); | ||
activities.add("applicationId"); | ||
|
||
String expectedSql = "INSERT INTO logonByApplication(userId, applicationId) VALUES (?,?)"; | ||
String sql = activitiesDao.buildSql("logonByApplication", activities); | ||
assertEquals(sql, expectedSql); | ||
|
||
} | ||
} |