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

📄 prepare.c

📁 一个可以替代windows ODBC驱动程序管理器的通用ODBC数据库引擎
💻 C
字号:
/* *  prepare.c * *  $Id: prepare.c,v 1.11 2001/06/04 14:01:25 source Exp $ * *  Prepare a query * *  The iODBC driver manager. *   *  Copyright (C) 1995 by Ke Jin <kejin@empress.com>  * *  This library is free software; you can redistribute it and/or *  modify it under the terms of the GNU Library General Public *  License as published by the Free Software Foundation; either *  version 2 of the License, or (at your option) any later version. * *  This library is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *  Library General Public License for more details. * *  You should have received a copy of the GNU Library General Public *  License along with this library; if not, write to the Free *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <iodbc.h>#include <sql.h>#include <sqlext.h>#include <dlproc.h>#include <herr.h>#include <henv.h>#include <hdbc.h>#include <hstmt.h>#include <itrace.h>SQLRETURN SQL_APISQLPrepare (    SQLHSTMT hstmt,    SQLCHAR FAR * szSqlStr,    SQLINTEGER cbSqlStr){  STMT (pstmt, hstmt);  HPROC hproc = SQL_NULL_HPROC;  SQLRETURN retcode = SQL_SUCCESS;  int sqlstat = en_00000;  ENTER_STMT (pstmt);  /* check state */  if (pstmt->asyn_on == en_NullProc)    {      /* not on asyn state */      switch (pstmt->state)	{	case en_stmt_fetched:	case en_stmt_xfetched:	  sqlstat = en_24000;	  break;	case en_stmt_needdata:	case en_stmt_mustput:	case en_stmt_canput:	  sqlstat = en_S1010;	  break;	default:	  break;	}    }  else if (pstmt->asyn_on != en_Prepare)    {      /* asyn on other */      sqlstat = en_S1010;    }  if (sqlstat != en_00000)    {      PUSHSQLERR (pstmt->herr, sqlstat);      LEAVE_STMT (pstmt, SQL_ERROR);    }  if (szSqlStr == NULL)    {      PUSHSQLERR (pstmt->herr, en_S1009);      LEAVE_STMT (pstmt, SQL_ERROR);    }  if (cbSqlStr < 0 && cbSqlStr != SQL_NTS)    {      PUSHSQLERR (pstmt->herr, en_S1090);      LEAVE_STMT (pstmt, SQL_ERROR);    }  hproc = _iodbcdm_getproc (pstmt->hdbc, en_Prepare);  if (hproc == SQL_NULL_HPROC)    {      PUSHSQLERR (pstmt->herr, en_IM001);      LEAVE_STMT (pstmt, SQL_ERROR);    }  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_Prepare,      (pstmt->dhstmt, szSqlStr, cbSqlStr));  /* stmt state transition */  if (pstmt->asyn_on == en_Prepare)    {      switch (retcode)	{	case SQL_SUCCESS:	case SQL_SUCCESS_WITH_INFO:	case SQL_ERROR:	  pstmt->asyn_on = en_NullProc;	   LEAVE_STMT (pstmt, retcode);	case SQL_STILL_EXECUTING:	default:	   LEAVE_STMT (pstmt, retcode);	}    }  switch (retcode)    {    case SQL_STILL_EXECUTING:      pstmt->asyn_on = en_Prepare;      break;    case SQL_SUCCESS:    case SQL_SUCCESS_WITH_INFO:      pstmt->state = en_stmt_prepared;      pstmt->prep_state = 1;      break;    case SQL_ERROR:      switch (pstmt->state)	{	case en_stmt_prepared:	case en_stmt_executed:	  pstmt->state = en_stmt_allocated;	  pstmt->prep_state = 0;	  break;	default:	  break;	}    default:      break;    }  LEAVE_STMT (pstmt, retcode);}SQLRETURN SQL_APISQLSetCursorName (    SQLHSTMT hstmt,    SQLCHAR FAR * szCursor,    SQLSMALLINT cbCursor){  STMT (pstmt, hstmt);  HPROC hproc = SQL_NULL_HPROC;  SQLRETURN retcode = SQL_SUCCESS;  int sqlstat = en_00000;  ENTER_STMT (pstmt);  if (szCursor == NULL)    {      PUSHSQLERR (pstmt->herr, en_S1009);      LEAVE_STMT (pstmt, SQL_ERROR);    }  if (cbCursor < 0 && cbCursor != SQL_NTS)    {      PUSHSQLERR (pstmt->herr, en_S1090);      LEAVE_STMT (pstmt, SQL_ERROR);    }  /* check state */  if (pstmt->asyn_on != en_NullProc)    {      sqlstat = en_S1010;    }  else    {      switch (pstmt->state)	{	case en_stmt_executed:	case en_stmt_cursoropen:	case en_stmt_fetched:	case en_stmt_xfetched:	  sqlstat = en_24000;	  break;	case en_stmt_needdata:	case en_stmt_mustput:	case en_stmt_canput:	  sqlstat = en_S1010;	  break;	default:	  break;	}    }  if (sqlstat != en_00000)    {      PUSHSQLERR (pstmt->herr, sqlstat);      LEAVE_STMT (pstmt, SQL_ERROR);    }  hproc = _iodbcdm_getproc (pstmt->hdbc, en_SetCursorName);  if (hproc == SQL_NULL_HPROC)    {      PUSHSQLERR (pstmt->herr, en_IM001);      LEAVE_STMT (pstmt, SQL_ERROR);    }  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_SetCursorName,      (pstmt->dhstmt, szCursor, cbCursor));  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)    {      pstmt->cursor_state = en_stmt_cursor_named;    }  LEAVE_STMT (pstmt, retcode);}SQLRETURN SQL_APISQLBindParameter (    SQLHSTMT hstmt,    SQLUSMALLINT ipar,    SQLSMALLINT fParamType,    SQLSMALLINT fCType,    SQLSMALLINT fSqlType,    SQLUINTEGER cbColDef,    SQLSMALLINT ibScale,    SQLPOINTER rgbValue,    SQLINTEGER cbValueMax,    SQLINTEGER FAR * pcbValue){  STMT (pstmt, hstmt);  HPROC hproc = SQL_NULL_HPROC;  int sqlstat = en_00000;  SQLRETURN retcode = SQL_SUCCESS;  ENTER_STMT (pstmt);#if (ODBCVER >= 0x0300)  if (0)#else  /* check param */  if (fSqlType > SQL_TYPE_MAX ||      (fSqlType < SQL_TYPE_MIN && fSqlType > SQL_TYPE_DRIVER_START))    /* Note: SQL_TYPE_DRIVER_START is a nagtive number      * So, we use ">" */#endif    {      sqlstat = en_S1004;    }  else if (ipar < 1)    {      sqlstat = en_S1093;    }  else if ((rgbValue == NULL && pcbValue == NULL)      && fParamType != SQL_PARAM_OUTPUT)    {      sqlstat = en_S1009;      /* This means, I allow output to nowhere       * (i.e. * junk output result). But I can't         * allow input from nowhere.        */    }/**********	else if( cbValueMax < 0L && cbValueMax != SQL_SETPARAM_VALUE_MAX )	{		sqlstat = en_S1090;	}**********/  else if (fParamType != SQL_PARAM_INPUT	&& fParamType != SQL_PARAM_OUTPUT      && fParamType != SQL_PARAM_INPUT_OUTPUT)    {      sqlstat = en_S1105;    }  else    {      switch (fCType)	{	case SQL_C_DEFAULT:	case SQL_C_BINARY:	case SQL_C_BIT:	case SQL_C_CHAR:	case SQL_C_DATE:	case SQL_C_DOUBLE:	case SQL_C_FLOAT:	case SQL_C_LONG:	case SQL_C_SHORT:	case SQL_C_SLONG:	case SQL_C_SSHORT:	case SQL_C_STINYINT:	case SQL_C_TIME:	case SQL_C_TIMESTAMP:	case SQL_C_TINYINT:	case SQL_C_ULONG:	case SQL_C_USHORT:	case SQL_C_UTINYINT:#if (ODBCVER >= 0x0300)	case SQL_C_GUID:	case SQL_C_INTERVAL_DAY:	case SQL_C_INTERVAL_DAY_TO_HOUR:	case SQL_C_INTERVAL_DAY_TO_MINUTE:	case SQL_C_INTERVAL_DAY_TO_SECOND:	case SQL_C_INTERVAL_HOUR:	case SQL_C_INTERVAL_HOUR_TO_MINUTE:	case SQL_C_INTERVAL_HOUR_TO_SECOND:	case SQL_C_INTERVAL_MINUTE:	case SQL_C_INTERVAL_MINUTE_TO_SECOND:	case SQL_C_INTERVAL_MONTH:	case SQL_C_INTERVAL_SECOND:	case SQL_C_INTERVAL_YEAR:	case SQL_C_INTERVAL_YEAR_TO_MONTH:	case SQL_C_NUMERIC:	case SQL_C_SBIGINT:	case SQL_C_TYPE_DATE:	case SQL_C_TYPE_TIME:	case SQL_C_TYPE_TIMESTAMP:	case SQL_C_UBIGINT:#endif	  break;	default:	  sqlstat = en_S1003;	  break;	}    }  if (sqlstat != en_00000)    {      PUSHSQLERR (pstmt->herr, sqlstat);      LEAVE_STMT (pstmt, SQL_ERROR);    }  /* check state */  if (pstmt->state >= en_stmt_needdata || pstmt->asyn_on != en_NullProc)    {      PUSHSQLERR (pstmt->herr, en_S1010);      retcode = SQL_ERROR;    }#if (ODBCVER >=0x0300)  if (fParamType == SQL_PARAM_INPUT)    {      hproc = _iodbcdm_getproc (pstmt->hdbc, en_BindParam);      if (hproc)	{	  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_BindParam,	      (pstmt->dhstmt, ipar, fCType, fSqlType, cbColDef,		  ibScale, rgbValue, pcbValue));	  LEAVE_STMT (pstmt, retcode);	}    }#endif  hproc = _iodbcdm_getproc (pstmt->hdbc, en_BindParameter);  if (hproc == SQL_NULL_HPROC)    {      PUSHSQLERR (pstmt->herr, en_IM001);      LEAVE_STMT (pstmt, SQL_ERROR);    }  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_BindParameter,      (pstmt->dhstmt, ipar, fParamType, fCType, fSqlType, cbColDef,	  ibScale, rgbValue, cbValueMax, pcbValue));  LEAVE_STMT (pstmt, retcode);}SQLRETURN SQL_APISQLParamOptions (    SQLHSTMT hstmt,    SQLUINTEGER crow,    SQLUINTEGER FAR * pirow){  STMT (pstmt, hstmt);  HPROC hproc;  SQLRETURN retcode;  ENTER_STMT (pstmt);  if (crow == (UDWORD) 0UL)    {      PUSHSQLERR (pstmt->herr, en_S1107);      LEAVE_STMT (pstmt, SQL_ERROR);    }  if (pstmt->state >= en_stmt_needdata || pstmt->asyn_on != en_NullProc)    {      PUSHSQLERR (pstmt->herr, en_S1010);      LEAVE_STMT (pstmt, SQL_ERROR);    }#if (ODBCVER >= 0x0300)  hproc = _iodbcdm_getproc (pstmt->hdbc, en_SetStmtAttr);  if (hproc != SQL_NULL_HPROC)    {      CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_SetStmtAttr,	  (pstmt->dhstmt, SQL_ATTR_PARAMSET_SIZE, crow, 0));      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)	{	  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_SetStmtAttr,	      (pstmt->dhstmt, SQL_ATTR_PARAMS_PROCESSED_PTR, pirow, 0));	}    }  else#endif    {      hproc = _iodbcdm_getproc (pstmt->hdbc, en_ParamOptions);      if (hproc == SQL_NULL_HPROC)	{	  PUSHSQLERR (pstmt->herr, en_IM001);	  LEAVE_STMT (pstmt, SQL_ERROR);	}      CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_ParamOptions,	  (pstmt->dhstmt, crow, pirow));    }  LEAVE_STMT (pstmt, retcode);}SQLRETURN SQL_APISQLSetScrollOptions (    SQLHSTMT hstmt,    SQLUSMALLINT fConcurrency,    SQLINTEGER crowKeyset,    SQLUSMALLINT crowRowset){  STMT (pstmt, hstmt);  HPROC hproc;  int sqlstat = en_00000;  SQLRETURN retcode;  ENTER_STMT (pstmt);  for (;;)    {      if (crowRowset == (UWORD) 0)	{	  sqlstat = en_S1107;	  break;	}      if (crowKeyset > (SDWORD) 0L && crowKeyset < (SDWORD) crowRowset)	{	  sqlstat = en_S1107;	  break;	}      if (crowKeyset < 1)	{	  if (crowKeyset != SQL_SCROLL_FORWARD_ONLY	      && crowKeyset != SQL_SCROLL_STATIC	      && crowKeyset != SQL_SCROLL_KEYSET_DRIVEN	      && crowKeyset != SQL_SCROLL_DYNAMIC)	    {	      sqlstat = en_S1107;	      break;	    }	}      if (fConcurrency != SQL_CONCUR_READ_ONLY	  && fConcurrency != SQL_CONCUR_LOCK	  && fConcurrency != SQL_CONCUR_ROWVER	  && fConcurrency != SQL_CONCUR_VALUES)	{	  sqlstat = en_S1108;	  break;	}#if (ODBCVER < 0x0300)      if (pstmt->state != en_stmt_allocated)	{	  sqlstat = en_S1010;	  break;	}#endif      hproc = _iodbcdm_getproc (pstmt->hdbc, en_SetScrollOptions);      if (hproc == SQL_NULL_HPROC)	{#if (ODBCVER >= 0x0300)	  SQLINTEGER InfoValue, InfoType, Value;	  HPROC hproc1 = _iodbcdm_getproc (pstmt->hdbc, en_SetStmtAttr);	  if (hproc1 == SQL_NULL_HPROC)	    {	      PUSHSQLERR (pstmt->herr, en_IM001);	      LEAVE_STMT (pstmt, SQL_ERROR);	    }	  switch (crowKeyset)	    {	    case SQL_SCROLL_FORWARD_ONLY:	      InfoType = SQL_FORWARD_ONLY_CURSOR_ATTRIBUTES2;	      Value = SQL_CURSOR_FORWARD_ONLY;	      break;	    case SQL_SCROLL_STATIC:	      InfoType = SQL_STATIC_CURSOR_ATTRIBUTES2;	      Value = SQL_CURSOR_STATIC;	      break;	    case SQL_SCROLL_DYNAMIC:	      InfoType = SQL_DYNAMIC_CURSOR_ATTRIBUTES2;	      Value = SQL_CURSOR_DYNAMIC;	      break;	    case SQL_SCROLL_KEYSET_DRIVEN:	    default:	      InfoType = SQL_KEYSET_CURSOR_ATTRIBUTES2;	      Value = SQL_CURSOR_KEYSET_DRIVEN;	      break;	    }	  retcode = SQLGetInfo (pstmt->hdbc, InfoType, &InfoValue, 0, NULL);	  if (retcode != SQL_SUCCESS)	    {	      LEAVE_STMT (pstmt, retcode);	    }	  switch (fConcurrency)	    {	    case SQL_CONCUR_READ_ONLY:	      if (!(InfoValue & SQL_CA2_READ_ONLY_CONCURRENCY))		{		  PUSHSQLERR (pstmt->herr, en_S1C00);		  LEAVE_STMT (pstmt, SQL_ERROR);		}	      break;	    case SQL_CONCUR_LOCK:	      if (!(InfoValue & SQL_CA2_LOCK_CONCURRENCY))		{		  PUSHSQLERR (pstmt->herr, en_S1C00);		  LEAVE_STMT (pstmt, SQL_ERROR);		}	      break;	    case SQL_CONCUR_ROWVER:	      if (!(InfoValue & SQL_CA2_OPT_ROWVER_CONCURRENCY))		{		  PUSHSQLERR (pstmt->herr, en_S1C00);		  LEAVE_STMT (pstmt, SQL_ERROR);		}	      break;	    case SQL_CONCUR_VALUES:	      if (!(InfoValue & SQL_CA2_OPT_VALUES_CONCURRENCY))		{		  PUSHSQLERR (pstmt->herr, en_S1C00);		  LEAVE_STMT (pstmt, SQL_ERROR);		}	      break;	    }	  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc1, en_SetStmtAttr,	      (pstmt->dhstmt, SQL_ATTR_CURSOR_TYPE, Value, 0));	  if (retcode != SQL_SUCCESS)	    LEAVE_STMT (pstmt, retcode);	  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc1, en_SetStmtAttr,	      (pstmt->dhstmt, SQL_ATTR_CONCURRENCY, fConcurrency, 0));	  if (retcode != SQL_SUCCESS)	    LEAVE_STMT (pstmt, retcode);	  if (crowKeyset > 0)	    {	      CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc1, en_SetStmtAttr,		  (pstmt->dhstmt, SQL_ATTR_KEYSET_SIZE, crowKeyset, 0));	      if (retcode != SQL_SUCCESS)		LEAVE_STMT (pstmt, retcode);	    }	  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc1, en_SetStmtAttr,	      (pstmt->dhstmt, SQL_ROWSET_SIZE, crowRowset, 0));	  if (retcode != SQL_SUCCESS)	    LEAVE_STMT (pstmt, retcode);#else	  sqlstat = en_IM001;	  break;#endif	}      sqlstat = en_00000;      if (1)			/* turn off solaris warning message */	break;    }  if (sqlstat != en_00000)    {      PUSHSQLERR (pstmt->herr, sqlstat);      LEAVE_STMT (pstmt, SQL_ERROR);    }  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_SetScrollOptions,      (pstmt->dhstmt, fConcurrency, crowKeyset, crowRowset));  LEAVE_STMT (pstmt, retcode);}SQLRETURN SQL_APISQLSetParam (    SQLHSTMT hstmt,    SQLUSMALLINT ipar,    SQLSMALLINT fCType,    SQLSMALLINT fSqlType,    SQLUINTEGER cbColDef,    SQLSMALLINT ibScale,    SQLPOINTER rgbValue,    SQLINTEGER FAR * pcbValue){  return SQLBindParameter (hstmt,      ipar,      (SWORD) SQL_PARAM_INPUT_OUTPUT,      fCType,      fSqlType,      cbColDef,      ibScale,      rgbValue,      SQL_SETPARAM_VALUE_MAX,      pcbValue);}

⌨️ 快捷键说明

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