Header And Logo

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

sql-describe.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 "describe.pgc"
00010 #include <stdlib.h>
00011 #include <string.h>
00012 
00013 
00014 #line 1 "regression.h"
00015 
00016 
00017 
00018 
00019 
00020 
00021 #line 4 "describe.pgc"
00022 
00023 
00024 #line 1 "sqlda.h"
00025 #ifndef ECPG_SQLDA_H
00026 #define ECPG_SQLDA_H
00027 
00028 #ifdef _ECPG_INFORMIX_H
00029 
00030 #include "sqlda-compat.h"
00031 typedef struct sqlvar_compat    sqlvar_t;
00032 typedef struct sqlda_compat sqlda_t;
00033 
00034 #else
00035 
00036 #include "sqlda-native.h"
00037 typedef struct sqlvar_struct    sqlvar_t;
00038 typedef struct sqlda_struct sqlda_t;
00039 
00040 #endif
00041 
00042 #endif /* ECPG_SQLDA_H */
00043 
00044 #line 5 "describe.pgc"
00045 
00046 
00047 /* exec sql whenever sqlerror  stop ; */
00048 #line 7 "describe.pgc"
00049 
00050 
00051 sqlda_t *sqlda1, *sqlda2, *sqlda3;
00052 
00053 int
00054 main (void)
00055 {
00056 /* exec sql begin declare section */
00057           
00058           
00059           
00060           
00061           
00062 
00063 #line 15 "describe.pgc"
00064  char * stmt1 = "SELECT id, t FROM descr_t2" ;
00065  
00066 #line 16 "describe.pgc"
00067  char * stmt2 = "SELECT id, t FROM descr_t2 WHERE id = -1" ;
00068  
00069 #line 17 "describe.pgc"
00070  int i , count1 , count2 ;
00071  
00072 #line 18 "describe.pgc"
00073  char field_name1 [ 30 ] = "not set" ;
00074  
00075 #line 19 "describe.pgc"
00076  char field_name2 [ 30 ] = "not set" ;
00077 /* exec sql end declare section */
00078 #line 20 "describe.pgc"
00079 
00080 
00081     char msg[128];
00082 
00083     ECPGdebug(1, stderr);
00084 
00085     strcpy(msg, "connect");
00086     { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); 
00087 #line 27 "describe.pgc"
00088 
00089 if (sqlca.sqlcode < 0) exit (1);}
00090 #line 27 "describe.pgc"
00091 
00092 
00093     strcpy(msg, "set");
00094     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
00095 #line 30 "describe.pgc"
00096 
00097 if (sqlca.sqlcode < 0) exit (1);}
00098 #line 30 "describe.pgc"
00099 
00100 
00101     strcpy(msg, "create");
00102     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table descr_t2 ( id serial primary key , t text )", ECPGt_EOIT, ECPGt_EORT);
00103 #line 33 "describe.pgc"
00104 
00105 if (sqlca.sqlcode < 0) exit (1);}
00106 #line 33 "describe.pgc"
00107 
00108 
00109     strcpy(msg, "insert");
00110     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into descr_t2 ( id , t ) values ( default , 'a' )", ECPGt_EOIT, ECPGt_EORT);
00111 #line 36 "describe.pgc"
00112 
00113 if (sqlca.sqlcode < 0) exit (1);}
00114 #line 36 "describe.pgc"
00115 
00116     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into descr_t2 ( id , t ) values ( default , 'b' )", ECPGt_EOIT, ECPGt_EORT);
00117 #line 37 "describe.pgc"
00118 
00119 if (sqlca.sqlcode < 0) exit (1);}
00120 #line 37 "describe.pgc"
00121 
00122     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into descr_t2 ( id , t ) values ( default , 'c' )", ECPGt_EOIT, ECPGt_EORT);
00123 #line 38 "describe.pgc"
00124 
00125 if (sqlca.sqlcode < 0) exit (1);}
00126 #line 38 "describe.pgc"
00127 
00128     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into descr_t2 ( id , t ) values ( default , 'd' )", ECPGt_EOIT, ECPGt_EORT);
00129 #line 39 "describe.pgc"
00130 
00131 if (sqlca.sqlcode < 0) exit (1);}
00132 #line 39 "describe.pgc"
00133 
00134 
00135     strcpy(msg, "commit");
00136     { ECPGtrans(__LINE__, NULL, "commit");
00137 #line 42 "describe.pgc"
00138 
00139 if (sqlca.sqlcode < 0) exit (1);}
00140 #line 42 "describe.pgc"
00141 
00142 
00143     /*
00144      * Test DESCRIBE with a query producing tuples.
00145      * DESCRIPTOR and SQL DESCRIPTOR are NOT the same in
00146      * Informix-compat mode.
00147      */
00148 
00149     strcpy(msg, "allocate");
00150     ECPGallocate_desc(__LINE__, "desc1");
00151 #line 51 "describe.pgc"
00152 
00153 if (sqlca.sqlcode < 0) exit (1);
00154 #line 51 "describe.pgc"
00155 
00156     ECPGallocate_desc(__LINE__, "desc2");
00157 #line 52 "describe.pgc"
00158 
00159 if (sqlca.sqlcode < 0) exit (1);
00160 #line 52 "describe.pgc"
00161 
00162 
00163     strcpy(msg, "prepare");
00164     { ECPGprepare(__LINE__, NULL, 0, "st_id1", stmt1);
00165 #line 55 "describe.pgc"
00166 
00167 if (sqlca.sqlcode < 0) exit (1);}
00168 #line 55 "describe.pgc"
00169 
00170 
00171     sqlda1 = sqlda2 = sqlda3 = NULL;
00172 
00173     strcpy(msg, "describe");
00174     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id1",
00175     ECPGt_descriptor, "desc1", 0L, 0L, 0L, 
00176     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00177 #line 60 "describe.pgc"
00178 
00179     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id1",
00180     ECPGt_descriptor, "desc2", 0L, 0L, 0L, 
00181     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00182 #line 61 "describe.pgc"
00183 
00184 
00185     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id1",
00186     ECPGt_sqlda, &sqlda1, 0L, 0L, 0L, 
00187     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00188 #line 63 "describe.pgc"
00189 
00190     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id1",
00191     ECPGt_sqlda, &sqlda2, 0L, 0L, 0L, 
00192     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00193 #line 64 "describe.pgc"
00194 
00195     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id1",
00196     ECPGt_sqlda, &sqlda3, 0L, 0L, 0L, 
00197     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00198 #line 65 "describe.pgc"
00199 
00200 
00201     if (sqlda1 == NULL)
00202     {
00203         printf("sqlda1 NULL\n");
00204         exit(1);
00205     }
00206 
00207     if (sqlda2 == NULL)
00208     {
00209         printf("sqlda2 NULL\n");
00210         exit(1);
00211     }
00212 
00213     if (sqlda3 == NULL)
00214     {
00215         printf("sqlda3 NULL\n");
00216         exit(1);
00217     }
00218 
00219     strcpy(msg, "get descriptor");
00220     { ECPGget_desc_header(__LINE__, "desc1", &(count1));
00221 
00222 #line 86 "describe.pgc"
00223 
00224 if (sqlca.sqlcode < 0) exit (1);}
00225 #line 86 "describe.pgc"
00226 
00227     { ECPGget_desc_header(__LINE__, "desc1", &(count2));
00228 
00229 #line 87 "describe.pgc"
00230 
00231 if (sqlca.sqlcode < 0) exit (1);}
00232 #line 87 "describe.pgc"
00233 
00234 
00235     if (count1 != count2)
00236     {
00237         printf("count1 (%d) != count2 (%d)\n", count1, count2);
00238         exit(1);
00239     }
00240 
00241     if (count1 != sqlda1->sqld)
00242     {
00243         printf("count1 (%d) != sqlda1->sqld (%d)\n", count1, sqlda1->sqld);
00244         exit(1);
00245     }
00246 
00247     if (count1 != sqlda2->sqld)
00248     {
00249         printf("count1 (%d) != sqlda2->sqld (%d)\n", count1, sqlda2->sqld);
00250         exit(1);
00251     }
00252 
00253     if (count1 != sqlda3->sqld)
00254     {
00255         printf("count1 (%d) != sqlda3->sqld (%d)\n", count1, sqlda3->sqld);
00256         exit(1);
00257     }
00258 
00259     for (i = 1; i <= count1; i++)
00260     {
00261         { ECPGget_desc(__LINE__, "desc1", i,ECPGd_name,
00262     ECPGt_char,(field_name1),(long)30,(long)1,(30)*sizeof(char), ECPGd_EODT);
00263 
00264 #line 115 "describe.pgc"
00265 
00266 if (sqlca.sqlcode < 0) exit (1);}
00267 #line 115 "describe.pgc"
00268 
00269         { ECPGget_desc(__LINE__, "desc2", i,ECPGd_name,
00270     ECPGt_char,(field_name2),(long)30,(long)1,(30)*sizeof(char), ECPGd_EODT);
00271 
00272 #line 116 "describe.pgc"
00273 
00274 if (sqlca.sqlcode < 0) exit (1);}
00275 #line 116 "describe.pgc"
00276 
00277         printf("%d\n\tfield_name1 '%s'\n\tfield_name2 '%s'\n\t"
00278             "sqlda1 '%s'\n\tsqlda2 '%s'\n\tsqlda3 '%s'\n",
00279             i, field_name1, field_name2,
00280             sqlda1->sqlvar[i-1].sqlname.data,
00281             sqlda2->sqlvar[i-1].sqlname.data,
00282             sqlda3->sqlvar[i-1].sqlname.data);
00283     }
00284 
00285     strcpy(msg, "deallocate");
00286     ECPGdeallocate_desc(__LINE__, "desc1");
00287 #line 126 "describe.pgc"
00288 
00289 if (sqlca.sqlcode < 0) exit (1);
00290 #line 126 "describe.pgc"
00291 
00292     ECPGdeallocate_desc(__LINE__, "desc2");
00293 #line 127 "describe.pgc"
00294 
00295 if (sqlca.sqlcode < 0) exit (1);
00296 #line 127 "describe.pgc"
00297 
00298     free(sqlda1);
00299     free(sqlda2);
00300     free(sqlda3);
00301 
00302     { ECPGdeallocate(__LINE__, 0, NULL, "st_id1");
00303 #line 132 "describe.pgc"
00304 
00305 if (sqlca.sqlcode < 0) exit (1);}
00306 #line 132 "describe.pgc"
00307 
00308 
00309     /* Test DESCRIBE with a query not producing tuples */
00310 
00311     strcpy(msg, "allocate");
00312     ECPGallocate_desc(__LINE__, "desc1");
00313 #line 137 "describe.pgc"
00314 
00315 if (sqlca.sqlcode < 0) exit (1);
00316 #line 137 "describe.pgc"
00317 
00318     ECPGallocate_desc(__LINE__, "desc2");
00319 #line 138 "describe.pgc"
00320 
00321 if (sqlca.sqlcode < 0) exit (1);
00322 #line 138 "describe.pgc"
00323 
00324 
00325     strcpy(msg, "prepare");
00326     { ECPGprepare(__LINE__, NULL, 0, "st_id2", stmt2);
00327 #line 141 "describe.pgc"
00328 
00329 if (sqlca.sqlcode < 0) exit (1);}
00330 #line 141 "describe.pgc"
00331 
00332 
00333     sqlda1 = sqlda2 = sqlda3 = NULL;
00334 
00335     strcpy(msg, "describe");
00336     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id2",
00337     ECPGt_descriptor, "desc1", 0L, 0L, 0L, 
00338     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00339 #line 146 "describe.pgc"
00340 
00341     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id2",
00342     ECPGt_descriptor, "desc2", 0L, 0L, 0L, 
00343     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00344 #line 147 "describe.pgc"
00345 
00346 
00347     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id2",
00348     ECPGt_sqlda, &sqlda1, 0L, 0L, 0L, 
00349     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00350 #line 149 "describe.pgc"
00351 
00352     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id2",
00353     ECPGt_sqlda, &sqlda2, 0L, 0L, 0L, 
00354     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00355 #line 150 "describe.pgc"
00356 
00357     { ECPGdescribe(__LINE__, 0, 0, NULL, "st_id2",
00358     ECPGt_sqlda, &sqlda3, 0L, 0L, 0L, 
00359     ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
00360 #line 151 "describe.pgc"
00361 
00362 
00363     if (sqlda1 == NULL || sqlda1 == NULL || sqlda2 == NULL)
00364         exit(1);
00365 
00366     strcpy(msg, "get descriptor");
00367     { ECPGget_desc_header(__LINE__, "desc1", &(count1));
00368 
00369 #line 157 "describe.pgc"
00370 
00371 if (sqlca.sqlcode < 0) exit (1);}
00372 #line 157 "describe.pgc"
00373 
00374     { ECPGget_desc_header(__LINE__, "desc1", &(count2));
00375 
00376 #line 158 "describe.pgc"
00377 
00378 if (sqlca.sqlcode < 0) exit (1);}
00379 #line 158 "describe.pgc"
00380 
00381 
00382     if (!(  count1 == count2 &&
00383         count1 == sqlda1->sqld &&
00384         count1 == sqlda2->sqld &&
00385         count1 == sqlda3->sqld))
00386         exit(1);
00387 
00388     for (i = 1; i <= count1; i++)
00389     {
00390         { ECPGget_desc(__LINE__, "desc1", i,ECPGd_name,
00391     ECPGt_char,(field_name1),(long)30,(long)1,(30)*sizeof(char), ECPGd_EODT);
00392 
00393 #line 168 "describe.pgc"
00394 
00395 if (sqlca.sqlcode < 0) exit (1);}
00396 #line 168 "describe.pgc"
00397 
00398         { ECPGget_desc(__LINE__, "desc2", i,ECPGd_name,
00399     ECPGt_char,(field_name2),(long)30,(long)1,(30)*sizeof(char), ECPGd_EODT);
00400 
00401 #line 169 "describe.pgc"
00402 
00403 if (sqlca.sqlcode < 0) exit (1);}
00404 #line 169 "describe.pgc"
00405 
00406         printf("%d\n\tfield_name1 '%s'\n\tfield_name2 '%s'\n\t"
00407             "sqlda1 '%s'\n\tsqlda2 '%s'\n\tsqlda3 '%s'\n",
00408             i, field_name1, field_name2,
00409             sqlda1->sqlvar[i-1].sqlname.data,
00410             sqlda2->sqlvar[i-1].sqlname.data,
00411             sqlda3->sqlvar[i-1].sqlname.data);
00412     }
00413 
00414     strcpy(msg, "deallocate");
00415     ECPGdeallocate_desc(__LINE__, "desc1");
00416 #line 179 "describe.pgc"
00417 
00418 if (sqlca.sqlcode < 0) exit (1);
00419 #line 179 "describe.pgc"
00420 
00421     ECPGdeallocate_desc(__LINE__, "desc2");
00422 #line 180 "describe.pgc"
00423 
00424 if (sqlca.sqlcode < 0) exit (1);
00425 #line 180 "describe.pgc"
00426 
00427     free(sqlda1);
00428     free(sqlda2);
00429     free(sqlda3);
00430 
00431     { ECPGdeallocate(__LINE__, 0, NULL, "st_id2");
00432 #line 185 "describe.pgc"
00433 
00434 if (sqlca.sqlcode < 0) exit (1);}
00435 #line 185 "describe.pgc"
00436 
00437 
00438     /* End test */
00439 
00440     strcpy(msg, "drop");
00441     { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table descr_t2", ECPGt_EOIT, ECPGt_EORT);
00442 #line 190 "describe.pgc"
00443 
00444 if (sqlca.sqlcode < 0) exit (1);}
00445 #line 190 "describe.pgc"
00446 
00447 
00448     strcpy(msg, "commit");
00449     { ECPGtrans(__LINE__, NULL, "commit");
00450 #line 193 "describe.pgc"
00451 
00452 if (sqlca.sqlcode < 0) exit (1);}
00453 #line 193 "describe.pgc"
00454 
00455 
00456     strcpy(msg, "disconnect");
00457     { ECPGdisconnect(__LINE__, "CURRENT");
00458 #line 196 "describe.pgc"
00459 
00460 if (sqlca.sqlcode < 0) exit (1);}
00461 #line 196 "describe.pgc"
00462 
00463 
00464     return (0);
00465 }