convert CHAR type to TEXT type (BLOB) and insert in table

convert CHAR type to TEXT type (BLOB) and insert in table

Post by Christian Hernou » Sun, 31 Dec 1899 09:00:00



Problem :
---------
 convert CHAR type to TEXT type (BLOB) and insert in table.

Solution :
----------
Here is the "C" functions.
One to write the string into the blob and other to read the blob.
(included two "4GL" test programs and two "C" functions.)

Many thanks to :


Rem : This code run with BLOB located in MEMORY.

----------------------------------------- Cut Here -------------------
=> C Code
#include <assert.h>
#include <stdio.h>
#include "/usr/informix/incl/locator.h"
#define BLOCK_SIZE 4096

/*
 --------------------------------------------------------------------
 append_string : append string to the blobspace
 --------------------------------------------------------------------
*/
int blob_append_string(n)
int n;
{
 loc_t *blob;
 int offset;
 int len, c, needed, cnter=0;
 char string[BLOCK_SIZE];
 if (n != 2)
  fgl_fatal(__FILE__, __LINE__, -1318);
 /* popquote(string, sizeof(string)); */
 popvchar(string, sizeof(string));
 poplocator(&blob);
 assert(blob->loc_loctype == LOCMEMORY);
 if((len=strlen(string)) == 0)
 {
  /* nothing to append */
  retint(-2);
  return(1);
 }
 /* append text to the blob */
 if(blob->loc_indicator == -1)
  needed = len;
 else
  needed = blob->loc_size + len;
 if(blob->loc_bufsize < needed)
 {
  if(blob->loc_buffer == 0)
    blob->loc_buffer = (char *)malloc(needed);
  else
    blob->loc_buffer = (char *)realloc(blob->loc_buffer, needed);
  assert(blob->loc_buffer != (char *)0);
  if(blob->loc_buffer == 0) /* error allocating space */
  {
    retint(-1);
    return(1);
  }
 }
 blob->loc_bufsize = needed;
 if(blob->loc_indicator == -1)
 {
  memcpy(blob->loc_buffer, string, len);
  blob->loc_size = len;
  blob->loc_indicator = 0;
 }
 else
 {
  memcpy(blob->loc_buffer + blob->loc_size,  string, len);
  blob->loc_size = len;
 }
 blob->loc_currdata_p = blob->loc_buffer + blob->loc_size;
 retint(0);
 return(1);

Quote:}

/*
--------------------------------------------------------------------
 return_string : reads the string from the blobspace
--------------------------------------------------------------------
*/
int blob_return_string(n)
int n;
{
 loc_t *blob;
 int offset;
 int c, cnter=0;
 char new_str[BLOCK_SIZE];
 if (n != 2)
  fgl_fatal(__FILE__, __LINE__, -1318);
 popint(&offset);
 poplocator(&blob);
 /* get the string from the begining of the blob
      plus the offset up to the loc_size */
 if (offset >= blob->loc_size || offset < 0)
 {
  /* error or at end of blob */
  offset = -1;
  strcpy(new_str, "END OF BLOB");
 }
 else
 {
  while(offset < blob->loc_size)
  {
   c = *(blob->loc_buffer + offset++);
   new_str[cnter++] = c;
   if (cnter == BLOCK_SIZE)
   {
    /* don't go out of range of array buffer */
    cnter=BLOCK_SIZE - 1;
    break;
   }
   /* stop if the string terminates with either a string
      terminator or a new-line*/
   if (c == '\0' || c == '\n')
   break;
  }
  new_str[cnter] = '\0';
 }
 retint(offset);
 retquote(new_str);
 return(2);
Quote:}

----------------------------------------- Cut Here -------------------
=> 4GL Programm

# --------------------------------------------------------------------
# 4GL program to write a blob
# --------------------------------------------------------------------
DATABASE indus
MAIN
 call test_function()
END MAIN
FUNCTION test_function()
 DEFINE xchamp    TEXT,
        xdonnees  TEXT,
        offset   SMALLINT,
        ret_val   SMALLINT,
        str1   CHAR(40)
 LOCATE xchamp IN MEMORY
 LOCATE xdonnees IN MEMORY
 LET offset = 0
 INITIALIZE xchamp TO NULL
 INITIALIZE xdonnees TO NULL
 LET str1 = "champ1;champ2"
 CALL blob_append_string(xchamp, str1) RETURNING ret_val
 LET str1 = "Christian;Hernoux"
 CALL blob_append_string(xdonnees, str1) RETURNING ret_val
 INSERT INTO edition VALUES (0, "ESSAI", xchamp, xdonnees);
END FUNCTION
----------------------------------------- Cut Here -------------------
# -----------------------------------------------------------------
# 4GL program to read blob
# -----------------------------------------------------------------
DATABASE indus
MAIN
 CALL test_function()
END MAIN
FUNCTION test_function()
DEFINE xchamps       TEXT,
       xdonnees      TEXT,
       octets        SMALLINT,
       str_champs    CHAR(4096),
       str_donnees   CHAR(4096)
 LOCATE xchamps  IN MEMORY
 LOCATE xdonnees IN MEMORY
 LET octets = 0
 INITIALIZE xchamps TO NULL
 INITIALIZE xdonnees TO NULL
 SELECT champs,donnees INTO xchamps, xdonnees
  FROM edition
  WHERE no_courrier = 1
 CALL blob_return_string(xchamps, octets) RETURNING octets, str_champs
 DISPLAY "Champs : ",str_champs CLIPPED
 CALL blob_return_string(xdonnees, octets) RETURNING octets, str_donnees
 DISPLAY "Donnees : ",str_donnees CLIPPED
END FUNCTION
----------------------------------------- Cut Here -------------------
=> SQL Table creation
CREATE TABLE edition {
   no_courrier  SERIAL,
   champs       TEXT IN TABLE,
   donnees      TEXT IN TABLE

Quote:}

----------------------------------------- Cut Here -------------------

Amicalement Net.

 
 
 

1. Convert char data type to Date type

Hello,

I have a user defined table in my databse with a variety
of information stored of different data types.  The field
d2_stuff is where we off load all this info.  There are
many dates stored depending on the table ID.

I want to run sql queries and need certain fields
converted to dates for this purpose.  I can get all the
info I need out of the query.  The field is a char(9)

I have tried many different ways to get this to work and
have failed at all of them. Can someone help me convert
d2_stuff char(9) to a date field

Brian

2. "select top"

3. convert a money type to a format char type

4. A question for a SQL expert :o)

5. Converting TEXT/BLOB types

6. TRUNC function in ORACLE?

7. Maximum size of text inserted into a table of type text,ntext

8. 7.3RC1 ?? Re: PostgreSQL compilation with custom table

9. Combined indexes (date-type +char type)?

10. inserting/updating tables with TEXT data types

11. convert char data type to date time

12. How to convert a money type to a format char

13. Convert varchar/char data type to datetime??