Esempio n. 1
0
// LCOV_EXCL_START
  Lng32 prepare(const char *stmtText)
  {
    if (!initialized_)
    {
      return -9999;
    }

    delete [] stmtText_;
    stmtText_ = new char[strlen(stmtText) + 1];
    strcpy(stmtText_, stmtText);
    
    Lng32 result = 0;

    if (!result)
      result = SQL_EXEC_ClearDiagnostics(NULL);
    if (!result)
      result = SQL_EXEC_SetDescItem(&stmtTextDesc_, 1, SQLDESC_TYPE,
                                    SQLTYPECODE_CHAR, 0);
    if (!result)
      result = SQL_EXEC_SetDescItem(&stmtTextDesc_, 1, SQLDESC_LENGTH,
                                    strlen(stmtText_), 0);
    if (!result)
      result = SQL_EXEC_SetDescItem(&stmtTextDesc_, 1, SQLDESC_VAR_PTR,
                                    (Long) stmtText_, 0);
    if (!result)
      result = SQL_EXEC_Prepare(&stmtId_, &stmtTextDesc_);

    if (!result)
      result = SQL_EXEC_DescribeStmt(&stmtId_, &inDesc_, &outDesc_);

    if (!result)
      result = SQL_EXEC_GetDescEntryCountBasic(&outDesc_, &numOutColumns_);

    if (!result)
      result = SQL_EXEC_GetDescEntryCountBasic(&inDesc_, &numInColumns_);

    SQL_ERROR_HANDLER(result);
    return result;
  }
Esempio n. 2
0
Lng32 HSSqTableDef::DescribeColumnNames()
  {
    Lng32 entry, len;
    NAString query;
    char colName[ComMAX_1_PART_INTERNAL_UTF8_NAME_LEN_IN_BYTES + 2];
    HSLogMan *LM = HSLogMan::Instance();

    SQLMODULE_ID module;
    init_SQLMODULE_ID(&module);

    SQLSTMT_ID *stmt = new(STMTHEAP) SQLSTMT_ID;
    init_SQLCLI_OBJ_ID(stmt);
    stmt->module = &module;
    stmt->name_mode = stmt_handle;

    SQLDESC_ID *srcDesc = new(STMTHEAP) SQLDESC_ID;
    init_SQLCLI_OBJ_ID(srcDesc);
    srcDesc->module = &module;
    srcDesc->name_mode = desc_handle;

    SQLDESC_ID *outputDesc = new(STMTHEAP) SQLDESC_ID;
    init_SQLCLI_OBJ_ID(outputDesc);
    outputDesc->module = &module;
    outputDesc->name_mode = desc_handle;

    // Use the header information from a 'select *' to get the column names. 
    // Note that this works for SJIS and UTF8 since the names returned through
    // CLI are encoded correctly.
    retcode_ = setHasSyskeyFlag();
    HSHandleError(retcode_);
    if (hasSyskey_)
      query  = "SELECT SYSKEY, * FROM ";
    else
      query  = "SELECT * FROM ";
    if(objActualFormat_ == SQLMP)
      query += getTableName(tableName_->data(), nameSpace_);
    else
      query += getTableName(ansiName_->data(), nameSpace_);

    retcode_ = SQL_EXEC_ClearDiagnostics(stmt);
    // to prevent false alarms for statement heap memory allocation "smt"
    // coverity[leaked_storage]
    HSHandleError(retcode_);

    retcode_ = SQL_EXEC_AllocStmt(stmt, 0);
    HSHandleError(retcode_);
    retcode_ = SQL_EXEC_AllocDesc(srcDesc, 1);
    HSHandleError(retcode_);
    retcode_ = SQL_EXEC_AllocDesc(outputDesc, 4096);
    HSHandleError(retcode_);

    retcode_ = SQL_EXEC_SetDescItem(srcDesc, 1, SQLDESC_TYPE_FS,
                                    REC_BYTE_V_ANSI, 0);
    HSHandleError(retcode_);
    retcode_ = SQL_EXEC_SetDescItem(srcDesc, 1, SQLDESC_VAR_PTR,
                                    (Long)query.data(), 0);
    HSHandleError(retcode_);
    retcode_ = SQL_EXEC_SetDescItem(srcDesc, 1, SQLDESC_LENGTH,
#pragma nowarn(1506)   // warning elimination
                                    query.length() + 1, 0);
#pragma warn(1506)  // warning elimination
    HSHandleError(retcode_);
    // SQLDESC_CHAR_SET must be the last descriptor item set, otherwise
    // it may get reset by other calls to SQL_EXEC_SetDescItem().
    NAString charSet = ActiveSchemaDB()->getDefaults().getValue(ISO_MAPPING);
    NAString defCS   = ActiveSchemaDB()->getDefaults().getValue(DEFAULT_CHARSET);
    retcode_ = SQL_EXEC_SetDescItem(srcDesc, 1, SQLDESC_CHAR_SET,
                                    SQLCHARSETCODE_UTF8
                                    , 0);
    HSHandleError(retcode_);

    // ---------------------------------------------------------------------
    // Prepare the statement
    // ---------------------------------------------------------------------
    SQL_QUERY_COST_INFO query_cost_info;
    SQL_QUERY_COMPILER_STATS_INFO comp_stats_info;
   
    retcode_ = SQL_EXEC_Prepare2(stmt, srcDesc,NULL,0,NULL,&query_cost_info, &comp_stats_info,NULL,0,0);
    HSHandleError( retcode_);

    // ---------------------------------------------------------------------
    // describe the column information into the output descriptor
    // ---------------------------------------------------------------------
    retcode_ = SQL_EXEC_DescribeStmt(stmt, 0, outputDesc);
    HSHandleError(retcode_);

    retcode_ = SQL_EXEC_GetDescEntryCount(outputDesc, &numCols_);
    HSHandleError(retcode_);

    colInfo_ = new(STMTHEAP) HSColumnStruct[numCols_];
    for (Int32 i = 0; i < numCols_; i++)
      {
                                                  /*==   GET COLUMN NAME   ==*/
        entry = i + 1;
        retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                        SQLDESC_NAME,
                                        0, colName, sizeof(colName), &len, 0);
        if ((retcode_ == 0) &&
            (len >= sizeof(colName) ))
          retcode_ = -1;
        HSHandleError(retcode_);
        colName[len] = '\0';
        *colInfo_[i].colname = &*colName;
                                                  /*== GET COLUMN DATATYPE ==*/
        retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                        SQLDESC_TYPE_FS,
                                        &colInfo_[i].datatype,
                                        0, 0, 0, 0);
        HSHandleError(retcode_);

        retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                        SQLDESC_NULLABLE,
                                        &colInfo_[i].nullflag,
                                        0, 0, 0, 0);
        HSHandleError(retcode_);
                                                  /*==  GET COLUMN LENGTH  ==*/
        retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                        SQLDESC_OCTET_LENGTH,
                                        &colInfo_[i].length,
                                        0, 0, 0, 0);
        HSHandleError(retcode_);

        // If applicable, get the character set, precision and scale
        if (DFS2REC::isAnyCharacter(colInfo_[i].datatype))
          {
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                            SQLDESC_CHAR_SET,
                                            (Lng32*)&colInfo_[i].charset, 0, 0, 0, 0);
            HSHandleError(retcode_);
            // UCS2 cols not supported in MODE_SPECIAL_1 or 2 and do not support case insensitivity.
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                            SQLDESC_CASEINSENSITIVE,
                                            (Lng32*)&colInfo_[i].caseInsensitive, 0, 0, 0, 0);
            HSHandleError(retcode_);
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                            SQLDESC_COLLATION,
                                            (Lng32*)&colInfo_[i].colCollation, 0, 0, 0, 0);
            HSHandleError(retcode_);
          }
        else if ((colInfo_[i].datatype >= REC_MIN_BINARY &&  // May be type NUMERIC
                  colInfo_[i].datatype <= REC_MAX_BINARY)    //    instead of INT
                     ||
                 (colInfo_[i].datatype >= REC_MIN_DECIMAL &&
                 colInfo_[i].datatype <= REC_MAX_DECIMAL))
          {
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,                
                                           SQLDESC_PRECISION,                  
                                           &colInfo_[i].precision, 0, 0, 0, 0);
            HSHandleError(retcode_);
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,                
                                           SQLDESC_SCALE,                  
                                           &colInfo_[i].scale, 0, 0, 0, 0);
            HSHandleError(retcode_);
          }
        else if (DFS2REC::isDateTime(colInfo_[i].datatype))
          {
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                           SQLDESC_DATETIME_CODE,
                                           &colInfo_[i].precision, 0, 0, 0, 0);
            HSHandleError(retcode_);
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,
                                           SQLDESC_PRECISION,
                                           &colInfo_[i].scale, 0, 0, 0, 0);
            HSHandleError(retcode_);
          }
        else if (DFS2REC::isInterval(colInfo_[i].datatype))
          {
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,                
                                            SQLDESC_INT_LEAD_PREC,              
                                            &colInfo_[i].precision, 0, 0, 0, 0);
            HSHandleError(retcode_);                                           
            retcode_ = SQL_EXEC_GetDescItem(outputDesc, entry,                
                                            SQLDESC_PRECISION,                  
                                            &colInfo_[i].scale, 0, 0, 0, 0);
            HSHandleError(retcode_);
          }
        else
          {
            /* No additional information about column attributes needed */
          }

        if (LM->LogNeeded())
          {
            sprintf(LM->msg, "COLUMN [%s]: (%d, %d, %d, %d, %d, %d)"
                                , colInfo_[i].colname->data()
                                , colInfo_[i].datatype
                                , colInfo_[i].nullflag
                                , colInfo_[i].charset
                                , colInfo_[i].length
                                , colInfo_[i].precision
                                , colInfo_[i].scale
                   );
            LM->Log(LM->msg);
          }
      }
    retcode_ = SQL_EXEC_DeallocStmt(stmt);
    HSHandleError(retcode_);

    return 0;
  }