[cvs]
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc
JDBCUtil.java
JCorporate Ltd
jcorp at jcorp2.servlets.net
Mon Sep 20 13:49:26 PDT 2004
Update of /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc
In directory jcorp2.servlets.net:/tmp/cvs-serv7656
Modified Files:
JDBCUtil.java
Log Message:
<Aucun commentaire entré>
Index: JDBCUtil.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/dataobjects/jdbc/JDBCUtil.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** JDBCUtil.java 20 Sep 2004 19:22:29 -0000 1.29
--- JDBCUtil.java 20 Sep 2004 20:49:24 -0000 1.30
***************
*** 75,85 ****
--- 75,90 ----
import com.jcorporate.expresso.core.misc.ConfigurationException;
import com.jcorporate.expresso.core.misc.StringUtil;
+ import com.jcorporate.expresso.core.db.TypeMapper;
import com.jcorporate.expresso.core.security.filters.Filter;
+ import com.jcorporate.expresso.core.security.filters.FilterManager;
import com.jcorporate.expresso.kernel.util.FastStringBuffer;
import org.apache.commons.collections.LRUMap;
import org.apache.log4j.Logger;
+ import java.sql.CallableStatement;
+ import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
***************
*** 775,778 ****
--- 780,791 ----
") No such field as " + fieldName);
}
+ boolean noTrim = false;
+ if (!oneField.isMasked() && !targetObject.isGlobalMasked()) {
+ try {
+ noTrim = ConfigManager.getJdbcRequired(targetObject.getMappedDataContext()).isStringNotTrim();
+ } catch (ConfigurationException ce) {
+ throw new DataException(ce);
+ }
+ }
String fieldValue = targetObject.getSerialForm(oneField);
***************
*** 787,790 ****
--- 800,812 ----
}
+ if (oneField.isNumericType()) {
+ if (fieldValue.length() == 0) {
+ return "0";
+ }
+
+ return fieldValue.trim();
+ } /* if a numeric type */
+
+
if (oneField.isQuotedTextType()) {
if (rangeString != null) {
***************
*** 794,802 ****
String returnString = null;
try {
returnValue.append("\'");
! returnValue.append(targetObject.getConnectionPool().getEscapeHandler()
! .escapeString(fieldValue.trim()));
returnValue.append("\'");
returnString = returnValue.toString();
} finally {
returnValue.release();
--- 816,832 ----
String returnString = null;
try {
+ String value = "";
+ if (noTrim) {
+ value = fieldValue;
+ } else {
+ value = fieldValue.trim();
+ }
returnValue.append("\'");
! // returnValue.append(targetObject.getConnectionPool().getEscapeHandler().escapeString(fieldValue.trim()));
! returnValue.append(targetObject.getConnectionPool().getEscapeHandler().escapeString(value));
returnValue.append("\'");
returnString = returnValue.toString();
+ } catch (DBException e) {
+ throw new DataException (e);
} finally {
returnValue.release();
***************
*** 870,880 ****
}
! if (oneField.isNumericType()) {
! if (fieldValue.length() == 0) {
! return "0";
! }
! }
! return fieldValue.trim();
} /* quoteIfNeeded(String) */
--- 900,915 ----
}
! if (oneField.isNumericType()) {
! if (fieldValue.length() == 0) {
! return "0";
! }
! }
! if (noTrim) {
! return fieldValue;
! } else {
!
! return fieldValue.trim();
! }
} /* quoteIfNeeded(String) */
***************
*** 906,909 ****
--- 941,1186 ----
} /* containsWildCards(String) */
+ /**
+ * Build and return a FastStringBuffer ring consisting of an SQL 'where' clause
+ * using the current field values as criteria for the search. See
+ * setCustomWhereClause for information on specifying a more complex where clause.
+ *
+ * @param criteria the JDBCDataObject to build from
+ * @param useAllFields True if all fields are to be used,
+ * false for only key fields
+ * @param allocatedBuffer - An already allocated FastStringBuffer to fill out.
+ * This allows for compatability with, for example, object pools.
+ * @throws DataException upon error
+ * @return A FastStringBuffer containing the "where" clause for the SQL statement
+ */
+ public void buildStoreProcedureCallableStatement(JDBCDataObject criteria, CallableStatement myCallableStatement)
+ throws DataException {
+ Iterator fieldsToUse = null;
+ FastStringBuffer myStatement = FastStringBuffer.getInstance();
+
+ fieldsToUse = criteria.getMetaData().getFieldListArray().iterator();
+
+ /* Now go thru each field - if it is non-empty, add it's criteria */
+
+ boolean inField = false;
+ boolean outField = false;
+ DataFieldMetaData oneField = null;
+ String oneFieldName = null;
+ String oneFieldValue = null;
+ boolean skipText = false;
+ boolean postgresql = false;
+ TypeMapper typeMapper = null;
+
+ try {
+ ConfigJdbc myConfig = ConfigManager.getJdbcRequired(criteria.getMappedDataContext());
+ skipText = myConfig.skipText();
+ //We have to do this because postgres won't be smart enough to
+ //cast floating point literals to truly a floating point value. :(
+ if ("org.postgresql.Driver".equals(myConfig.getDriver())) {
+ postgresql = true;
+ }
+ typeMapper = TypeMapper.getInstance(criteria.getDataContext());
+ } catch (ConfigurationException ce) {
+ throw new DataException(ce);
+ } catch (DBException de) {
+ throw new DataException(de);
+ }
+
+ boolean skipField = false;
+ try {
+
+ while (fieldsToUse.hasNext()) {
+ oneFieldName = (String) fieldsToUse.next();
+ oneField = criteria.getFieldMetaData(oneFieldName);
+ skipField = false;
+
+ if (oneField.isVirtual()) {
+ skipField = true;
+ }
+
+ if (criteria.getDef().isInField(oneField.getName())) {
+ inField = true;
+ }
+
+ if (criteria.getDef().isOutField(oneField.getName())) {
+ outField = true;
+ }
+
+ try {
+ oneFieldValue = StringUtil.notNull(criteria.getDataField(oneField.getName()).asString());
+ } catch (DBException ex) {
+ if (ex instanceof DataException) {
+ throw ((DataException) ex);
+ } else {
+ throw new DataException("Error getting field value", ex);
+ }
+ }
+
+ String rangeString = rangeParser.denotesRange(oneFieldValue);
+ if (!oneFieldValue.equals("")) {
+ if (oneFieldValue.trim().equalsIgnoreCase("is null") ||
+ oneFieldValue.trim().equalsIgnoreCase("is not null")) {
+ ;
+ } else {
+ oneFieldValue = quoteIfNeeded(criteria, oneFieldName, rangeString);
+ }
+ }
+ if (oneFieldValue == null) {
+ skipField = true;
+ }
+ if (oneFieldValue.trim().equals("\'\'")) {
+ skipField = true;
+ }
+
+ //
+ //There was a TODO item asking about why \r and \n's aren't allowed
+ //in text fiels here. and the reason is that the JDBC parsers expect
+ //all quoted fields to be without crlf in them. in all honesty,
+ //you shouldn't be searching for a text field anyway since it's arbitrary
+ //length and you SERIOUSLY dog performance by doing it that way.
+ //If you must search for a text field, you can deal with cr/lf's by
+ //using a prepared statement such as the LOBSupport class.
+ //
+ if (oneField.getTypeString().equalsIgnoreCase("text")) {
+ if (skipText) {
+ skipField = true;
+
+ if (log.isDebugEnabled()) {
+ log.debug("Skipping criteria in text field '" +
+ oneFieldName + "'");
+ }
+ } else {
+ if (oneFieldValue.indexOf("\n") > 0) {
+ oneFieldValue = StringUtil.replace(oneFieldValue, "\n",
+ "");
+ }
+ if (oneFieldValue.indexOf("\r") > 0) {
+ oneFieldValue = StringUtil.replace(oneFieldValue, "\r",
+ "");
+ }
+ if (oneFieldValue.equals("\'\'")) {
+ skipField = true;
+ }
+ }
+ } /* if text field */
+
+ if (oneFieldValue.trim().equals("")) {
+ skipField = true;
+ }
+ if (!skipField) {
+ if (rangeString != null) {
+ String theValue = rangeString + " " + oneFieldValue;
+ boolean valid = rangeParser.isValidRange(criteria.getFieldMetaData(oneField.getName()), theValue);
+ if (!valid) {
+ throw new DataException("Invalid field range value: " + theValue);
+ }
+
+ if (inField) {
+ myCallableStatement.setString(Integer.parseInt(oneFieldName), theValue);
+ }
+ if (outField) {
+ myCallableStatement.registerOutParameter(Integer.parseInt(oneFieldName), typeMapper.getJavaSQLType(oneField.getTypeString()));
+ }
+ } else if ((oneFieldValue.trim().equalsIgnoreCase("is null")) ||(oneFieldValue.trim().equalsIgnoreCase("is not null"))) {
+ if (inField) {
+ myCallableStatement.setString(Integer.parseInt(oneFieldName), oneFieldValue.trim());
+ }
+ if (outField) {
+ myCallableStatement.registerOutParameter(Integer.parseInt(oneFieldName), typeMapper.getJavaSQLType(oneField.getTypeString()));
+ }
+ } else if (oneField.isDateType()) {
+ Object tmpData = null;
+ try {
+ tmpData = criteria.getDataField(oneFieldName).getValue();
+ } catch (DBException ex) {
+ if (ex instanceof DataException) {
+ throw ((DataException) ex);
+ } else {
+ throw new DataException("Error getting field value", ex);
+ }
+ }
+ String data;
+ //
+ //FIXME allow for appropriate support of other data types.
+ //
+ if (tmpData == null) {
+ data = null;
+ } else if (tmpData instanceof String) {
+ data = (String) tmpData;
+ } else {
+ data = tmpData.toString();
+ }
+
+ if (data == null || (data.length() == 0)) {
+ if (inField) {
+ myCallableStatement.setString(Integer.parseInt(oneFieldName), "null");
+ }
+ } else {
+ myCallableStatement.setString(Integer.parseInt(oneFieldName), JDBCUtil.getInstance().formatDateTime(criteria, oneField.getName()));
+ }
+ if (outField) {
+ myCallableStatement.registerOutParameter(Integer.parseInt(oneFieldName), typeMapper.getJavaSQLType(oneField.getTypeString()));
+ }
+ } else if (oneField.isFloatingPointType()) {
+ //Floating point types have to be searched within a certain
+ //precision. Thus we compare the ABS to the less than
+ //the field precision.
+ myStatement.append("ABS(");
+ myStatement.append(oneFieldName);
+ myStatement.append(" - ");
+ if (postgresql) {
+ myStatement.append(" CAST (");
+ }
+
+ myStatement.append(oneFieldValue);
+ if (postgresql) {
+ myStatement.append(" as FLOAT)");
+ }
+ myStatement.append(") <");
+ if (postgresql) {
+ myStatement.append(" CAST (");
+ }
+ myStatement.append(".");
+ int precision = oneField.getPrecision();
+ if (precision == 0) precision = 1;
+ for (int i = 0; i < oneField.getPrecision() - 1; i++) {
+ myStatement.append("0");
+ }
+ myStatement.append("1");
+ if (postgresql) {
+ myStatement.append(" as FLOAT)");
+ }
+ if (inField) {
+ myCallableStatement.setString(Integer.parseInt(oneFieldName), myStatement.toString());
+ }
+ myStatement.clear();
+ if (outField) {
+ myCallableStatement.registerOutParameter(Integer.parseInt(oneFieldName), typeMapper.getJavaSQLType(oneField.getTypeString()), oneField.getPrecision());
+ }
+ } else {
+ if (inField) {
+ myCallableStatement.setString(Integer.parseInt(oneFieldName), oneFieldValue);
+ }
+ if (outField) {
+ myCallableStatement.registerOutParameter(Integer.parseInt(oneFieldName), typeMapper.getJavaSQLType(oneField.getTypeString()));
+ }
+ }/* if (oneField.isFloatingPointType()) */
+
+ /* if field is not skipped for some reason */
+ myStatement.release();
+ myStatement = null;
+ } /* if (!skipField) */
+
+ } /* While (for each field) */
+ } catch (SQLException ce) {
+ throw new DataException(ce);
+ } catch (DBException de) {
+ throw new DataException(de);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("Built callable statement for store procedure " );
+ }
+ } /* buildStoreProcedureCallableStatement(JDBCDataObject, boolean, CallableStatement) */
}
+
More information about the cvs
mailing list