Header And Logo

PostgreSQL
| The world's most advanced open source database.

sql-dyntest.c

Go to the documentation of this file.
00001 /* Processed by ecpg (regression mode) */
00002 /* These include files are added by the preprocessor */
00003 #include <ecpglib.h>
00004 #include <ecpgerrno.h>
00005 #include <sqlca.h>
00006 /* End of automatic include section */
00007 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
00008 
00009 #line 1 "dyntest.pgc"
00010 /* dynamic SQL test program
00011  */
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 
00016 
00017 #line 1 "sql3types.h"
00018 #ifndef _ECPG_SQL3TYPES_H
00019 #define _ECPG_SQL3TYPES_H
00020 
00021 /* SQL3 dynamic type codes */
00022 
00023 /* chapter 13.1 table 2: Codes used for SQL data types in Dynamic SQL */
00024 
00025 enum
00026 {
00027     SQL3_CHARACTER = 1,
00028     SQL3_NUMERIC,
00029     SQL3_DECIMAL,
00030     SQL3_INTEGER,
00031     SQL3_SMALLINT,
00032     SQL3_FLOAT,
00033     SQL3_REAL,
00034     SQL3_DOUBLE_PRECISION,
00035     SQL3_DATE_TIME_TIMESTAMP,
00036     SQL3_INTERVAL,              /* 10 */
00037     SQL3_CHARACTER_VARYING = 12,
00038     SQL3_ENUMERATED,
00039     SQL3_BIT,
00040     SQL3_BIT_VARYING,
00041     SQL3_BOOLEAN,
00042     SQL3_abstract
00043     /* the rest is xLOB stuff */
00044 };
00045 
00046 /* chapter 13.1 table 3: Codes associated with datetime data types in Dynamic SQL */
00047 
00048 enum
00049 {
00050     SQL3_DDT_DATE = 1,
00051     SQL3_DDT_TIME,
00052     SQL3_DDT_TIMESTAMP,
00053     SQL3_DDT_TIME_WITH_TIME_ZONE,
00054     SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE,
00055 
00056     SQL3_DDT_ILLEGAL            /* not a datetime data type (not part of
00057                                  * standard) */
00058 };
00059 
00060 #endif   /* !_ECPG_SQL3TYPES_H */
00061 
00062 #line 7 "dyntest.pgc"
00063 
00064 
00065 #line 1 "sqlca.h"
00066 #ifndef POSTGRES_SQLCA_H
00067 #define POSTGRES_SQLCA_H
00068 
00069 #ifndef PGDLLIMPORT
00070 #if  defined(WIN32) || defined(__CYGWIN__)
00071 #define PGDLLIMPORT __declspec (dllimport)
00072 #else
00073 #define PGDLLIMPORT
00074 #endif   /* __CYGWIN__ */
00075 #endif   /* PGDLLIMPORT */
00076 
00077 #define SQLERRMC_LEN    150
00078 
00079 #ifdef __cplusplus
00080 extern      "C"
00081 {
00082 #endif
00083 
00084 struct sqlca_t
00085 {
00086     char        sqlcaid[8];
00087     long        sqlabc;
00088     long        sqlcode;
00089     struct
00090     {
00091         int         sqlerrml;
00092         char        sqlerrmc[SQLERRMC_LEN];
00093     }           sqlerrm;
00094     char        sqlerrp[8];
00095     long        sqlerrd[6];
00096     /* Element 0: empty                     */
00097     /* 1: OID of processed tuple if applicable          */
00098     /* 2: number of rows processed              */
00099     /* after an INSERT, UPDATE or               */
00100     /* DELETE statement                 */
00101     /* 3: empty                     */
00102     /* 4: empty                     */
00103     /* 5: empty                     */
00104     char        sqlwarn[8];
00105     /* Element 0: set to 'W' if at least one other is 'W'   */
00106     /* 1: if 'W' at least one character string      */
00107     /* value was truncated when it was          */
00108     /* stored into a host variable.             */
00109 
00110     /*
00111      * 2: if 'W' a (hopefully) non-fatal notice occurred
00112      */ /* 3: empty */
00113     /* 4: empty                     */
00114     /* 5: empty                     */
00115     /* 6: empty                     */
00116     /* 7: empty                     */
00117 
00118     char        sqlstate[5];
00119 };
00120 
00121 struct sqlca_t *ECPGget_sqlca(void);
00122 
00123 #ifndef POSTGRES_ECPG_INTERNAL
00124 #define sqlca (*ECPGget_sqlca())
00125 #endif
00126 
00127 #ifdef __cplusplus
00128 }
00129 #endif
00130 
00131 #endif
00132 
00133 #line 8 "dyntest.pgc"
00134 
00135 
00136 #line 1 "regression.h"
00137 
00138 
00139 
00140 
00141 
00142 
00143 #line 9 "dyntest.pgc"
00144 
00145 
00146 static void
00147 error (void)
00148 {
00149   printf ("\n#%ld:%s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
00150   exit (1);
00151 }
00152 
00153 int
00154 main ()
00155 {
00156   /* exec sql begin declare section */
00157    
00158    
00159    
00160    
00161         
00162    
00163     
00164    
00165    
00166    
00167   
00168 #line 22 "dyntest.pgc"
00169  int COUNT ;
00170  
00171 #line 23 "dyntest.pgc"
00172  int INTVAR ;
00173  
00174 #line 24 "dyntest.pgc"
00175  int INDEX ;
00176  
00177 #line 25 "dyntest.pgc"
00178  int INDICATOR ;
00179  
00180 #line 26 "dyntest.pgc"
00181  int TYPE , LENGTH , OCTET_LENGTH , PRECISION , SCALE , RETURNED_OCTET_LENGTH ;
00182  
00183 #line 27 "dyntest.pgc"
00184  int DATETIME_INTERVAL_CODE ;
00185  
00186 #line 28 "dyntest.pgc"
00187  char NAME [ 120 ] , BOOLVAR ;
00188  
00189 #line 29 "dyntest.pgc"
00190  char STRINGVAR [ 1024 ] ;
00191  
00192 #line 30 "dyntest.pgc"
00193  double DOUBLEVAR ;
00194  
00195 #line 31 "dyntest.pgc"
00196  char * QUERY ;
00197 /* exec sql end declare section */
00198 #line 32 "dyntest.pgc"
00199 
00200   int done = 0;
00201 
00202   /* exec sql var BOOLVAR is bool */
00203 #line 35 "dyntest.pgc"
00204 
00205 
00206   ECPGdebug (1, stderr);
00207 
00208   QUERY = "select * from dyntest";
00209 
00210   /* exec sql whenever sqlerror  do error ( ) ; */
00211 #line 43 "dyntest.pgc"
00212 
00213 
00214   ECPGallocate_desc(__LINE__, "MYDESC");
00215 #line 45 "dyntest.pgc"
00216 
00217 if (sqlca.sqlcode < 0) error ( );
00218 #line 45 "dyntest.pgc"
00219 
00220 
00221   { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); 
00222 #line 47 "dyntest.pgc"
00223 
00224 if (sqlca.sqlcode < 0) error ( );}
00225 #line 47 "dyntest.pgc"
00226 
00227 
00228   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to german", ECPGt_EOIT, ECPGt_EORT);
00229 #line 49 "dyntest.pgc"
00230 
00231 if (sqlca.sqlcode < 0) error ( );}
00232 #line 49 "dyntest.pgc"
00233 
00234 
00235   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table dyntest ( name char ( 14 ) , d float8 , i int , bignumber int8 , b boolean , comment text , day date )", ECPGt_EOIT, ECPGt_EORT);
00236 #line 53 "dyntest.pgc"
00237 
00238 if (sqlca.sqlcode < 0) error ( );}
00239 #line 53 "dyntest.pgc"
00240 
00241   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into dyntest values ( 'first entry' , 14.7 , 14 , 123045607890 , true , 'The world''s most advanced open source database.' , '1987-07-14' )", ECPGt_EOIT, ECPGt_EORT);
00242 #line 54 "dyntest.pgc"
00243 
00244 if (sqlca.sqlcode < 0) error ( );}
00245 #line 54 "dyntest.pgc"
00246 
00247   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into dyntest values ( 'second entry' , 1407.87 , 1407 , 987065403210 , false , 'The elephant never forgets.' , '1999-11-5' )", ECPGt_EOIT, ECPGt_EORT);
00248 #line 55 "dyntest.pgc"
00249 
00250 if (sqlca.sqlcode < 0) error ( );}
00251 #line 55 "dyntest.pgc"
00252 
00253 
00254   { ECPGprepare(__LINE__, NULL, 0, "myquery", QUERY);
00255 #line 57 "dyntest.pgc"
00256 
00257 if (sqlca.sqlcode < 0) error ( );}
00258 #line 57 "dyntest.pgc"
00259 
00260   /* declare MYCURS cursor for $1 */
00261 #line 58 "dyntest.pgc"
00262 
00263 
00264   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare MYCURS cursor for $1", 
00265     ECPGt_char_variable,(ECPGprepared_statement(NULL, "myquery", __LINE__)),(long)1,(long)1,(1)*sizeof(char), 
00266     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
00267 #line 60 "dyntest.pgc"
00268 
00269 if (sqlca.sqlcode < 0) error ( );}
00270 #line 60 "dyntest.pgc"
00271 
00272 
00273   while (1)
00274     {
00275       { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch in MYCURS", ECPGt_EOIT, 
00276     ECPGt_descriptor, "MYDESC", 0L, 0L, 0L, 
00277     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
00278 #line 64 "dyntest.pgc"
00279 
00280 if (sqlca.sqlcode < 0) error ( );}
00281 #line 64 "dyntest.pgc"
00282 
00283 
00284       if (sqlca.sqlcode)
00285     break;
00286 
00287       { ECPGget_desc_header(__LINE__, "MYDESC", &(COUNT));
00288 
00289 #line 69 "dyntest.pgc"
00290 
00291 if (sqlca.sqlcode < 0) error ( );}
00292 #line 69 "dyntest.pgc"
00293 
00294       if (!done)
00295     {
00296       printf ("Found %d columns\n", COUNT);
00297       done = 1;
00298     }
00299 
00300       for (INDEX = 1; INDEX <= COUNT; ++INDEX)
00301     {
00302     { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_indicator,
00303     ECPGt_int,&(INDICATOR),(long)1,(long)1,sizeof(int), ECPGd_name,
00304     ECPGt_char,(NAME),(long)120,(long)1,(120)*sizeof(char), ECPGd_scale,
00305     ECPGt_int,&(SCALE),(long)1,(long)1,sizeof(int), ECPGd_precision,
00306     ECPGt_int,&(PRECISION),(long)1,(long)1,sizeof(int), ECPGd_ret_octet,
00307     ECPGt_int,&(RETURNED_OCTET_LENGTH),(long)1,(long)1,sizeof(int), ECPGd_octet,
00308     ECPGt_int,&(OCTET_LENGTH),(long)1,(long)1,sizeof(int), ECPGd_length,
00309     ECPGt_int,&(LENGTH),(long)1,(long)1,sizeof(int), ECPGd_type,
00310     ECPGt_int,&(TYPE),(long)1,(long)1,sizeof(int), ECPGd_EODT);
00311 
00312 #line 86 "dyntest.pgc"
00313 
00314 if (sqlca.sqlcode < 0) error ( );}
00315 #line 86 "dyntest.pgc"
00316 
00317       printf ("%2d\t%s (type: %d length: %d precision: %d scale: %d = " , INDEX, NAME, TYPE, LENGTH, PRECISION, SCALE);
00318       switch (TYPE)
00319         {
00320         case SQL3_BOOLEAN:
00321           printf ("bool");
00322           break;
00323         case SQL3_NUMERIC:
00324           printf ("numeric(%d,%d)", PRECISION, SCALE);
00325           break;
00326         case SQL3_DECIMAL:
00327           printf ("decimal(%d,%d)", PRECISION, SCALE);
00328           break;
00329         case SQL3_INTEGER:
00330           printf ("integer");
00331           break;
00332         case SQL3_SMALLINT:
00333           printf ("smallint");
00334           break;
00335         case SQL3_FLOAT:
00336           printf ("float(%d,%d)", PRECISION, SCALE);
00337           break;
00338         case SQL3_REAL:
00339           printf ("real");
00340           break;
00341         case SQL3_DOUBLE_PRECISION:
00342           printf ("double precision");
00343           break;
00344         case SQL3_DATE_TIME_TIMESTAMP:
00345         { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_di_code,
00346     ECPGt_int,&(DATETIME_INTERVAL_CODE),(long)1,(long)1,sizeof(int), ECPGd_EODT);
00347 
00348 #line 116 "dyntest.pgc"
00349 
00350 if (sqlca.sqlcode < 0) error ( );}
00351 #line 116 "dyntest.pgc"
00352 
00353           switch (DATETIME_INTERVAL_CODE)
00354         {
00355         case SQL3_DDT_DATE:
00356           printf ("date");
00357           break;
00358         case SQL3_DDT_TIME:
00359           printf ("time");
00360           break;
00361         case SQL3_DDT_TIMESTAMP:
00362           printf ("timestamp");
00363           break;
00364         case SQL3_DDT_TIME_WITH_TIME_ZONE:
00365           printf ("time with time zone");
00366           break;
00367         case SQL3_DDT_TIMESTAMP_WITH_TIME_ZONE:
00368           printf ("timestamp with time zone");
00369           break;
00370         }
00371           break;
00372         case SQL3_INTERVAL:
00373           printf ("interval");
00374           break;
00375         case SQL3_CHARACTER:
00376           if (LENGTH > 0)
00377         printf ("char(%d)", LENGTH);
00378           else
00379         printf ("text");
00380           break;
00381         case SQL3_CHARACTER_VARYING:
00382           if (LENGTH > 0)
00383         printf ("varchar(%d)", LENGTH);
00384           else
00385         printf ("varchar()");
00386           break;
00387         default:
00388           printf ("<SQL3 %d>", TYPE);
00389           break;
00390         }
00391       printf (")\n\toctet_length: %d returned_octet_length: %d)\n\t= ",
00392           OCTET_LENGTH, RETURNED_OCTET_LENGTH);
00393       if (INDICATOR == -1)
00394         printf ("NULL\n");
00395       else
00396         switch (TYPE)
00397           {
00398           case SQL3_BOOLEAN:
00399           { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
00400     ECPGt_bool,&(BOOLVAR),(long)1,(long)1,sizeof(bool), ECPGd_EODT);
00401 
00402 #line 163 "dyntest.pgc"
00403 
00404 if (sqlca.sqlcode < 0) error ( );}
00405 #line 163 "dyntest.pgc"
00406 
00407         printf ("%s\n", BOOLVAR ? "true" : "false");
00408         break;
00409           case SQL3_INTEGER:
00410           case SQL3_SMALLINT:
00411           { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
00412     ECPGt_int,&(INTVAR),(long)1,(long)1,sizeof(int), ECPGd_EODT);
00413 
00414 #line 168 "dyntest.pgc"
00415 
00416 if (sqlca.sqlcode < 0) error ( );}
00417 #line 168 "dyntest.pgc"
00418 
00419         printf ("%d\n", INTVAR);
00420         break;
00421           case SQL3_DOUBLE_PRECISION:
00422           { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
00423     ECPGt_double,&(DOUBLEVAR),(long)1,(long)1,sizeof(double), ECPGd_EODT);
00424 
00425 #line 172 "dyntest.pgc"
00426 
00427 if (sqlca.sqlcode < 0) error ( );}
00428 #line 172 "dyntest.pgc"
00429 
00430         printf ("%.*f\n", PRECISION, DOUBLEVAR);
00431         break;
00432           case SQL3_DATE_TIME_TIMESTAMP:
00433           { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
00434     ECPGt_char,(STRINGVAR),(long)1024,(long)1,(1024)*sizeof(char), ECPGd_di_code,
00435     ECPGt_int,&(DATETIME_INTERVAL_CODE),(long)1,(long)1,sizeof(int), ECPGd_EODT);
00436 
00437 #line 178 "dyntest.pgc"
00438 
00439 if (sqlca.sqlcode < 0) error ( );}
00440 #line 178 "dyntest.pgc"
00441 
00442         printf ("%d \"%s\"\n", DATETIME_INTERVAL_CODE, STRINGVAR);
00443         break;
00444           case SQL3_CHARACTER:
00445           case SQL3_CHARACTER_VARYING:
00446           { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
00447     ECPGt_char,(STRINGVAR),(long)1024,(long)1,(1024)*sizeof(char), ECPGd_EODT);
00448 
00449 #line 183 "dyntest.pgc"
00450 
00451 if (sqlca.sqlcode < 0) error ( );}
00452 #line 183 "dyntest.pgc"
00453 
00454         printf ("\"%s\"\n", STRINGVAR);
00455         break;
00456           default:
00457           { ECPGget_desc(__LINE__, "MYDESC", INDEX,ECPGd_data,
00458     ECPGt_char,(STRINGVAR),(long)1024,(long)1,(1024)*sizeof(char), ECPGd_EODT);
00459 
00460 #line 187 "dyntest.pgc"
00461 
00462 if (sqlca.sqlcode < 0) error ( );}
00463 #line 187 "dyntest.pgc"
00464 
00465         printf ("<\"%s\">\n", STRINGVAR);
00466         break;
00467           }
00468     }
00469     }
00470 
00471   { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close MYCURS", ECPGt_EOIT, ECPGt_EORT);
00472 #line 194 "dyntest.pgc"
00473 
00474 if (sqlca.sqlcode < 0) error ( );}
00475 #line 194 "dyntest.pgc"
00476 
00477 
00478   ECPGdeallocate_desc(__LINE__, "MYDESC");
00479 #line 196 "dyntest.pgc"
00480 
00481 if (sqlca.sqlcode < 0) error ( );
00482 #line 196 "dyntest.pgc"
00483 
00484 
00485   return 0;
00486   }