C example--dynamic joins


The following example shows you how to update an existing code to use the groupBy and having clauses and the related structures introduced in release 7.6.03. Lines with required code changes are marked with //

For information about using the groupBy and having clauses and the related aggregate functions, see Using aggregate functions, groupBy clauses, and having clauses.

void queryPrinterAndComputer(ARControlStruct *ctrl, ARStatusList *status)
{
  ARMultiSchemaFieldFuncValueListList msEntryList = {0, NULL}; /**/
  ARMultiSchemaFuncQueryFromList fromList = {0, NULL}; /**/
  ARMultiSchemaFieldFuncList getListFields = {0, NULL}; /**/
  ARMultiSchemaSortList sortList;
  unsigned int numMatches;
  unsigned int *numMatchesPtr;
  int result;
  ARMultiSchemaFuncQueryFromStruct fromItem[2] = {{0,},}; /* 0-fill */ /**/
  ARMultiSchemaQualifierStruct *qualifier;
  ARMultiSchemaQualifierStruct wherequal={0, NULL};
  ARMultiSchemaQualifierStruct *qual=NULL;
  ARMultiSchemaRelOpStruct relop;
  ARMultiSchemaFieldFuncStruct fieldId[7] = {{"",},}; /* 0-fill */ /**/
  ARMultiSchemaFieldFuncStruct fieldId[7] = {{"",},}; /* 0-fill */ /**/
  /* fill up query struct */
  fromList.numItems = 2;
  fromList.listPtr = fromItem;
  qualifier = &wherequal; /*empty qual*/
  /* The following sort list is used to generate the SQL query's ORDER BY */
  /* clause. It instructs the function to sort first on the Printer ID field */
  /* and then on the Computer ID field. */
  sortList.numItems = 2;
  sortList.listPtr = (ARMultiSchemaSortStruct*)
     malloc(sizeof(ARMultiSchemaSortStruct) * sortList.numItems);
  sortList.listPtr[0].fieldId.fieldId = 536870914; // Printer ID
  strcpy(sortList.listPtr[0].fieldId.queryFromAlias, "Printer");
  sortList.listPtr[0].sortOrder = 1;
  sortList.listPtr[1].fieldId.fieldId = 536870914; // Computer ID
  strcpy(sortList.listPtr[1].fieldId.queryFromAlias, "Computer");
  sortList.listPtr[1].sortOrder = 1;
  fromItem[0].type = AR_MULTI_SCHEMA_SCHEMA_NAME;
  strcpy(fromItem[0].u.schemaName, "Printer");
  fromItem[0].joinType = 0;
  fromItem[0].joinQual = NULL;
  fromItem[1].type = AR_MULTI_SCHEMA_SCHEMA_NAME;
  strcpy(fromItem[1].u.schemaName, "Computer");
  fromItem[1].joinType = 0;
  fromItem[1].joinQual = qualifier;
  numMatchesPtr = &numMatches;
  /* The following fields are used in the getListFields parameter, which */
  /* specifies the fields to include in the results. This parameter */
  /* generates the SQL query's SELECT list. */
  strcpy(fieldId[0].queryFromAlias, "Printer");
  fieldId[0].fieldId = 1; /*Printer entryId */
  fieldId[0].funcId = AR_MULTI_SCHEMA_FUNC_NONE;  /**/
  strcpy(fieldId[1].queryFromAlias, "Printer");
  fieldId[1].fieldId = 536870913; /* Printer name */
  fieldId[1].funcId = AR_MULTI_SCHEMA_FUNC_NONE;  /**/
  strcpy(fieldId[2].queryFromAlias, "Printer");
  fieldId[2].fieldId = 536870914; /* Printer ID */
  fieldId[2].funcId = AR_MULTI_SCHEMA_FUNC_NONE;  /**/
  strcpy(fieldId[3].queryFromAlias, "Printer");
  fieldId[3].fieldId = 536870915; /* Printer type */
  fieldId[3].funcId = AR_MULTI_SCHEMA_FUNC_NONE;  /**/
  strcpy(fieldId[4].queryFromAlias, "Computer");
  fieldId[4].fieldId = 1; /* Computer entryId */
  fieldId[4].funcId = AR_MULTI_SCHEMA_FUNC_NONE;  /**/
  strcpy(fieldId[5].queryFromAlias, "Computer");
  fieldId[5].fieldId = 536870913; /* Computer name */
  fieldId[5].funcId = AR_MULTI_SCHEMA_FUNC_NONE;  /**/
  strcpy(fieldId[6].queryFromAlias, "Computer");
  fieldId[6].fieldId = 536870914; /* Computer ID */
  fieldId[6].funcId = AR_MULTI_SCHEMA_FUNC_NONE;  /**/
  getListFields.numItems = 7;
  getListFields.listPtr = fieldId;
  /* This is the join qualifier, which is used to generate the SQL query's */
  /* ON clause. */
  qualifier->operation = AR_COND_OP_REL_OP;
  qualifier->u.relOp = &relop;
  relop.operation = AR_REL_OP_EQUAL;
  relop.operandLeft.tag = AR_FIELD;
  strcpy(relop.operandLeft.u.fieldId.queryFromAlias, "Printer");
  relop.operandLeft.u.fieldId.fieldId = 536870914;
  relop.operandRight.tag = AR_FIELD;
  strcpy(relop.operandRight.u.fieldId.queryFromAlias, "Computer");
  relop.operandRight.u.fieldId.fieldId = 536870914;
  /* Call routine */
  result = ARGetListEntryWithMultiSchemaFields(ctrl, &fromList,
        &getListFields, qualifier, &sortList, AR_START_WITH_FIRST_ENTRY,
        AR_NO_MAX_LIST_RETRIEVE, FALSE,
        NULL, NULL,  /* groupBy and having */ /**/
        &msEntryList, numMatchesPtr, status);
}

 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*

BMC Helix Innovation Suite 25.2