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

📄 oracall32.cpp

📁 对Oracle OCI的封装
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/* Set a parameter to a Oracle Float value. 
   @param index - the first parameter is 1, the second is 2, ... 
   @param val - the parameter value.
   */
int CStatement::setFloat(const short index, const float* val)
{
	sword status;
	OCIBind*	bndp = (OCIBind*) 0;	/* the bind handle */	
	
	/* Bind the placeholders in the statement. */
	checkerr(con->errhp, status = OCIBindByPos(stmthp, &bndp, con->errhp, (ub4)index, 
			(dvoid*) val, sizeof(float), SQLT_FLT, (dvoid *) 0,
			(ub2*) 0, (ub2*) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));

	return (status);
}

/* Set a parameter to a Oracle Double value.
   @param index - the first parameter is 1, the second is 2, ... 
   @param val - the parameter value.
   */
int CStatement::setDouble(const short index, const double* val)
{
	sword status;
	OCIBind*	bndp = (OCIBind*) 0;	/* the bind handle */	
	
	/* Bind the placeholders in the statement. */
	checkerr(con->errhp, status = OCIBindByPos(stmthp, &bndp, con->errhp, (ub4)index, 
			(dvoid*) val, sizeof(double), SQLT_FLT, (dvoid *) 0,
			(ub2*) 0, (ub2*) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));

	return (status);
}

/* Set a parameter to a Oracle Date value.
   @param index - the first parameter is 1, the second is 2, ... 
   @param val - the parameter value.
   */
int CStatement::setDate(const short index, const COCIDate* val)
{
	sword status;
	OCIBind*	bndp = (OCIBind*) 0;	/* the bind handle */	

	/* Bind the placeholders in the statement. */
	checkerr(con->errhp, status = OCIBindByPos(stmthp, &bndp, con->errhp, (ub4)index, 
			(dvoid*) val->datetime, (sb4) sizeof(val->datetime), SQLT_DAT, (dvoid *) 0,
			(ub2*) 0, (ub2*) 0, (ub4) 0, (ub4 *) 0, OCI_DEFAULT));

	return (status);
}

/* Return the type of this statement. */
bool CStatement::isStmSelect()
{
	return (type == OCI_STMT_SELECT ? true : false);
}

/* CResultSet constructor. 
   @param statement - the CStatement handle.
   */
CResultSet::CResultSet(CStatement* statement)
{
	stm = statement;
	columns = 0;
	fieldInfos = NULL;
	fieldBuffers = NULL;
	fieldOffset = NULL;
	describeResult();
}

/* CResultSet destructor */
CResultSet::~CResultSet()
{
	close();
}

/* close the resultset. */
void CResultSet::close()
{
	if(stm == NULL)
		return;
	
	if(fieldOffset != NULL)
	{
		delete [] fieldOffset;
		fieldOffset = NULL;
	}
	
	if(fieldBuffers != NULL)
	{
		for(int i = 0; i < columns; i++)
			if(fieldBuffers[i].tDataBuffer == SQLT_CLOB) // free lob locator
				(void) OCIDescriptorFree((dvoid *) fieldBuffers[i].pDataBuffer, (ub4) OCI_DTYPE_LOB); 
			else
				delete fieldBuffers[i].pDataBuffer;

		delete [] fieldBuffers;
		fieldBuffers = NULL;
	}

	if(fieldInfos != NULL)
	{
		for(int i = 0; i < columns; i++)
			delete fieldInfos[i].name;
		
		delete [] fieldInfos;
		fieldInfos = NULL;
	}

	columns = 0;
}

/* retrieve information about multiple select-list items. */
void CResultSet::describeResult()
{
	if(stm == NULL)
		return;
	
	if(fieldInfos != NULL || fieldBuffers != NULL)
		close();

	OCIParam* pard;		/* the descriptor of the parameter */
	OCIDefine *defnp = (OCIDefine *) 0;
	sword status = 0;
	int counter = 0;
	
	/* get the number of columns in the table */
	while(status == OCI_SUCCESS) 
	{
		counter++;
		status = OCIParamGet(stm->stmthp, OCI_HTYPE_STMT, stm->con->errhp, (dvoid**)&pard,
								(ub4) counter);
	}	
	
	if((--counter) == 0)
		return;
	
	// Allocate buffer and get the OCI meta data
	fieldInfos = new CFieldInfo[counter];
	fieldBuffers = new CFieldBuffer[counter];
	fieldOffset = new unsigned int[counter];	

	/* Loop only if a descriptor was successfully retrieved for
	current position, starting at 1 */
	for(int i = 0; i < counter; i++) 
	{
		fieldInfos[i].name = NULL;
		fieldInfos[i].dtype = 0;
		fieldInfos[i].dsize = 0;
		fieldInfos[i].precision = fieldInfos[i].scale = 0;
		fieldInfos[i].nullability = 0;

		/* increment counter and get next descriptor, if there is one */
		(void) OCIParamGet(stm->stmthp, OCI_HTYPE_STMT, stm->con->errhp, (dvoid**)&pard,
								(ub4) (1 + i));
		
		/* Retrieve the data type attribute */
		checkerr(stm->con->errhp, status = OCIAttrGet((dvoid*) pard, (ub4) OCI_DTYPE_PARAM, 
					(dvoid*) &fieldInfos[i].dtype, (ub4 *) 0, (ub4) OCI_ATTR_DATA_TYPE, stm->con->errhp));
		
		/* Retrieve the column name attribute */
		text* tmpbuff;
		ub4 str_len;
		checkerr(stm->con->errhp, status = OCIAttrGet((dvoid*) pard, (ub4) OCI_DTYPE_PARAM, 
					(dvoid**) &tmpbuff, (ub4*) &str_len, (ub4) OCI_ATTR_NAME, 
					stm->con->errhp));
		
		fieldInfos[i].name = new char[str_len + 1];
		strncpy(fieldInfos[i].name, (char*)tmpbuff, str_len);
		fieldInfos[i].name[str_len] = '\0';

		/* Retrieve the data size attribute */
		checkerr(stm->con->errhp, status = OCIAttrGet((dvoid*) pard, (ub4) OCI_DTYPE_PARAM, 
					(dvoid*) &fieldInfos[i].dsize, (ub4 *) 0, (ub4) OCI_ATTR_DATA_SIZE, stm->con->errhp));
	
		/* Retrieve the data precision attribute */
		checkerr(stm->con->errhp, status = OCIAttrGet((dvoid*) pard, (ub4) OCI_DTYPE_PARAM, 
					(dvoid*) &fieldInfos[i].precision, (ub4 *) 0, (ub4) OCI_ATTR_PRECISION, stm->con->errhp));
		
		/* Retrieve the data scale attribute */
		checkerr(stm->con->errhp, status = OCIAttrGet((dvoid*) pard, (ub4) OCI_DTYPE_PARAM, 
					(dvoid*) &fieldInfos[i].scale, (ub4 *) 0, (ub4) OCI_ATTR_SCALE, stm->con->errhp));
		
		/* Retrieve the data nullability attribute */
		checkerr(stm->con->errhp, status = OCIAttrGet((dvoid*) pard, (ub4) OCI_DTYPE_PARAM, 
					(dvoid*) &fieldInfos[i].nullability, (ub4 *) 0, (ub4) OCI_ATTR_IS_NULL, stm->con->errhp));
		
		// allocate an output buffer
		switch(fieldInfos[i].dtype)
		{
		case 1:		// VARCHAR2, NVARCHAR2 
		case 96:	// CHAR, NCHAR 
			fieldBuffers[i].pDataBuffer = new byte[1 + fieldInfos[i].dsize];
			fieldBuffers[i].lDataBuffer = 1 + fieldInfos[i].dsize;
			fieldBuffers[i].tDataBuffer = SQLT_STR;
			break;
		case 2: // NUMBER
			fieldBuffers[i].pDataBuffer = new byte[fieldInfos[i].dsize];
			fieldBuffers[i].lDataBuffer = fieldInfos[i].dsize;
			fieldBuffers[i].tDataBuffer = SQLT_VNU;
			break;
		case 12: // DATE
			fieldBuffers[i].pDataBuffer = new byte[fieldInfos[i].dsize];
			fieldBuffers[i].lDataBuffer = fieldInfos[i].dsize;
			fieldBuffers[i].tDataBuffer = SQLT_DAT;
			break;
		case 112: // CLOB
			if(status = OCIDescriptorAlloc((dvoid *) stm->con->envhp, 
				(dvoid **) &fieldBuffers[i].pDataBuffer, 
				(ub4) OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0))
			{
				checkerr(stm->con->errhp, status);
				fieldBuffers[i].pDataBuffer = NULL;
				fieldBuffers[i].lDataBuffer = 0;
				fieldBuffers[i].tDataBuffer = 0;
			}
			else
			{
				fieldBuffers[i].lDataBuffer = -1;
				fieldBuffers[i].tDataBuffer = SQLT_CLOB;
			}
			break;
		default:
			fieldBuffers[i].pDataBuffer = NULL;
			fieldBuffers[i].lDataBuffer = 0;
			fieldBuffers[i].tDataBuffer = 0;
		}

		// define the output variable
		if(fieldBuffers[i].pDataBuffer != NULL)
		{
			if(fieldBuffers[i].tDataBuffer == SQLT_CLOB)
				checkerr(stm->con->errhp, status = OCIDefineByPos(stm->stmthp, &defnp, stm->con->errhp,
						(ub4)(1 + i), (dvoid*) &fieldBuffers[i].pDataBuffer, (sb4) fieldBuffers[i].lDataBuffer,
						(ub2) fieldBuffers[i].tDataBuffer, (dvoid*) 0, (ub2*) 0, (ub2*)0, OCI_DEFAULT));
			else 
				checkerr(stm->con->errhp, status = OCIDefineByPos(stm->stmthp, &defnp, stm->con->errhp,
						(ub4)(1 + i), (dvoid*) fieldBuffers[i].pDataBuffer, (sb4) fieldBuffers[i].lDataBuffer,
						(ub2) fieldBuffers[i].tDataBuffer, (dvoid*) 0, (ub2*) 0, (ub2*)0, OCI_DEFAULT));
		}
	}

	columns = counter;
}

/* Returns specific kinds of information about the fields in a resultset.
   @param index - The zero-based index of the field.
   @param fieldinfo - A reference to a CFieldInfo structure.
   */
void CResultSet::getFieldInfo(const short index, CFieldInfo& fieldinfo)
{
	if(stm == NULL || fieldInfos == NULL || (1 + index) > columns)
		return;
	
	fieldinfo = fieldInfos[index];
}

/* Returns the number of fields in the recordset. */
short CResultSet::getFieldInfoCount()
{
	return (columns);
}

/* Gets the length of a LOB. 
   @param index - The zero-based index of the field.
   */
unsigned int CResultSet::getLobLenth(const short index)
{
	unsigned int ret = 0;
	sword status;

	if(stm == NULL || fieldBuffers == NULL || (1 + index) > columns || 
		fieldBuffers[index].pDataBuffer == NULL || fieldBuffers[index].tDataBuffer != SQLT_CLOB)
		return ret;
	
	/* Length of the Lob */
	checkerr(stm->con->errhp, status = OCILobGetLength(stm->con->svchp, stm->con->errhp, 
			(OCILobLocator*)fieldBuffers[index].pDataBuffer, &ret));
		
	return ret;
}

/* A CResultSet is initially positioned before its first row; 
   the first call to next makes the first row the current row; 
   the second call makes the second row the current row, etc. 
   */
int CResultSet::next()
{
	if(stm == NULL)
		return -1;
	
	sword status;
	
	// initialize the result buffers
	for(int i = 0; i < columns; i++)
	{
		if(fieldBuffers[i].tDataBuffer == SQLT_STR)
			strcpy((char*)fieldBuffers[i].pDataBuffer, (fieldInfos[i].nullability) ? "NULL" : "");
		else if(fieldBuffers[i].tDataBuffer == SQLT_VNU)
			OCINumberSetZero(stm->con->errhp, (OCINumber*)fieldBuffers[i].pDataBuffer);

		fieldOffset[i] = 1;
	}

	if((status = OCIStmtFetch(stm->stmthp, stm->con->errhp, (ub4)1, 
					OCI_FETCH_NEXT, OCI_DEFAULT)) && (status != OCI_NO_DATA))
		checkerr(stm->con->errhp, status);

	return (status);
}

/* Get the value of a column in the current row as a String.
   @param index - The zero-based index of the field.
   @param dest - Buffer into which the converted string is placed. 
   @param len - Size of the buffer.
   */
char* CResultSet::getString(const short index, char* dest, const short len)
{
	sword status;

	memset((dvoid *)dest, '\0', len);
	
	if(stm == NULL || fieldInfos == NULL || 
			(1 + index) > columns || fieldBuffers[index].pDataBuffer == NULL)
		return dest;

	if(fieldBuffers[index].tDataBuffer == SQLT_STR)
	{
		strncpy(dest, (char*) fieldBuffers[index].pDataBuffer, len - 1);
	}
	else if(fieldBuffers[index].tDataBuffer == SQLT_VNU)
	{
		text temp[22]; 
		char* fmt = "FM9999999999999999999";
		ub4 temp_size = sizeof(temp);
		
//		if(fieldInfos[index].precision > fieldInfos[index].scale &&
//			fieldInfos[index].scale > 0 && (strlen(fmt) - 2) > fieldInfos[index].scale)
//			strnset(fmt + (strlen(fmt) - (1 + fieldInfos[index].scale)), 'D', 1);

		if(status = OCINumberToText(stm->con->errhp, (OCINumber*)fieldBuffers[index].pDataBuffer, 
				(text*) fmt, (ub4) strlen(fmt), 
				(text*) 0, (ub4) 0,
				(ub4*) &temp_size, (text*) temp))
		{
			checkerr(stm->con->errhp, status);
		}
		else
			strncpy(dest, (char*) temp, len - 1);
	}
	else if(fieldBuffers[index].tDataBuffer == SQLT_DAT)
	{
		byte* pb = (byte*) fieldBuffers[index].pDataBuffer;
		tm time; 
		(time.tm_sec = pb[6])--;	// second
		(time.tm_min = pb[5])--;	// minute
		(time.tm_hour = pb[4])--;	// hour
		time.tm_mday = pb[3];		// day
		(time.tm_mon = pb[2])--;	// month
		time.tm_year = pb[1];		// year
		
		strftime(dest, len, "%c", &time);
	}
	else if(fieldBuffers[index].tDataBuffer == SQLT_CLOB)
	{
		strncpy(dest, "{CLOB}", len - 1);
	}

	return dest;
}

/* Get the value of a column in the current row as an int.
   @param index - The zero-based index of the field.
   */
int CResultSet::getInt(const short index)
{
	sword ret = 0;
	sword status;

	if(stm == NULL || fieldInfos == NULL || 
			(1 + index) > columns || fieldBuffers[index].pDataBuffer == NULL)
		return ret;

	if(fieldBuffers[index].tDataBuffer == SQLT_VNU)
	{
		if(status = OCINumberToInt(stm->con->errhp, (OCINumber*)fieldBuffers[index].pDataBuffer, 
				sizeof(ret), OCI_NUMBER_SIGNED, (dvoid *)&ret))
		{
			checkerr(stm->con->errhp, status);
		}
	}

	return ret;
}

/* Get the value of a column in the current row as a float.
   @param index - The zero-based index of the field.   
   */
float CResultSet::getFloat(const short index)
{
	float ret = 0;
	sword status;

	if(stm == NULL || fieldInfos == NULL || 
			(1 + index) > columns || fieldBuffers[index].pDataBuffer == NULL)
		return ret;

	if(fieldBuffers[index].tDataBuffer == SQLT_VNU)
	{
		if(status = OCINumberToReal(stm->con->errhp, (OCINumber*)fieldBuffers[index].pDataBuffer, 
				sizeof(ret), (dvoid*)&ret))
		{
			checkerr(stm->con->errhp, status);
		}
	}

	return ret;
}

/* Get the value of a column in the current row as a double.
   @param index - The zero-based index of the field.
   */
double CResultSet::getDouble(const short index)
{
	double ret = 0;
	sword status;

	if(stm == NULL || fieldInfos == NULL || 
			(1 + index) > columns || fieldBuffers[index].pDataBuffer == NULL)
		return ret;

	if(fieldBuffers[index].tDataBuffer == SQLT_VNU)
	{
		if(status = OCINumberToReal(stm->con->errhp, (OCINumber*)fieldBuffers[index].pDataBuffer, 
				sizeof(ret), (dvoid*)&ret))
		{
			checkerr(stm->con->errhp, status);
		}
	}

	return ret;
}

/* Get the value of a column in the current row as a long double.
   @param index - The zero-based index of the field.
   */
long double CResultSet::getLongDouble(const short index)
{
	long double ret = 0;
	sword status;

	if(stm == NULL || fieldInfos == NULL || 
			(1 + index) > columns || fieldBuffers[index].pDataBuffer == NULL)
		return ret;

	if(fieldBuffers[index].tDataBuffer == SQLT_VNU)
	{
		if(status = OCINumberToReal(stm->con->errhp, (OCINumber*)fieldBuffers[index].pDataBuffer, 
				sizeof(ret), (dvoid*)&ret))
		{
			checkerr(stm->con->errhp, status);
		}
	}

	return ret;
}

/* Get the value of a column in the current row as a tm structure.
   @param index - The zero-based index of the field.
   @param time - Pointer to a tm structure.
   */
int CResultSet::getDate(const short index, tm* time)
{
	sword status = -1;
	
	time->tm_sec = 0;	// Seconds after minute (0 

⌨️ 快捷键说明

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