home *** CD-ROM | disk | FTP | other *** search
- /* orafns.c
- *
- * Simple C interface to Oracle, intended to be linked to Perl.
- */
- /* Copyright 1991 Kevin Stock.
- *
- * You may copy this under the terms of the GNU General Public License,
- * a copy of which should have accompanied your Perl kit.
- */
-
- #include "INTERN.h"
- #include <stdio.h>
- #include <ctype.h>
- #include "orafns.h"
-
-
- /* address[] is used to return cursor addresses to the perl program
- * it is used so that we can get the addresses exactly right, without
- * worrying about rounding errors or playing with oracle.mus
- */
-
- char address[20];
-
-
- /* NOSID is returned by set_sid if the environment can't be set */
-
- #define NOSID ((char *) -1)
-
-
- /* set_sid(database)
- *
- * Sets the environment variable ORACLE_SID to the given string.
- * Returns the previous value.
- * If the parameter is NULL, restores the previous saved value, if any.
- */
-
- char *set_sid(database)
- char *database;
- {
- char *sid;
- static char *oldsid = NULL,
- *newsid = NULL;
-
- DEBUG(8, (fprintf(stderr, "set_sid(%s)\n",
- (database == NULL) ? "<NULL>" : database)));
-
- if (database != NULL)
- {
- /* normal case - save old value and set new */
-
- if ((sid = getenv("ORACLE_SID")) != NULL)
- {
- if (oldsid != NULL)
- {
- DEBUG(128, (fprintf(stderr,
- "set_sid: freeing oldsid (%#lx)\n",
- (long) oldsid)));
- free(oldsid);
- }
- if ((oldsid = malloc(strlen(sid) + 1)) == NULL)
- {
- DEBUG(128, (fputs("set_sid: out of memory\n",
- stderr)));
- DEBUG(8, (fputs("set_sid: returning NOSID\n",
- stderr)));
- ora_errno = ORAP_NOMEM;
- return(NOSID);
- }
- DEBUG(128, (fprintf(stderr,
- "set_sid: got oldsid at %#lx\n", (long) oldsid)));
- strcpy(oldsid, sid);
- }
-
- if (newsid != NULL)
- {
- DEBUG(128, (fprintf(stderr,
- "set_sid: freeing newsid (%#lx)\n",
- (long) newsid)));
- free(newsid);
- }
- if ((newsid = malloc(strlen(database) + 12)) == NULL)
- {
- DEBUG(128, (fputs("set_sid: out of memory\n", stderr)));
- DEBUG(8, (fputs("set_sid: returning NOSID\n", stderr)));
- ora_errno = ORAP_NOMEM;
- return(NOSID);
- }
- DEBUG(128, (fprintf(stderr,
- "set_sid: got newsid at %#lx\n", (long) newsid)));
- strcpy(newsid, "ORACLE_SID=");
- strcat(newsid, database);
-
- DEBUG(8, (fprintf(stderr, "set_sid: setting %s\n", newsid)));
- return (putenv(newsid)) ? oldsid : NULL;
- }
- else
- {
- if (oldsid == NULL)
- {
- DEBUG(8, (fputs("set_sid: oldsid not set\n", stderr)));
- return(NULL);
- }
-
- if (newsid != NULL)
- {
- DEBUG(128, (fprintf(stderr,
- "set_sid: freeing newsid (%#lx)\n", (long)newsid)));
- free(newsid);
- }
- if ((newsid = malloc(strlen(oldsid) + 12)) == NULL)
- {
- DEBUG(128, (fputs("set_sid: out of memory\n", stderr)));
- DEBUG(8, (fputs("set_sid: returning NOSID\n", stderr)));
- ora_errno = ORAP_NOMEM;
- return(NOSID);
- }
- DEBUG(128, (fprintf(stderr,
- "set_sid: got newsid at %#lx\n", (long) newsid)));
- strcpy(newsid, "ORACLE_SID=");
- strcat(newsid, oldsid);
-
- DEBUG(8, (fprintf(stderr, "set_sid: setting %s\n", newsid)));
- return (putenv(newsid)) ? oldsid : NULL;
- }
-
- /* NOTREACHED */
- }
-
-
- /* ora_login(database, name, password)
- *
- * logs into the current database under the given name and password.
- */
-
- char *ora_login(database, name, password)
- char *database, *name, *password;
- {
- int logged;
- char *tmp;
- struct cursor *lda;
-
- DEBUG(8, (fprintf(stderr,
- "ora_login(%s, %s, %s)\n", database, name, password)));
-
- if ((lda = ora_getlda()) == NULL)
- {
- DEBUG(8, (fputs("ora_login: couldn't get an lda\n", stderr)));
- return(NULL);
- }
-
- if (set_sid(database) == NOSID)
- {
- DEBUG(8, (fputs("ora_login: couldn't set database\n", stderr)));
- ora_dropcursor(lda);
- return(NULL);
- }
- else if (strcmp(database, getenv("ORACLE_SID")) != 0)
- {
- DEBUG(8, (fprintf(stderr,"ora_login: ORACLE_SID misset to %s\n",
- (tmp = getenv("ORACLE_SID")) ? tmp : NULL)));
- ora_dropcursor(lda);
- ora_errno = ORAP_NOSID;
- return(NULL);
- }
-
- logged = orlon(lda->csr, lda->hda, name, -1, password, -1, 0);
- set_sid(NULL); /* don't really care if this fails */
-
- if (logged == 0)
- {
- sprintf(address, "%#lx", (long) lda);
- DEBUG(8, (fprintf(stderr,
- "ora_login: returning lda %s\n", address)));
- ora_errno = 0;
- return(address);
- }
- else
- {
- ora_errno = lda->csr->csrrc;
- ora_droplda(lda);
- DEBUG(8, (fprintf(stderr,
- "ora_login: failed (error %d)\n", ora_errno)));
- return((char *) NULL);
- }
- }
-
-
- /* ora_open(lda, query)
- *
- * sets and executes the specified sql query
- */
-
- char *ora_open(lda_s, query)
- char *lda_s;
- char *query;
- {
- int i;
- struct cursor *csr;
- struct cursor *lda = (struct cursor *) strtol(lda_s, (char **) NULL, 0);
- short dbsize;
-
- DEBUG(8, (fprintf(stderr, "ora_open(%#lx, %s)\n", (long) lda, query)));
-
- if (check_lda(lda) == 0)
- {
- DEBUG(8, (fputs("ora_open: returning NULL\n", stderr)));
- ora_errno = ORAP_INVLDA;
- return((char *) NULL);
- }
-
- if ((csr = ora_getcursor()) == NULL)
- {
- /* ora_errno is set by ora_getcursor */
- DEBUG(8, (fprintf(stderr, "ora_open: can't get a cursor\n")));
- return((char *) NULL);
- }
-
- if ((oopen(csr->csr, lda->csr, (char *)-1, -1, -1, (char *)-1, -1) != 0)
- || (osql3(csr->csr, query, -1) != 0)
- || (oexec(csr->csr) != 0))
- {
- ora_errno = csr->csr->csrrc;
- ora_dropcursor(csr);
- DEBUG(8, (fprintf(stderr,
- "couldn't run SQL statement (error %d)\n", ora_errno)));
- return((char *) NULL);
- }
-
- /* set up csr->data to receive the information when we do a fetch */
-
- i = 0;
- do
- {
- odsc(csr->csr, ++i, (short *) 0, (short *) 0, (short *) 0,
- (short *) 0, (char *) 0, (short *) 0, (short *) 0);
- } while (csr->csr->csrrc == 0);
- --i;
-
- ora_errno = 0;
-
- if ((csr->data = (char **) malloc(i * sizeof(char *))) == NULL)
- {
- DEBUG(128, (fputs("ora_open: out of memory\n", stderr)));
- DEBUG(8, (fputs("ora_open: returning NOMEM\n", stderr)));
- ora_errno = ORAP_NOMEM;
- ora_dropcursor(csr);
- return(0);
- }
- DEBUG(128, (fprintf(stderr, "ora_open: got data at %#lx\n",csr->data)));
- csr->nfields = i;
-
- for (i = 0 ; i < csr->nfields ; i++)
- {
- odsc(csr->csr, i + 1, &dbsize, (short *) 0, (short *) 0,
- (short *) 0, (char *) 0, (short *) 0, (short *) 0);
-
- if ((csr->data[i] = (char *) malloc(dbsize + 1)) == NULL)
- {
- csr->nfields = i;
- ora_dropcursor(csr);
-
- DEBUG(128, (fputs("ora_open: out of memory\n",stderr)));
- DEBUG(8, (fputs("ora_open: returning NOMEM\n",stderr)));
- ora_errno = ORAP_NOMEM;
- return((char *) NULL);
- }
- DEBUG(128, (fprintf(stderr, "ora_open: got field %d at %#lx\n",
- i, csr->data[i])));
- odefin(csr->csr, i + 1, csr->data[i], dbsize + 1, 5, 0,
- (short *) 0, (char *) 0, 0, 0, (short *) 0, (char *) 0);
- }
-
- sprintf(address, "%#lx", (long) csr);
- DEBUG(8, (fprintf(stderr, "ora_open: returning csr %s\n", address)));
- return(address);
- }
-
-
- /* ora_fetch(csr)
- *
- * returns the next set of data from the cursor
- */
-
- int ora_fetch(csr_s)
- char *csr_s;
- {
- struct cursor *csr = (struct cursor *) strtol(csr_s, (char **) NULL, 0);
-
- DEBUG(8, (fprintf(stderr, "ora_fetch(%#lx)\n", (long) csr)));
-
- if (check_csr(csr) == 0)
- {
- DEBUG(8, (fputs("ora_fetch: returning NULL\n", stderr)));
- ora_errno = ORAP_INVCSR;
- return(NULL);
- }
-
- if ((csr->nfields == 0) || (ofetch(csr->csr) != 0))
- {
- DEBUG(8, (fputs("ora_fetch: ofetch failed, returing 0\n",
- stderr)));
- ora_result = NULL;
- ora_errno = csr->csr->csrrc;
- return(0);
- }
-
- ora_result = csr->data;
- ora_errno = 0;
- DEBUG(8, (fprintf(stderr,"ora_fetch: returning <%d>\n", csr->nfields)));
- return(csr->nfields);
- }
-
-
- char *OK = "OK"; /* valid return from ora_close, ora_logoff */
-
- /* ora_close(csr)
- *
- * Closes an oracle statement, releasing resources
- */
-
- char *ora_close(csr_s)
- char *csr_s;
- {
- struct cursor *csr = (struct cursor *) strtol(csr_s, (char **) NULL, 0);
-
- DEBUG(8, (fprintf(stderr, "ora_close(%#lx)\n", (long) csr)));
-
- if (check_csr(csr) == 0)
- {
- DEBUG(8, (fputs("ora_close: returning NULL\n", stderr)));
- ora_errno = ORAP_INVCSR;
- return(NULL);
- }
-
- oclose(csr->csr);
- ora_errno = csr->csr->csrrc;
- ora_dropcursor(csr);
-
- DEBUG(8, (fputs("ora_close: returning OK\n", stderr)));
- return(OK);
- }
-
-
- /* ora_logoff(lda)
- *
- * Logs the user off of Oracle, releasing all resources
- */
-
- char *ora_logoff(lda_s)
- char *lda_s;
- {
- struct cursor *lda = (struct cursor *) strtol(lda_s, (char **) NULL, 0);
-
- DEBUG(8, (fprintf(stderr, "ora_logoff(%#lx)\n", (long) lda)));
-
- if (check_lda(lda) == 0)
- {
- DEBUG(8, (fputs("ora_logoff: returning NULL\n", stderr)));
- ora_errno = ORAP_INVLDA;
- return(NULL);
- }
-
- ologof(lda->csr);
- ora_errno = lda->csr->csrrc;
- ora_droplda(lda);
-
- DEBUG(8, (fputs("ora_logoff: returning OK\n", stderr)));
- return(OK);
- }
-