请教petstore中GenericCatalogDAO中的一个问题!

02-11-05 hysnoww

请教petstore中GenericCatalogDAO中的一个问题!

private PreparedStatement buildSQLStatement(Connection connection, Statement sqlStatement, String[] parameterValues)
	    throws SQLException {
	StringBuffer buffer = new StringBuffer();
	int totalParameterValueNb = parameterValues != null ? parameterValues.length : 0;

	
	for (int i = 0; i < sqlStatement.fragments.length; i++) {

	
	    if (sqlStatement.fragments[i].variableOccurrence) {
		while (totalParameterValueNb > 0 && totalParameterValueNb >= sqlStatement.fragments[i].parameterNumber) {
		    buffer.append(sqlStatement.fragments[i].text);
		    totalParameterValueNb -= sqlStatement.fragments[i].parameterNumber;
		}
	    } else {
		buffer.append(sqlStatement.fragments[i].text);
		totalParameterValueNb -= sqlStatement.fragments[i].parameterNumber;
	    }
	}

	if (totalParameterValueNb > 0) {
	    System.err.println("Number of values doesn't match number of parameters: " +
				totalParameterValueNb + "/" + parameterValues.length);
	}

	
	
	PreparedStatement statement = connection.prepareStatement(buffer.toString(),ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	if (parameterValues != null) {
	    for (int i = 0; i < parameterValues.length; i++) {
		statement.setString(i + 1, parameterValues[i]);
	    }
	}
	return statement;
    }
    
    以上方法中的以下代码段
    for (int i = 0; i < sqlStatement.fragments.length; i++) {

	
	    if (sqlStatement.fragments[i].variableOccurrence) {
		while (totalParameterValueNb > 0 && totalParameterValueNb >= sqlStatement.fragments[i].parameterNumber) {
		    buffer.append(sqlStatement.fragments[i].text);
		    totalParameterValueNb -= sqlStatement.fragments[i].parameterNumber;
		}
	    } else {
		buffer.append(sqlStatement.fragments[i].text);
		totalParameterValueNb -= sqlStatement.fragments[i].parameterNumber;
	    }
	}
	
	去掉while 循环语句功能会发生变化吗?
	在SQL的.xml配置文件中每个sqlfragment标记中的VARIABLE的含义是什么(如下)?
	<SQLStatement method="SEARCH_ITEMS">
            <SQLFragment parameterNb="4">
                select catid, a.productid, name, a.itemid, b.image, b.descn, attr1, 
                        attr2, attr3, attr4, attr5, listprice, unitcost 
                    from (((item a join item_details b on a.itemid=b.itemid)
                        join product_details c on a.productid=c.productid)
                        join product d on d.productid=c.productid and b.locale = c.locale)
                    where b.locale = ? and ((lower(name) like ? or lower(catid) like ? or lower(b.descn) like ?)
            </SQLFragment>
            <SQLFragment parameterNb="3" occurrence="VARIABLE">
                or (lower(name) like ? or lower(catid) like ? or lower(b.descn) like ?)  
            </SQLFragment>
            <SQLFragment parameterNb="0">
                )
            </SQLFragment>
        </SQLStatement>
	十分感谢!
<p>

banq
2002-11-05 21:22

1.3.1的DAO比1.3要复杂多。

我没仔细看过,觉得这是在玩SQL语句的组合游戏,不知哪位有过研究?