예제 #1
0
void SetParam::setUTF16ParamStrLit(const NAWchar * utf16Str, size_t ucs2StrLen)
{
  if (m_convUTF16ParamStrLit)
  {
    delete [] m_convUTF16ParamStrLit;
    m_convUTF16ParamStrLit = NULL;
  }
  m_convUTF16ParamStrLit = new NAWchar[ucs2StrLen + 1];
  NAWstrncpy(m_convUTF16ParamStrLit, utf16Str, ucs2StrLen);
  m_convUTF16ParamStrLit[ucs2StrLen] = L'\0';
}
예제 #2
0
SqlciCmd::SqlciCmd(const sqlci_cmd_type cmd_type_, NAWchar * argument_, Lng32 arglen_)
                  : SqlciNode(SqlciNode::SQLCI_CMD_TYPE),
		    cmd_type(cmd_type_)
{
  arglen = 2*arglen_;

  if (argument_)
    {
      NAWchar* tgt = new NAWchar[arglen_ + 1]; // extra byte for NAWchar typed argument
      NAWstrncpy(tgt, argument_,arglen_);
      tgt[arglen_] = 0;
      argument = (char*) tgt;
    }
  else
    {
      argument = 0;
    }
};
예제 #3
0
void ErrorMessage::insertParams(NAError * errcb)
{
  if (errcb->getErrParamCount() > 0)
    {
      // Note that we allocate twice the size for tmp, in order to forestall
      // array overrun problems (i.e. memory corruption)
      NAWchar      tmp[MSG_BUF_SIZE * 2];
      NAWchar      paramName[MSG_BUF_SIZE];

      NAWchar	paramVal[MSG_BUF_SIZE];		// regular char, not TCHAR
      Int32	paramLen;
      Lng32	paramPos;
      Int32	tmpLen = 0;
      Int32	truncation = FALSE;

#pragma nowarn(1506)   // warning elimination 
      Int32	msgBufOrigLen = NAWstrlen(msgBuf_);
#pragma warn(1506)  // warning elimination 

      for (Int32 i = 0; i < msgBufOrigLen; i++)
	{
	  if (msgBuf_[i] == ERRORPARAM_BEGINMARK)
	    {
	      // Get the formal parameter name, excluding the leading '$' mark
	      NAWchar *p = paramName;
	      while (++i < msgBufOrigLen &&
	             (isalnum(msgBuf_[i]) || msgBuf_[i] == ERRORPARAM_TYPESEP))
	        *p++ = msgBuf_[i];
	      *p = NAWchar('\0');
	      i--;				// let's not lose a character!
	      paramPos = FixupMessageParam(paramName, POSITIONAL_PARAM);
	      if (paramPos >= 0)
	        {
		  paramVal[0] = NAWchar('\0');	// default is empty param
		  NAErrorParam * param = errcb->getNAErrorParam(paramPos);
		  if (param)
		    switch (param->getNAErrorParamType())
		      {
		      case NAErrorParam::NAERROR_PARAM_TYPE_INTEGER:
			NAWsprintf(paramVal,WIDE_("%d"),param->getIntegerNAErrorParam());
			break;
		      case NAErrorParam::NAERROR_PARAM_TYPE_CHAR_STRING:
			NAWsprintf(paramVal,WIDE_("%s"),param->getStringNAErrorParam());
			break;
		      }	      
#pragma nowarn(1506)   // warning elimination 
		  paramLen = NAWstrlen(paramVal);
#pragma warn(1506)  // warning elimination 

		  NAWstrncpy(&tmp[tmpLen], paramVal, paramLen);
	        }
	      else	// invalid formal param (e.g. "$ab" "$9~" "$~9" "$9x")
		{
		  tmp[tmpLen++] = ERRORPARAM_BEGINMARK;

#pragma nowarn(1506)   // warning elimination 
		  paramLen = NAWstrlen(paramName);
#pragma warn(1506)  // warning elimination 
		  NAWstrncpy(&tmp[tmpLen], paramName, paramLen);
	        }
	      tmpLen += paramLen;
	    }
	  else
	    {
	      tmp[tmpLen++] = msgBuf_[i];
	    }
	    
	  // If necessary, truncate the message and exit loop early.
	  // The -1 is for the terminating '\0' below the loop.
	  if (tmpLen > MSG_BUF_SIZE - 1)
	    {
	      tmpLen = MSG_BUF_SIZE - 1;
	      truncation = TRUE;
	      break;
	    }
	} // for

      // Indicate truncation by overwriting last three characters with '...'
      if (truncation)
	tmp[tmpLen-3] = tmp[tmpLen-2] = tmp[tmpLen-1] = NAWchar('.');

      NAWstrncpy(msgBuf_, tmp, tmpLen);
      msgBuf_[tmpLen] = NAWchar('\0');

    }

} // ErrorMessage::insertParams()
예제 #4
0
short Param::convertValue(SqlciEnv * sqlci_env, short targetType,
			  Lng32 &targetLen,
			  Lng32 targetPrecision,
			  Lng32 targetScale,
                          Lng32 vcIndLen,
   			  ComDiagsArea* diags) {

  // get rid of the old converted value
  if (converted_value) {
    delete [] converted_value;
    converted_value = 0;
  };

  short sourceType;
  Lng32 sourceLen;

  // set up the source and its length based on the how the value is passed-in.
  if ( isInSingleByteForm() == FALSE ) {
    sourceLen = (Lng32)(NAWstrlen((NAWchar*)value) * BYTES_PER_NAWCHAR);
    switch (getCharSet()) {
      case CharInfo::UNICODE:
        sourceType = REC_NCHAR_F_UNICODE;
        break;

      case CharInfo::KANJI_MP:
      case CharInfo::KSC5601_MP:
        sourceType = REC_BYTE_F_ASCII; // KANJI/KSC passed in as NAWchar*
        break;

      default:
        return SQL_Error; // error case
    }
  } else {
    sourceLen = (Lng32)strlen(value); // for any source in single-byte format
    sourceType = REC_BYTE_F_ASCII;
  }

  char * pParamValue = value;

  if ( DFS2REC::isAnyCharacter(targetType) ) {

    if (termCS_ == CharInfo::UnknownCharSet)
      termCS_ = sqlci_env->getTerminalCharset();
    if (cs == CharInfo::UnknownCharSet)
    {
      isQuotedStrWithoutCharSetPrefix_ = TRUE;
      cs = termCS_;
    }

    // If the target is CHARACTER and param is set as [_cs_prefix]'...', then 
    // make sure the source is assignment compatible with the target.
    CharInfo::CharSet targetCharSet = (CharInfo::CharSet)targetScale;
    if ( targetCharSet == CharInfo::UNICODE )
    {
      if (getUTF16StrLit() == (NAWchar*)NULL)
      {
        utf16StrLit_ = new NAWchar [ sourceLen * 2 + 1 ]; // plenty of room
        Lng32 utf16StrLenInNAWchars =
          LocaleStringToUnicode(cs/*sourceCS*/, /*sourceStr*/value, sourceLen,
                                utf16StrLit_/*outputBuf*/, sourceLen+1/*outputBufSizeInNAWchars*/,
                                TRUE /* in - NABoolean addNullAtEnd*/);
        if (sourceLen > 0 && utf16StrLenInNAWchars == 0)
          return SQL_Error;

        // ComASSERT(utf16StrLenInNAWchars == NAWstrlen(getUTF16StrLit()));
        // Resize the NAWchar buffer to save space
        NAWchar *pNAWcharBuf = new NAWchar [ utf16StrLenInNAWchars + 1 ];
        NAWstrncpy (pNAWcharBuf, utf16StrLit_, utf16StrLenInNAWchars + 1);
        pNAWcharBuf[utf16StrLenInNAWchars] = NAWCHR('\0'); // play it safe
        delete [] utf16StrLit_;
        utf16StrLit_ = pNAWcharBuf; // do not deallocate pNAWcharBuf
      }
      sourceLen = (Lng32)(NAWstrlen(getUTF16StrLit()) * BYTES_PER_NAWCHAR);
      // check to see if the parameter utf16 string fits in the target
      if ( sourceLen > targetLen )
        return SQL_Error;

      pParamValue = (char *)getUTF16StrLit();
      sourceType = REC_NCHAR_F_UNICODE;
    }

  } else {
 
    // MP NCHAR (KANJI/KSC) can not be converted to non-character objects
   if ( CharInfo::is_NCHAR_MP(cs) )
      return SQL_Error;
  }


  switch(targetType) {
  case REC_BIN16_SIGNED:
  case REC_BIN16_UNSIGNED:
  case REC_BPINT_UNSIGNED:
  case REC_BIN32_SIGNED:
  case REC_BIN32_UNSIGNED:
  case REC_BIN64_SIGNED:
  case REC_DECIMAL_UNSIGNED:
  case REC_DECIMAL_LSE:
  case REC_FLOAT32:
  case REC_FLOAT64:
  case REC_TDM_FLOAT32:
  case REC_TDM_FLOAT64:
  case REC_BYTE_F_ASCII:
  case REC_BYTE_V_ASCII:
  case REC_BYTE_V_ASCII_LONG:
  case REC_NCHAR_F_UNICODE:
  case REC_NCHAR_V_UNICODE:
  {
    char *VCLen = NULL;
    short VCLenSize = 0;

// 5/27/98: added VARNCHAR cases
    if ((targetType == REC_BYTE_V_ASCII) || 
        (targetType == REC_BYTE_V_ASCII_LONG) ||
        (targetType == REC_NCHAR_V_UNICODE)) 
    {
      // add bytes for variable length field
      VCLenSize = vcIndLen; //sizeof(short);
      VCLen = converted_value = new char[targetLen + VCLenSize];
    } else
      converted_value = new char[targetLen];


#pragma nowarn(1506)   // warning elimination 
    ex_expr::exp_return_type ok;
    CharInfo::CharSet TCS = sqlci_env->getTerminalCharset();
    CharInfo::CharSet ISOMAPCS = sqlci_env->getIsoMappingCharset();
    
    NAString* tempstr;
    if ( 
        DFS2REC::isAnyCharacter(sourceType) && DFS2REC::isAnyCharacter(targetType) &&
        !(getUTF16StrLit() != NULL && sourceType == REC_NCHAR_F_UNICODE && targetScale == CharInfo::UCS2) &&
        /*source*/cs != targetScale/*i.e., targetCharSet*/
        )
    {
      charBuf cbuf((unsigned char*)pParamValue, sourceLen);
      NAWcharBuf* wcbuf = 0;
      Int32 errorcode = 0;
      wcbuf = csetToUnicode(cbuf, 0, wcbuf,
                            cs/*sourceCharSet*/
                            , errorcode);
      if (errorcode != 0) return SQL_Error;
      tempstr = unicodeToChar(wcbuf->data(),wcbuf->getStrLen(),
                              targetScale/*i.e., targetCharSet*/
                              );
      if (tempstr == NULL) 
	return SQL_Error;  //Avoid NULL ptr reference if conversion error
      sourceType = targetType; // we just converted it to the target type
      sourceLen = tempstr->length();
      pParamValue = (char *)tempstr->data();

      if ( sourceLen > targetLen )
        return SQL_Error;
    }

    ok = convDoIt(pParamValue,
		  sourceLen, 
		  sourceType,
		  0, // source Precision
		  targetScale, // new charset we converted to
		  &converted_value[VCLenSize],
		  targetLen,
		  targetType,
		  targetPrecision,
		  targetScale,
		  VCLen,
		  VCLenSize,
		  0,
		  &diags);
    
    if ( ok != ex_expr::EXPR_OK)
    {
      // No need to delete allocated memory before return because class member
      // converted_value still points to allocated memory that is deleted in 
      // desctructor.
      return SQL_Error; // error case
    }
#pragma warn(1506)  // warning elimination
    
  };
  break;


  case REC_DATETIME: {

    char *VCLen = NULL;
    short VCLenSize = 0;
    converted_value = new char[targetLen + 1];

#pragma nowarn(1506)   // warning elimination 
    ex_expr::exp_return_type ok = convDoIt(value,
					   sourceLen, 
					   sourceType,
					   0, // source Precision
					   0, // source Scale
					   converted_value,
					   targetLen,
					   targetType,
					   targetPrecision,
					   targetScale,
					   VCLen,
					   VCLenSize,
					   0,
					   &diags);
    
    if ( ok != ex_expr::EXPR_OK)
      {
	return SQL_Error; // error case
      }
#pragma warn(1506)  // warning elimination
  };
  break;

  case REC_INT_YEAR:
  case REC_INT_MONTH:
  case REC_INT_YEAR_MONTH:
  case REC_INT_DAY:
  case REC_INT_HOUR:
  case REC_INT_DAY_HOUR:
  case REC_INT_MINUTE:
  case REC_INT_HOUR_MINUTE:
  case REC_INT_DAY_MINUTE:
  case REC_INT_SECOND:
  case REC_INT_MINUTE_SECOND:
  case REC_INT_HOUR_SECOND:
  case REC_INT_DAY_SECOND: {

    // convert target back to string.
    converted_value = new char[targetLen];
    Lng32 convFlags = CONV_ALLOW_SIGN_IN_INTERVAL;
#pragma nowarn(1506)   // warning elimination 
    short ok = 
      convDoItMxcs(value,
		   sourceLen, 
		   sourceType,
		   0, // source Precision
		   0, // source Scale
		   converted_value,
		   targetLen,
		   targetType,
		   targetPrecision,
		   targetScale,
		   convFlags);
    
    if ( ok != 0 )
    {
      // No need to delete allocated memory before return because class member
      // converted_value still points to allocated memory that is deleted in 
      // desctructor.
      return SQL_Error; // error case
    }
#pragma warn(1506)  // warning elimination
  };
  break;

  case REC_NUM_BIG_UNSIGNED:
  case REC_NUM_BIG_SIGNED:
  {
    converted_value = new char[targetLen];
#pragma nowarn(1506)   // warning elimination 
    short ok = 
      convDoItMxcs(value,
		   sourceLen, 
		   sourceType,
		   0, // source Precision
		   0, // source Scale
		   converted_value,
		   targetLen,
		   targetType,
		   targetPrecision,
		   targetScale,
		   0);
    
    if ( ok != 0 )
    {
      // No need to delete allocated memory before return because class member
      // converted_value still points to allocated memory that is deleted in 
      // desctructor.
      return SQL_Error; // error case
    }
#pragma warn(1506)  // warning elimination
    
  };
  break;

  default:
    break;
  };

  return 0;
}
예제 #5
0
NAWcharBuf* csetToUnicode(const charBuf& input, 
	CollHeap *heap, NAWcharBuf*& unicodeString, Int32 cset, Int32 &errorcode,
        NABoolean addNullAtEnd, Int32 *charCount, Int32 *errorByteOff)
{
    char * err_ptr = NULL;
    UInt32 byteCount = 0, lv_charCount = 0, computedMaxBufSizeInNAWchars = 0;
    NABoolean outputBufferAllocatedByThisRoutine = (unicodeString == NULL) ? TRUE : FALSE;
    enum cnv_charset cnvSet = convertCharsetEnum (cset);

    computedMaxBufSizeInNAWchars = (input.getStrLen()+1)*2; // in NAWchar elements for the worst case
      
    NAWcharBuf* output = checkSpace(heap, computedMaxBufSizeInNAWchars, unicodeString, addNullAtEnd);

    if ( output == NULL ) {errorcode = CNV_ERR_BUFFER_OVERRUN; return NULL;}

    NAWchar* target = output->data();

    errorcode = LocaleToUTF16(
         cnv_version1,
         (const char *)input.data(), input.getStrLen(),
         (const char *)target, output->getBufSize()*BYTES_PER_NAWCHAR /* in bytes */,
         cnvSet,
         err_ptr,
         &byteCount,
         0,
         addNullAtEnd,
         &lv_charCount);
    if (errorcode == CNV_ERR_NOINPUT) errorcode=0;  // empty string is OK
    if (errorByteOff) *errorByteOff = err_ptr - (char *)input.data();
    if (charCount)    *charCount    = (Int32)lv_charCount;
    // If errorcode != 0, LocaleToUTF16 will not add the NULL terminator
    if (errorcode == 0 && addNullAtEnd && byteCount > 0)
       {
         // Exclude the size (in bytes) of the NULL terminator from the byte count.
         if (byteCount > BYTES_PER_NAWCHAR)
           byteCount -= BYTES_PER_NAWCHAR;
         else
           byteCount = 0;
       }
        
    output->setStrLen/*in_NAWchar_s*/(byteCount/BYTES_PER_NAWCHAR); // excluding the NULL terminator

    if (outputBufferAllocatedByThisRoutine &&
        output->getBufSize() > output->getStrLen() + 500) // allocated too much space
    {
      // Try to save space in the heap but still allocate 50 extra NAWchars so we do not need
      // to resize the buffer if there is a need to append a few more characters later on.
      // The additional 1 NAWchar is for the NULL terminator.

      NAWcharBuf * outNAWcharBuf2 = new (heap) NAWcharBuf ( output->getStrLen() + 51 // in NAWchars
                                                          , heap );
      if (outNAWcharBuf2 != NULL) // successful allocation
      {
        // Copy data to the newly allocated, smaller buffer.
        NAWstrncpy(outNAWcharBuf2->data(), output->data(), output->getStrLen());
        outNAWcharBuf2->setStrLen/*in_NAWchar_s*/(output->getStrLen());
        // Always append a UCS-2 NULL terminator but exclude it from the string length count.
        outNAWcharBuf2->data()[outNAWcharBuf2->getStrLen()] = 0;

        // Remove the old buffer and set up for the returned value and out parameter.
        unicodeString = outNAWcharBuf2; // return via the out parameter NAWcharBuf*& unicodeString
        NADELETE(output, NAWcharBuf, heap);
        output = outNAWcharBuf2;
      }
    }
    return output;

}