Ingres Community Forums Login Register Ingres.com  

Ingres Community Forum


Go Back   Ingres Community Forums > Community > The Ingres Community
 

Reply
 
LinkBack Thread Tools Display Modes
Old 2009-12-28   #1 (permalink)
Junior Member
 
Join Date: Dec 2009
Posts: 9
Default How to output a NVARCHAR data in UDF

Hi,

I want to return a data with the type NVARCHAR. How could I do it?

Here is my function :
Code:
II_STATUS
my_function(
   II_SCB          *scb,
   II_DATA_VALUE   *p1, /*type nvarchar*/
   II_DATA_VALUE   *p1, /*type nvarchar*/
   II_DATA_VALUE   *rdv /* return_text need nvarchar  */
){
	char str[400];
	unsigned char this_byte1[2],this_byte2[2],byte1[40], byte2[40];
	unsigned int true_length1,true_length2,iLen;
	unsigned short offset;
	offset=sizeof(short);
	int i;

true_length1 = *(short*)p1->db_data;
true_length2 = *(short*)p2->db_data;

memset(byte1, 0, sizeof(byte1));	
memset(this_byte1, 0, sizeof(this_byte1));
for (i=0; i < true_length1; i++) {
       	strncpy(this_byte1, (char *)(p1->db_data + offset + i*2), 2);
       	strcat(byte1,this_byte1);
  };

memset(byte2, 0, sizeof(byte2));
memset(this_byte2, 0, sizeof(this_byte2));
for (i=0; i < true_length2; i++) {
       	strncpy(this_byte2, (char *)(p2->db_data + offset + i*2), 2);
       	strcat(byte2,this_byte2);
  };

int iLen; 
memset(str, 0, sizeof(str));
strcpy(str,byte1);
iLen = strlen(byte1);
memcpy(str + iLen, ".", 1);
iLen += 1;
memcpy(str + iLen ,byte2,true_length2 );
iLen += true_length2;
*(short *) rdv->db_data = iLen;


//Here, I don't know how to input the value into (char *)rdv->db_data?
strcat((char *)rdv->db_data + offset, str);
}
Could someone read it and answer my question?
Mille thx for you.

Zhao Xu
zhaoxu710 is offline   Reply With Quote
Old 2009-12-29   #2 (permalink)
Ingres Community
 
kschendel's Avatar
 
Join Date: Mar 2007
Location: Pittsburgh, PA
Posts: 1,230
Send a message via Skype™ to kschendel
Default

rdv->db_data will point to a DB_NVCHR_STRING structure, defined in iicommon.h (common/hdr/hdr). Your function wants to finish with something like:

Code:
DB_NVCHR_STRING *result;
...
result = (DB_NVCHR_STRING *) rdv->db_data;
result->count = length_in_characters;
memcpy(result->element_array, source, length_in_characters);
By the way, I'm not sure what your function does, but using strcpy, strcat, etc on nvarchar data is probably not a good idea. The characters in a DB_NVCHR_STRING are UCS2 and the likelihood of hitting a premature null byte is very high.
kschendel is offline   Reply With Quote
Old 2009-12-29   #3 (permalink)
Junior Member
 
Join Date: Dec 2009
Posts: 9
Default

Hi kschendel,

Thx for your answer. As you say, I got the wrong result due to strcpy. And do you have any idea for getting the values of p1 and doing the operations like in my exemple code?

I'm blocked now.

Thx again for your help!

Zhao Xu
zhaoxu710 is offline   Reply With Quote

Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


© 2009 Ingres Corporation. All Rights Reserved