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

📄 hstmt.c

📁 一个可以替代windows ODBC驱动程序管理器的通用ODBC数据库引擎
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  hstmt.c * *  $Id: hstmt.c,v 1.11 2001/06/04 14:01:25 source Exp $ * *  Query statement object management functions * *  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>#if (ODBCVER >= 0x0300)#include <hdesc.h>#endif#include <henv.h>#include <hdbc.h>#include <hstmt.h>#include <itrace.h>#if (ODBCVER >= 0x300)static const SQLINTEGER desc_attrs[4] = {  SQL_ATTR_APP_ROW_DESC,  SQL_ATTR_APP_PARAM_DESC,  SQL_ATTR_IMP_ROW_DESC,  SQL_ATTR_IMP_PARAM_DESC};#endifSQLRETURN SQL_API SQLAllocStmt (    SQLHDBC hdbc,    SQLHSTMT FAR * phstmt){  CONN (pdbc, hdbc);  STMT_t FAR *pstmt = NULL;  HPROC hproc = SQL_NULL_HPROC;  SQLRETURN retcode = SQL_SUCCESS;  ENTER_HDBC (pdbc);  if (phstmt == NULL)    {      PUSHSQLERR (pdbc->herr, en_S1009);      LEAVE_HDBC (pdbc, SQL_ERROR);    }  /* check state */  switch (pdbc->state)     {     case en_dbc_connected:     case en_dbc_hstmt:       break;     case en_dbc_allocated:     case en_dbc_needdata:       PUSHSQLERR (pdbc->herr, en_08003);       *phstmt = SQL_NULL_HSTMT;       LEAVE_HDBC (pdbc, SQL_ERROR);     default:       LEAVE_HDBC (pdbc, SQL_INVALID_HANDLE);     }  pstmt = (STMT_t FAR *) MEM_ALLOC (sizeof (STMT_t));  if (pstmt == NULL)    {      PUSHSQLERR (pdbc->herr, en_S1001);      *phstmt = SQL_NULL_HSTMT;      LEAVE_HDBC (pdbc, SQL_ERROR);    }  pstmt->rc = 0;  /*   *  Initialize this handle   */  pstmt->type = SQL_HANDLE_STMT;  /* initiate the object */  pstmt->herr = SQL_NULL_HERR;  pstmt->hdbc = (HSTMT) hdbc;  pstmt->state = en_stmt_allocated;  pstmt->cursor_state = en_stmt_cursor_no;  pstmt->prep_state = 0;  pstmt->asyn_on = en_NullProc;  pstmt->need_on = en_NullProc;  pstmt->stmt_cip = 0;  /* call driver's function */#if (ODBCVER >= 0x0300)  pstmt->row_array_size = 1;  pstmt->rowset_size = 1;  pstmt->fetch_bookmark_ptr = NULL;  pstmt->params_processed_ptr = NULL;  pstmt->paramset_size = 0;  pstmt->rows_fetched_ptr = NULL;  if (((ENV_t FAR *)((DBC_t FAR *)pstmt->hdbc)->henv)->dodbc_ver == SQL_OV_ODBC2 &&       ((GENV_t FAR *)((DBC_t FAR *)pstmt->hdbc)->genv)->odbc_ver == SQL_OV_ODBC3)    { /* if it's a odbc3 app calling odbc2 driver */      pstmt->row_status_ptr = MEM_ALLOC(sizeof(SQLUINTEGER) * pstmt->row_array_size);      if (!pstmt->row_status_ptr)	{	  PUSHSQLERR(pstmt->herr, en_HY001);	  *phstmt = SQL_NULL_HSTMT;	  pstmt->type = 0;	  MEM_FREE (pstmt);	  LEAVE_HDBC (pdbc, SQL_ERROR);	}      pstmt->row_status_allocated = SQL_TRUE;    }  else    {      pstmt->row_status_ptr = NULL;      pstmt->row_status_allocated = SQL_FALSE;    }  hproc = _iodbcdm_getproc (pdbc, en_AllocHandle);  if (hproc)    {      CALL_DRIVER (pstmt->hdbc, pdbc, retcode, hproc, en_AllocHandle,         (SQL_HANDLE_STMT, pdbc->dhdbc, &(pstmt->dhstmt)));    }  else#endif    {      hproc = _iodbcdm_getproc (pdbc, en_AllocStmt);      if (hproc == SQL_NULL_HPROC)	{	  PUSHSQLERR (pstmt->herr, en_IM001);	  *phstmt = SQL_NULL_HSTMT;	  pstmt->type = 0;	  MEM_FREE (pstmt);	  LEAVE_HDBC (pdbc, SQL_ERROR);	}      CALL_DRIVER (hdbc, pdbc, retcode, hproc, en_AllocStmt,         (pdbc->dhdbc, &(pstmt->dhstmt)));    }  if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)    {      *phstmt = SQL_NULL_HSTMT;      pstmt->type = 0;      MEM_FREE (pstmt);      LEAVE_HDBC (pdbc, retcode);    }#if (ODBCVER >= 0x0300)    /* get the descriptors */  memset(&pstmt->imp_desc, 0, sizeof(pstmt->imp_desc));  memset(&pstmt->desc, 0, sizeof(pstmt->desc));  if (((ENV_t *)pdbc->henv)->dodbc_ver == SQL_OV_ODBC2)    { /* this is an ODBC2 driver - so alloc dummy implicit desc handles  (dhdesc = NULL) */      int i, i1;      for (i = 0; i < 4; i++)	{	  pstmt->imp_desc[i] = (DESC_t FAR *) MEM_ALLOC (sizeof (DESC_t));	  memset(pstmt->imp_desc[i], 0, sizeof(DESC_t));	  if (pstmt->imp_desc[i] == NULL)	    {	      for (i1 = 0; i1 < i; i++)		{		  pstmt->imp_desc[i1]->type = 0;		  MEM_FREE(pstmt->imp_desc[i1]);		}	      PUSHSQLERR(pdbc->herr, en_HY001);	      pstmt->type = 0;	      MEM_FREE(pstmt);	      LEAVE_HDBC (pdbc, SQL_ERROR);	    }	  pstmt->imp_desc[i]->type = SQL_HANDLE_DESC;	  pstmt->imp_desc[i]->hstmt = pstmt;	  pstmt->imp_desc[i]->dhdesc = NULL;	  pstmt->imp_desc[i]->hdbc = hdbc;	  pstmt->imp_desc[i]->herr = NULL;	}    }  else    { /* the ODBC3 driver */      hproc = _iodbcdm_getproc(pdbc, en_GetStmtAttr);      if (hproc == SQL_NULL_HPROC)	{  /* with no GetStmtAttr ! */	  int i;	  PUSHSQLERR(pdbc->herr, en_HYC00);	  pstmt->type = 0;	  MEM_FREE(pstmt);	  LEAVE_HDBC (pdbc, SQL_ERROR);	}      else	{ /* get the implicit descriptors */	  int i, i1;	  RETCODE rc1;	  for (i = 0; i < 4; i++)	    {	      pstmt->imp_desc[i] = (DESC_t FAR *) MEM_ALLOC (sizeof (DESC_t));	      memset(pstmt->imp_desc[i], 0, sizeof(DESC_t));	      if (pstmt->imp_desc[i] == NULL)		{ /* memory allocation error */		  PUSHSQLERR(pdbc->herr, en_HY001);		  for (i1 = 0; i1 < i; i++)		    {		      pstmt->imp_desc[i1]->type = 0;		      MEM_FREE(pstmt->imp_desc[i1]);		    }		  pstmt->type = 0;		  MEM_FREE(pstmt);		  LEAVE_HDBC (pdbc, SQL_ERROR);		}	      pstmt->imp_desc[i]->type = SQL_HANDLE_DESC;	      pstmt->imp_desc[i]->hdbc = hdbc;	      pstmt->imp_desc[i]->hstmt = *phstmt;	      pstmt->imp_desc[i]->herr = NULL;	      CALL_DRIVER(hdbc, pstmt, rc1, hproc, en_GetStmtAttr,		  (pstmt->dhstmt, SQL_ATTR_IMP_PARAM_DESC, &pstmt->imp_desc[i]->dhdesc, 0, NULL));	      if (rc1 != SQL_SUCCESS && rc1 != SQL_SUCCESS_WITH_INFO)		{ /* no descriptor returned from the driver */		  pstmt->type = 0;		  MEM_FREE(pstmt);		  for (i1 = 0; i1 < i + 1; i++)		    {		      pstmt->imp_desc[i1]->type = 0;		      MEM_FREE(pstmt->imp_desc[i1]);		    }		  pstmt->type = 0;		  MEM_FREE(pstmt);		  pdbc->rc = SQL_ERROR;		  LEAVE_HDBC (pdbc, SQL_ERROR);		}	    }	}    }#endif      /* insert into list */  pstmt->next = pdbc->hstmt;  pdbc->hstmt = pstmt;  *phstmt = (SQLHSTMT) pstmt;  /* state transition */  pdbc->state = en_dbc_hstmt;  LEAVE_HDBC (pdbc, SQL_SUCCESS);}SQLRETURN _iodbcdm_dropstmt (HSTMT hstmt){  STMT (pstmt, hstmt);  STMT_t FAR *tpstmt;  DBC_t FAR *pdbc;  if (!IS_VALID_HSTMT (pstmt))    {      return SQL_INVALID_HANDLE;    }  CLEAR_ERRORS (pstmt);  pdbc = (DBC_t FAR *) (pstmt->hdbc);  for (tpstmt = (STMT_t FAR *) pdbc->hstmt;      tpstmt != NULL;      tpstmt = tpstmt->next)    {      if (tpstmt == pstmt)	{	  pdbc->hstmt = (HSTMT) pstmt->next;	  break;	}      if (tpstmt->next == pstmt)	{	  tpstmt->next = pstmt->next;	  break;	}    }  if (tpstmt == NULL)    {      return SQL_INVALID_HANDLE;    }#if (ODBCVER >= 0x0300)  if (pstmt->row_status_allocated == SQL_TRUE && pstmt->row_status_ptr)    MEM_FREE(pstmt->row_status_ptr);    /* drop the implicit descriptors */  if (pstmt->imp_desc[0])    {      int i, i1;      for (i = 0; i < 4; i++)	{	  _iodbcdm_freesqlerrlist (pstmt->imp_desc[i]->herr);	  pstmt->imp_desc[i]->type = 0;	  MEM_FREE(pstmt->imp_desc[i]);	}    }#endif     /*   *  Invalidate this handle   */  pstmt->type = 0;  MEM_FREE (pstmt);  return SQL_SUCCESS;}SQLRETURN SQL_API SQLFreeStmt (    SQLHSTMT hstmt,    SQLUSMALLINT fOption){  STMT (pstmt, hstmt);  DBC_t FAR *pdbc;  HPROC hproc = SQL_NULL_HPROC;  SQLRETURN retcode;  ODBC_LOCK ();  if (!IS_VALID_HSTMT (pstmt))    {      ODBC_UNLOCK ();      return SQL_INVALID_HANDLE;    }  CLEAR_ERRORS (pstmt);  pdbc = (DBC_t FAR *) (pstmt->hdbc);  /* check option */  switch (fOption)    {    case SQL_DROP:    case SQL_CLOSE:    case SQL_UNBIND:    case SQL_RESET_PARAMS:      break;    default:      PUSHSQLERR (pstmt->herr, en_S1092);      ODBC_UNLOCK ();      return SQL_ERROR;    }  /* check state */  if (pstmt->state >= en_stmt_needdata || pstmt->asyn_on != en_NullProc)    {      PUSHSQLERR (pstmt->herr, en_S1010);      ODBC_UNLOCK ();      return SQL_ERROR;    }  hproc = SQL_NULL_HPROC;#if (ODBCVER >= 0x0300)  if (fOption == SQL_DROP)    {      hproc = _iodbcdm_getproc (pstmt->hdbc, en_FreeHandle);      if (hproc)	{	  CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_FreeHandle,	      (SQL_HANDLE_STMT, pstmt->dhstmt));	}    }#endif  if (hproc == SQL_NULL_HPROC)    {      hproc = _iodbcdm_getproc (pstmt->hdbc, en_FreeStmt);      if (hproc == SQL_NULL_HPROC)	{	  PUSHSQLERR (pstmt->herr, en_IM001);	  ODBC_UNLOCK ();	  return SQL_ERROR;	}      CALL_DRIVER (pstmt->hdbc, pstmt, retcode, hproc, en_FreeStmt,	  (pstmt->dhstmt, fOption));    }  if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)    {      ODBC_UNLOCK ();      return retcode;    }  /* state transition */  switch (fOption)    {    case SQL_DROP:

⌨️ 快捷键说明

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