added findbugs checks

This commit is contained in:
Timo Westkämper 2010-03-08 09:09:38 +00:00
parent c55225f5ba
commit edd18f1663
8 changed files with 133 additions and 1 deletions

View File

@ -41,6 +41,7 @@ import com.mysema.util.JDBCUtil;
* @author tiwe
* @version $Id$
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public abstract class AbstractSQLQuery<Q extends AbstractSQLQuery<Q>>
extends ProjectableQuery<Q>{

View File

@ -25,6 +25,7 @@ import com.mysema.query.types.expr.Expr;
import com.mysema.query.types.operation.Operator;
import com.mysema.query.types.operation.Ops;
import com.mysema.query.types.path.PEntity;
import com.mysema.query.types.path.Path;
import com.mysema.query.types.query.SubQuery;
/**
@ -238,6 +239,23 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
}
}
public void serializeForInsert(PEntity<?> entity, List<Expr<?>> columns, List<Expr<?>> values) {
append(templates.getInsertInto());
handle(entity);
// columns
if (!columns.isEmpty()){
append("(");
handle(COMMA, columns);
append(")");
}
// values
append(templates.getValues());
append("(");
handle(COMMA, values);
append(")");
}
@SuppressWarnings("unchecked")
public void serializeUnion(SubQuery[] sqs,
List<OrderSpecifier<?>> orderBy) {
@ -321,5 +339,4 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
}
}
}

View File

@ -61,6 +61,8 @@ public class SQLTemplates extends Templates {
private String having = "\nhaving ";
private String insertInto = "insert into ";
private String innerJoin = "\ninner join ";
private String join = "\njoin ";
@ -102,6 +104,8 @@ public class SQLTemplates extends Templates {
private String union = "\nunion\n";
private String update = "update ";
private String values = "\nvalues ";
private String where = "\nwhere ";
@ -481,6 +485,14 @@ public class SQLTemplates extends Templates {
this.update = update;
}
public final String getValues() {
return values;
}
public final void setValues(String values) {
this.values = values;
}
public final String getWhere() {
return where;
}
@ -493,6 +505,14 @@ public class SQLTemplates extends Templates {
return true;
}
public final String getInsertInto() {
return insertInto;
}
public final void setInsertInto(String insertInto) {
this.insertInto = insertInto;
}
public final String getJoinSymbol(JoinType joinType){
switch (joinType) {
case FULLJOIN: return fullJoin;
@ -502,4 +522,6 @@ public class SQLTemplates extends Templates {
}
return ", ";
}
}

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.sql;
import java.math.BigDecimal;

View File

@ -25,6 +25,7 @@ import com.mysema.util.JDBCUtil;
* @author tiwe
*
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public class SQLDeleteClause implements DeleteClause<SQLDeleteClause>{
private final QueryMetadata metadata = new DefaultQueryMetadata();

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query.sql.dml;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.mysema.query.QueryException;
import com.mysema.query.sql.SQLSerializer;
import com.mysema.query.sql.SQLTemplates;
import com.mysema.query.types.expr.Expr;
import com.mysema.query.types.path.PEntity;
import com.mysema.util.JDBCUtil;
@edu.umd.cs.findbugs.annotations.SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public class SQLInsertClause {
private final PEntity<?> entity;
private final Connection connection;
private final SQLTemplates templates;
private final List<Expr<?>> columns = new ArrayList<Expr<?>>();
private final List<Expr<?>> values = new ArrayList<Expr<?>>();
public SQLInsertClause(Connection connection, SQLTemplates templates, PEntity<?> entity){
this.connection = connection;
this.templates = templates;
this.entity = entity;
}
@edu.umd.cs.findbugs.annotations.SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public long execute() {
SQLSerializer serializer = new SQLSerializer(templates);
serializer.serializeForInsert(entity, columns, values);
String queryString = serializer.toString();
PreparedStatement stmt = null;
try {
stmt = connection.prepareStatement(queryString);
JDBCUtil.setParameters(stmt, serializer.getConstants());
return stmt.executeUpdate();
} catch (SQLException e) {
throw new QueryException("Caught " + e.getClass().getSimpleName() + " for " + queryString, e);
}finally{
if (stmt != null){
close(stmt);
}
}
}
public SQLInsertClause columns(Expr<?>... columns){
this.columns.addAll(Arrays.asList(columns));
return this;
}
public SQLInsertClause values(Expr<?>... values){
this.values.addAll(Arrays.asList(values));
return this;
}
protected void close(PreparedStatement stmt) {
try {
stmt.close();
} catch (SQLException e) {
throw new QueryException(e);
}
}
}

View File

@ -26,6 +26,7 @@ import com.mysema.util.JDBCUtil;
* @author tiwe
*
*/
@edu.umd.cs.findbugs.annotations.SuppressWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public class SQLUpdateClause implements UpdateClause<SQLUpdateClause>{
private final QueryMetadata metadata = new DefaultQueryMetadata();

View File

@ -1,3 +1,8 @@
/*
* Copyright (c) 2009 Mysema Ltd.
* All rights reserved.
*
*/
package com.mysema.query;
import static org.junit.Assert.assertEquals;