⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oracle.cxx

📁 oracle oci 轻量级封装库,很方便和实用!适合于简单的数据库操作.绝对精品.垃圾就不上传了.
💻 CXX
字号:

/*
   NAME
     oracle.cxx - Oracle Call Interface Program
				  can use the OCI interface to access ORACLE database.				
   MODIFIED   (MM/DD/YY)
    lhj   07/20/2000   
*/
#include "dbclient.h"
#include "oracle.h"


static sword numwidth = 8;


void err_report(FILE *file, text *errmsg, sword func_code);

/* connection destructor */
connection::~connection() 
{
  // disconnect if connection exists
  if (state == connected)
  {
    //if (disconnect())display_error(stderr);
  }
}

/* connect to ORACLE */
sword connection::connect(text *user, text *pass,text *service)
{
  sword status;

  if (state == connected)
  {
    // this object is already connected
    return (CONERR_ALRCON);
  }

  if ((status = olog(&lda, hda, (text *) user, -1,
                     (text *)pass, -1, (text *) service, -1,
                     OCI_LM_DEF)) == 0)
  {
    // successful login
    state = connected;
    printf("Connected to ORACLE as %s\n", user);
  }

  return (status);
}

/* disconnect from ORACLE */
sword connection::disconnect()
{
  sword status;

  if (state == not_connected)
  {
    // this object has not been connected
    return (CONERR_NOTCON);
  }

  if ((status = ologof(&lda)) == 0)
  {
    // successful logout
    state = not_connected;
  }

  return (status);
}

/* write error message to the given file */
void connection::display_error(FILE *file) const
{
  if (lda.rc != 0)
  {
    sword n;
    text msg[512];

    n = oerhms((cda_def *)&lda, lda.rc, msg, (sword) sizeof(msg));
    err_report(file, msg, lda.fc);
  }
}

/* cursor destructor */
cursor::~cursor()
{
  if (state == opened)
  {
//    if (close()) display_error(stderr);
  }
}

/* open the cursor */
sword cursor::open(connection *conn_param)
{
  sword status;

  if (state == opened)
  {
    // this cursor has already been opened
    return (CURERR_ALROPN);
  }

  if ((status = oopen(&cda, &conn_param->lda, (text *)0, -1, -1, 
		      (text *)0, -1)) == 0)
  {
    // successfull open
    state = opened;
    conn = conn_param;
  }

  return (status);
}

/* close the cursor */
sword cursor::close()
{
  sword status;

  if (state == not_opened)
  {
    // this cursor has not been opened
    return (CURERR_NOTOPN);
  }

  if ((status = oclose(&cda)) == 0)
  {
    // successful cursor close
    state = not_opened;
    conn = (connection *)0;
  }

  return (status);
}

/* write error message to the given file */
void cursor::display_error(FILE *file) const
{
  if (cda.rc != 0)
  {
    sword n;
    text msg[512];

    n = oerhms(&conn->lda, cda.rc, msg, (sword) sizeof(msg));
    err_report(file, msg, cda.fc);
  }
}
sword cursor::describe()
{
    sword col, deflen, deftyp;
    static ub1 *defptr;

    /* Describe the select-list items. */
    for (col = 0; col < MAX_SELECT_LIST_SIZE; col++)
    {
        desc[col].namelen = MAX_ITEM_BUFFER_SIZE;
        if (odescr(&cda, col + 1, &desc[col].maxlength,
                   &desc[col].datatype, &desc[col].name[0],
                   &desc[col].namelen, &desc[col].dsize,
                   &desc[col].precision, &desc[col].scale,
                   &desc[col].status))
        {
            /* Break on end of select list. */
            if (cda.rc == VAR_NOT_IN_LIST)
                break;
            else
            {
                //oci_error(cda);
                return -1;
            }
        }
		desc[col].name[desc[col].namelen]=0;
        /* adjust sizes and types for display */
        switch (desc[col].datatype)
        {
		case NUMBER_TYPE:
            desc[col].maxlength = numwidth;
        default:
            if (desc[col].datatype == DATE_TYPE)
                desc[col].maxlength = 12;
            if (desc[col].datatype == ROWID_TYPE)
                desc[col].maxlength = 18;

			def[col].value=(ub1*)malloc(desc[col].maxlength*sizeof(ub1));
            defptr = def[col].value;
            deflen = desc[col].maxlength > MAX_ITEM_BUFFER_SIZE ?
              MAX_ITEM_BUFFER_SIZE : desc[col].maxlength + 1;
            deftyp = STRING_TYPE;
            break;
        }
        if (odefin(&cda, col + 1,
                   defptr, deflen, deftyp,
                   -1, &def[col].valuelen, (text *) 0, -1, -1,
                   &def[col].col_retlen,
                   &def[col].col_retcode))
        {
            display_error(stderr);
            return -1;
        }
    }
    return col;
}

void err_report(FILE *file, text *errmsg, sword func_code)
{
    fprintf(file, "\n-- ORACLE error--\n\n%s\n", errmsg);
    if (func_code > 0)
        fprintf(file, "Processing OCI function %s\n",
            oci_func_tab[func_code]);
}

void cursor::print_header(sword ncols)
{
    sword col, n;

    fputc('\n', stdout);

    for (col = 0; col < ncols; col++)
    {
        n = desc[col].maxlength - desc[col].namelen;
        if (desc[col].datatype == FLOAT_TYPE ||
            desc[col].datatype == INT_TYPE)
        {
            printf("%*c", n, ' ');
            printf("%*.*s", desc[col].namelen,
                   desc[col].namelen, desc[col].name);
        }
        else
        {
            printf("%*.*s", desc[col].namelen,
                   desc[col].namelen, desc[col].name);
            printf("%*c", n, ' ');
        }
        fputc(' ', stdout);
    }            
    fputc('\n', stdout);

    for (col = 0; col < ncols; col++)
    {
        for (n = desc[col].maxlength; --n >= 0; )
            fputc('-', stdout);
        fputc(' ', stdout);
    }
    fputc('\n', stdout);
}
void cursor::print_rows(sword ncols)
{
    sword col, n;

    for (;;)
    {
        fputc('\n', stdout);
        /* Fetch a row.  Break on end of fetch,
           disregard null fetch "error". */
        if (ofetch(&cda))
        {
            if (cda.rc == NO_DATA_FOUND)
                break;
            if (cda.rc != NULL_VALUE_RETURNED)
                display_error(stderr);
        }
        for (col = 0; col < ncols ; col++)
        {
            /* Check col. return code for null.  If
               null, print n spaces, else print value. */
            if (def[col].valuelen < 0)
                printf("%*c", desc[col].maxlength, ' ');
            else
            {
                switch (desc[col].datatype)
                {
                case FLOAT_TYPE:
                //    printf("%*.*f", numwidth, 2, def[col].flt_buf);
                //    break;
                case INT_TYPE:
                //    printf("%*d", numwidth, def[col].int_buf);
                //    break;
                default:
                    printf("%s", def[col].value);
                    n = desc[col].maxlength - strlen((char *) def[col].value);
                    if (n > 0)
                        printf("%*c", n, ' ');
                    break;
                }
            }
            fputc(' ', stdout);
        }
    }  /* end for (;;) */
}
sword execute_cmd(connection conn,cursor crsr,char* sql)
{
	sword cols;
    if (crsr.parse((text*)sql))
    {
      crsr.display_error(stderr);
      return(EXIT_FAILURE);
    }

    crsr.sql_function = crsr.getfunction();

    if(crsr.sql_function == FT_SELECT)
		if ((cols=crsr.describe()) == -1)
		{
			crsr.display_error(stderr);
			return(EXIT_FAILURE);
		}


	if (crsr.execute())
	{
		if (crsr.get_error_code() != NO_DATA_FOUND)
		{
			crsr.display_error(stderr);
			return(EXIT_FAILURE);
		}  
		else
		printf("\n 0 row.\n");
    }
	else
	{
		if (crsr.sql_function == FT_SELECT)
		{
			crsr.print_header(cols);
			crsr.print_rows(cols);
		}
        if (crsr.sql_function == FT_SELECT ||
            crsr.sql_function == FT_UPDATE ||
            crsr.sql_function == FT_DELETE ||
            crsr.sql_function == FT_INSERT)
            printf("\n%d row%c processed.\n", crsr.getrows(), 
                   crsr.getrows() == 1 ? ' ' : 's');
        else
            printf("\nStatement processed.\n");
	}
	return crsr.getrows();
}
sword execute_cmd(connection conn,cursor crsr,char* sql,RecordSet* pSet)
{
    sword cols;
	if (crsr.parse((text*)sql))
    {
      crsr.display_error(stderr);
      return(EXIT_FAILURE);
    }

    crsr.sql_function = crsr.getfunction();

    if(crsr.sql_function == FT_SELECT)
		if ((cols=crsr.describe()) == -1)
		{
			crsr.display_error(stderr);
			return(EXIT_FAILURE);
		}


	if (crsr.execute())
	{
		if (crsr.get_error_code() != NO_DATA_FOUND)
		{
			crsr.display_error(stderr);
			return(EXIT_FAILURE);
		}  
		else
		printf("\n 0 row.\n");
    }
	else
	{
		if (crsr.sql_function == FT_SELECT)
		{
			pSet->SetFieldsCount(cols);
			crsr.fetch_data(pSet);
		}
        if (crsr.sql_function == FT_SELECT ||
            crsr.sql_function == FT_UPDATE ||
            crsr.sql_function == FT_DELETE ||
            crsr.sql_function == FT_INSERT)
            printf("\n%d row%c processed.\n", crsr.getrows(), 
                   crsr.getrows() == 1 ? ' ' : 's');
        else
            printf("\nStatement processed.\n");
	}

	return crsr.getrows();
}
sword cursor::fetch_data(RecordSet* pSet)
{
	sword col, n;
	sword ncols=pSet->GetFieldsCount();
	
	pSet->datafmt= (CS_DATAFMT *)malloc( ncols* sizeof (CS_DATAFMT));
	
	for(n=0;n<ncols;n++)
		pSet->datafmt[n]=desc[n];

    for (;;)
    {
       	COLUMN_DATA* columndata=(COLUMN_DATA *)malloc(ncols * sizeof (COLUMN_DATA));
		/* Fetch a row.  Break on end of fetch,
           disregard null fetch "error". */
        if (ofetch(&cda))
        {
            free(columndata);
			if (cda.rc == NO_DATA_FOUND)
                break;
            if (cda.rc != NULL_VALUE_RETURNED)
                display_error(stderr);
        }
        for (col = 0; col < ncols ; col++)
        {
       		columndata[col].value = (text *)malloc(pSet->datafmt[col].maxlength);
            strcpy((char*)(columndata[col].value),(char*)(def[col].value));  
			columndata[col].valuelen=(sb2)(pSet->datafmt[col].maxlength);
        }
		pSet->AddRecord(columndata);
    }  /* end for (;;) */
	pSet->SetRecordsCount(getrows());
	return getrows();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -