Beispiel #1
0
// Create a new SQL module id.
SQLCLI_LIB_FUNC
SQLMODULE_ID* new_SQLMODULE_ID(
	Lng32 version, const char* name, 
	Lng32 timestamp, 
	const char* charset,
	Lng32 name_len)
{
   SQLMODULE_ID * m = new SQLMODULE_ID;
   init_SQLMODULE_ID(m, version, name, timestamp, charset, name_len);
   return m;
}
// returns -1, if a transaction is active; 0, otherwise.
// Optionally returns the transaction identifier, if transid is passed in.
short SqlciEnv::statusTransaction(Int64 * transid)
{
  // if a transaction is active, get the transid by calling the CLI procedure. 
  SQLDESC_ID transid_desc; // added for multi charset module names
  SQLMODULE_ID module;

  init_SQLCLI_OBJ_ID(&transid_desc);
  init_SQLMODULE_ID(&module);

  module.module_name = 0;
  transid_desc.module = &module;
  transid_desc.name_mode = desc_handle;

  HandleCLIErrorInit();
  
  Lng32 rc = SQL_EXEC_AllocDesc(&transid_desc, 1);

  HandleCLIError(rc, this);
  
  Int64 transid_;
  rc = SQL_EXEC_SetDescItem(&transid_desc, 1, SQLDESC_VAR_PTR,
			    (Long)&transid_, 0);
  if (rc)
    SQL_EXEC_DeallocDesc(&transid_desc);
  HandleCLIError(rc, this);
  
  rc = SQL_EXEC_Xact(SQLTRANS_STATUS, &transid_desc);
  if (rc == 0)
    {
      if (transid) *transid = transid_;	// return transID if arg was passed in.
      rc = -1;				// transaction is active.
    }
  else
    rc = 0;
  SQL_EXEC_DeallocDesc(&transid_desc);
	
  return (short)rc;
	
}
Beispiel #3
0
short QueryId::process(SqlciEnv * sqlci_env)
{
  Lng32 retcode = 0;

  HandleCLIErrorInit();
  
  char * stmtName = get_argument();

  if ((stmtName) &&
      (! sqlci_env->get_prep_stmts()->get(stmtName)))
    {
       sqlci_env->diagsArea() << DgSqlCode(-SQLCI_STMT_NOT_FOUND)
				 << DgString0(stmtName);
       return 0;
    }

  Logfile *log = sqlci_env->get_logfile();
  char sprintfBuf[225];    

  if (!stmtName)
  {
    //try to find the last executed statement if present
    if (sqlci_env->lastExecutedStmt() && sqlci_env->lastExecutedStmt()->getStmtNameLen() > 0)
      stmtName = sqlci_env->lastExecutedStmt()->getStmtName();
    //if not, try to find the last prepared statement if present
    else if (sqlci_env->getLastAllocatedStmt() && sqlci_env->getLastAllocatedStmt()->identifier_len > 0)
      stmtName = (char *) sqlci_env->getLastAllocatedStmt()->identifier;
  }

  if (!stmtName) 
  // no statement name found Display error.
  {
    sprintf(sprintfBuf, "No statment found. Enter command with valid statement name.");
    log->WriteAll(sprintfBuf);
    return 0;
  }

  SQLSTMT_ID stmt;
  SQLMODULE_ID module;
  init_SQLMODULE_ID(&module);
  init_SQLSTMT_ID(&stmt, SQLCLI_CURRENT_VERSION, 
		  stmt_name, &module);

  char * id = new char[strlen(stmtName) + 1];
#pragma nowarn(1506)   // warning elimination 
  stmt.identifier_len = strlen(stmtName);
#pragma warn(1506)  // warning elimination 
  str_cpy_all(id,stmtName, 
	 stmt.identifier_len);
  id[stmt.identifier_len] = 0;
  stmt.identifier = id;
  char queryId[200];
  Lng32 queryIdLen;

  retcode = SQL_EXEC_GetStmtAttr(&stmt, SQL_ATTR_UNIQUE_STMT_ID,
                      NULL, queryId, 200, &queryIdLen); 
  delete [] id;

  if (queryIdLen < 200)
    queryId[queryIdLen] = 0;
  else
    queryId[199] = 0;

  HandleCLIError(retcode, sqlci_env);

  if (retcode == 0)
  {
    snprintf(sprintfBuf, 225, "QID is %s",queryId);
    log->WriteAll(sprintfBuf);
    log->WriteAll("");
  }  
    
  // display details of this query
  // for string attributes, use {type,maxLength,char[maxLength+1]}
  UNIQUEQUERYID_ATTR queryIdAttrs[11] =
  {
    {UNIQUEQUERYID_SEGMENTNUM, 0, 0},
    {UNIQUEQUERYID_SEGMENTNAME, 10, new char[11]},
    {UNIQUEQUERYID_CPU, 0, 0},
    {UNIQUEQUERYID_PIN, 0, 0},
    {UNIQUEQUERYID_EXESTARTTIME, 0, 0},
    {UNIQUEQUERYID_SESSIONNUM, 0, 0},
    {UNIQUEQUERYID_USERNAME, 24, new char[25]},
    {UNIQUEQUERYID_SESSIONNAME, 32, new char[33]},
    {UNIQUEQUERYID_QUERYNUM, 0, 0},
    {UNIQUEQUERYID_STMTNAME, 110, new char[111]},
    {UNIQUEQUERYID_SESSIONID, 104, new char[105]}
  };

  retcode = SQL_EXEC_GetUniqueQueryIdAttrs(queryId, queryIdLen,
                                           11, queryIdAttrs);
  HandleCLIError(retcode, sqlci_env);
  
  if (retcode == 0)
    {
      snprintf(sprintfBuf, 225, "QID details: ");
      log->WriteAll(sprintfBuf);
      log->WriteAll("============");

      // display UNIQUEQUERYID_SEGMENTNUM
      snprintf(sprintfBuf, 225, "  Segment Num:  " PFLL,queryIdAttrs[0].num_val_or_len);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_SEGMENTNAME
      snprintf(sprintfBuf, 225, "  Segment Name: %s",queryIdAttrs[1].string_val);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_CPU
      snprintf(sprintfBuf, 225, "  Cpu:          " PFLL,queryIdAttrs[2].num_val_or_len);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_PIN
      snprintf(sprintfBuf, 225, "  Pin:          " PFLL,queryIdAttrs[3].num_val_or_len);
      log->WriteAll(sprintfBuf);
 
      // UNIQUEQUERYID_EXESTARTTIME
      short startTimeArray[8];
      _int64 startTime = queryIdAttrs[4].num_val_or_len;
      short error;

      INTERPRETTIMESTAMP( CONVERTTIMESTAMP(startTime, 0, // GMT to LCT
                                                    -1, // use current node
                                                  &error), startTimeArray);
      sprintf(sprintfBuf, "  ExeStartTime: " PF64 "= %02d/%02d/%02d %02d:%02d:%02d.%03d%03d LCT",
                    startTime, startTimeArray[0],startTimeArray[1],startTimeArray[2],
                    startTimeArray[3], startTimeArray[4], startTimeArray[5],
                    startTimeArray[6], startTimeArray[7]);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_SESSIONNUM
      sprintf(sprintfBuf,"  SessionNum:   " PFLL,queryIdAttrs[5].num_val_or_len);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_USERNAME
      sprintf(sprintfBuf,"  UserName:     %s",queryIdAttrs[6].string_val);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_SESSIONNAME
      if (strlen(queryIdAttrs[7].string_val) > 0)
	sprintf(sprintfBuf,"  SessionName:  %s",queryIdAttrs[7].string_val);
      else
        sprintf(sprintfBuf,"  SessionName:  NULL");

      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_QUERYNUM
      sprintf(sprintfBuf,"  QueryNum:     " PFLL,queryIdAttrs[8].num_val_or_len);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_STMTNAME:
      sprintf(sprintfBuf,"  StmtName:     %s",queryIdAttrs[9].string_val);
      log->WriteAll(sprintfBuf);

      // display UNIQUEQUERYID_SESSIONID:
      sprintf(sprintfBuf,"  SessionId:    %s",queryIdAttrs[10].string_val);
      log->WriteAll(sprintfBuf);

      log->WriteAll("");
    }  
  
  return 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;
  }