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

📄 sybaseconnection.c

📁 适合于Unix/Linux下的一个持久数据库连接池
💻 C
📖 第 1 页 / 共 3 页
字号:
	prepared=true;	return true;}void sybasecursor::checkRePrepare() {	// Sybase doesn't allow you to rebind and re-execute when using 	// ct_command.  You have to re-prepare too.  I'll make this transparent	// to the user.	if (!prepared) {		prepareQuery(query,length);	}}bool sybasecursor::inputBindString(const char *variable,						uint16_t variablesize,						const char *value,						uint16_t valuesize,						int16_t *isnull) {	checkRePrepare();	(CS_VOID)rawbuffer::zero(&parameter[paramindex],				sizeof(parameter[paramindex]));	if (charstring::isInteger(variable+1,variablesize-1)) {		parameter[paramindex].name[0]=(char)NULL;		parameter[paramindex].namelen=0;	} else {		charstring::copy(parameter[paramindex].name,variable);		parameter[paramindex].namelen=variablesize;	}	parameter[paramindex].datatype=CS_CHAR_TYPE;	parameter[paramindex].maxlength=CS_UNUSED;	parameter[paramindex].status=CS_INPUTVALUE;	parameter[paramindex].locale=NULL;	if (ct_param(cmd,&parameter[paramindex],		(CS_VOID *)value,valuesize,0)!=CS_SUCCEED) {		return false;	}	paramindex++;	return true;}bool sybasecursor::inputBindInteger(const char *variable,						uint16_t variablesize,						int64_t *value) {	checkRePrepare();	(CS_VOID)rawbuffer::zero(&parameter[paramindex],				sizeof(parameter[paramindex]));	if (charstring::isInteger(variable+1,variablesize-1)) {		parameter[paramindex].name[0]=(char)NULL;		parameter[paramindex].namelen=0;	} else {		charstring::copy(parameter[paramindex].name,variable);		parameter[paramindex].namelen=variablesize;	}	parameter[paramindex].datatype=CS_INT_TYPE;	parameter[paramindex].maxlength=CS_UNUSED;	parameter[paramindex].status=CS_INPUTVALUE;	parameter[paramindex].locale=NULL;	if (ct_param(cmd,&parameter[paramindex],		(CS_VOID *)value,sizeof(int64_t),0)!=CS_SUCCEED) {		return false;	}	paramindex++;	return true;}bool sybasecursor::inputBindDouble(const char *variable,						uint16_t variablesize,						double *value,						uint32_t precision,						uint32_t scale) {	checkRePrepare();	(CS_VOID)rawbuffer::zero(&parameter[paramindex],				sizeof(parameter[paramindex]));	if (charstring::isInteger(variable+1,variablesize-1)) {		parameter[paramindex].name[0]=(char)NULL;		parameter[paramindex].namelen=0;	} else {		charstring::copy(parameter[paramindex].name,variable);		parameter[paramindex].namelen=variablesize;	}	parameter[paramindex].datatype=CS_FLOAT_TYPE;	parameter[paramindex].maxlength=CS_UNUSED;	parameter[paramindex].status=CS_INPUTVALUE;	parameter[paramindex].precision=precision;	parameter[paramindex].scale=scale;	parameter[paramindex].locale=NULL;	if (ct_param(cmd,&parameter[paramindex],		(CS_VOID *)value,sizeof(double),0)!=CS_SUCCEED) {		return false;	}	paramindex++;	return true;}bool sybasecursor::outputBindString(const char *variable, 					uint16_t variablesize,					char *value, 					uint16_t valuesize, 					int16_t *isnull) {	checkRePrepare();	outbindtype[outbindindex]=CS_CHAR_TYPE;	outbindstrings[outbindindex]=value;	outbindstringlengths[outbindindex]=valuesize;	outbindindex++;	(CS_VOID)rawbuffer::zero(&parameter[paramindex],				sizeof(parameter[paramindex]));	if (charstring::isInteger(variable+1,variablesize-1)) {		parameter[paramindex].name[0]=(char)NULL;		parameter[paramindex].namelen=0;	} else {		charstring::copy(parameter[paramindex].name,variable);		parameter[paramindex].namelen=variablesize;	}	parameter[paramindex].datatype=CS_CHAR_TYPE;	parameter[paramindex].maxlength=valuesize;	parameter[paramindex].status=CS_RETURN;	parameter[paramindex].locale=NULL;	if (ct_param(cmd,&parameter[paramindex],			(CS_VOID *)NULL,0,			(CS_SMALLINT)*isnull)!=CS_SUCCEED) {		return false;	}	paramindex++;	return true;}bool sybasecursor::outputBindInteger(const char *variable,						uint16_t variablesize,						int64_t *value,						int16_t *isnull) {	checkRePrepare();	outbindtype[outbindindex]=CS_INT_TYPE;	outbindints[outbindindex]=value;	outbindindex++;	(CS_VOID)rawbuffer::zero(&parameter[paramindex],				sizeof(parameter[paramindex]));	if (charstring::isInteger(variable+1,variablesize-1)) {		parameter[paramindex].name[0]=(char)NULL;		parameter[paramindex].namelen=0;	} else {		charstring::copy(parameter[paramindex].name,variable);		parameter[paramindex].namelen=variablesize;	}	parameter[paramindex].datatype=CS_INT_TYPE;	parameter[paramindex].maxlength=CS_UNUSED;	parameter[paramindex].status=CS_RETURN;	parameter[paramindex].locale=NULL;	if (ct_param(cmd,&parameter[paramindex],			(CS_VOID *)NULL,0,			(CS_SMALLINT)*isnull)!=CS_SUCCEED) {		return false;	}	paramindex++;	return true;}bool sybasecursor::outputBindDouble(const char *variable,						uint16_t variablesize,						double *value,						uint32_t *precision,						uint32_t *scale,						int16_t *isnull) {	checkRePrepare();	outbindtype[outbindindex]=CS_FLOAT_TYPE;	outbinddoubles[outbindindex]=value;	outbindindex++;	(CS_VOID)rawbuffer::zero(&parameter[paramindex],				sizeof(parameter[paramindex]));	if (charstring::isInteger(variable+1,variablesize-1)) {		parameter[paramindex].name[0]=(char)NULL;		parameter[paramindex].namelen=0;	} else {		charstring::copy(parameter[paramindex].name,variable);		parameter[paramindex].namelen=variablesize;	}	parameter[paramindex].datatype=CS_FLOAT_TYPE;	parameter[paramindex].maxlength=CS_UNUSED;	parameter[paramindex].status=CS_RETURN;	parameter[paramindex].locale=NULL;	if (ct_param(cmd,&parameter[paramindex],			(CS_VOID *)NULL,0,			(CS_SMALLINT)*isnull)!=CS_SUCCEED) {		return false;	}	paramindex++;	return true;}bool sybasecursor::executeQuery(const char *query, uint32_t length,							bool execute) {	// clear out any errors	if (sybaseconn->errorstring) {		sybaseconn->deadconnection=false;		delete sybaseconn->errorstring;		sybaseconn->errorstring=NULL;	}	// initialize return values	ncols=0;	affectedrows=0;	row=0;	maxrow=0;	totalrows=0;	if (cmd==cursorcmd) {		if (ct_cursor(cursorcmd,CS_CURSOR_ROWS,					NULL,CS_UNUSED,					NULL,CS_UNUSED,					(CS_INT)FETCH_AT_ONCE)!=CS_SUCCEED) {			return false;		}		if (ct_cursor(cursorcmd,CS_CURSOR_OPEN,					NULL,CS_UNUSED,					NULL,CS_UNUSED,					CS_UNUSED)!=CS_SUCCEED) {			return false;		}	}	if (ct_send(cmd)!=CS_SUCCEED) {		cleanUpData(true,true);		return false;	}	for (;;) {		results=ct_results(cmd,&resultstype);		if (results==CS_FAIL || resultstype==CS_CMD_FAIL) {			cleanUpData(true,true);			return false;		}		if (cmd==languagecmd) {			if (isrpcquery) {				// For rpc commands, there could be several				// result sets - CS_STATUS_RESULT,				// maybe a CS_PARAM_RESULT and maybe a				// CS_ROW_RESULT, we're not guaranteed				// what order they'll come in though, what				// a pickle...				// For now, we care about the CS_PARAM_RESULT,				// or the CS_ROW_RESULT, whichever we get first,				// presumably there will only be 1 row in the				// CS_PARAM_RESULT...				if (resultstype==CS_PARAM_RESULT ||						resultstype==CS_ROW_RESULT) {					break;				}			} else {				// For non-rpc language commands (non-selects),				// there should be only one result set.				break;			}		} else if (resultstype==CS_ROW_RESULT ||					resultstype==CS_CURSOR_RESULT ||					resultstype==CS_COMPUTE_RESULT) {			// For cursor commands (selects), each call to			// ct_cursor will have generated a result set.  There			// will be result sets for the CS_CURSOR_DECLARE,			// CS_CURSOR_ROWS and CS_CURSOR_OPEN calls.  We need to			// skip past the first 2, unless they failed.  If they			// failed, it will be caught above.			break;		}		// if we got here, then we don't want to process this result		// set, cancel it and move on to the next one...		if (ct_cancel(NULL,cmd,CS_CANCEL_CURRENT)==CS_FAIL) {			sybaseconn->deadconnection=true;			// FIXME: call ct_close(CS_FORCE_CLOSE)			return false;		}	}	checkForTempTable(query,length);	// reset the prepared flag	prepared=false;	// For queries which return rows or parameters (output bind variables),	// get the column count and bind columns.  For DML queries, get the	// affected row count.	affectedrows=0;	if (resultstype==CS_ROW_RESULT ||			resultstype==CS_CURSOR_RESULT ||			resultstype==CS_COMPUTE_RESULT ||			resultstype==CS_PARAM_RESULT) {		if (ct_res_info(cmd,CS_NUMDATA,(CS_VOID *)&ncols,				CS_UNUSED,(CS_INT *)NULL)!=CS_SUCCEED) {			return false;		}		if (ncols>MAX_SELECT_LIST_SIZE) {			ncols=MAX_SELECT_LIST_SIZE;		}		// bind columns		for (CS_INT i=0; i<ncols; i++) {			// get the field as a null terminated character string			// no longer than MAX_ITEM_BUFFER_SIZE, override some			// other values that might have been set also			column[i].datatype=CS_CHAR_TYPE;			column[i].format=CS_FMT_NULLTERM;			column[i].maxlength=MAX_ITEM_BUFFER_SIZE;			column[i].scale=CS_UNUSED;			column[i].precision=CS_UNUSED;			column[i].status=CS_UNUSED;			column[i].count=FETCH_AT_ONCE;			column[i].usertype=CS_UNUSED;			column[i].locale=NULL;				// bind the columns for the fetches			if (ct_bind(cmd,i+1,&column[i],(CS_VOID *)data[i],				datalength[i],nullindicator[i])!=CS_SUCCEED) {				break;			}		}	} else if (resultstype==CS_CMD_SUCCEED) {		if (ct_res_info(cmd,CS_ROW_COUNT,(CS_VOID *)&affectedrows,				CS_UNUSED,(CS_INT *)NULL)!=CS_SUCCEED) {			return false;		} 	}	// if we're doing an rpc query, the result set should be a single	// row of output parameter results, fetch it and populate the output	// bind variables...	if (isrpcquery && resultstype==CS_PARAM_RESULT) {		if (ct_fetch(cmd,CS_UNUSED,CS_UNUSED,CS_UNUSED,				&rowsread)!=CS_SUCCEED && !rowsread) {			return false;		}				// copy data into output bind values		CS_INT	maxindex=outbindindex;		if (ncols<outbindindex) {			// this shouldn't happen...			maxindex=ncols;		}		for (CS_INT i=0; i<maxindex; i++) {			if (outbindtype[i]==CS_CHAR_TYPE) {				CS_INT	length=outbindstringlengths[i];				if (datalength[i][0]<length) {					length=datalength[i][0];				}				rawbuffer::copy(outbindstrings[i],						data[i][0],length);			} else if (outbindtype[i]==CS_INT_TYPE) {				*outbindints[i]=charstring::toInteger(								data[i][0]);			} else if (outbindtype[i]==CS_FLOAT_TYPE) {				*outbinddoubles[i]=charstring::toFloat(								data[i][0]);			}		}		discardResults();		ncols=0;	}	// return success only if no error was generated	if (sybaseconn->errorstring) {		return false;	}	return true;}const char *sybasecursor::errorMessage(bool *liveconnection) {	if (sybaseconn->deadconnection) {		*liveconnection=false;	} else {		*liveconnection=true;	}	if (sybaseconn->errorstring) {		return sybaseconn->errorstring->getString();	} else {		return NULL;	}}

⌨️ 快捷键说明

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