📄 sqljdbcutil.java
字号:
String viewClause = makeViewWhereClause(modelEntity, joinStyle); if (viewClause.length() > 0) { if (whereString.length() > 0) { whereString.append(' '); whereString.append(operator); whereString.append(' '); } whereString.append(viewClause); } if (whereString.length() > 0) { return " WHERE " + whereString.toString(); } return ""; } public static String makeViewWhereClause(ModelEntity modelEntity, String joinStyle) throws GenericEntityException { if (modelEntity instanceof ModelViewEntity) { StringBuffer whereString = new StringBuffer(); ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity; if ("ansi".equals(joinStyle) || "ansi-no-parenthesis".equals(joinStyle)) { // nothing to do here, all done in the JOIN clauses } else if ("theta-oracle".equals(joinStyle) || "theta-mssql".equals(joinStyle)) { boolean isOracleStyle = "theta-oracle".equals(joinStyle); boolean isMssqlStyle = "theta-mssql".equals(joinStyle); for (int i = 0; i < modelViewEntity.getViewLinksSize(); i++) { ModelViewEntity.ModelViewLink viewLink = modelViewEntity.getViewLink(i); ModelEntity linkEntity = modelViewEntity.getMemberModelEntity(viewLink.getEntityAlias()); ModelEntity relLinkEntity = modelViewEntity.getMemberModelEntity(viewLink.getRelEntityAlias()); if (linkEntity == null) { throw new GenericEntityException("Link entity not found with alias: " + viewLink.getEntityAlias() + " for entity: " + modelViewEntity.getEntityName()); } if (relLinkEntity == null) { throw new GenericEntityException("Rel-Link entity not found with alias: " + viewLink.getRelEntityAlias() + " for entity: " + modelViewEntity.getEntityName()); } // ModelViewEntity.ModelMemberEntity linkMemberEntity = modelViewEntity.getMemberModelMemberEntity(viewLink.getEntityAlias()); // ModelViewEntity.ModelMemberEntity relLinkMemberEntity = modelViewEntity.getMemberModelMemberEntity(viewLink.getRelEntityAlias()); for (int j = 0; j < viewLink.getKeyMapsSize(); j++) { ModelKeyMap keyMap = viewLink.getKeyMap(j); ModelField linkField = linkEntity.getField(keyMap.getFieldName()); ModelField relLinkField = relLinkEntity.getField(keyMap.getRelFieldName()); if (whereString.length() > 0) { whereString.append(" AND "); } whereString.append(viewLink.getEntityAlias()); whereString.append("."); whereString.append(filterColName(linkField.getColName())); // check to see whether the left or right members are optional, if so: // oracle: use the (+) on the optional side // mssql: use the * on the required side // NOTE: not testing if original table is optional, ONLY if related table is optional; otherwise things get really ugly... // if (isOracleStyle && linkMemberEntity.getOptional()) whereString.append(" (+) "); if (isMssqlStyle && viewLink.isRelOptional()) whereString.append("*"); whereString.append("="); // if (isMssqlStyle && linkMemberEntity.getOptional()) whereString.append("*"); if (isOracleStyle && viewLink.isRelOptional()) whereString.append(" (+) "); whereString.append(viewLink.getRelEntityAlias()); whereString.append("."); whereString.append(filterColName(relLinkField.getColName())); } } } else { throw new GenericModelException("The join-style " + joinStyle + " is not supported"); } if (whereString.length() > 0) { return "(" + whereString.toString() + ")"; } } return ""; } public static String makeOrderByClause(ModelEntity modelEntity, List orderBy, DatasourceInfo datasourceInfo) throws GenericModelException { return makeOrderByClause(modelEntity, orderBy, false, datasourceInfo); } public static String makeOrderByClause(ModelEntity modelEntity, List orderBy, boolean includeTablenamePrefix, DatasourceInfo datasourceInfo) throws GenericModelException { StringBuffer sql = new StringBuffer(""); //String fieldPrefix = includeTablenamePrefix ? (modelEntity.getTableName(datasourceInfo) + ".") : ""; if (orderBy != null && orderBy.size() > 0) { if (Debug.verboseOn()) Debug.logVerbose("Order by list contains: " + orderBy.size() + " entries.", module); OrderByList orderByList = new OrderByList(orderBy); orderByList.checkOrderBy(modelEntity); orderByList.makeOrderByString(sql, modelEntity, includeTablenamePrefix, datasourceInfo); } if (Debug.verboseOn()) Debug.logVerbose("makeOrderByClause: " + sql.toString(), module); return sql.toString(); } public static String makeViewTable(ModelEntity modelEntity, DatasourceInfo datasourceInfo) throws GenericEntityException { if (modelEntity instanceof ModelViewEntity) { StringBuffer sql = new StringBuffer("(SELECT "); Iterator fieldsIter = modelEntity.getFieldsIterator(); if (fieldsIter.hasNext()) { ModelField curField = (ModelField) fieldsIter.next(); String colname = curField.getColName(); sql.append(colname); sql.append(" AS "); sql.append(filterColName(colname)); while (fieldsIter.hasNext()) { curField = (ModelField) fieldsIter.next(); colname = curField.getColName(); sql.append(", "); sql.append(colname); sql.append(" AS "); sql.append(filterColName(colname)); } } sql.append(makeFromClause(modelEntity, datasourceInfo)); String viewWhereClause = makeViewWhereClause(modelEntity, datasourceInfo.joinStyle); if (UtilValidate.isNotEmpty(viewWhereClause)) { sql.append(" WHERE "); sql.append(viewWhereClause); } ModelViewEntity modelViewEntity = (ModelViewEntity)modelEntity; String groupByString = modelViewEntity.colNameString(modelViewEntity.getGroupBysCopy(), ", ", "", false); if (UtilValidate.isNotEmpty(groupByString)) { sql.append(" GROUP BY "); sql.append(groupByString); } sql.append(")"); return sql.toString(); } else { return modelEntity.getTableName(datasourceInfo); } } public static String filterColName(String colName) { return colName.replace('.', '_').replace('(','_').replace(')','_'); } /* ====================================================================== */ /* ====================================================================== */ /** * The elements (ModelFields) of the list are bound to an SQL statement * (SQL-Processor) * * @param sqlP * @param list * @param entity * @throws GenericEntityException */ public static void setValues(SQLProcessor sqlP, List list, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { Iterator fieldIter = list.iterator(); while (fieldIter.hasNext()) { ModelField curField = (ModelField) fieldIter.next(); setValue(sqlP, curField, entity, modelFieldTypeReader); } } /** * The elements (ModelFields) of the list are bound to an SQL statement * (SQL-Processor), but values must not be null. * * @param sqlP * @param list * @param dummyValue * @param modelFieldTypeReader * @throws GenericEntityException */ public static void setValuesWhereClause(SQLProcessor sqlP, List list, GenericValue dummyValue, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { Iterator fieldIter = list.iterator(); while (fieldIter.hasNext()) { ModelField curField = (ModelField) fieldIter.next(); // for where clause variables only setValue if not null... if (dummyValue.get(curField.getName()) != null) { setValue(sqlP, curField, dummyValue, modelFieldTypeReader); } } } /** * Get all primary keys from the model entity and bind their values * to the an SQL statement (SQL-Processor) * * @param sqlP * @param modelEntity * @param entity * @param modelFieldTypeReader * @throws GenericEntityException */ public static void setPkValues(SQLProcessor sqlP, ModelEntity modelEntity, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { Iterator pksIter = modelEntity.getPksIterator(); while (pksIter.hasNext()) { ModelField curField = (ModelField) pksIter.next(); // for where clause variables only setValue if not null... if (entity.dangerousGetNoCheckButFast(curField) != null) { setValue(sqlP, curField, entity, modelFieldTypeReader); } } } public static void getValue(ResultSet rs, int ind, ModelField curField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException { ModelFieldType mft = modelFieldTypeReader.getModelFieldType(curField.getType()); if (mft == null) { throw new GenericModelException("definition fieldType " + curField.getType() + " not found, cannot getValue for field " + entity.getEntityName() + "." + curField.getName() + "."); } String fieldType = mft.getJavaType(); try { // checking to see if the object is null is really only necessary for the numbers int typeValue = getType(fieldType); ResultSetMetaData rsmd = rs.getMetaData(); int colType = rsmd.getColumnType(ind); if (typeValue <= 4 || typeValue >= 11) { switch (typeValue) { case 1: if (java.sql.Types.CLOB == colType) { // Debug.logInfo("For field " + curField.getName() + " of entity " + entity.getEntityName() + " getString is a CLOB, trying getCharacterStream", module); // if the String is empty, try to get a text input stream, this is required for some databases for larger fields, like CLOBs Clob valueClob = rs.getClob(ind); Reader valueReader = null; if (valueClob != null) { valueReader = valueClob.getCharacterStream(); } //Reader valueReader = rs.getCharacterStream(ind); if (valueReader != null) { char[] inCharBuffer = new char[CHAR_BUFFER_SIZE]; StringBuffer strBuf = new StringBuffer(); int charsRead = 0; try { while ((charsRead = valueReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) { strBuf.append(inCharBuffer, 0, charsRead); } valueReader.close(); } catch (IOException e) { throw new GenericEntityException("Error reading long character stream for field " + curField.getName() + " of entity " + entity.getEntityName(), e); } entity.dangerousSetNoCheckButFast(curField, strBuf.toString()); } else { entity.dangerousSetNoCheckButFast(curField, null); } } else { String value = rs.getString(ind); entity.dangerousSetNoCheckButFast(curField, value); } break; case 2: entity.dangerousSetNoCheckButFast(curField, rs.getTimestamp(ind)); break; case 3: entity.dangerousSetNoCheckButFast(curField, rs.getTime(ind)); break; case 4: entity.dangerousSetNoCheckButFast(curField, rs.getDate(ind)); break; case 11: Object obj = null; InputStream binaryInput = null; byte[] fieldBytes = rs.getBytes(ind); if (fieldBytes != null && fieldBytes.length > 0) { binaryInput = new ByteArrayInputStream(fieldBytes); } if (fieldBytes != null && fieldBytes.length <= 0) { Debug.logWarning("Got bytes back for Object field with length: " + fieldBytes.length + " while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + "): ", module);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -