(file) Return to CQLChainedIdentifierRep.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / CQL

  1 chuck 1.1.2.6 //%2004////////////////////////////////////////////////////////////////////////
  2               //
  3               // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
  4               // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
  5               // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L. P.;
  6               // IBM Corp.; EMC Corporation, The Open Group.
  7               //
  8               // Permission is hereby granted, free of charge, to any person obtaining a copy
  9               // of this software and associated documentation files (the "Software"), to
 10               // deal in the Software without restriction, including without limitation the
 11               // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 12               // sell copies of the Software, and to permit persons to whom the Software is
 13               // furnished to do so, subject to the following conditions:
 14               // 
 15               // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 16               // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 17               // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 18               // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 19               // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 20               // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 21               // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 22 chuck 1.1.2.6 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 23               //
 24               //==============================================================================
 25               //
 26               // Authors: David Rosckes (rosckes@us.ibm.com)
 27               //          Bert Rivero (hurivero@us.ibm.com)
 28               //          Chuck Carmack (carmack@us.ibm.com)
 29               //          Brian Lucier (lucier@us.ibm.com)
 30               //
 31               // Modified By: 
 32               //
 33               //%/////////////////////////////////////////////////////////////////////////////
 34               
 35 humberto 1.1.2.1 #include "CQLChainedIdentifier.h"
 36                  #include "CQLChainedIdentifierRep.h"
 37 humberto 1.1.2.2 #include <Pegasus/CQL/CQLFactory.h>
 38 humberto 1.1.2.1 PEGASUS_NAMESPACE_BEGIN
 39                  
 40 humberto 1.1.2.3 CQLChainedIdentifierRep::CQLChainedIdentifierRep(){
 41                  //	printf("CQLChainedIdentifier()\n");
 42                  }
 43                  
 44 humberto 1.1.2.1 CQLChainedIdentifierRep::CQLChainedIdentifierRep(String inString)
 45                  {
 46                  	parse(inString);
 47                  }
 48                  
 49                  CQLChainedIdentifierRep::CQLChainedIdentifierRep(CQLIdentifier &id)
 50                  {
 51                          _subIdentifiers.append(id);
 52 humberto 1.1.2.3 //	printf("CQLChainedIdentifier(CQLIdentifier &id)\n");
 53 humberto 1.1.2.1 }
 54                  
 55 humberto 1.1.2.3 CQLChainedIdentifierRep::CQLChainedIdentifierRep(const CQLChainedIdentifierRep* rep){
 56                  	_subIdentifiers = rep->_subIdentifiers;
 57                  //	printf("CQLChainedIdentifier COPY CONSTR\n");	
 58                  }
 59                  
 60                  CQLChainedIdentifierRep::~CQLChainedIdentifierRep(){
 61                  	
 62                  //	printf("~CQLChainedIdentifierRep()\n");
 63 humberto 1.1.2.1 }
 64                  
 65                  const Array<CQLIdentifier>& CQLChainedIdentifierRep::getSubIdentifiers()const
 66                  {
 67                  	return _subIdentifiers;
 68                  }
 69                  
 70                  CQLIdentifier CQLChainedIdentifierRep::getLastIdentifier(){
 71                  	if(_subIdentifiers.size() > 0)
 72                  		return _subIdentifiers[_subIdentifiers.size()-1];
 73                  	return CQLIdentifier();
 74                  }
 75                  
 76                  String CQLChainedIdentifierRep::toString()const{
 77                  	String s;
 78                  	for(Uint32 i = 0; i < _subIdentifiers.size(); i++){
 79                  		s.append(_subIdentifiers[i].toString());
 80                  		if(i < _subIdentifiers.size() - 1)
 81                  			s.append(".");
 82                  	}
 83                  	return s;
 84 humberto 1.1.2.1 }
 85                  
 86                  void CQLChainedIdentifierRep::append(CQLIdentifier & id){
 87                  	_subIdentifiers.append(id);
 88                  }
 89                  
 90                  Boolean CQLChainedIdentifierRep::isSubChain(CQLChainedIdentifier & chain){
 91                  	Array<CQLIdentifier> ids = chain.getSubIdentifiers();
 92                  	for(Uint32 i = 0; i < ids.size(); i++){
 93                  		if(ids[i] != _subIdentifiers[i].getName())
 94                  			return false;
 95                  	}
 96                  	return true;
 97                  }
 98                  
 99                  CQLIdentifier& CQLChainedIdentifierRep::operator[](Uint32 index){
100                  	return _subIdentifiers[index];
101                  }
102                  
103 humberto 1.1.2.3 CQLChainedIdentifierRep& CQLChainedIdentifierRep::operator=(const CQLChainedIdentifierRep& rhs){
104                  	if(&rhs != this){
105                  		_subIdentifiers = rhs._subIdentifiers;
106                  //		printf("CQLChainedIdentifierRep::operator=\n");
107                  	}
108                  	return *this;
109                  }
110                  
111 humberto 1.1.2.1 Uint32 CQLChainedIdentifierRep::size(){
112                  	return _subIdentifiers.size();
113                  }
114                  
115                  Boolean CQLChainedIdentifierRep::prepend(CQLIdentifier & id){
116                  	/*
117                  	   Compare id against the first element in _subIdentifiers, 
118                  	   if not an exact match, then prepend.  This is used to fully
119                  	   qualify the chained identifier.
120                  	*/
121                  	if(id != _subIdentifiers[0]){
122                  		_subIdentifiers.prepend(id);
123                  		return true;
124                  	}
125                  	return false;
126                  }
127                  
128 chuck    1.1.2.4 void CQLChainedIdentifierRep::applyContext(QueryContext& inContext)
129                  {
130 chuck    1.1.2.5   CQLIdentifier firstId = _subIdentifiers[0];
131                    
132                    if (!firstId.isScoped())
133                    {
134                      Array<CQLIdentifier> fromList = inContext.getFromList();
135                  
136                      CQLIdentifier matchedId = inContext.findClass(firstId.getName().getString());
137                  
138                      if (firstId.isWildcard())
139                      {
140                        // Example:  SELECT * FROM F AS A 
141                        _subIdentifiers.prepend(fromList[0]);
142                      }  
143                      else
144                      {
145                        if (matchedId.getName().getString() == String::EMPTY)
146                        {
147                  	// Could not find the firstId in the FROM list.
148                  	// Assume the firstId is a property, so prepend the FROM class.
149                  	// Examples:  
150                  	// SELECT p FROM F AS A  
151 chuck    1.1.2.5 	// SELECT p.p1 FROM F AS A (illegal, but caught elsewhere)  
152                          // SELECT p.* FROM F AS A 
153                          // SELECT * FROM F AS A WHERE p ISA B
154                  	_subIdentifiers.prepend(fromList[0]);
155                        }
156                        else
157                        {	
158                  	// The firstId was found in the FROM list, but it could have been 
159                  	// an alias
160                  	if (!matchedId.getName().equal(firstId.getName()))
161                  	{
162                  	  // It was an alias.
163                  	  // Replace the alias with the FROM class
164                  	  // Examples:  
165                  	  // SELECT A.p FROM F AS A 
166                  	  // SELECT A FROM F AS A  (illegal, but caught elsewhere) 
167                  	  // SELECT A.* FROM F AS A
168                  	  // SELECT * FROM F AS A WHERE A ISA B 
169                  	  // SELECT * FROM F AS A WHERE A.p ISA B
170                  	  _subIdentifiers[0] = matchedId;
171                          }
172 chuck    1.1.2.5 	else 
173                  	{
174                  	  // It was not an alias, and it is the only sub-identifier.
175                  	  // Do nothing.
176                  	  // Examples:
177                  	  // SELECT F.p FROM F AS A
178                  	  // SELECT F FROM F AS A (illegal, but caught elsewhere) 
179                  	  // SELECT F.* FROM F AS A
180                  	  // SELECT * FROM F AS A WHERE F ISA B
181                  	  // SELECT * FROM F AS A WHERE F.p ISA B
182                  	  ;
183                  	}
184                        }
185                      }
186                    }
187 humberto 1.1.2.1 }
188                  
189                  void CQLChainedIdentifierRep::parse(String & string){
190                  	/* 
191                  	  - parse string on "."
192                  	  - start from the end of string
193                  	  - if more than one substring found, 
194                  		-- store first found string then
195                  		-- prepend remaining substrings 
196                  	*/
197                  	Char16 delim('.');
198                  	Uint32 index;
199                  	String range;
200                  
201                  	/* remove any array ranges so we dont parse a false . */
202                  	if((index = string.find("[")) != PEG_NOT_FOUND){
203                  		range = string.subString(index);
204                  		string.remove(index);
205                  	}
206                  
207                  	index = string.reverseFind(delim);
208 humberto 1.1.2.1 	if(index == PEG_NOT_FOUND){
209                  		/* append the range we may have removed */
210                  		string.append(range);
211                  		_subIdentifiers.append(CQLIdentifier(string));
212                  	}else{
213                  		/* append the range we may have removed */
214                  		String tmp = string.subString(index+1);
215                  		tmp.append(range);
216                  		PEGASUS_STD(cout) << "tmp = " << tmp << PEGASUS_STD(endl);
217                  		_subIdentifiers.append(CQLIdentifier(tmp));
218                  
219                  		while(index != PEG_NOT_FOUND){
220                  			tmp = string.subString(0,index);
221                  			index = tmp.reverseFind(delim);
222                  			if(index == PEG_NOT_FOUND){
223                  				_subIdentifiers.prepend(CQLIdentifier(tmp));
224                  			}
225                  			else{
226                  				_subIdentifiers.prepend(CQLIdentifier(tmp.subString(index+1)));
227                  			}
228                  		}
229 humberto 1.1.2.1 	}
230                  }
231                  
232                  PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2