📄 cqlcli.cpp
字号:
// instance of CQL_EmbeddedTestPropertyTypes // property InstanceID has value 1001 // property TEArray has array of instance of CQL_TestElement // the array elements have InstanceID properties // set to 10, 11, 12 // property TE has instance of CQL_TestElement // property InstanceID has value 10 // property CS has instance of CIM_ComputerSystem // note that the PrimaryOwnerName property is removed // property TPTArray has array of instance of CQL_TestPropertyTypes // the array is built from the instances compiled in the repository // property TPT has instance of CQL_TestPropertyTypes // this instance is the first instance found in the repository // property SomeClass has class of CIM_Process // property SomeString has a string // property SomeUint8 has a uint8 // CIMInstance embTE("CQL_EmbeddedTestElement"); embTE.addProperty(CIMProperty("InstanceID", CIMValue((Uint64)1000))); embTE.addProperty(CIMProperty("TEArray", CIMValue(testElemArray))); CIMObject _obj1 = testElemArray[0]; CIMValue testElemVal(_obj1); embTE.addProperty(CIMProperty("TE", testElemVal)); CIMValue csVal(testCS); embTE.addProperty(CIMProperty("CS", csVal)); embTE.addProperty(CIMProperty("SomeClass", CIMValue(someClass))); embTE.addProperty(CIMProperty("SomeString", CIMValue(String("Huh?")))); CIMInstance embTPT("CQL_EmbeddedTestPropertyTypes"); embTPT.addProperty(CIMProperty("InstanceID", CIMValue((Uint64)1001))); embTPT.addProperty(CIMProperty("TEArray", CIMValue(testElemArray))); embTPT.addProperty(CIMProperty("TE", testElemVal)); embTPT.addProperty(CIMProperty("CS", csVal)); embTPT.addProperty(CIMProperty("TPTArray", CIMValue(testPropTypesArray))); embTPT.addProperty(CIMProperty("TPT", CIMValue(testPropTypesArray[0]))); embTPT.addProperty(CIMProperty("SomeClass", CIMValue(someClass))); embTPT.addProperty(CIMProperty("SomeString", CIMValue(String("What?")))); embTPT.addProperty(CIMProperty("SomeUint8", CIMValue((Uint8)3))); CIMInstance embSub("CQL_EmbeddedSubClass"); embSub.addProperty(CIMProperty("InstanceID", CIMValue((Uint64)100))); embSub.addProperty(CIMProperty("EmbObjBase", CIMValue(embTE))); embSub.addProperty(CIMProperty("EmbObjSub", CIMValue(embTPT))); instances.clear(); instances.append(embSub);}void sortInstances(Array<CIMInstance>& x){ Uint32 n = x.size(); if (n < 2) return; for (Uint32 i = 0; i < n - 1; i++) { for (Uint32 j = 0; j < n - 1; j++) { // This is not quite a lexigraphical sort because // CQL_TestPropertyTypesMissing will sort before // CQL_TestPropertyTypes. It was done this way // so that the .resgood files don't need to change. String str1 = x[j].getClassName().getString(); String str2 = x[j+1].getClassName().getString(); Uint32 sz1 = str1.size(); Uint32 sz2 = str2.size(); if (sz1 > sz2) { str1.remove(sz2); } else if (sz2 > sz1) { str2.remove(sz1); } int comp = String::compareNoCase(str1, str2); if ((comp == 0 && sz2 > sz1) || comp > 0) { CIMInstance t = x[j]; x[j] = x[j+1]; x[j+1] = t; } } }}Boolean populateInstances(Array<CIMInstance>& _instances, String& className, CIMNamespaceName& _ns, CIMRepository* _rep){ String embSubName("CQL_EmbeddedSubClass"); String embBaseName("CQL_EmbeddedBase"); // IF the class is CIM_RunningOS, then we will setup some references to CIM_ComputerSystem if (className == "CIM_RunningOS") { Array<CIMInstance> cSystems; const CIMName CSClass(String("CIM_ComputerSystem")); try { cSystems.appendArray(_rep->enumerateInstancesForClass( _ns, CSClass)); } catch(Exception& e) { cout << endl << endl << "Exception: Invalid namespace/class: " << e.getMessage() << endl << endl; return false; } // For every computer system instance, make a runningOS that has a reference to it. //The RunningOS will be the instance that is stored. for (Uint32 i=0; i < cSystems.size(); i++) { CIMInstance runOS("CIM_RunningOS"); runOS.addProperty(CIMProperty("Dependent", CIMValue(cSystems[i].getPath()))); _instances.append(runOS); } return true; } if(className != String::EMPTY && className != embSubName && className != embBaseName) { // If the classname was specified, and was not an embedded object class, then // load its instances from the repository. try { const CIMName _testclass(className); _instances = _rep->enumerateInstancesForSubtree( _ns, _testclass, true ); // deep inh true // Sort the CQL instances to avoid the class ordering problem that happens // because the order depends on how the file system orders the class files // in the repository. sortInstances(_instances); } catch(Exception& e){ cout << endl << endl << "Exception: Invalid namespace/class: " << e.getMessage() << endl << endl; return false; } } else { // load all the non-embedded instances we support cout << endl << "Using default class names to test queries. " << endl << endl; const CIMName _testclass1(String("CQL_TestElement")); const CIMName _testclass2(String("CIM_ComputerSystem")); try { // Deep inh = true for CQL_TestElement to also get CQL_TestPropertyTypes // and CQL_TestPropertyTypesMissing _instances = _rep->enumerateInstancesForSubtree( _ns, _testclass1, true ); // deep inh true // Sort the CQL instances to avoid the class ordering problem that happens // because the order depends on how the file system orders the class files // in the repository. // NOTE - do not sort the CIM_ComputerSystem because the resgood files expect // the CIM_ComputerSystem to be after the CQL instances. sortInstances(_instances); // only get the CIM_ComputerSystem _instances.appendArray(_rep->enumerateInstancesForClass( _ns, _testclass2)); } catch(Exception& e) { cout << endl << endl << "Exception: Invalid namespace/class: " << e.getMessage() << endl << endl; return false; } if (className == embSubName || className == embBaseName) { // If the embedded object classname was specified, then build its instances. // Note: this will remove the other instances from the array. buildEmbeddedObjects(_ns, _instances, _rep); } } return true;}void help(const char* command){ cout << command << " queryFile [option]" << endl; cout << " options:" << endl; cout << " -test: "; cout << "1 = evaluate" << endl << " 2 = apply projection" << endl << " 3 = get property list" << endl; cout << " 4 = validate properties" << endl; cout << " 5 = normalize to DOC" << endl; cout << " -className class" << endl; cout << " -nameSpace namespace (Example: root/SampleProvider)" << endl; cout << " -verbose" << endl << endl;}int main(int argc, char ** argv){ // process options if(argc == 1 || (argc > 1 && strcmp(argv[1],"-h") == 0) ){ help(argv[0]); exit(0); } // Since the output of this program will be compared with // a master output file, and the master file will have default // messages, turn off ICU message loading. MessageLoader::_useDefaultMsg = true; String testOption; String className = String::EMPTY; String nameSpace; for(int i = 0; i < argc; i++){ if((strcmp(argv[i],"-test") == 0) && (i+1 < argc)) testOption = argv[i+1]; if((strcmp(argv[i],"-className") == 0) && (i+1 < argc)) className = argv[i+1]; if((strcmp(argv[i],"-nameSpace") == 0) && (i+1 < argc)) nameSpace = argv[i+1]; if((strcmp(argv[i],"-verbose") == 0)) cqlcli_verbose = true; } { // necessary when testing with Purifty so that _statements goes out of scope before the program exits Array<CQLSelectStatement> _statements; // setup test environment // get the configuration variable PEGASUS_HOME const char* peg_home = getenv("PEGASUS_HOME"); if (peg_home == NULL) { cout << "PEGASUS_HOME needs to be set to run this test." << endl; exit(-1); } String repositoryDir(peg_home); repositoryDir.append("/"); // get the makefile build config variable REPOSITORY_NAME const char* repo_name = getenv("REPOSITORY_NAME"); if (repo_name == NULL) repositoryDir.append("repository"); else repositoryDir.append(repo_name); // // Comment out the above 3 lines and umcomment the line below when testing with Rational Purify // //String repositoryDir("c:/pegasus-cvs/pegasus"); CIMNamespaceName _ns; if(nameSpace != String::EMPTY){ _ns = nameSpace; }else{ cout << "Using root/SampleProvider as default namespace." << endl; _ns = String("root/SampleProvider"); } CIMRepository* _rep = new CIMRepository(repositoryDir); RepositoryQueryContext _ctx(_ns, _rep); String lang("CIM:CQL"); String query("dummy statement"); CQLSelectStatement _ss(lang,query,_ctx); if (_ss.getQuery() != query || _ss.getQueryLanguage() != lang) { cout << "ERROR: unable to get query or query language from select statement" << endl; return 1; } char text[1024]; char* _text; // setup Test Instances Array<CIMInstance> _instances; if (!populateInstances(_instances, className, _ns, _rep)) return 1; // demo setup if(argc == 3 && strcmp(argv[2],"Demo") == 0){ cout << "Running Demo..." << endl; _instances.clear(); const CIMName _testclassDEMO(String("CIM_Process")); _instances.appendArray(_rep->enumerateInstancesForSubtree( _ns, _testclassDEMO )); _instances.remove(6,6); } for(Uint32 i = 0; i < _instances.size(); i++){ CIMObjectPath op = _instances[i].getPath(); op.setHost("a.b.com"); op.setNameSpace(_ns); _instances[i].setPath(op); } // setup input stream if(argc >= 2){ ifstream queryInputSource(argv[1]); if(!queryInputSource){ cout << "Cannot open input file.\n" << endl; return 1; } int statementsInError = 0; int lineNum = 0; while(!queryInputSource.eof()) { lineNum++; queryInputSource.getline(text, 1024); char* _ptr = text; _text = strcat(_ptr,"\n"); // check for comments and ignore // a comment starts with a # as the first non whitespace character on the line char _comment = '#'; int i = 0; while(text[i] == ' ' || text[i] == '\t') i++; // ignore whitespace if(text[i] != _comment) { if(!(strlen(_text) < 2) && i == 0) { try { CQLParser::parse(text,_ss); _statements.append(_ss); } // end-try catch(Exception& e){ cout << "Caught Exception: " << getStatementString(e.getMessage()) << endl; try { String stmt(text); cout << "Statement with error = " << getStatementString(stmt) << endl; } catch (Exception & e1) { cout << "Error printing statement: " << getStatementString(e1.getMessage()) << endl; } _ss.clear(); statementsInError++; } // end-catch } // end-if else { if (cqlcli_verbose) cout << "IGNORING line " << lineNum << endl; } } // while !eof behaves differently on HP-UX, seems like it takes an extra iteration to hit eof, this leaves "text" with // the previous value from getline(..), which causes a duplicate parse of the last select statement in the query file, // FIX: we clear text before doing another getline(..) text[0] = 0; } // end-while queryInputSource.close(); if (statementsInError) { cout << "There were " << statementsInError << " statements that did NOT parse." << endl; // return 1; } try{ _applyProjection(_statements,_instances, testOption); _validateProperties(_statements,_instances, testOption); _getPropertyList(_statements,_instances, _ns, testOption); _evaluate(_statements,_instances, testOption); _normalize(_statements,_instances, testOption); } catch(Exception e){ cout << getStatementString(e.getMessage()) << endl; } catch(...){ cout << "CAUGHT ... BADNESS HAPPENED!!!" << endl; } }else{ cout << "Invalid number of arguments.\n" << endl; } delete _rep; // cleanup repository pointer }// necessary when testing with Purifty so that _statements goes out of scope before the program exits return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -