Updated on 2024-05-07 GMT+08:00

Examples

ecpg Example Code

#include <locale.h>
#include <string.h> 
#include <stdlib.h>

exec sql whenever sqlerror sqlprint;
exec sql include sqlca;

int main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
    char *temp_str = (char *)malloc(11);
EXEC SQL END DECLARE SECTION;

    ECPGdebug(1, stderr);

    exec sql connect to postgres;

    /* Enable the automatic commit function. You do not need to manually commit the exec sql command. */
    exec sql set autocommit = on;
    exec sql drop table if exists test_t;
    /* Create a table and insert data. */
    exec sql create table test_t(f float, i int, a int[10], mstr char(10));
    exec sql insert into test_t(f, i, a, mstr) values(1.01,1,'{0,1,2,3,4,5,6,7,8,9}', 'China');
    
    /* Disable the automatic commit function. The following SQL statements for inserting data must be manually committed: */
    exec sql set autocommit = off;
    exec sql insert into test_t(f, i, a, mstr) values(2.01,2,'{0,1,2,3,4,5,6,7,8,9}', 'USA');
    exec sql commit;

    exec sql insert into test_t(f, i, a, mstr) values(3.01,3,'{0,1,2,3,4,5,6,7,8,9}', 'AUS');
    exec sql insert into test_t(f, i, a, mstr) values(4.01,4,'{0,1,2,3,4,5,6,7,8,9}', 'JAP');
    exec sql commit;

EXEC SQL BEGIN DECLARE SECTION;
    int a[10] = {9,8,7,6,5,4,3,2,1,0};
    int id = 6;
EXEC SQL END DECLARE SECTION;

    /* Fetch data from the host variable and insert the data into the table. The type of the host variable is the same as that defined in the table. */
    strcpy(temp_str, "RUS");
    exec sql insert into test_t(f, i, a, mstr) values(5.01,5,:a,:temp_str);
    exec sql commit;

    exec sql set autocommit = on;
    exec sql begin;
    exec sql insert into test_t(f, i, a, mstr) values(6.01,:id,:a,'SIG');
    exec sql commit;
    exec sql set autocommit = off;

exec sql begin declare section;
    float ff;
    char tmp_text[25] = "klmnopqrst";
exec sql end declare section;

    exec sql set autocommit = on;
    exec sql begin work;

    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    /* Example of a conditional query statement */
    exec sql select f, mstr into :ff,:tmp_text from test_t where f > (select f from test_t where i = 4 or i < 0) order by a limit 1;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select f, mstr into :ff,:tmp_text from test_t where mstr = 'JAP' order by i;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select f, mstr into :ff,:tmp_text from  test_t order by i DESC limit 1;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select f, mstr into :ff,:tmp_text from  test_t order by mstr limit 1;
    printf("Found ff=%f tmp_text=%10.10s\n", ff, tmp_text);

    exec sql select count(f), a into :ff,:tmp_text from test_t where i > 2 group by a limit 1;
    printf("Found ff=%f tmp_text=%20.30s\n", ff, tmp_text);

    exec sql select count(f), a into :ff,:tmp_text from test_t where i > 3 group by a order by a limit 1;
    printf("Found ff=%f tmp_text=%20.30s\n", ff, tmp_text);

    exec sql select sum(f), a into :ff,:tmp_text from test_t where i > 2  group by a order by a limit 1;
    printf("Found ff=%f tmp_text=%20.30s\n", ff, tmp_text);

    exec sql select distinct a into :tmp_text from test_t order by a limit 1;

    exec sql drop table test_t;

    exec sql commit;
    /* Release the connection and release the memory allocated to the host variable. */
    exec sql disconnect;
    free(temp_str);

    return 0;
}

Example Code of the pgtypes Library Function

Example 1: Use library functions to perform different operations on time and date types. For details, see Using Library Functions.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <pgtypes_date.h>
#include <pgtypes_timestamp.h>

char *dates[] = { "19990108foobar",
                  "19990108 foobar",
                  "1999-01-08 foobar",
                  "January 8, 1999",
                  "1999-01-08",
                  "1/8/1999",
                  "1/18/1999",
                  "01/02/03",
                  "1999-Jan-08",
                  "Jan-08-1999",
                  "08-Jan-1999",
                  "99-Jan-08",
                  "08-Jan-99",
                  "08-Jan-06",
                  "Jan-08-99",
                  "19990108",
                  "990108",
                  "1999.008",
                  "J2451187",
                  "January 8, 99 BC",
                  NULL
                 };

/* The value cannot conflict with the value of times of libc. */
static char *times[] = { "0:04",
                         "1:59 PDT",
                         "13:24:40 -8:00",
                         "13:24:40.495+3",
                          NULL 
                        };

char *intervals[] = { "1 minute",
                      "1 12:59:10",
                      "2 day 12 hour 59 minute 10 second",
                      "1 days 12 hrs 59 mins 10 secs",
                      "1 days 1 hours 1 minutes 1 seconds",
                      "1 year 59 mins",
                      "1 year 59 mins foobar",
                      NULL 
                    };

int main(void)
{
 exec sql begin declare section;
    date date1;
    timestamp ts1, ts2;
    char *text;
    interval *i1;
    date *dc;
 exec sql end declare section;

    int i, j;
    char *endptr;

    ECPGdebug(1, stderr);

    /* Parse a timestamp from its textual representation and convert the date into a character string. */
    ts1 = PGTYPEStimestamp_from_asc("2003-12-04 17:34:29", NULL);
    text = PGTYPEStimestamp_to_asc(ts1);

    printf("timestamp: %s\n", text);
    free(text);

    /* Extract the date part from the timestamp. */
    date1 = PGTYPESdate_from_timestamp(ts1);
    dc = PGTYPESdate_new();
    *dc = date1;
    /* Return the textual representation of a date variable. */
    text = PGTYPESdate_to_asc(*dc);
    printf("Date of timestamp: %s\n", text);
    free(text);
    PGTYPESdate_free(dc);

    for (i = 0; dates[i]; i++)
    {
        bool err = false;
        /* Parse a date from the textual representation of the date. */
        date1 = PGTYPESdate_from_asc(dates[i], &endptr);
        if (date1 == INT_MIN) {
            err = true;
        }
        /* Return the textual representation of a date variable. */
        text = PGTYPESdate_to_asc(date1);
        printf("Date[%d]: %s (%c - %c)\n",
                i, err ? "-" : text,
                endptr ? 'N' : 'Y',
                err ? 'T' : 'F');
        free(text);
        if (!err)
        {
            for (j = 0; times[j]; j++)
            {
                int length = strlen(dates[i])+ 1+ strlen(times[j])+ 1;
                char* t = (char *)malloc(length);
                sprintf(t, "%s %s", dates[i], times[j]);
                /* Parse a timestamp from its textual representation and convert the date into a character string. */
                ts1 = PGTYPEStimestamp_from_asc(t, NULL);
                text = PGTYPEStimestamp_to_asc(ts1);
                if (i != 19 || j != 3)
                    printf("TS[%d,%d]: %s\n",i, j, errno ? "-" : text);
                free(text);
                free(t);
           }
        }
    }

    /* Parse a timestamp from its textual representation. */
    ts1 = PGTYPEStimestamp_from_asc("2004-04-04 23:23:23", NULL);

    for (i = 0; intervals[i]; i++)
    {
        interval *ic;
        /* Parse an interval from its textual representation. */
        i1 = PGTYPESinterval_from_asc(intervals[i], &endptr);
        if (*endptr)
            printf("endptr set to %s\n", endptr);
        if (!i1)
        {
             printf("Error parsing interval %d\n", i);
             continue;
         }
        /* Add an interval variable to the timestamp variable. */
         j = PGTYPEStimestamp_add_interval(&ts1, i1, &ts2);
         if (j < 0)
             continue;
        /* Convert a variable of the interval type to the text format. */
         text = PGTYPESinterval_to_asc(i1);
         printf("interval[%d]: %s\n", i, text ? text : "-");
         free(text);

        /* Return a pointer to an allocated interval variable. */
         ic = PGTYPESinterval_new();
        /*Copy a variable of the interval type.*/
         PGTYPESinterval_copy(i1, ic);
        /* Convert a variable of the interval type to the text format. */
         text = PGTYPESinterval_to_asc(i1);
         printf("interval_copy[%d]: %s\n", i, text ? text : "-");
         free(text);
        /* Release the memory that has been allocated to an interval variable. */
         PGTYPESinterval_free(ic);
         PGTYPESinterval_free(i1);
    }

    return (0);
}
Example 2: Use the pgtypes library function to perform different operations on the numeric type.
#include <stdio.h>
#include <stdlib.h>
#include <pgtypes_numeric.h>
#include <pgtypes_error.h>
#include <decimal.h>

char* nums[] = { "2E394", "-2", ".794", "3.44", "592.49E21", "-32.84e4",
                 "2E-394", ".1E-2", "+.0", "-592.49E-07", "+32.84e-4",
                 ".500001", "-.5000001",
                 "1234567890123456789012345678.91", /* A 30-bit number should be converted into a decimal number.*/
                 "1234567890123456789012345678.921", /* A 31-bit number cannot be converted into a decimal number. */
                 "not a number",
                 NULL
                };

static void check_errno(void);

int main(void)
{
    char *text="error\n";
    char *endptr;
    numeric *num, *nin;
    decimal *dec;
    long l;
    int i, j, k, q, r, count = 0;
    double d;
    numeric **numarr = (numeric **) calloc(1, sizeof(numeric));

    ECPGdebug(1, stderr);

    for (i = 0; nums[i]; i++)
    {
         /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type nums[i]. */
         num = PGTYPESnumeric_from_asc(nums[i], &endptr);
         if (!num) check_errno();
         if (endptr != NULL)
         {
              printf("endptr of %d is not NULL\n", i);
              if (*endptr != '\0')
                  printf("*endptr of %d is not \\0\n", i);
         }
         if (!num) continue;

         numarr = (numeric **)realloc(numarr, sizeof(numeric *) * (count + 1));
         numarr[count++] = num;

         /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type num. */
         text = PGTYPESnumeric_to_asc(num, -1);
         if (!text) check_errno();
         printf("num[%d,1]: %s\n", i, text); free(text);
         text = PGTYPESnumeric_to_asc(num, 0);
         if (!text) check_errno();
          printf("num[%d,2]: %s\n", i, text); free(text);
         text = PGTYPESnumeric_to_asc(num, 1);
         if (!text) check_errno();
          printf("num[%d,3]: %s\n", i, text); free(text);
         text = PGTYPESnumeric_to_asc(num, 2);
         if (!text) check_errno();
         printf("num[%d,4]: %s\n", i, text); free(text);

         /* Request a pointer to a newly allocated numeric variable. */
         nin = PGTYPESnumeric_new();
         text = PGTYPESnumeric_to_asc(nin, 2);
         if (!text) check_errno();
         printf("num[%d,5]: %s\n", i, text); free(text);

         /* Convert a numeric variable into a long int variable. */
         r = PGTYPESnumeric_to_long(num, &l);
         if (r) check_errno();
         printf("num[%d,6]: %ld (r: %d)\n", i, r?0L:l, r);
         if (r == 0)
         {
            /* Convert a long int variable into a numeric variable. */
             r = PGTYPESnumeric_from_long(l, nin);
             if (r) check_errno();
         /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type nin. */
             text = PGTYPESnumeric_to_asc(nin, 2);
             /* Compare two numeric variables. */
             q = PGTYPESnumeric_cmp(num, nin);
             printf("num[%d,7]: %s (r: %d - cmp: %d)\n", i, text, r, q);
             free(text);
          }

          /* Convert a numeric variable into an int variable. */
          r = PGTYPESnumeric_to_int(num, &k);
          if (r) check_errno();
          printf("num[%d,8]: %d (r: %d)\n", i, r?0:k, r);
          if (r == 0)
          {
              /* Convert an int variable into a numeric variable. */
              r = PGTYPESnumeric_from_int(k, nin);
              if (r) check_errno();
         /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type nin. */
              text = PGTYPESnumeric_to_asc(nin, 2);
              q = PGTYPESnumeric_cmp(num, nin);
              printf("num[%d,9]: %s (r: %d - cmp: %d)\n", i, text, r, q);
              free(text);
          }

          if (i != 6)
          {
               /* Convert a variable of the numeric type to the double-precision type. */
               r = PGTYPESnumeric_to_double(num, &d);
               if (r) check_errno();
               printf("num[%d,10]: %g (r: %d)\n", i, r?0.0:d, r);
           }

          /* Request a pointer to a newly allocated numeric variable. */
          dec = PGTYPESdecimal_new();
          /* Convert a decimal variable into a numeric variable. */
          r = PGTYPESnumeric_to_decimal(num, dec);
          if (r) check_errno();
          printf("num[%d,11]: - (r: %d)\n", i, r);
          if (r == 0)
          {
             /* Convert a decimal variable into a numeric variable. */
              r = PGTYPESnumeric_from_decimal(dec, nin);
              if (r) check_errno();
         /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type nin. */
              text = PGTYPESnumeric_to_asc(nin, 2);
             /* Compare two numeric variables. */
              q = PGTYPESnumeric_cmp(num, nin);
              printf("num[%d,12]: %s (r: %d - cmp: %d)\n", i, text, r, q);
              free(text);
          }

          /* Release the memory allocated to the numeric variable. */
          PGTYPESdecimal_free(dec);
          PGTYPESnumeric_free(nin);
          printf("\n");
    }

    for (i = 0; i < count; i++)
    {
        for (j = 0; j < count; j++)
        {
           /* Request a pointer to a newly allocated numeric variable. */
            numeric* a = PGTYPESnumeric_new();
            numeric* s = PGTYPESnumeric_new();
            numeric* m = PGTYPESnumeric_new();
            numeric* d = PGTYPESnumeric_new();
            /* Add two numeric variables to the third numeric variable. */
            r = PGTYPESnumeric_add(numarr[i], numarr[j], a);
            if (r)
            {
               check_errno();
               printf("r: %d\n", r);
             }
            else
            {
              /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type a. */
                text = PGTYPESnumeric_to_asc(a, 10);
                printf("num[a,%d,%d]: %s\n", i, j, text);
                free(text);
            }
            /* Subtract two numeric variables and returns the result to the third numeric variable. */
            r = PGTYPESnumeric_sub(numarr[i], numarr[j], s);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
              /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type s. */
                text = PGTYPESnumeric_to_asc(s, 10);
                printf("num[s,%d,%d]: %s\n", i, j, text);
                free(text);
            }
            /* Multiply two numeric variables and returns the result to the third numeric variable. */
            r = PGTYPESnumeric_mul(numarr[i], numarr[j], m);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
              /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type m. */
                text = PGTYPESnumeric_to_asc(m, 10);
                printf("num[m,%d,%d]: %s\n", i, j, text);
                free(text);
            }
            /* Divide two numeric variables and returns the result to the third numeric variable. */
            r = PGTYPESnumeric_div(numarr[i], numarr[j], d);
            if (r)
            {
                check_errno();
                printf("r: %d\n", r);
            }
            else
            {
              /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type d. */
                text = PGTYPESnumeric_to_asc(d, 10);
                printf("num[d,%d,%d]: %s\n", i, j, text);
                free(text);
            }

          /* Release the memory allocated to the numeric variable. */
            PGTYPESnumeric_free(a);
            PGTYPESnumeric_free(s);
            PGTYPESnumeric_free(m);
            PGTYPESnumeric_free(d);
       }
   }

    for (i = 0; i < count; i++)
    {
         /* Return a pointer to a string allocated by malloc that contains the string representation of the numeric type numarr[i]. */
        text = PGTYPESnumeric_to_asc(numarr[i], -1);
        printf("%d: %s\n", i, text);
        free(text);
        /* Free memory. */
        PGTYPESnumeric_free(numarr[i]);
    }
    free(numarr);

    return (0);
}

/* Error handling... */
static void
check_errno(void)
{
    switch(errno)
    {
        case 0:
        printf("(no errno set) - ");
        break;
        case PGTYPES_NUM_OVERFLOW:
        printf("(errno == PGTYPES_NUM_OVERFLOW) - ");
        break;
        case PGTYPES_NUM_UNDERFLOW:
        printf("(errno == PGTYPES_NUM_UNDERFLOW) - ");
        break;
        case PGTYPES_NUM_BAD_NUMERIC:
        printf("(errno == PGTYPES_NUM_BAD_NUMERIC) - ");
        break;
        case PGTYPES_NUM_DIVIDE_ZERO:
        printf("(errno == PGTYPES_NUM_DIVIDE_ZERO) - ");
        break;
        default:
        printf("(unknown errno (%d))\n", errno);
        printf("(libc: (%s)) ", strerror(errno));
        break;
    }
}