Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 11.55 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Pseudo-code for Getting the Table Expression
  2. // This example code shows how the IP can use the various API calls to get details of the Search
  3. // Expression and Join Expression. The starting point is a call to dam_getTableSearchExp from the EXECUTE // function of the IP.
  4. {
  5.     int iTableNum;
  6.     DAM_HLOGEXP hSearchExp, hJoinExp;
  7.     int iJoinType;
  8.     int bPartialExp;
  9.     /* get the search expression and Join expression handles */
  10.     iTableNum = 0;
  11.     dam_getTableSearchExp(hstmt, &iTableNum, &hSearchExp, &iJoinType, &hJoinExp, &bPartialExp);
  12.     /* get filter conditions */
  13.     ip_print_logexp(hSearchExp);
  14.     /* check the Join type */
  15.     if (hJoinExp) {
  16.         if (iJoinType == SQL_JOIN_LEFT_OUTER) printf(" OUTER JOIN ");
  17.         if (iJoinType == SQL_JOIN_LEFT_INNER || iJoinType == SQL_JOIN_OLD_STYLE)
  18.             printf("INNER JOIN ");
  19.         /* get Join conditions */
  20.         ip_print_logexp(hJoinExp);
  21.     }
  22.     int    ip_print_logexp(DAM_HLOGEXP hSearchExp)
  23.     {
  24.         int     iType; /* AND, OR, NOT or CONDITION */
  25.         DAM_HLOGEXP hLeft, hRight;
  26.         DAM_HCOND hCond;
  27.         /* get and print the logical expression */
  28.         damex_describeLogicExp(hSearchExp,
  29.             &iType, /* AND, OR, NOT or CONDITION */
  30.             &hLeft,
  31.             &hRight,&hCond);
  32.         switch (iType) {
  33.         case SQL_EXP_COND:
  34.             ip_print_cond(hCond);
  35.             break;
  36.         case SQL_EXP_AND:
  37.             /* left-expression AND right-expression */
  38.             ip_print_logexp(hLeft);
  39.             printf( " AND ");
  40.             ip_print_logexp(hRight);
  41.             break;
  42.         case SQL_EXP_OR:
  43.             /* left-expression OR right-expression */
  44.             ip_print_logexp(hLeft);
  45.             printf( " OR ");
  46.             ip_print_logexp(hRight);
  47.             break;
  48.         case SQL_EXP_NOT:
  49.             /* NOT left-expression */
  50.             printf( " NOT ");
  51.             ip_print_logexp(hLeft);
  52.             break;
  53.         }
  54.         return DAM_SUCCESS;
  55.     }
  56.     int    ip_print_cond(DAM_HCOND hCond)
  57.     {
  58.         int             iType;
  59.         DAM_HVALEXP    hLeft, hRight, hExtra;
  60.         damex_describeCond(hCond,
  61.             &iType, /* >, <, =, BETWEEN etc.*/
  62.             &hLeft,
  63.             &hRight,
  64.             &hExtra); /* used for BETWEEN */
  65.         /* EXISTS and UNIQUE predicates */
  66.         if (iType & (SQL_OP_EXISTS | SQL_OP_UNIQUE)) {
  67.             if (iType & SQL_OP_NOT) printf( " NOT ");
  68.             if (iType & SQL_OP_EXISTS) printf( " EXISTS ");
  69.             if (iType & SQL_OP_UNIQUE) printf( " UNIQUE ");
  70.             ip_print_valexp(hLeft);
  71.         }
  72.         /* conditional predicates */
  73.         if (iType & ( SQL_OP_SMALLER | SQL_OP_GREATER | SQL_OP_EQUAL)) {
  74.             ip_print_valexp(hLeft);
  75.             if (iType & SQL_OP_NOT) {
  76.                if (iType & SQL_OP_EQUAL) printf( " <> ");
  77.             }
  78.             else {
  79.                 printf( " ");
  80.                 if (iType & SQL_OP_SMALLER) printf( "<");
  81.                 if (iType & SQL_OP_GREATER) printf( ">");
  82.                 if (iType & SQL_OP_EQUAL) printf( "=");
  83.                 printf( " ");
  84.             }
  85.             if (iType & (SQL_OP_QUANTIFIER_ALL | SQL_OP_QUANTIFIER_SOME | SQL_OP_QUANTIFIER_ANY) {
  86.                 if (iType & SQL_OP_QUANTIFIER_ALL) printf( " ALL ");
  87.                 if (iType & SQL_OP_QUANTIFIER_SOME) printf( " SOME ");
  88.                 if (iType & SQL_OP_QUANTIFIER_ANY) printf( " ANY ");
  89.             }
  90.             ip_print_valexp(hRight);
  91.         } /* conditional predicate */
  92.         /* like predicate */
  93.         if (iType & SQL_OP_LIKE) {
  94.             ip_print_valexp(hLeft);
  95.             if (iType & SQL_OP_NOT) printf( " NOT ");
  96.             printf( " LIKE ");
  97.             ip_print_valexp(hRight);
  98.             if (hExtra) {
  99.                 printf( " ESCAPE ");
  100.                 ip_print_valexp(hExtra);
  101.             }
  102.         } /* like predicate */
  103.         /* Is NULL predicate */
  104.         if (iType & SQL_OP_ISNULL) {
  105.             ip_print_valexp(hLeft);
  106.             if (iType & SQL_OP_NOT)
  107.                 printf( " IS NOT NULL ");
  108.             else
  109.                 printf( " IS NULL ");
  110.         }
  111.         /* IN predicate */
  112.         if (iType & SQL_OP_IN) {
  113.             ip_print_valexp(hLeft);
  114.             if (iType & SQL_OP_NOT) printf( " NOT ");
  115.             printf( " IN (");
  116.             ip_print_valexp(hRight);
  117.             pintf( " )");
  118.         }
  119.         /* BETWEEN predicate */
  120.         if (iType & SQL_OP_BETWEEN) {
  121.         /* check if the between is a form of ( >= and < ) OR (> and <)
  122.         OR (> and <=)
  123.             */
  124.             if ((iType & SQL_OP_BETWEEN_OPEN_LEFT) ||
  125.                 (iType & SQL_OP_BETWEEN_OPEN_RIGHT)) {
  126.                 /* format it as two conditions */
  127.                 ip_print_valexp(hLeft);
  128.                 if (iType & SQL_OP_BETWEEN_OPEN_LEFT)
  129. printf( " > ");
  130.                 else
  131. printf( " >= ");
  132.                 ip_print_valexp(hRight);
  133.                 printf( " AND ");
  134.                 ip_print_valexp(hLeft);
  135.                 if (iType & SQL_OP_BETWEEN_OPEN_RIGHT)
  136. printf( " < ");
  137.                 else
  138.                     printf( " <= ");
  139.                 ip_print_valexp(hExtra);
  140.             }
  141.             else {
  142.                 /* standard BETWEEN pattern */
  143.                 ip_print_valexp(hLeft);
  144.                 if (iType & SQL_OP_NOT) printf( " NOT ");
  145.                 printf( " BETWEEN ");
  146.                 ip_print_valexp(hRight);
  147.                 printf( " AND ");
  148.                 ip_print_valexp(hExtra);
  149.             }
  150.         } /* BETWEEN */
  151.         return DAM_SUCCESS;
  152. }
  153.  
  154. int      ip_print_valexp(DAM_HVALEXP hValExp)
  155. {
  156.     int         iType; /* literal value, column, +, -, *, / etc */
  157.     int         iFuncType;
  158.     DAM_HVALEXP hLeftValExp;
  159.     DAM_HVALEXP hRightValExp;
  160.     DAM_HVAL hVal;
  161.     DAM_HSCALAR_VALEXP hScalarValExp;
  162.     damex_describeValExp(hValExp, &iType, /* literal value, column, +, -, *, / etc */
  163.         &iFuncType,
  164.         &hLeftValExp,
  165.         &hRightValExp,
  166.         &hVal,
  167.         &hScalarValExp
  168.         );
  169.     /* function type (SQL_F_COUNT, SQL_F_MIN etc will not occur in search expressions */
  170.     switch (iType) {
  171.     case SQL_VAL_EXP_VAL:
  172.         ip_print_val(hVal);
  173.         break;
  174.     case SQL_VAL_EXP_ADD:
  175.     case SQL_VAL_EXP_SUBTRACT:
  176.         ip_print_valexp(hLeftValExp);
  177.         if (iType == SQL_VAL_EXP_AND) printf( "+");
  178.         if (iType == SQL_VAL_EXP_SUBTRACT) printf( "-");
  179.         if (iType == SQL_VAL_EXP_MULTIPLE) printf( "*");
  180.         if (iType == SQL_VAL_EXP_DIVIDE) printf( "/");
  181.         ip_print_valexp(hRightValExp);
  182.         break;
  183.     case SQL_VAL_EXP_SCALAR:
  184.         ip_print_scalar_valexp(hScalarValExp);
  185.         break;
  186.     }
  187.     return DAM_SUCCESS;
  188. }
  189.  
  190.  
  191. int     ip_print_scalar_valexp(DAM_HSCALAR_VALEXP    hScalarValExp)
  192. {
  193.     char sName[DAM_MAX_ID_LEN+1];
  194.     DAM_HVALEXP_LIST hValExpList;
  195.     damex_describeScalarValExp(hScalarValExp, sName, &hValExpList);
  196.     printf( "%s", sName);
  197.     printf( "( ");
  198.     if (hValExpList) ip_print_valexp_list(hValExpList);
  199.     printf( ") ");
  200.     return DAM_SUCCESS;
  201. }
  202.  
  203. int    ip_print_valexp_list(DAM_HVALEXP_LIST hValExpList)
  204. {
  205.     DAM_HVALEXP    hValExp;
  206.     int            iFirst = TRUE;
  207.     hValExp = damex_getFirstValExp(hValExpList);
  208.     while (hValExp) {
  209.         if (!iFirst)
  210.             printf( ", ");
  211.         else
  212.             iFirst = FALSE;
  213.         ip_print_valexp(hValExp);
  214.         hValExp = damex_getNextValExp(hValExpList);
  215.     }
  216.     printf( " ");
  217.     return DAM_SUCCESS;
  218. }
  219. int    ip_print_val(DAM_HQUERY hquery, DAM_HVAL hVal)
  220. {
  221.     int         iType; /* literal value, column */
  222.     int        iXoType; /* type of literal value – INTEGER, CHAR etc */
  223.     void       *pData;
  224.     DAM_HCOL    hCol;
  225.     DAM_HQUERY hSubQuery;
  226.     damex_describeVal(hVal, &iType,
  227.         &iXoType,
  228.         &pData,
  229.         &hCol,
  230.         &hSubQuery);
  231.     switch (iType) {
  232.     case SQL_VAL_DATA_CHAIN:
  233.         break;
  234. case SQL_VAL_NULL:
  235.         printf( "NULL"); break;
  236. case SQL_VAL_QUERY: /* query */
  237.         ip_print_query_values(hSubQuery);
  238.         break;
  239.     case SQL_VAL_COL: /* value is the column value */
  240.         ip_print_col(hCol); break;
  241.     case SQL_VAL_INTERVAL:
  242.         break;
  243.     case SQL_VAL_LITERAL: /* value is a Xo Type literal */
  244.         ip_print_data(iXoType, pData);
  245.         break;
  246.     default:
  247.         printf( "Invalid Value Type:%d", iType); break;
  248.     }
  249.     return DAM_SUCCESS;
  250. }
  251.  
  252. int    ip_print_query_values(DAM_HQUERY hQuery)
  253. {
  254.     int         iRetCode;
  255.     int         iXoType;
  256.     void       *pVal;
  257.     int         iValLen;
  258.     int         bFoundVal = FALSE;
  259.     iRetCode = dam_getQueryFirstResultValue(hQuery, &iXoType, &pVal, &iValLen);
  260.     while (iRetCode == DAM_SUCCESS) {
  261.         if (bFoundVal)
  262.             printf(", ");
  263.         if (iValLen == XO_NULL_DATA)
  264.             printf( "NULL");
  265.         else
  266.             ip_print_data(iXoType, pVal);
  267.         bFoundVal = TRUE;
  268.         iRetCode = dam_getQueryNextResultValue(hQuery, &iXoType, &pVal, &iValLen);
  269.     }
  270.     /* when results are empty, we will use NULL value */
  271.     if (!bFoundVal)
  272.         printf("NULL");
  273.     if (iRetCode != DAM_NO_DATA_FOUND) return iRetCode;
  274.     return DAM_SUCCESS;
  275. }
  276.  
  277. int     ip_print_data(int iXoType, void *pData)
  278. {
  279.     xo_tm        *pxoTime;
  280.     switch (iXoType) {
  281.     case XO_TYPE_CHAR: /* pVal is a char literal */
  282.     case XO_TYPE_VARCHAR:
  283.     case XO_TYPE_NUMERIC:
  284.     case XO_TYPE_DECIMAL:
  285.         printf( "'%s'", (char *)pData);
  286.         break;
  287.     case XO_TYPE_INTEGER: /* pVal is a integer literal */
  288.         printf( "%ld", *(long *)pData);
  289.         break;
  290.     case XO_TYPE_SMALLINT: /* pVal is small integer literal */
  291.         printf( "%d", (int)(*(short *)pData));
  292.         break;
  293.     case XO_TYPE_FLOAT: /* pVal is a double literal */
  294.     case XO_TYPE_DOUBLE:
  295.         printf( "%Lf", *(double *)pData);
  296.         break;
  297.     case XO_TYPE_REAL: /* pVal is a float literal */
  298.         printf( "%f", *(float *)pData);
  299.         break;
  300.     case XO_TYPE_DATE:
  301.         pxoTime = (xo_tm *)pData;
  302.         printf( "{d '%d-%02d-%02d'}", pxoTime->tm_year,
  303.             pxoTime->tm_mon+1, pxoTime->tm_mday);
  304.         break;
  305.     case XO_TYPE_TIME:
  306.         pxoTime = (xo_tm *)pData;
  307.         printf( "{t '%02d:%02d:%02d'}", pxoTime->tm_hour,
  308.             pxoTime->tm_min, pxoTime->tm_sec);
  309.         break;
  310.     case XO_TYPE_TIMESTAMP:
  311.         pxoTime = (xo_tm *)pData;
  312.         if (pxoTime->tm_frac > 0) {
  313.             int frac;
  314.             frac = (int) (pxoTime->tm_frac * 0.000001);
  315.             printf( "{ts '%d-%02d-%02d %02d:%02d:%02d.%03d'}",
  316.                 pxoTime->tm_year, pxoTime->tm_mon+1, pxoTime->tm_mday,
  317.                 pxoTime->tm_hour, pxoTime->tm_min, pxoTime->tm_sec, frac);
  318.         }
  319.         else
  320.             printf( "{ts '%d-%02d-%02d %02d:%02d:%02d'}",
  321.             pxoTime->tm_year, pxoTime->tm_mon+1, pxoTime->tm_mday,
  322.             pxoTime->tm_hour, pxoTime->tm_min, pxoTime->tm_sec);
  323.         break;
  324.     default:
  325.         printf( "Invalid Xo Value Type:%d", iXoType);
  326.         break;
  327.     }
  328.     return DAM_SUCCESS;
  329. }
  330.  
  331. int     ip_print_col(DAM_HCOL hCol)
  332. {
  333.     int         iTableNum, iColNum;
  334.     char        sColName[DAM_MAX_ID_LEN+1];
  335.     DAM_HSTMT   hstmt = pStmtDA->dam_hstmt; /* we need to pass this variable as argument */
  336.     damex_describeCol(hCol,
  337.         &iTableNum,
  338.         &iColNum,
  339.         sColName, NULL, NULL, NULL, NULL);
  340.     /* check for correlated column and join columns */
  341.     if (damex_isCorrelatedCol(hCol) ||
  342.         (pStmtDA->iCurTableNum != DAM_NOT_SET && iTableNum != pStmtDA->iCurTableNum)) {
  343.         int iValLen;
  344.         void *pVal;
  345.         dam_getJoinColValue(hstmt, hCol, XO_TYPE_CHAR, &pVal, &iValLen);
  346.         if (iValLen == XO_NULL_DATA)
  347. printf( " NULL ");
  348.         else
  349.             printf( " '%s' ", (char *)pVal);
  350.     }
  351.     else {
  352.         printf( "T%d.%s", iTableNum, sColName);
  353.      }
  354. return DAM_SUCCESS;
  355. }