connect.c

来自「一个可以替代windows ODBC驱动程序管理器的通用ODBC数据库引擎」· C语言 代码 · 共 1,380 行 · 第 1/2 页

C
1,380
字号
      return SQL_ERROR;    }  /* check state */  if (pdbc->state != en_dbc_allocated)    {      PUSHSQLERR (pdbc->herr, en_08002);      ODBC_UNLOCK ();      return SQL_ERROR;    }  setopterr |= _iodbcdm_settracing (pdbc,      (char *) szDSN, cbDSN);  /*   *  Check whether driver is thread safe   */  ptr = _iodbcdm_getkeyvalbydsn (szDSN, cbDSN, "ThreadManager",      (char FAR *) buf, sizeof (buf));  thread_safe = 1;		/* Assume driver is thread safe */  if (ptr != NULL)    {      if (STREQ (ptr, "ON")	  || STREQ (ptr, "On")	  || STREQ (ptr, "on")	  || STREQ (ptr, "1"))	{	  thread_safe = 0;	/* Driver needs a thread manager */	}    }  /*   *  Get the name of the driver module and load it   */  ptr = _iodbcdm_getkeyvalbydsn (szDSN, cbDSN, "Driver",      (char FAR *) driver, sizeof (driver));  if (ptr == NULL)    /* No specified or default dsn section or     * no driver specification in this dsn section */    {      PUSHSQLERR (pdbc->herr, en_IM002);      ODBC_UNLOCK ();      return SQL_ERROR;    }  retcode = _iodbcdm_driverload (driver, pdbc, thread_safe);  switch (retcode)    {    case SQL_SUCCESS:      break;    case SQL_SUCCESS_WITH_INFO:      setopterr = SQL_ERROR;      /* unsuccessed in calling driver's        * SQLSetConnectOption() to set login       * timeout.       */      break;    default:      ODBC_UNLOCK ();      return retcode;    }  hproc = _iodbcdm_getproc (pdbc, en_Connect);  if (hproc == SQL_NULL_HPROC)    {      _iodbcdm_driverunload (pdbc);      PUSHSQLERR (pdbc->herr, en_IM001);      ODBC_UNLOCK ();      return SQL_ERROR;    }  CALL_DRIVER (hdbc, pdbc, retcode, hproc, en_Connect, (	  pdbc->dhdbc,	  szDSN, cbDSN,	  szUID, cbUID,	  szAuthStr, cbAuthStr));  if (retcode != SQL_SUCCESS      && retcode != SQL_SUCCESS_WITH_INFO)    {      /* not unload driver for retrive error        * messge from driver */		/*********		_iodbcdm_driverunload( hdbc );		**********/      ODBC_UNLOCK ();      return retcode;    }  /* state transition */  pdbc->state = en_dbc_connected;  /* do delaid option setting */  setopterr |= _iodbcdm_dbcdelayset (pdbc);  if (setopterr != SQL_SUCCESS)    {      ODBC_UNLOCK ();      return SQL_SUCCESS_WITH_INFO;    }  ODBC_UNLOCK ();  return retcode;}#ifdef _MACXextern SQLRETURN SQL_API _iodbcdm_drvconn_dialbox (HWND hwnd,    LPSTR szInOutConnStr, DWORD cbInOutConnStr, int FAR * sqlStat);#endifSQLRETURN SQL_APISQLDriverConnect (    SQLHDBC hdbc,    SQLHWND hwnd,    SQLCHAR FAR * szConnStrIn,    SQLSMALLINT cbConnStrIn,    SQLCHAR FAR * szConnStrOut,    SQLSMALLINT cbConnStrOutMax,    SQLSMALLINT FAR * pcbConnStrOut,    SQLUSMALLINT fDriverCompletion){  CONN (pdbc, hdbc);  HDLL hdll;  char FAR *drv = NULL;  char drvbuf[1024];  char FAR *dsn = NULL;  char dsnbuf[SQL_MAX_DSN_LENGTH + 1];  UCHAR cnstr2drv[1024];  SWORD thread_safe;  char buf[100];  char *ptr;  int i;  HPROC hproc;  HPROC dialproc;  int sqlstat = en_00000;  SQLRETURN retcode = SQL_SUCCESS;  SQLRETURN setopterr = SQL_SUCCESS;  ODBC_LOCK ();  if (!IS_VALID_HDBC (pdbc))    {      ODBC_UNLOCK ();      return SQL_INVALID_HANDLE;    }  CLEAR_ERRORS (pdbc);  /* check arguments */  if ((cbConnStrIn < 0 && cbConnStrIn != SQL_NTS) || cbConnStrOutMax < 0)    {      PUSHSQLERR (pdbc->herr, en_S1090);      ODBC_UNLOCK ();      return SQL_ERROR;    }  /* check state */  if (pdbc->state != en_dbc_allocated)    {      PUSHSQLERR (pdbc->herr, en_08002);      ODBC_UNLOCK ();      return SQL_ERROR;    }  drv = _iodbcdm_getkeyvalinstr (szConnStrIn, cbConnStrIn,      "DRIVER", drvbuf, sizeof (drvbuf));  dsn = _iodbcdm_getkeyvalinstr (szConnStrIn, cbConnStrIn,      "DSN", dsnbuf, sizeof (dsnbuf));  switch (fDriverCompletion)    {    case SQL_DRIVER_NOPROMPT:      break;    case SQL_DRIVER_COMPLETE:    case SQL_DRIVER_COMPLETE_REQUIRED:      if (dsn != NULL || drv != NULL)	{	  break;	}      /* fall to next case */    case SQL_DRIVER_PROMPT:      /*        *  Get data source dialog box function from       * current executable or the driver DLL        */      if (dsn == NULL || dsn[0] == '\0')	dsn = "default";      if (drv == NULL || drv[0] == '\0')	drv = _iodbcdm_getkeyvalbydsn (dsn, SQL_NTS, "Driver", drvbuf,	    sizeof (drvbuf));#ifndef _MACX      if (!(hdll = _iodbcdm_dllopen ("libiodbcadm.so"))	  || !(dialproc = _iodbcdm_dllproc (hdll, "iodbcdm_drvconn_dialbox")))	{	  sqlstat = en_IM008;	  retcode = SQL_ERROR;	  break;	}#endif      if (cbConnStrOutMax < cbConnStrIn && cbConnStrIn != SQL_NTS)	{#if (ODBCVER>=0x3000)	  sqlstat = en_HY092;#else	  sqlstat = en_S1000;#endif	  retcode = SQL_ERROR;	  break;	}      STRNCPY (szConnStrOut, szConnStrIn, (cbConnStrOutMax != SQL_NTS) ?	  cbConnStrOutMax : STRLEN (szConnStrIn) + 1);      for (i = STRLEN (szConnStrOut); i; i--)	if (szConnStrOut[i] == ';')	  szConnStrOut[i] = 0;#ifdef _MACX      retcode = iodbcdm_drvconn_dialbox (#else      retcode = dialproc (#endif	  hwnd,			/* window or display handle */	  szConnStrOut,		/* input/output dsn buf */	  cbConnStrOutMax,	/* buf size */	  &sqlstat);		/* error code */      if (retcode != SQL_SUCCESS)	{	  break;	}      dsn = dsnbuf;      if (dsn[0] == '\0')	dsn = "default";      if (STRLEN (szConnStrOut) > sizeof (cnstr2drv) - 1)	{	  sqlstat = en_S1001;	/* a lazy way to avoid using heap memory */	  break;	}      STRNCPY (cnstr2drv, szConnStrOut, sizeof (cnstr2drv));      szConnStrIn = cnstr2drv;      break;    default:      sqlstat = en_S1110;      break;    }  if (sqlstat != en_00000)    {      PUSHSQLERR (pdbc->herr, sqlstat);      ODBC_UNLOCK ();      return SQL_ERROR;    }  if (dsn == NULL || dsn[0] == '\0')    {      dsn = "default";    }  else    /* if you want tracing, you must use a DSN */    {      setopterr |= _iodbcdm_settracing (pdbc, (char *) dsn, SQL_NTS);    }  /*   *  Check whether driver is thread safe   */  ptr = _iodbcdm_getkeyvalbydsn (dsn, SQL_NTS, "ThreadManager",      (char FAR *) buf, sizeof (buf));  thread_safe = 1;		/* Assume driver is thread safe */  if (ptr != NULL)    {      if (STREQ (ptr, "ON")	  || STREQ (ptr, "On") || STREQ (ptr, "on") || STREQ (ptr, "1"))	{	  thread_safe = 0;	/* Driver needs a thread manager */	}    }  /*   *  Get the name of the driver module and load it   */  if (drv == NULL || drv[0] == '\0')    {      drv = _iodbcdm_getkeyvalbydsn (dsn, SQL_NTS, "Driver",	  drvbuf, sizeof (drvbuf));    }  if (drv == NULL)    {      PUSHSQLERR (pdbc->herr, en_IM002);      ODBC_UNLOCK ();      return SQL_ERROR;    }  retcode = _iodbcdm_driverload (drv, pdbc, thread_safe);  switch (retcode)    {    case SQL_SUCCESS:      break;    case SQL_SUCCESS_WITH_INFO:      setopterr = SQL_ERROR;      /* unsuccessed in calling driver's        * SQLSetConnectOption() to set login       * timeout.       */      break;    default:      ODBC_UNLOCK ();      return retcode;    }  hproc = _iodbcdm_getproc (pdbc, en_DriverConnect);  if (hproc == SQL_NULL_HPROC)    {      _iodbcdm_driverunload (pdbc);      PUSHSQLERR (pdbc->herr, en_IM001);      ODBC_UNLOCK ();      return SQL_ERROR;    }  if (cbConnStrIn == SQL_NTS)    {      cbConnStrIn = STRLEN (szConnStrIn);    }  CALL_DRIVER (hdbc, pdbc, retcode, hproc, en_DriverConnect, (pdbc->dhdbc,	  hwnd, szConnStrIn, cbConnStrIn, szConnStrOut, cbConnStrOutMax,	  pcbConnStrOut, fDriverCompletion));  if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)    {      /* don't unload driver here for retrive        * error message from driver */		/********		_iodbcdm_driverunload( hdbc );		*********/      ODBC_UNLOCK ();      return retcode;    }  /* state transition */  pdbc->state = en_dbc_connected;  /* do delaid option setting */  setopterr |= _iodbcdm_dbcdelayset (pdbc);  if (setopterr != SQL_SUCCESS)    {      ODBC_UNLOCK ();      return SQL_SUCCESS_WITH_INFO;    }  ODBC_UNLOCK ();  return retcode;}SQLRETURN SQL_APISQLBrowseConnect (SQLHDBC hdbc,    SQLCHAR FAR * szConnStrIn,    SQLSMALLINT cbConnStrIn,    SQLCHAR FAR * szConnStrOut,    SQLSMALLINT cbConnStrOutMax, SQLSMALLINT FAR * pcbConnStrOut){  CONN (pdbc, hdbc);  char FAR *drv;  char drvbuf[1024];  char FAR *dsn;  char dsnbuf[SQL_MAX_DSN_LENGTH + 1];  SWORD thread_safe;  char buf[100];  char *ptr;  HPROC hproc;  SQLRETURN retcode = SQL_SUCCESS;  SQLRETURN setopterr = SQL_SUCCESS;  ODBC_LOCK ();  if (!IS_VALID_HDBC (pdbc))    {      ODBC_UNLOCK ();      return SQL_INVALID_HANDLE;    }  CLEAR_ERRORS (pdbc);  /* check arguments */  if ((cbConnStrIn < 0 && cbConnStrIn != SQL_NTS) || cbConnStrOutMax < 0)    {      PUSHSQLERR (pdbc->herr, en_S1090);      ODBC_UNLOCK ();      return SQL_ERROR;    }  if (pdbc->state == en_dbc_allocated)    {      drv = _iodbcdm_getkeyvalinstr (szConnStrIn, cbConnStrIn,	  "DRIVER", drvbuf, sizeof (drvbuf));      dsn = _iodbcdm_getkeyvalinstr (szConnStrIn, cbConnStrIn,	  "DSN", dsnbuf, sizeof (dsnbuf));      if (dsn == NULL || dsn[0] == '\0')	{	  dsn = "default";	}      else	/* if you want tracing, you must use a DSN */	{	  setopterr |= _iodbcdm_settracing (pdbc, (char *) dsn, SQL_NTS);	}      /*       *  Check whether driver is thread safe       */      ptr = _iodbcdm_getkeyvalbydsn (dsn, SQL_NTS, "ThreadManager",	  (char FAR *) buf, sizeof (buf));      thread_safe = 1;		/* Assume driver is thread safe */      if (ptr != NULL)	{	  if (STREQ (ptr, "ON") || STREQ (ptr, "On") ||	      STREQ (ptr, "on") || STREQ (ptr, "1"))	    {	      thread_safe = 0;	/* Driver needs a thread manager */	    }	}      /*        *  Get the name of the driver module and load it       */      if (drv == NULL || drv[0] == '\0')	{	  drv = _iodbcdm_getkeyvalbydsn (dsn, SQL_NTS, "Driver",	      drvbuf, sizeof (drvbuf));	}      if (drv == NULL)	{	  PUSHSQLERR (pdbc->herr, en_IM002);	  ODBC_UNLOCK ();	  return SQL_ERROR;	}      retcode = _iodbcdm_driverload (drv, pdbc, thread_safe);      switch (retcode)	{	case SQL_SUCCESS:	  break;	case SQL_SUCCESS_WITH_INFO:	  setopterr = SQL_ERROR;	  /* unsuccessed in calling driver's 	   * SQLSetConnectOption() to set login	   * timeout.	   */	  break;	default:	  ODBC_UNLOCK ();	  return retcode;	}    }  else if (pdbc->state != en_dbc_needdata)    {      PUSHSQLERR (pdbc->herr, en_08002);      ODBC_UNLOCK ();      return SQL_ERROR;    }  hproc = _iodbcdm_getproc (pdbc, en_BrowseConnect);  if (hproc == SQL_NULL_HPROC)    {      _iodbcdm_driverunload (pdbc);      pdbc->state = en_dbc_allocated;      PUSHSQLERR (pdbc->herr, en_IM001);      ODBC_UNLOCK ();      return SQL_ERROR;    }  CALL_DRIVER (hdbc, pdbc, retcode, hproc, en_BrowseConnect, (pdbc->dhdbc,	  szConnStrIn, cbConnStrIn,	  szConnStrOut, cbConnStrOutMax, pcbConnStrOut));  switch (retcode)    {    case SQL_SUCCESS:    case SQL_SUCCESS_WITH_INFO:      pdbc->state = en_dbc_connected;      setopterr |= _iodbcdm_dbcdelayset (pdbc);      if (setopterr != SQL_SUCCESS)	{	  retcode = SQL_SUCCESS_WITH_INFO;	}      break;    case SQL_NEED_DATA:      pdbc->state = en_dbc_needdata;      break;    case SQL_ERROR:      pdbc->state = en_dbc_allocated;      /* but the driver will not unloaded        * to allow application retrive err       * message from driver        */      break;    default:      break;    }  ODBC_UNLOCK ();  return retcode;}SQLRETURN SQL_APISQLDisconnect (SQLHDBC hdbc){  CONN (pdbc, hdbc);  STMT_t FAR *pstmt;  SQLRETURN retcode;  HPROC hproc;  int sqlstat = en_00000;  ODBC_LOCK ();  if (!IS_VALID_HDBC (pdbc))    {      ODBC_UNLOCK ();      return SQL_INVALID_HANDLE;    }  CLEAR_ERRORS (pdbc);  /* check hdbc state */  if (pdbc->state == en_dbc_allocated)    {      sqlstat = en_08003;    }  /* check stmt(s) state */  for (pstmt = (STMT_t FAR *) pdbc->hstmt;      pstmt != NULL && sqlstat == en_00000;      pstmt = (STMT_t FAR *) pstmt->next)    {      if (pstmt->state >= en_stmt_needdata	  || pstmt->asyn_on != en_NullProc)	/* In this case one need to call 	 * SQLCancel() first */	{	  sqlstat = en_S1010;	}    }  if (sqlstat == en_00000)    {      hproc = _iodbcdm_getproc (pdbc, en_Disconnect);      if (hproc == SQL_NULL_HPROC)	{	  sqlstat = en_IM001;	}    }  if (sqlstat != en_00000)    {      PUSHSQLERR (pdbc->herr, sqlstat);      ODBC_UNLOCK ();      return SQL_ERROR;    }  CALL_DRIVER (hdbc, pdbc, retcode, hproc, en_Disconnect, (	  pdbc->dhdbc));  if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)    {      /* diff from MS specs. We disallow       * driver SQLDisconnect() return       * SQL_SUCCESS_WITH_INFO and post       * error message.       */      retcode = SQL_SUCCESS;    }  else    {      ODBC_UNLOCK ();      return retcode;    }  /* free all statement handle(s) on this connection */  for (; pdbc->hstmt;)    {      _iodbcdm_dropstmt (pdbc->hstmt);    }  /* state transition */  if (retcode == SQL_SUCCESS)    {      pdbc->state = en_dbc_allocated;    }  ODBC_UNLOCK ();  return retcode;}SQLRETURN SQL_APISQLNativeSql (    SQLHDBC hdbc,    SQLCHAR FAR * szSqlStrIn,    SQLINTEGER cbSqlStrIn,    SQLCHAR FAR * szSqlStr,    SQLINTEGER cbSqlStrMax,    SQLINTEGER FAR * pcbSqlStr){  CONN (pdbc, hdbc);  HPROC hproc;  int sqlstat = en_00000;  SQLRETURN retcode;  ENTER_HDBC (pdbc);  /* check argument */  if (szSqlStrIn == NULL)    {      sqlstat = en_S1009;    }  else if (cbSqlStrIn < 0 && cbSqlStrIn != SQL_NTS)    {      sqlstat = en_S1090;    }  if (sqlstat != en_00000)    {      PUSHSQLERR (pdbc->herr, sqlstat);      LEAVE_HDBC (pdbc, SQL_ERROR);    }  /* check state */  if (pdbc->state <= en_dbc_needdata)    {      PUSHSQLERR (pdbc->herr, en_08003);      LEAVE_HDBC (pdbc, SQL_ERROR);    }  /* call driver */  hproc = _iodbcdm_getproc (pdbc, en_NativeSql);  if (hproc == SQL_NULL_HPROC)    {      PUSHSQLERR (pdbc->herr, en_IM001);      LEAVE_HDBC (pdbc, SQL_ERROR);    }  CALL_DRIVER (hdbc, pdbc, retcode, hproc, en_NativeSql,      (pdbc->dhdbc, szSqlStrIn, cbSqlStrIn, szSqlStr, cbSqlStrMax, pcbSqlStr));  LEAVE_HDBC (pdbc, retcode);}

⌨️ 快捷键说明

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