private void append()

in openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SQLBuffer.java [125:221]


    private void append(SQLBuffer buf, int sqlIndex, int paramIndex,
        boolean subsels, boolean paramOnly) {
        if (subsels) {
            // only allow appending of buffers with subselects, not insertion
            if (_subsels != null && !_subsels.isEmpty()
                && sqlIndex != _sql.length())
                throw new IllegalStateException();
            if (buf._subsels != null && !buf._subsels.isEmpty()) {
                if (sqlIndex != _sql.length())
                    throw new IllegalStateException();
                if (_subsels == null)
                    _subsels = new ArrayList(buf._subsels.size());
                for (int i = 0; i < buf._subsels.size(); i++)
                    _subsels.add(((Subselect) buf._subsels.get(i)).
                        clone(sqlIndex, paramIndex));
            }
        }

        if (!paramOnly) {
            if (sqlIndex == _sql.length())
                _sql.append(buf._sql);
            else
                _sql.insert(sqlIndex, buf._sql);
        }

        if (buf._params != null) {
            if (_params == null)
                _params = new ArrayList();
            if (_cols == null && buf._cols != null) {
                _cols = new ArrayList();
                while (_cols.size() < _params.size())
                    _cols.add(null);
            }

            if (paramIndex == _params.size()) {
                _params.addAll(buf._params);
                if (buf._userParams != null) {
                    if (_userParams == null)
                        _userParams = new ArrayList();
                   _userParams.addAll(paramIndex, buf._userParams);
                }
                if (buf._userIndex != null) {
                    if (_userIndex == null)
                        _userIndex = new ArrayList();
                    _userIndex.addAll(buf._userIndex);
                }
                if (buf._cols != null)
                    _cols.addAll(buf._cols);
                else if (_cols != null)
                    while (_cols.size() < _params.size())
                        _cols.add(null);
            } else {
                _params.addAll(paramIndex, buf._params);
                if ( buf._userParams != null) {
                    if (_userParams == null)
                        _userParams = new ArrayList();
                    _userParams.addAll(paramIndex, buf._userParams);
                }
                if (buf._userIndex != null) {
                     if (_userIndex == null)
                         _userIndex = new ArrayList();
                     _userIndex.addAll(buf._userIndex);
                }
                if (buf._cols != null)
                    _cols.addAll(paramIndex, buf._cols);
                else if (_cols != null)
                    while (_cols.size() < _params.size())
                        _cols.add(paramIndex, null);
            }
        }

        if (_userIndex != null) {
            // fix up user parameter index(s)
            for (int i = 0; i < _userIndex.size(); i+=2) {
            	Object param = _userIndex.get(i+1);

            	Object previousParam = (i > 0) ? _userIndex.get(i-1) : null;
            	if ( param != previousParam) {
            		_userIndex.set(i, _userParams.indexOf(param));
            	}else{
            		//if there are multiple parameters for the in clause or the combined PK field,
            		//we got duplicate param objects in _userParams list.
            		//In order to find the right index, we have to skip params that's checked already.
            		int previousUserIndex = (Integer)_userIndex.get(i-2);
            		int userParamindex = 0;

                	for(Object next : _userParams){
                        if (next == param && userParamindex > previousUserIndex) {
                            _userIndex.set(i, userParamindex);
                            break;
                      }
                      userParamindex++;
                	}
            	}
            }
        }
    }