Skip to content
Snippets Groups Projects
Commit 974695a4 authored by srosse's avatar srosse
Browse files

OO-4152: refactor to use parametrized queries

parent 3526c425
No related branches found
No related tags found
No related merge requests found
......@@ -24,16 +24,14 @@
*/
package org.olat.course.statistic;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;
import java.util.Date;
import javax.sql.DataSource;
import org.apache.logging.log4j.Logger;
import org.olat.core.logging.Tracing;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameterValue;
/**
* MySQL specific class which creates a temporary table with the
......@@ -45,26 +43,16 @@ import org.springframework.jdbc.core.JdbcTemplate;
public class MySQLTempStatTableCreator implements IStatisticUpdater {
/** the logging object used in this class **/
private static final Logger log_ = Tracing.createLoggerFor(MySQLTempStatTableCreator.class);
private static final Logger log = Tracing.createLoggerFor(MySQLTempStatTableCreator.class);
/** the jdbcTemplate is used to allow access to other than the default database and
* allow raw sql code
*/
private JdbcTemplate jdbcTemplate_;
private JdbcTemplate jdbcTemplate;
/** set via spring **/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
jdbcTemplate_ = jdbcTemplate;
DataSource dataSource = jdbcTemplate==null ? null : jdbcTemplate.getDataSource();
Connection connection = null;
try{
if (dataSource!=null) {
connection = dataSource.getConnection();
}
} catch(SQLException e) {
log_.warn("setJdbcTemplate: SQLException while trying to get connection for logging", e);
}
log_.info("setJdbcTemplate: jdbcTemplate="+jdbcTemplate+", dataSource="+dataSource+", connection="+connection);
public void setJdbcTemplate(JdbcTemplate template) {
jdbcTemplate = template;
}
@Override
......@@ -72,17 +60,17 @@ public class MySQLTempStatTableCreator implements IStatisticUpdater {
// create temp table
final long startTime = System.currentTimeMillis();
try{
log_.info("updateStatistic: dropping o_stat_temptable if still existing");
jdbcTemplate_.execute("drop table if exists o_stat_temptable;");
log.info("updateStatistic: dropping o_stat_temptable if still existing");
jdbcTemplate.execute("drop table if exists o_stat_temptable;");
log_.info("updateStatistic: creating o_stat_temptable");
jdbcTemplate_.execute(
log.info("updateStatistic: creating o_stat_temptable");
jdbcTemplate.execute(
"create table o_stat_temptable (" +
"creationdate datetime not null," +
"businesspath varchar(2048) not null" +
");");
log_.info("updateStatistic: inserting logging actions from "+from+" until "+until);
log.info("updateStatistic: inserting logging actions from "+from+" until "+until);
// same month optimization
Calendar lastUpdatedCalendar = Calendar.getInstance();
......@@ -93,20 +81,18 @@ public class MySQLTempStatTableCreator implements IStatisticUpdater {
long fromSeconds = from.getTime() / 1000l;
long untilSeconds = until.getTime() / 1000l;
long numLoggingActions = jdbcTemplate_.update(
long numLoggingActions = jdbcTemplate.update(
"insert into o_stat_temptable (creationdate,businesspath) " +
"select creationdate,businesspath" +
" from o_loggingtable" +
" where actionverb='launch' and actionobject='node' and creationdate>from_unixtime('"+ fromSeconds +"') and creationdate<=from_unixtime('"+ untilSeconds +"');");
log_.info("updateStatistic: insert done. number of logging actions: " + numLoggingActions);
} catch(RuntimeException e) {
log_.warn("updateStatistic: ran into a RuntimeException: "+e, e);
} catch(Error er) {
log_.warn("updateStatistic: ran into an Error: "+er, er);
" where actionverb='launch' and actionobject='node' and creationdate>from_unixtime(?) and creationdate<=from_unixtime(?);",
new SqlParameterValue(Types.VARCHAR, Long.toString(fromSeconds)), new SqlParameterValue(Types.VARCHAR, Long.toString(untilSeconds)));
log.info("updateStatistic: insert done. number of logging actions: {}", numLoggingActions);
} catch(Exception e) {
log.warn("updateStatistic: ran into a RuntimeException: ", e);
} finally {
final long diff = System.currentTimeMillis() - startTime;
log_.info("updateStatistic: END. duration="+diff);
log.info("updateStatistic: END. duration="+diff);
}
}
......
......@@ -45,11 +45,11 @@ public class MySQLTempStatTableDropper implements IStatisticUpdater {
/** the jdbcTemplate is used to allow access to other than the default database and
* allow raw sql code
*/
private JdbcTemplate jdbcTemplate_;
private JdbcTemplate jdbcTemplate;
/** set via spring **/
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
jdbcTemplate_ = jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate template) {
jdbcTemplate = template;
}
@Override
......@@ -59,16 +59,12 @@ public class MySQLTempStatTableDropper implements IStatisticUpdater {
// create temp table
final long startTime = System.currentTimeMillis();
try{
jdbcTemplate_.execute("drop table o_stat_temptable;");
} catch(RuntimeException e) {
jdbcTemplate.execute("drop table o_stat_temptable;");
} catch(Exception e) {
log.error("", e);
} catch(Error er) {
log.error("", er);
} finally {
final long diff = System.currentTimeMillis() - startTime;
log.info("updateStatistic: END. duration=" + diff);
}
}
}
......@@ -92,7 +92,7 @@ public class AnalysisFilterDAO {
.createQuery(sb.toString(), AvailableAttributes.class);
appendParameters(query, searchParams);
return query.getResultList().get(0);
};
}
AnlaysisFigures loadAnalyticFigures(AnalysisSearchParameter searchParams) {
QueryBuilder sb = new QueryBuilder();
......@@ -136,26 +136,26 @@ public class AnalysisFilterDAO {
}
List<Curriculum> loadContextCurriculums(AnalysisSearchParameter searchParams) {
String statement = dbInstance.isOracle()
QueryBuilder sb = dbInstance.isOracle()
? getContextCurriculumsOraQuery(searchParams)
: getContextCurriculumsQuery(searchParams);
TypedQuery<Curriculum> query = dbInstance.getCurrentEntityManager()
.createQuery(statement, Curriculum.class);
.createQuery(sb.toString(), Curriculum.class);
appendParameters(query, searchParams);
return query.getResultList();
}
private String getContextCurriculumsQuery(AnalysisSearchParameter searchParams) {
private QueryBuilder getContextCurriculumsQuery(AnalysisSearchParameter searchParams) {
QueryBuilder sb = new QueryBuilder();
sb.append("select distinct contextCurriculum");
appendFrom(sb, searchParams);
appendWhere(sb, searchParams);
sb.and().append("contextCurriculum.key is not null");
return sb.toString();
return sb;
}
private String getContextCurriculumsOraQuery(AnalysisSearchParameter searchParams) {
private QueryBuilder getContextCurriculumsOraQuery(AnalysisSearchParameter searchParams) {
QueryBuilder sb = new QueryBuilder();
sb.append("select dCurriculum from curriculum as dCurriculum where dCurriculum.key in (");
sb.append("select distinct contextCurriculum.key");
......@@ -163,7 +163,7 @@ public class AnalysisFilterDAO {
appendWhere(sb, searchParams);
sb.and().append("contextCurriculum.key is not null");
sb.append(")");
return sb.toString();
return sb;
}
List<CurriculumElement> loadContextCurriculumElements(AnalysisSearchParameter searchParams) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment