home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume18 / oraperl / part01 / orafns.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-11  |  8.4 KB  |  370 lines

  1. /* orafns.c
  2.  *
  3.  * Simple C interface to Oracle, intended to be linked to Perl.
  4.  */
  5. /* Copyright 1991 Kevin Stock.
  6.  *
  7.  * You may copy this under the terms of the GNU General Public License,
  8.  * a copy of which should have accompanied your Perl kit.
  9.  */
  10.  
  11. #include    "INTERN.h"
  12. #include    <stdio.h>
  13. #include    <ctype.h>
  14. #include    "orafns.h"
  15.  
  16.  
  17. /* address[] is used to return cursor addresses to the perl program
  18.  * it is used so that we can get the addresses exactly right, without
  19.  * worrying about rounding errors or playing with oracle.mus
  20.  */
  21.  
  22. char    address[20];
  23.  
  24.  
  25. /* NOSID is returned by set_sid if the environment can't be set */
  26.  
  27. #define        NOSID    ((char *) -1)
  28.  
  29.  
  30. /* set_sid(database)
  31.  *
  32.  * Sets the environment variable ORACLE_SID to the given string.
  33.  * Returns the previous value.
  34.  * If the parameter is NULL, restores the previous saved value, if any.
  35.  */
  36.  
  37. char *set_sid(database)
  38. char *database;
  39. {
  40.     char        *sid;
  41.     static    char    *oldsid = NULL,
  42.             *newsid = NULL;
  43.  
  44.     DEBUG(8, (fprintf(stderr, "set_sid(%s)\n",
  45.         (database == NULL) ? "<NULL>" : database)));
  46.  
  47.     if (database != NULL)
  48.     {
  49.         /* normal case - save old value and set new */
  50.  
  51.         if ((sid = getenv("ORACLE_SID")) != NULL)
  52.         {
  53.             if  (oldsid != NULL)
  54.             {
  55.                 DEBUG(128, (fprintf(stderr,
  56.                     "set_sid: freeing oldsid (%#lx)\n",
  57.                     (long) oldsid)));
  58.                 free(oldsid);
  59.             }
  60.             if ((oldsid = malloc(strlen(sid) + 1)) == NULL)
  61.             {
  62.                 DEBUG(128, (fputs("set_sid: out of memory\n",
  63.                     stderr)));
  64.                 DEBUG(8, (fputs("set_sid: returning NOSID\n",
  65.                     stderr)));
  66.                 ora_errno = ORAP_NOMEM;
  67.                 return(NOSID);
  68.             }
  69.             DEBUG(128, (fprintf(stderr,
  70.                 "set_sid: got oldsid at %#lx\n", (long) oldsid)));
  71.             strcpy(oldsid, sid);
  72.         }
  73.  
  74.         if (newsid != NULL)
  75.         {
  76.             DEBUG(128, (fprintf(stderr,
  77.                 "set_sid: freeing newsid (%#lx)\n",
  78.                 (long) newsid)));
  79.             free(newsid);
  80.         }
  81.         if ((newsid = malloc(strlen(database) + 12)) == NULL)
  82.         {
  83.             DEBUG(128, (fputs("set_sid: out of memory\n", stderr)));
  84.             DEBUG(8, (fputs("set_sid: returning NOSID\n", stderr)));
  85.             ora_errno = ORAP_NOMEM;
  86.             return(NOSID);
  87.         }
  88.         DEBUG(128, (fprintf(stderr,
  89.             "set_sid: got newsid at %#lx\n", (long) newsid)));
  90.         strcpy(newsid, "ORACLE_SID=");
  91.         strcat(newsid, database);
  92.  
  93.         DEBUG(8, (fprintf(stderr, "set_sid: setting %s\n", newsid)));
  94.         return (putenv(newsid)) ? oldsid : NULL;
  95.     }
  96.     else
  97.     {
  98.         if (oldsid == NULL)
  99.         {
  100.             DEBUG(8, (fputs("set_sid: oldsid not set\n", stderr)));
  101.             return(NULL);
  102.         }
  103.  
  104.         if (newsid != NULL)
  105.         {
  106.             DEBUG(128, (fprintf(stderr,
  107.                 "set_sid: freeing newsid (%#lx)\n", (long)newsid)));
  108.             free(newsid);
  109.         }
  110.         if ((newsid = malloc(strlen(oldsid) + 12)) == NULL)
  111.         {
  112.             DEBUG(128, (fputs("set_sid: out of memory\n", stderr)));
  113.             DEBUG(8, (fputs("set_sid: returning NOSID\n", stderr)));
  114.             ora_errno = ORAP_NOMEM;
  115.             return(NOSID);
  116.         }
  117.         DEBUG(128, (fprintf(stderr,
  118.             "set_sid: got newsid at %#lx\n", (long) newsid)));
  119.         strcpy(newsid, "ORACLE_SID=");
  120.         strcat(newsid, oldsid);
  121.  
  122.         DEBUG(8, (fprintf(stderr, "set_sid: setting %s\n", newsid)));
  123.         return (putenv(newsid)) ? oldsid : NULL;
  124.     }
  125.  
  126.     /* NOTREACHED */
  127. }
  128.  
  129.  
  130. /* ora_login(database, name, password)
  131.  *
  132.  * logs into the current database under the given name and password.
  133.  */
  134.  
  135. char *ora_login(database, name, password)
  136. char *database, *name, *password;
  137. {
  138.     int logged;
  139.     char *tmp;
  140.     struct cursor *lda;
  141.  
  142.     DEBUG(8, (fprintf(stderr,
  143.         "ora_login(%s, %s, %s)\n", database, name, password)));
  144.  
  145.     if ((lda = ora_getlda()) == NULL)
  146.     {
  147.         DEBUG(8, (fputs("ora_login: couldn't get an lda\n", stderr)));
  148.         return(NULL);
  149.     }
  150.  
  151.     if (set_sid(database) == NOSID)
  152.     {
  153.         DEBUG(8, (fputs("ora_login: couldn't set database\n", stderr)));
  154.         ora_dropcursor(lda);
  155.         return(NULL);
  156.     }
  157.     else if (strcmp(database, getenv("ORACLE_SID")) != 0)
  158.     {
  159.         DEBUG(8, (fprintf(stderr,"ora_login: ORACLE_SID misset to %s\n",
  160.             (tmp = getenv("ORACLE_SID")) ? tmp : NULL)));
  161.         ora_dropcursor(lda);
  162.         ora_errno = ORAP_NOSID;
  163.         return(NULL);
  164.     }
  165.  
  166.     logged = orlon(lda->csr, lda->hda, name, -1, password, -1, 0);
  167.     set_sid(NULL);        /* don't really care if this fails */
  168.  
  169.     if (logged == 0)
  170.     {
  171.         sprintf(address, "%#lx", (long) lda);
  172.         DEBUG(8, (fprintf(stderr,
  173.             "ora_login: returning lda %s\n", address)));
  174.         ora_errno = 0;
  175.         return(address);
  176.     }
  177.     else
  178.     {
  179.         ora_errno = lda->csr->csrrc;
  180.         ora_droplda(lda);
  181.         DEBUG(8, (fprintf(stderr,
  182.             "ora_login: failed (error %d)\n", ora_errno)));
  183.         return((char *) NULL);
  184.     }
  185. }
  186.  
  187.  
  188. /* ora_open(lda, query)
  189.  *
  190.  * sets and executes the specified sql query
  191.  */
  192.  
  193. char *ora_open(lda_s, query)
  194. char *lda_s;
  195. char *query;
  196. {
  197.     int i;
  198.     struct cursor *csr;
  199.     struct cursor *lda = (struct cursor *) strtol(lda_s, (char **) NULL, 0);
  200.     short dbsize;
  201.  
  202.     DEBUG(8, (fprintf(stderr, "ora_open(%#lx, %s)\n", (long) lda, query)));
  203.  
  204.     if (check_lda(lda) == 0)
  205.     {
  206.         DEBUG(8, (fputs("ora_open: returning NULL\n", stderr)));
  207.         ora_errno = ORAP_INVLDA;
  208.         return((char *) NULL);
  209.     }
  210.  
  211.     if ((csr = ora_getcursor()) == NULL)
  212.     {
  213.         /* ora_errno is set by ora_getcursor */
  214.         DEBUG(8, (fprintf(stderr, "ora_open: can't get a cursor\n")));
  215.         return((char *) NULL);
  216.     }
  217.  
  218.     if ((oopen(csr->csr, lda->csr, (char *)-1, -1, -1, (char *)-1, -1) != 0)
  219.         || (osql3(csr->csr, query, -1) != 0)
  220.         || (oexec(csr->csr) != 0))
  221.     {
  222.         ora_errno = csr->csr->csrrc;
  223.         ora_dropcursor(csr);
  224.         DEBUG(8, (fprintf(stderr,
  225.             "couldn't run SQL statement (error %d)\n", ora_errno)));
  226.         return((char *) NULL);
  227.     }
  228.  
  229.     /* set up csr->data to receive the information when we do a fetch */
  230.  
  231.     i = 0;
  232.     do
  233.     {
  234.         odsc(csr->csr, ++i, (short *) 0, (short *) 0, (short *) 0,
  235.             (short *) 0, (char *) 0, (short *) 0, (short *) 0);
  236.     } while (csr->csr->csrrc == 0);
  237.     --i;
  238.  
  239.     ora_errno = 0;
  240.  
  241.     if ((csr->data = (char **) malloc(i * sizeof(char *))) == NULL)
  242.     {
  243.         DEBUG(128, (fputs("ora_open: out of memory\n", stderr)));
  244.         DEBUG(8, (fputs("ora_open: returning NOMEM\n", stderr)));
  245.         ora_errno = ORAP_NOMEM;
  246.         ora_dropcursor(csr);
  247.         return(0);
  248.     }
  249.     DEBUG(128, (fprintf(stderr, "ora_open: got data at %#lx\n",csr->data)));
  250.     csr->nfields = i;
  251.  
  252.     for (i = 0 ; i < csr->nfields ; i++)
  253.     {
  254.         odsc(csr->csr, i + 1, &dbsize, (short *) 0, (short *) 0,
  255.             (short *) 0, (char *) 0, (short *) 0, (short *) 0);
  256.  
  257.         if ((csr->data[i] = (char *) malloc(dbsize + 1)) == NULL)
  258.         {
  259.             csr->nfields = i;
  260.             ora_dropcursor(csr);
  261.  
  262.             DEBUG(128, (fputs("ora_open: out of memory\n",stderr)));
  263.             DEBUG(8, (fputs("ora_open: returning NOMEM\n",stderr)));
  264.             ora_errno = ORAP_NOMEM;
  265.             return((char *) NULL);
  266.         }
  267.         DEBUG(128, (fprintf(stderr, "ora_open: got field %d at %#lx\n",
  268.             i, csr->data[i])));
  269.         odefin(csr->csr, i + 1, csr->data[i], dbsize + 1, 5, 0,
  270.             (short *) 0, (char *) 0, 0, 0, (short *) 0, (char *) 0);
  271.     }
  272.  
  273.     sprintf(address, "%#lx", (long) csr);
  274.     DEBUG(8, (fprintf(stderr, "ora_open: returning csr %s\n", address)));
  275.     return(address);
  276. }
  277.  
  278.  
  279. /* ora_fetch(csr)
  280.  *
  281.  * returns the next set of data from the cursor
  282.  */
  283.  
  284. int ora_fetch(csr_s)
  285. char *csr_s;
  286. {
  287.     struct cursor *csr = (struct cursor *) strtol(csr_s, (char **) NULL, 0);
  288.  
  289.     DEBUG(8, (fprintf(stderr, "ora_fetch(%#lx)\n", (long) csr)));
  290.  
  291.     if (check_csr(csr) == 0)
  292.     {
  293.         DEBUG(8, (fputs("ora_fetch: returning NULL\n", stderr)));
  294.         ora_errno = ORAP_INVCSR;
  295.         return(NULL);
  296.     }
  297.  
  298.     if ((csr->nfields == 0) || (ofetch(csr->csr) != 0))
  299.     {
  300.         DEBUG(8, (fputs("ora_fetch: ofetch failed, returing 0\n",
  301.             stderr)));
  302.         ora_result = NULL;
  303.         ora_errno = csr->csr->csrrc;
  304.         return(0);
  305.     }
  306.  
  307.     ora_result = csr->data;
  308.     ora_errno = 0;
  309.     DEBUG(8, (fprintf(stderr,"ora_fetch: returning <%d>\n", csr->nfields)));
  310.     return(csr->nfields);
  311. }
  312.  
  313.  
  314. char    *OK    = "OK";        /* valid return from ora_close, ora_logoff */
  315.  
  316. /* ora_close(csr)
  317.  *
  318.  * Closes an oracle statement, releasing resources
  319.  */
  320.  
  321. char *ora_close(csr_s)
  322. char *csr_s;
  323. {
  324.     struct cursor *csr = (struct cursor *) strtol(csr_s, (char **) NULL, 0);
  325.  
  326.     DEBUG(8, (fprintf(stderr, "ora_close(%#lx)\n", (long) csr)));
  327.  
  328.     if (check_csr(csr) == 0)
  329.     {
  330.         DEBUG(8, (fputs("ora_close: returning NULL\n", stderr)));
  331.         ora_errno = ORAP_INVCSR;
  332.         return(NULL);
  333.     }
  334.  
  335.     oclose(csr->csr);
  336.     ora_errno = csr->csr->csrrc;
  337.     ora_dropcursor(csr);
  338.  
  339.     DEBUG(8, (fputs("ora_close: returning OK\n", stderr)));
  340.     return(OK);
  341. }
  342.  
  343.  
  344. /* ora_logoff(lda)
  345.  *
  346.  * Logs the user off of Oracle, releasing all resources
  347.  */
  348.  
  349. char *ora_logoff(lda_s)
  350. char *lda_s;
  351. {
  352.     struct cursor *lda = (struct cursor *) strtol(lda_s, (char **) NULL, 0);
  353.  
  354.     DEBUG(8, (fprintf(stderr, "ora_logoff(%#lx)\n", (long) lda)));
  355.  
  356.     if (check_lda(lda) == 0)
  357.     {
  358.         DEBUG(8, (fputs("ora_logoff: returning NULL\n", stderr)));
  359.         ora_errno = ORAP_INVLDA;
  360.         return(NULL);
  361.     }
  362.  
  363.     ologof(lda->csr);
  364.     ora_errno = lda->csr->csrrc;
  365.     ora_droplda(lda);
  366.  
  367.     DEBUG(8, (fputs("ora_logoff: returning OK\n", stderr)));
  368.     return(OK);
  369. }
  370.