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

📄 odbcconnection.c

📁 适合于Unix/Linux下的一个持久数据库连接池
💻 C
📖 第 1 页 / 共 2 页
字号:
						uint32_t *scale,						int16_t *isnull) {	erg=SQLBindParameter(stmt,				charstring::toInteger(variable+1),				SQL_PARAM_OUTPUT,				SQL_C_DOUBLE,				SQL_DOUBLE,				0,				0,				value,				sizeof(double),				#ifdef SQLBINDPARAMETER_SQLLEN				(SQLLEN *)isnull				#elif defined(SQLBINDPARAMETER_SQLLEN)				(unsigned long *)isnull				#else				(SQLINTEGER *)isnull				#endif				);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}short odbccursor::nonNullBindValue() {	return 0;}short odbccursor::nullBindValue() {	return SQL_NULL_DATA;}bool odbccursor::bindValueIsNull(short isnull) {	return (isnull==SQL_NULL_DATA);}bool odbccursor::executeQuery(const char *query, uint32_t length,							bool execute) {	// initialize counts	ncols=0;	row=0;	maxrow=0;	totalrows=0;	// execute the query	erg=SQLExecute(stmt);	if (erg!=SQL_SUCCESS &&		erg!=SQL_SUCCESS_WITH_INFO#if defined(SQL_NO_DATA)		&& erg!=SQL_NO_DATA#elif defined(SQL_NO_DATA_FOUND)		&& erg!=SQL_NO_DATA_FOUND#endif		) {		return false;	}	checkForTempTable(query,length);	// get the column count	erg=SQLNumResultCols(stmt,&ncols);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	if (ncols>MAX_SELECT_LIST_SIZE) {		ncols=MAX_SELECT_LIST_SIZE;	}	// run through the columns	for (SQLSMALLINT i=0; i<ncols; i++) {		if (conn->sendColumnInfo()) {#if (ODBCVER >= 0x0300)			// column name					erg=SQLColAttribute(stmt,i+1,SQL_DESC_LABEL,					col[i].name,MAX_ITEM_BUFFER_SIZE,					(SQLSMALLINT *)&(col[i].namelength),					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)NULL					#else					NULL					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}//orbb			col[i].namelength=charstring::length(col[i].name);//orbb			// column length			erg=SQLColAttribute(stmt,i+1,SQL_DESC_LENGTH,					NULL,0,NULL,					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)&(col[i].length)					#else					(SQLINTEGER *)&(col[i].length)					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}				// column type			erg=SQLColAttribute(stmt,i+1,SQL_DESC_TYPE,					NULL,0,NULL,					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)&(col[i].type)					#else					(SQLINTEGER *)&(col[i].type)					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column precision			erg=SQLColAttribute(stmt,i+1,SQL_DESC_PRECISION,					NULL,0,NULL,					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)&(col[i].precision)					#else					(SQLINTEGER *)&(col[i].precision)					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column scale			erg=SQLColAttribute(stmt,i+1,SQL_DESC_SCALE,					NULL,0,NULL,					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)&(col[i].scale)					#else					(SQLINTEGER *)&(col[i].scale)					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column nullable			erg=SQLColAttribute(stmt,i+1,SQL_DESC_NULLABLE,					NULL,0,NULL,					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)&(col[i].nullable)					#else					(SQLINTEGER *)&(col[i].nullable)					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// primary key			// unique			// part of key			// unsigned number			erg=SQLColAttribute(stmt,i+1,SQL_DESC_UNSIGNED,					NULL,0,NULL,					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)&(col[i].unsignednumber)					#else					(SQLINTEGER *)&(col[i].unsignednumber)					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// zero fill			// binary			// autoincrement			erg=SQLColAttribute(stmt,i+1,SQL_DESC_AUTO_UNIQUE_VALUE,					NULL,0,NULL,					#ifdef SQLCOLATTRIBUTE_SQLLEN					(SQLLEN *)&(col[i].autoincrement)					#else					(SQLINTEGER *)&(col[i].autoincrement)					#endif					);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}#else			// column name			erg=SQLColAttributes(stmt,i+1,SQL_COLUMN_LABEL,					col[i].name,MAX_ITEM_BUFFER_SIZE,					(SQLSMALLINT *)&(col[i].namelength),					NULL);			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column length			erg=SQLColAttributes(stmt,i+1,SQL_COLUMN_LENGTH,					NULL,0,NULL,					(SQLINTEGER *)&(col[i].length));			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column type			erg=SQLColAttributes(stmt,i+1,SQL_COLUMN_TYPE,					NULL,0,NULL,					(SQLINTEGER *)&(col[i].type));			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column precision			erg=SQLColAttributes(stmt,i+1,SQL_COLUMN_PRECISION,					NULL,0,NULL,					(SQLINTEGER *)&(col[i].precision));			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column scale			erg=SQLColAttributes(stmt,i+1,SQL_COLUMN_SCALE,					NULL,0,NULL,					(SQLINTEGER *)&(col[i].scale));			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// column nullable			erg=SQLColAttributes(stmt,i+1,SQL_COLUMN_NULLABLE,					NULL,0,NULL,					(SQLINTEGER *)&(col[i].nullable));			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// primary key			// unique			// part of key			// unsigned number			erg=SQLColAttributes(stmt,i+1,SQL_COLUMN_UNSIGNED,					NULL,0,NULL,					(SQLINTEGER *)&(col[i].unsignednumber));			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}			// zero fill			// binary			// autoincrement#ifdef SQL_DESC_AUTO_UNIQUE_VALUE			erg=SQLColAttributes(stmt,i+1,					SQL_COLUMN_AUTO_UNIQUE_VALUE,					NULL,0,NULL,					(SQLINTEGER *)&(col[i].autoincrement));			if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {				return false;			}#else			col[i].autoincrement=0;#endif#endif		}		// bind the column to a buffer//		erg=SQLBindCol(stmt,i+1,SQL_C_CHAR,//				field[i],MAX_ITEM_BUFFER_SIZE,//				#ifdef SQLBINDCOL_SQLLEN//				(SQLLEN *)&indicator[i]//				#else//				(SQLINTEGER *)&indicator[i]//				#endif//				);						//orbb		if(col[i].type==-9 || col[i].type==-8)		{			//bind varchar and char fields as wchar			// bind the column to a buffer//			erg=SQLBindCol(stmt,i+1,SQL_C_WCHAR,//					field[i],MAX_ITEM_BUFFER_SIZE,//					(SQLINTEGER *)&indicator[i]);		erg=SQLBindCol(stmt,i+1,SQL_C_WCHAR,				field[i],MAX_ITEM_BUFFER_SIZE,				#ifdef SQLBINDCOL_SQLLEN				(SQLLEN *)&indicator[i]				#else				(SQLINTEGER *)&indicator[i]				#endif				);		}		else		{			// bind the column to a buffer			if(col[i].type==93 || col[i].type==91)			{//				erg=SQLBindCol(stmt,i+1,SQL_C_BINARY,//						field[i],MAX_ITEM_BUFFER_SIZE,//						(SQLINTEGER *)&indicator[i]);				erg=SQLBindCol(stmt,i+1,SQL_C_BINARY,						field[i],MAX_ITEM_BUFFER_SIZE,						#ifdef SQLBINDCOL_SQLLEN						(SQLLEN *)&indicator[i]						#else						(SQLINTEGER *)&indicator[i]						#endif						);			}			else			{//				erg=SQLBindCol(stmt,i+1,SQL_C_CHAR,//									field[i],MAX_ITEM_BUFFER_SIZE,//									(SQLINTEGER *)&indicator[i]);				erg=SQLBindCol(stmt,i+1,SQL_C_CHAR,						field[i],MAX_ITEM_BUFFER_SIZE,						#ifdef SQLBINDCOL_SQLLEN						(SQLLEN *)&indicator[i]						#else						(SQLINTEGER *)&indicator[i]						#endif						);			}		}		//orbb								if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {			return false;		}	}	// get the row count#ifdef SQLROWCOUNT_SQLLEN	erg=SQLRowCount(stmt,(SQLLEN *)&affectedrows);#else	erg=SQLRowCount(stmt,&affectedrows);#endif	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}	return true;}const char *odbccursor::errorMessage(bool *liveconnection) {	SQLCHAR		error[501];	SQLCHAR		state[10];	SQLINTEGER	nativeerrnum;	SQLSMALLINT	errnum;	// need to use SQLGetDiagRec and SQLGetDiagField here...	SQLError(odbcconn->env,odbcconn->dbc,stmt,state,&nativeerrnum,							error,500,&errnum);	if (errormsg) {		delete errormsg;	}	errormsg=new stringbuffer();	errormsg->append((const char *)error);	*liveconnection=true;	return errormsg->getString();}bool odbccursor::knowsRowCount() {	return false;}uint64_t odbccursor::rowCount() {	return 0;}bool odbccursor::knowsAffectedRows() {	return true;}uint64_t odbccursor::affectedRows() {	return affectedrows;}uint32_t odbccursor::colCount() {	return ncols;}const char * const * odbccursor::columnNames() {	for (SQLSMALLINT i=0; i<ncols; i++) {		columnnames[i]=col[i].name;	}	return columnnames;}uint16_t odbccursor::columnTypeFormat() {	return (uint16_t)COLUMN_TYPE_IDS;}void odbccursor::returnColumnInfo() {	// a useful variable	uint16_t	type;	// for each column...	for (SQLSMALLINT i=0; i<ncols; i++) {		uint16_t	binary=0;		if (col[i].type==SQL_BIGINT) {			type=BIGINT_DATATYPE;		} else if (col[i].type==SQL_BINARY) {			type=BINARY_DATATYPE;			binary=1;		} else if (col[i].type==SQL_BIT) {			type=BIT_DATATYPE;		} else if (col[i].type==SQL_CHAR) {			type=CHAR_DATATYPE;		} else if (col[i].type==SQL_DATE) {			type=DATE_DATATYPE;		} else if (col[i].type==SQL_DECIMAL) {			type=DECIMAL_DATATYPE;		} else if (col[i].type==SQL_DOUBLE) {			type=DOUBLE_DATATYPE;		} else if (col[i].type==SQL_FLOAT) {			type=FLOAT_DATATYPE;		} else if (col[i].type==SQL_INTEGER) {			type=INTEGER_DATATYPE;		} else if (col[i].type==SQL_LONGVARBINARY) {			type=LONGVARBINARY_DATATYPE;			binary=1;		} else if (col[i].type==SQL_LONGVARCHAR) {			type=LONGVARCHAR_DATATYPE;		} else if (col[i].type==SQL_NUMERIC) {			type=NUMERIC_DATATYPE;		} else if (col[i].type==SQL_REAL) {			type=REAL_DATATYPE;		} else if (col[i].type==SQL_SMALLINT) {			type=SMALLINT_DATATYPE;		} else if (col[i].type==SQL_TIME) {			type=TIME_DATATYPE;		} else if (col[i].type==SQL_TIMESTAMP) {			type=TIMESTAMP_DATATYPE;		} else if (col[i].type==SQL_TINYINT) {			type=TINYINT_DATATYPE;		} else if (col[i].type==SQL_VARBINARY) {			type=VARBINARY_DATATYPE;			binary=1;		} else if (col[i].type==SQL_VARCHAR) {			type=VARCHAR_DATATYPE;		} else {			type=UNKNOWN_DATATYPE;		}		// send column definition		conn->sendColumnDefinition(col[i].name,col[i].namelength,type,						col[i].length,col[i].precision,						col[i].scale,col[i].nullable,						0,0,0,						col[i].unsignednumber,0,binary,						col[i].autoincrement);	}}bool odbccursor::noRowsToReturn() {	// if there are no columns, then there can't be any rows either	return (!ncols);}bool odbccursor::skipRow() {	return fetchRow();}bool odbccursor::fetchRow() {// this code is here in case unixodbc ever // successfully supports array fetches/*#if (ODBCVER >= 0x0300)	if (row==FETCH_AT_ONCE) {		row=0;	}	if (row>0 && row==maxrow) {		return false;	}	if (!row) {		erg=SQLFetchScroll(stmt,SQL_FETCH_NEXT,0);		if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {			return false;		}		SQLGetStmtAttr(stmt,SQL_ATTR_ROW_NUMBER,				(SQLPOINTER)&rownumber,0,NULL);		if (rownumber==totalrows) {			return false;		}		maxrow=rownumber-totalrows;		totalrows=rownumber;	}	return true;#else*/	erg=SQLFetch(stmt);	if (erg!=SQL_SUCCESS && erg!=SQL_SUCCESS_WITH_INFO) {		return false;	}		//orbb	//convert char and varchar data to user coding from ucs-2	for (int i=0; i<ncols; i++)	{						if(col[i].type==-9 || col[i].type==-8)		{			if(indicator[i]!=-1 && field[i])			{				char *u=conv_to_user_coding(field[i]);				int len=charstring::length(u);				charstring::copy(field[i],u);				indicator[i]=len;							if(u)free(u);						}		}	}	//orbb	return true;//#endif}void odbccursor::returnRow() {// this code is here in case unixodbc ever // successfully supports array fetches/*#if (ODBCVER >= 0x0300)	for (SQLSMALLINT index=0; index<ncols; index++) {		// handle a null field		if (indicator[index][row]==SQL_NULL_DATA) {			conn->sendNullField();			continue;		}		// handle a non-null field		conn->sendField(field[index][row],indicator[index][row]);	}	row++;#else*/	for (SQLSMALLINT index=0; index<ncols; index++) {		// handle a null field		if (indicator[index]==SQL_NULL_DATA) {			conn->sendNullField();			continue;		}		// handle a non-null field		conn->sendField(field[index],indicator[index]);	}//#endif}

⌨️ 快捷键说明

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