connect.c
上传用户:jmzj888
上传日期:2007-01-02
资源大小:220k
文件大小:17k
- /* Copyright Abandoned 1997 TCX DataKonsult AB & Monty Program KB & Detron HB
- This file is public domain and comes with NO WARRANTY of any kind */
- /*
- ** CONNECT.C - This is the ODBC sample driver code for
- ** allocation and connection.
- */
- #include "myodbc.h"
- #ifndef _UNIX_
- #include <odbcinst.h>
- #include "dialogs.h"
- #endif /* IS NOT UNIX */
- #ifndef CLIENT_NO_SCHEMA /* Temporary fix for 3.21.29 */
- #define CLIENT_NO_SCHEMA 16
- #endif
- // CONNECT.C
- //
- // SQLC connection functions.
- // - - - - - - - - -
- // Allocate an environment (ENV) block.
- RETCODE SQL_API SQLAllocEnv(HENV FAR * phenv)
- {
- myodbc_init();
- #ifndef _UNIX_
- {
- HGLOBAL henv= GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (ENV));
- if (henv == NULL || (*phenv = (HENV)GlobalLock (henv)) == NULL)
- {
- GlobalFree (henv); /* Free it if lock fails */
- *phenv = SQL_NULL_HENV;
- return SQL_ERROR;
- }
- }
- #else
- if (!(*phenv = (HENV) my_malloc(sizeof(ENV),MYF(MY_ZEROFILL))))
- {
- *phenv = SQL_NULL_HENV;
- return SQL_ERROR;
- }
- #endif /* IS UNIX */
- return SQL_SUCCESS;
- }
- RETCODE SQL_API SQLFreeEnv(HENV henv)
- {
- #ifndef _UNIX_
- GlobalUnlock(GlobalHandle((HGLOBAL) henv));
- GlobalFree(GlobalHandle((HGLOBAL) henv));
- #else
- my_free(henv,MYF(0));
- #endif /* IS UNIX */
- return SQL_SUCCESS;
- }
- // - - - - - - - - -
- // Allocate a DBC block.
- RETCODE SQL_API SQLAllocConnect(HENV henv, HDBC FAR *phdbc)
- {
- DBC FAR *dbc;
- #ifndef _UNIX_
- {
- HGLOBAL hdbc = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (DBC));
- if (!hdbc || (*phdbc = (HDBC)GlobalLock (hdbc)) == SQL_NULL_HDBC)
- {
- GlobalFree (hdbc); /* Free it if lock fails */
- *phdbc = SQL_NULL_HENV;
- return SQL_ERROR;
- }
- }
- #else /* IS UNIX */
- if (!(*phdbc = (HDBC) my_malloc(sizeof(DBC),MYF(MY_ZEROFILL))))
- {
- *phdbc = SQL_NULL_HDBC;
- return SQL_ERROR;
- }
- #endif
- dbc=(DBC FAR*) *phdbc;
- dbc->env=henv;
- dbc->mysql.net.fd = -1; /* Marker if open */
- dbc->flag=0;
- dbc->stmt_options.max_rows= dbc->stmt_options.max_length= 0L;
- dbc->stmt_options.bind_type=SQL_BIND_BY_COLUMN;
- dbc->stmt_options.rows_in_set=1;
- dbc->stmt_options.cursor_type=SQL_CURSOR_FORWARD_ONLY; /* ODBC default */
- dbc->login_timeout=0;
- return SQL_SUCCESS;
- }
- RETCODE SQL_API SQLFreeConnect(HDBC hdbc)
- {
- DBC FAR *dbc=(DBC FAR*) hdbc;
- my_free(dbc->dsn,MYF(MY_ALLOW_ZERO_PTR));
- my_free(dbc->database,MYF(MY_ALLOW_ZERO_PTR));
- my_free(dbc->server,MYF(MY_ALLOW_ZERO_PTR));
- my_free(dbc->user,MYF(MY_ALLOW_ZERO_PTR));
- my_free(dbc->password,MYF(MY_ALLOW_ZERO_PTR));
- #ifndef _UNIX_
- GlobalUnlock(GlobalHandle((HGLOBAL) hdbc));
- GlobalFree(GlobalHandle((HGLOBAL) hdbc));
- #else /* IS UNIX */
- my_free(hdbc,MYF(0));
- #endif
- return SQL_SUCCESS;
- }
- /******************************************************************************
- * Connect to mysql server
- ******************************************************************************/
- RETCODE SQL_API SQLConnect(HDBC hdbc, UCHAR FAR *szDSN, SWORD cbDSN,
- UCHAR FAR *szUID, SWORD cbUID, UCHAR FAR *szAuthStr,
- SWORD cbAuthStr)
- {
- char host[64],user[64],passwd[64],dsn[NAME_LEN+1],database[NAME_LEN+1],
- port[10],flag[10];
- uint port_nr=0,flag_nr=0,client_flag=0;
- DBC FAR *dbc=(DBC FAR*) hdbc;
- DBUG_ENTER("SQLConnect");
- if (dbc->mysql.net.fd >= 0)
- {
- DBUG_RETURN(set_error(hdbc,"08002","Connection in use",0));
- }
- dbc->sqlstate[0]=0;
- #ifndef _UNIX_
- SQLGetPrivateProfileString(fix_str(dsn,szDSN,cbDSN),"server",
- "localhost", host, sizeof(host),"ODBC.INI");
- SQLGetPrivateProfileString(dsn,"database",
- dsn, database, sizeof(database),"ODBC.INI");
- SQLGetPrivateProfileString(dsn,"port",
- "0", port, sizeof(port),"ODBC.INI");
- port_nr=(uint) atoi(port);
- SQLGetPrivateProfileString(dsn,"flag",
- "0", flag, sizeof(flag),"ODBC.INI");
- flag_nr=(uint) atoi(flag);
- #else
- strmov(database,szDSN);
- {
- char *cp;
- strmov(host, (cp = getenv("MYSQL_HOST")) ? cp : "localhost");
- }
- #endif /* IS UNIX */
- if (flag_nr & FLAG_FOUND_ROWS)
- client_flag=CLIENT_FOUND_ROWS;
- if (flag_nr & FLAG_NO_SCHEMA)
- client_flag|=CLIENT_NO_SCHEMA;
- if (flag_nr & FLAG_DEBUG)
- mysql_debug("d:t:S:O,c::\myodbc.log");
- DBUG_PRINT("info",("MyODBC Version %s Options: %d",DRIVER_VERSION,flag_nr));
- if (flag_nr & FLAG_BIG_PACKETS)
- max_allowed_packet=~0L;
- #if MYSQL_VERSION_ID >= 32200
- if (!mysql_real_connect(&dbc->mysql,host,fix_str(user,(char FAR *) szUID,
- cbUID),
- fix_str(passwd,(char FAR*) szAuthStr,cbAuthStr),
- database, port_nr,
- NullS, client_flag))
- {
- strmov(dbc->sqlstate,"S1000");
- DBUG_RETURN(SQL_ERROR);
- }
- #else
- if (!mysql_real_connect(&dbc->mysql,host,fix_str(user,(char FAR *) szUID,
- cbUID),
- fix_str(passwd,(char FAR*) szAuthStr,cbAuthStr),
- port_nr, NullS, client_flag))
- {
- strmov(dbc->sqlstate,"S1000");
- DBUG_RETURN(SQL_ERROR);
- }
- if (mysql_select_db(&dbc->mysql,database))
- {
- strmov(dbc->sqlstate,"08004");
- mysql_close(&dbc->mysql);
- DBUG_RETURN(SQL_ERROR);
- }
- #endif
- dbc->dsn=my_strdup(database,MYF(MY_WME));
- dbc->database=my_strdup(database,MYF(MY_WME));
- dbc->server=my_strdup(host,MYF(MY_WME));
- dbc->user=my_strdup(user,MYF(MY_WME));
- dbc->password=my_strdup(passwd,MYF(MY_WME));
- dbc->port=port_nr;
- dbc->flag=flag_nr;
- DBUG_RETURN(SQL_SUCCESS);
- }
- // This function as its "normal" behavior is supposed to bring up a
- // dialog box if it isn't given enough information via "szConnStrIn". If
- // it is given enough information, it's supposed to use "szConnStrIn" to
- // establish a database connection. In either case, it returns a
- // string to the user that is the string that was eventually used to
- // establish the connection.
- #ifdef _UNIX_ /* By Tsurng-Chen Chern */
- char *my_strtok(char *s1, char *s2)
- {
- char *start;
- static char *p;
- p = (s1 ? s1 : p + 1);
- if ((!p) || (!*p))
- return(NULL);
- start = p;
- while ((*p) && (!strchr(s2, *p)))
- p++;
- *p = 0;
- return(start);
- }
- #endif
- RETCODE SQL_API SQLDriverConnect(HDBC hdbc,HWND hwnd,UCHAR FAR *szConnStrIn,
- SWORD cbConnStrIn,UCHAR FAR *szConnStrOut,
- SWORD cbConnStrOutMax,
- SWORD FAR *pcbConnStrOut,
- UWORD fDriverCompletion)
- {
- #ifndef _UNIX_
- short iRet;
- BOOL fPrompt = FALSE,maybe_prompt;
- DBC FAR *dbc=(DBC FAR*) hdbc;
- GLOBALHANDLE hglbAttr;
- LPSETUPDLG lpsetupdlg;
- uint client_flag=0;
- DBUG_ENTER("SQLDriverConnect");
- DBUG_PRINT("enter",("fDriverCompletion: %d",fDriverCompletion));
- hglbAttr = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(SETUPDLG));
- if (!hglbAttr)
- DBUG_RETURN(FALSE);
- lpsetupdlg = (LPSETUPDLG)GlobalLock(hglbAttr);
- maybe_prompt=(fDriverCompletion == SQL_DRIVER_COMPLETE ||
- fDriverCompletion == SQL_DRIVER_COMPLETE_REQUIRED);
- if ((szConnStrIn == NULL) || (!cbConnStrIn) ||
- ((cbConnStrIn == SQL_NTS) && (!szConnStrIn[0])))
- fPrompt = TRUE;
- else
- {
- int flag;
- /* Check connection string for completeness. Prompt if not all params */
- ParseAttributes(szConnStrIn, lpsetupdlg);
- set_attributes(lpsetupdlg);
- flag=atoi(lpsetupdlg->aAttr[KEY_FLAG].szAttr);
- if (fDriverCompletion == SQL_DRIVER_PROMPT && ! (flag & FLAG_NO_PROMPT) ||
- (maybe_prompt &&
- (!lpsetupdlg->aAttr[KEY_DSN].szAttr[0] ||
- !lpsetupdlg->aAttr[KEY_SERVER].szAttr[0] ||
- !lpsetupdlg->aAttr[KEY_USER].szAttr[0])))
- fPrompt = TRUE;
- }
- retry:
- if (fPrompt)
- {
- iRet = DialogBoxParam(s_hModule, MAKEINTRESOURCE(DRIVERCONNECT), hwnd,
- DriverConnectProc,
- (LONG)(LPSTR) lpsetupdlg);
- if ((!iRet) || (iRet == -1))
- {
- GlobalUnlock(hglbAttr);
- GlobalFree(hglbAttr);
- DBUG_RETURN(SQL_NO_DATA_FOUND);
- }
- }
- if (szConnStrOut)
- {
- char *pos,buff[1024];
- long length;
- ulong tmp= ~0L;
- pos=strxmov(buff,
- "DSN=",lpsetupdlg->aAttr[KEY_DSN].szAttr,
- ";DB=",lpsetupdlg->aAttr[KEY_DB].szAttr,
- ";SERVER=",lpsetupdlg->aAttr[KEY_SERVER].szAttr,
- ";UID=",lpsetupdlg->aAttr[KEY_USER].szAttr,
- ";PWD=",lpsetupdlg->aAttr[KEY_PASSWORD].szAttr,
- ";PORT=",lpsetupdlg->aAttr[KEY_PORT].szAttr,
- ";FLAG=",lpsetupdlg->aAttr[KEY_FLAG].szAttr,
- ";",NullS);
- (void) copy_lresult((DBC FAR*) 0,szConnStrOut,cbConnStrOutMax,
- &length,buff,(uint) (pos-buff),0L,0L,&tmp);
- if (pcbConnStrOut)
- *pcbConnStrOut=(SWORD) length;
- DBUG_PRINT("info",("Connect string out: %s",szConnStrOut));
- }
- dbc->port=atoi(lpsetupdlg->aAttr[KEY_PORT].szAttr);
- dbc->flag=atoi(lpsetupdlg->aAttr[KEY_FLAG].szAttr);
- if (dbc->flag & FLAG_FOUND_ROWS)
- client_flag=CLIENT_FOUND_ROWS;
- if (dbc->flag & FLAG_NO_SCHEMA)
- client_flag|=CLIENT_NO_SCHEMA;
- if (dbc->flag & FLAG_DEBUG)
- mysql_debug("d:t:S:O,c::\myodbc.log");
- DBUG_PRINT("info",("MyODBC Version %s Options: %d",DRIVER_VERSION,
- dbc->flag));
- if (dbc->flag & FLAG_BIG_PACKETS)
- max_allowed_packet=~0L;
- /* If no DB, use DSN as database name */
- if (!lpsetupdlg->aAttr[KEY_DB].szAttr[0])
- {
- strmov(lpsetupdlg->aAttr[KEY_DB].szAttr,
- lpsetupdlg->aAttr[KEY_DSN].szAttr);
- }
- if (!mysql_real_connect(&dbc->mysql,lpsetupdlg->aAttr[KEY_SERVER].szAttr,
- lpsetupdlg->aAttr[KEY_USER].szAttr,
- lpsetupdlg->aAttr[KEY_PASSWORD].szAttr,
- lpsetupdlg->aAttr[KEY_DB].szAttr,
- dbc->port,NullS,client_flag))
- {
- if (maybe_prompt && !fPrompt)
- {
- fPrompt=TRUE;
- goto retry;
- }
- strmov(dbc->sqlstate,"S1000");
- GlobalUnlock(hglbAttr);
- GlobalFree(hglbAttr);
- DBUG_RETURN(SQL_ERROR);
- }
- dbc->dsn=my_strdup(lpsetupdlg->aAttr[KEY_DSN].szAttr,MYF(MY_WME));
- dbc->database=my_strdup(lpsetupdlg->aAttr[KEY_DB].szAttr,MYF(MY_WME));
- dbc->server=my_strdup(lpsetupdlg->aAttr[KEY_SERVER].szAttr,MYF(MY_WME));
- dbc->user=my_strdup(lpsetupdlg->aAttr[KEY_USER].szAttr,MYF(MY_WME));
- dbc->password=my_strdup(lpsetupdlg->aAttr[KEY_PASSWORD].szAttr,MYF(MY_WME));
- GlobalUnlock(hglbAttr);
- GlobalFree(hglbAttr);
- DBUG_RETURN(SQL_SUCCESS);
- #endif /* IS NOT UNIX */
- #ifdef _UNIX_
- UCHAR FAR *connstrin=NULL;
- char *KEY_DSN=NULL;
- char *KEY_SERVER=NULL;
- char *KEY_USER=NULL;
- char *KEY_PASSWORD=NULL;
- char *KEY_PORT=NULL;
- char *KEY_OPTION=NULL;
- char *KEY_DB=NULL;
- char *KEY_SOCKET=NULL;
- char *tok[9];
- char *tok2=NULL;
- char *tok3=NULL;
- int i;
- uint client_flag=0;
- BOOL fPrompt = FALSE;
- DBC FAR *dbc=(DBC FAR*) hdbc;
- DBUG_ENTER("SQLDriverConnect");
- DBUG_PRINT("enter",("fDriverCompletion: %d",fDriverCompletion));
- if ((szConnStrIn == NULL) || (!cbConnStrIn) ||
- ((cbConnStrIn == SQL_NTS) && (!szConnStrIn[0])))
- fPrompt = TRUE;
- else
- {
- /* Check connection string for completeness. */
- /* Prompt if not all params */
- connstrin=(UCHAR FAR *) my_strdup(szConnStrIn,MYF(MY_WME));
- tok[0]=my_strtok(connstrin,";");
- for (i=1 ; i <= 7 ; i++)
- tok[i]=my_strtok(NULL,";");
- tok[i]=NULL;
- for (i=0 ; tok[i] ; i++)
- {
- if (tok2 = my_strtok(tok[i], "="))
- {
- if (strcmp("DSN", tok2) == 0)
- {
- if ((tok3 = my_strtok(NULL, "