19 Star 33 Fork 26

openLooKeng / hetu-odbc-driver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ma_dsn.c 20.84 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
/************************************************************************************
Copyright (C) 2013,2019 MariaDB Corporation AB
Copyright (C) 2018-2020. Huawei Technologies Co., Ltd. All rights reserved.
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.1 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 see <http://www.gnu.org/licenses>
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
#include <ma_odbc.h>
#define DSNKEY_OPTIONS_INDEX 3
#define DSNKEY_OPTION_INDEX 4
#define DSNKEY_NAMEDPIPE_INDEX 5
#define DSNKEY_TCPIP_INDEX 6
#define DSNKEY_SERVER_INDEX 7
#define DSNKEY_UID_INDEX 8
#define DSNKEY_PWD_INDEX 9
#define DSNKEY_DATABASE_INDEX 10
#define DSNKEY_FP_INDEX 25
#define DSNKEY_FPLIST_INDEX 26
MADB_DsnKey DsnKeys[]=
{
{"DSN", offsetof(MADB_Dsn, DSNName), DSN_TYPE_STRING, 0, 0}, /* 0 */
{"DESCRIPTION", offsetof(MADB_Dsn, Description), DSN_TYPE_STRING, 0, 0},
{"DRIVER", offsetof(MADB_Dsn, Driver), DSN_TYPE_STRING, 0, 0},
/* OPTIONS should go above all DSN_TYPE_OPTION. They are not saved in DSN separately, and then DSN is read, corresponding
properties are filled from OPTIONS. Also putting its alias here - it should not appear on Windows(unless somebody edits
registry manually), but on *nix we can expect everything. Array index used in some places to decide if the key is OPTIONS */
{"OPTIONS", offsetof(MADB_Dsn, Options), DSN_TYPE_INT, 0, 0}, /* DSNKEY_OPTIONS_INDEX */
{"OPTION", DSNKEY_OPTIONS_INDEX, DSN_TYPE_INT, 0, 1}, /* DSNKEY_OPTION_INDEX */
{"NamedPipe", offsetof(MADB_Dsn, IsNamedPipe), DSN_TYPE_OPTION, MADB_OPT_FLAG_NAMED_PIPE, 0}, /* MADB_DSNKEY_NAMEDPIPE_INDEX */
{"TCPIP", offsetof(MADB_Dsn, IsTcpIp), DSN_TYPE_BOOL, 0, 0}, /* DSNKEY_TCPIP_INDEX */
{"SERVER", offsetof(MADB_Dsn, ServerName), DSN_TYPE_STRING, 0, 0}, /* DSNKEY_SERVER_INDEX */
{"UID", offsetof(MADB_Dsn, UserName), DSN_TYPE_STRING, 0, 0}, /* DSNKEY_UID_INDEX */
{"PWD", offsetof(MADB_Dsn, Password), DSN_TYPE_STRING, 0, 0}, /* DSNKEY_PWD_INDEX */
{"DATABASE", offsetof(MADB_Dsn, Catalog), DSN_TYPE_COMBO, 0, 0}, /* 10 DSNKEY_DATABASE_INDEX */
{"PORT", offsetof(MADB_Dsn, Port), DSN_TYPE_INT, 0, 0},
{"INITSTMT", offsetof(MADB_Dsn, InitCommand), DSN_TYPE_STRING, 0, 0},
{"CONN_TIMEOUT", offsetof(MADB_Dsn, ConnectionTimeout), DSN_TYPE_INT, 0, 0},
{"AUTO_RECONNECT", offsetof(MADB_Dsn, Reconnect), DSN_TYPE_OPTION, MADB_OPT_FLAG_AUTO_RECONNECT,0},
{"NO_PROMPT", offsetof(MADB_Dsn, ConnectPrompt), DSN_TYPE_OPTION, MADB_OPT_FLAG_NO_PROMPT,0},
{"CHARSET", offsetof(MADB_Dsn, CharacterSet), DSN_TYPE_COMBO, 0, 0},
{"TRACE", offsetof(MADB_Dsn, TraceFile), DSN_TYPE_STRING, 0, 0},
{"PLUGIN_DIR", offsetof(MADB_Dsn, ConnCPluginsDir), DSN_TYPE_STRING, 0, 0},
/* SSL */
{"SSLKEY", offsetof(MADB_Dsn, SslKey), DSN_TYPE_STRING, 0, 0},
{"SSLCERT", offsetof(MADB_Dsn, SslCert), DSN_TYPE_STRING, 0, 0}, /* 20 */
{"SSLCA", offsetof(MADB_Dsn, SslCa), DSN_TYPE_STRING, 0, 0},
{"SSLCAPATH", offsetof(MADB_Dsn, SslCaPath), DSN_TYPE_STRING, 0, 0},
{"SSLCIPHER", offsetof(MADB_Dsn, SslCipher), DSN_TYPE_STRING, 0, 0},
{"SSLVERIFY", offsetof(MADB_Dsn, SslVerify), DSN_TYPE_BOOL, 0, 0},
{"TLSPEERFP", offsetof(MADB_Dsn, TlsPeerFp), DSN_TYPE_STRING, 0, 0},
{"TLSPEERFPLIST", offsetof(MADB_Dsn, TlsPeerFpList), DSN_TYPE_STRING, 0, 0},
{"SSLCRL", offsetof(MADB_Dsn, SslCrl), DSN_TYPE_STRING, 0, 0},
{"SSLCRLPATH", offsetof(MADB_Dsn, SslCrlPath), DSN_TYPE_STRING, 0, 0},
{"SOCKET", offsetof(MADB_Dsn, Socket), DSN_TYPE_STRING, 0, 0},
{"SAVEFILE", offsetof(MADB_Dsn, SaveFile), DSN_TYPE_STRING, 0, 0}, /* 30 */
{"USE_MYCNF", offsetof(MADB_Dsn, ReadMycnf), DSN_TYPE_OPTION, MADB_OPT_FLAG_USE_CNF, 0},
{"TLSVERSION", offsetof(MADB_Dsn, TlsVersion), DSN_TYPE_CBOXGROUP, 0, 0},
{"FORCETLS", offsetof(MADB_Dsn, ForceTls), DSN_TYPE_BOOL, 0, 0},
{"SERVERKEY", offsetof(MADB_Dsn, ServerKey), DSN_TYPE_STRING, 0, 0},
/* Aliases. Here offset is index of aliased key */
{"SERVERNAME", DSNKEY_SERVER_INDEX, DSN_TYPE_STRING, 0, 1},
{"USER", DSNKEY_UID_INDEX, DSN_TYPE_STRING, 0, 1},
{"PASSWORD", DSNKEY_PWD_INDEX, DSN_TYPE_STRING, 0, 1},
{"DB", DSNKEY_DATABASE_INDEX, DSN_TYPE_COMBO, 0, 1},
{"SSLFP", DSNKEY_FP_INDEX, DSN_TYPE_STRING, 0, 1},
{"SSLFPLIST", DSNKEY_FPLIST_INDEX, DSN_TYPE_STRING, 0, 1},
/*Add DB server schema*/
{ "SCHEMA", offsetof(MADB_Dsn, Schema), DSN_TYPE_STRING, 0, 0 },
/*Add DB server connect config file path*/
{ "CONNECTCFGFILE", offsetof(MADB_Dsn, ConnectCfgFile), DSN_TYPE_STRING, 0, 0 },
/*Add DB server connect whole url*/
{ "CONNECTURL", offsetof(MADB_Dsn, ConnectUrl), DSN_TYPE_STRING, 0, 0 },
{ "BINARY_EXCHANGE",offsetof(MADB_Dsn, BinaryExec), DSN_TYPE_BOOL, 0, 0 },
/* Terminating Null */
{NULL, 0, DSN_TYPE_BOOL,0,0}
};
/* TODO: Shouln't 2nd be DSNKEY_OPTIONS_INDEX? But I seemed to remember it was removed for a reason... */
#define IS_OPTIONS_BITMAP(key_index) (key_index == DSNKEY_OPTIONS_INDEX || key_index == DSNKEY_OPTIONS_INDEX)
typedef struct
{
unsigned int Key;
unsigned int Dependent;
BOOL Same; /* Should dependent be switched same way, or in reverse */
} MADB_DsnKeyDep;
/* Define pairs of keys that are switches, i.e. setting one should reset the other.
Transitive dependencies have to be defined as direct dependencies here as well */
const MADB_DsnKeyDep DsnKeysSwitch[]=
{
{DSNKEY_NAMEDPIPE_INDEX, DSNKEY_TCPIP_INDEX, 0},
{DSNKEY_TCPIP_INDEX, DSNKEY_NAMEDPIPE_INDEX, 0}
};
const char TlsVersionName[][8]= {"TLSv1.1", "TLSv1.2", "TLSv1.3"};
const char TlsVersionBits[]= {MADB_TLSV11, MADB_TLSV12, MADB_TLSV13};
/* {{{ MADB_DSN_SetDefaults() */
void MADB_DSN_SetDefaults(MADB_Dsn *Dsn)
{
Dsn->IsTcpIp= 1;
}
/* }}} */
/* {{{ MADB_Dsn_Init() */
MADB_Dsn *MADB_DSN_Init()
{
MADB_Dsn *Dsn;
if ((Dsn= (MADB_Dsn *)MADB_CALLOC(sizeof(MADB_Dsn))))
{
Dsn->FreeMe= TRUE;
Dsn->Keys= (MADB_DsnKey *)&DsnKeys;
}
return Dsn;
}
/* }}} */
/* {{{ MADB_Dsn_Free */
void MADB_DSN_Free(MADB_Dsn *Dsn)
{
if (!Dsn)
return;
MADB_FREE(Dsn->DSNName);
MADB_FREE(Dsn->Driver);
MADB_FREE(Dsn->Description);
MADB_FREE(Dsn->ServerName);
MADB_FREE(Dsn->UserName);
MADB_FREE(Dsn->Password);
MADB_FREE(Dsn->Catalog);
MADB_FREE(Dsn->CharacterSet);
MADB_FREE(Dsn->InitCommand);
MADB_FREE(Dsn->TraceFile);
MADB_FREE(Dsn->Socket);
MADB_FREE(Dsn->ConnCPluginsDir);
MADB_FREE(Dsn->SslKey);
MADB_FREE(Dsn->SslCert);
MADB_FREE(Dsn->SslCa);
MADB_FREE(Dsn->SslCaPath);
MADB_FREE(Dsn->SslCipher);
MADB_FREE(Dsn->SslCrl);
MADB_FREE(Dsn->SslCrlPath);
MADB_FREE(Dsn->TlsPeerFp);
MADB_FREE(Dsn->TlsPeerFpList);
MADB_FREE(Dsn->SaveFile);
MADB_FREE(Dsn->ServerKey);
MADB_FREE(Dsn->Schema);
MADB_FREE(Dsn->ConnectCfgFile);
MADB_FREE(Dsn->ConnectUrl);
if (Dsn->FreeMe)
MADB_FREE(Dsn);
}
/* }}} */
void MADB_SetOptionValue(MADB_Dsn *Dsn, MADB_DsnKey *DsnKey, my_bool value)
{
*GET_FIELD_PTR(Dsn, DsnKey, my_bool)= value;
if (value)
{
Dsn->Options |= DsnKey->FlagValue;
}
else
{
Dsn->Options &= ~DsnKey->FlagValue;
}
}
my_bool MADB_DsnStoreValue(MADB_Dsn *Dsn, unsigned int DsnKeyIdx, char *Value, my_bool OverWrite);
/* {{{ MADB_DsnSwitchDependents */
/* If TCPIP selected, we have to reset NAMEDPIPE */
BOOL MADB_DsnSwitchDependents(MADB_Dsn *Dsn, unsigned int Changed)
{
int i;
for (i= 0; i < sizeof(DsnKeysSwitch)/sizeof(MADB_DsnKeyDep); ++i)
{
if (DsnKeysSwitch[i].Key == Changed)
{
my_bool KeySet;
switch (DsnKeys[Changed].Type)
{
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
{
char *str= *GET_FIELD_PTR(Dsn, &DsnKeys[Changed], char*);
KeySet= str && *str;
}
break;
case DSN_TYPE_OPTION:
case DSN_TYPE_BOOL:
case DSN_TYPE_CBOXGROUP:
{
KeySet= *GET_FIELD_PTR(Dsn, &DsnKeys[Changed], my_bool);
}
break;
case DSN_TYPE_INT:
{
KeySet= *GET_FIELD_PTR(Dsn, &DsnKeys[Changed], int) != 0;
}
}
/* No problem to deal with aliases here as well, but let's keep things simple */
if (DsnKeys[DsnKeysSwitch[i].Dependent].IsAlias != 0)
{
return FALSE;
}
switch(DsnKeys[DsnKeysSwitch[i].Dependent].Type)
{
case DSN_TYPE_BOOL:
*GET_FIELD_PTR(Dsn, &DsnKeys[DsnKeysSwitch[i].Dependent], my_bool)= DsnKeysSwitch[i].Same == KeySet ? 1 : 0;
break;
case DSN_TYPE_OPTION:
MADB_SetOptionValue(Dsn, &DsnKeys[DsnKeysSwitch[i].Dependent], DsnKeysSwitch[i].Same == KeySet ? 1 : 0);
break;
default:
return FALSE; /* Only boolean fields are supported as dependent atm */
}
}
}
return TRUE;
}
/* }}} */
/* {{{ MADB_DsnStoreValue */
my_bool MADB_DsnStoreValue(MADB_Dsn *Dsn, unsigned int DsnKeyIdx, char *Value, my_bool OverWrite)
{
MADB_DsnKey *DsnKey= &DsnKeys[DsnKeyIdx];
if (!Dsn || DsnKey->IsAlias)
return FALSE;
switch(DsnKey->Type) {
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
{
char **p= GET_FIELD_PTR(Dsn, DsnKey, char*);
if (*p && OverWrite == FALSE)
break;
/* For the case of making copy of currently stored values */
MADB_RESET(*p, Value);
}
break;
case DSN_TYPE_BOOL:
/* If value is not set or we may overwrite it */
if (!(*GET_FIELD_PTR(Dsn, DsnKey, my_bool) && OverWrite == FALSE))
{
*GET_FIELD_PTR(Dsn, DsnKey, my_bool)= atoi(Value);
}
break;
case DSN_TYPE_CBOXGROUP:
/* If value is not set or we may overwrite it */
if (! (*GET_FIELD_PTR(Dsn, DsnKey, char) && OverWrite == FALSE))
{
char IntValue= atoi(Value);
/* Atm we have only one DSN_TYPE_CBOXGROUP!!!, and following works only for it. If sometime another such field is added,
we will need another data structure array, that will bind DSN field with string values and bits for this field.
So far we use hardcoded arrays for the singe such field we have atm */
if (IntValue == '\0')
{
unsigned int i;
IntValue= 0;
for (i= 0; i < sizeof(TlsVersionBits); ++i)
{
if (strcasestr(Value, TlsVersionName[i]) != NULL)
{
IntValue|= TlsVersionBits[i];
}
}
}
*GET_FIELD_PTR(Dsn, DsnKey, char)= IntValue;
}
break;
case DSN_TYPE_INT:
if (*GET_FIELD_PTR(Dsn, DsnKey, int) && OverWrite == FALSE)
break;
*GET_FIELD_PTR(Dsn, DsnKey, int)= strtoul(Value, NULL, 10);
break;
case DSN_TYPE_OPTION:
if (*GET_FIELD_PTR(Dsn, DsnKey, my_bool) && OverWrite == FALSE)
break;
MADB_SetOptionValue(Dsn, DsnKey, strtoul(Value, NULL, 10) != 0 ? 1 : 0);
break;
}
return MADB_DsnSwitchDependents(Dsn, DsnKeyIdx);
}
/* }}} */
/* {{{ MADB_DsnUpdateOptionsFields */
void MADB_DsnUpdateOptionsFields(MADB_Dsn *Dsn)
{
int i= 0;
while (DsnKeys[i].DsnKey != NULL)
{
if (DsnKeys[i].IsAlias == 0)
{
if (DsnKeys[i].Type == DSN_TYPE_OPTION)
{
*GET_FIELD_PTR(Dsn, &DsnKeys[i], my_bool)= (my_bool)(DSN_OPTION(Dsn, DsnKeys[i].FlagValue) ? 1 : 0);
MADB_DsnSwitchDependents(Dsn, i);
}
}
++i;
}
}
/* }}} */
/* {{{ MADB_ReadDSN */
my_bool MADB_ReadDSN(MADB_Dsn *Dsn, const char *KeyValue, my_bool OverWrite)
{
char *Value;
/* if no key/value pair was specified, we will try to read Dsn->DSNName */
if (!KeyValue)
{
Value= Dsn->DSNName;
}
else
{
if ((Value= strchr(KeyValue, '=')) != NULL)
{
++Value;
MADB_RESET(Dsn->DSNName, Value);
}
}
if (Value)
{
int i= 1;
char KeyVal[1024];
while (DsnKeys[i].DsnKey)
{
unsigned int KeyIdx= DsnKeys[i].IsAlias ? DsnKeys[i].DsnOffset : i;
if (SQLGetPrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, "", KeyVal, 1024, "ODBC.INI") > 0)
{
if (!MADB_DsnStoreValue(Dsn, KeyIdx, KeyVal, OverWrite))
return FALSE;
}
else if (DsnKeys[i].Type == DSN_TYPE_OPTION)
{
*GET_FIELD_PTR(Dsn, &DsnKeys[KeyIdx], my_bool)= (my_bool)(DSN_OPTION(Dsn, DsnKeys[KeyIdx].FlagValue) ? 1 : 0);
}
++i;
}
return TRUE;
}
return FALSE;
}
/* }}} */
my_bool MADB_DSN_Exists(const char *DsnName)
{
my_bool ret;
char buffer[1024];
char *p= "";
if (!DsnName)
return FALSE;
ret= (SQLGetPrivateProfileString(DsnName, NULL, p, buffer, 1024, "ODBC.INI") > 0);
return ret;
}
/* {{{ MADB_SaveDSN */
my_bool MADB_SaveDSN(MADB_Dsn *Dsn)
{
int i= 1;
char Value[32];
my_bool ret;
DWORD ErrNum;
if (!SQLValidDSN(Dsn->DSNName))
{
strcpy_s(Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, "Invalid Data Source Name");
return FALSE;
}
if (!SQLRemoveDSNFromIni(Dsn->DSNName))
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
if (!SQLWriteDSNToIni(Dsn->DSNName, Dsn->Driver))
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
while(DsnKeys[i].DsnKey)
{
/* Skipping aliases - options are saved by primary name only */
if (!DsnKeys[i].IsAlias)
{
ret= TRUE;
/* We do not save DSN_TYPE_OPTION - they are saved as OPTIONS bits */
switch (DsnKeys[i].Type) {
case DSN_TYPE_BOOL:
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey,
*GET_FIELD_PTR(Dsn, &DsnKeys[i], my_bool) ? "1" : "0", "ODBC.INI");
break;
case DSN_TYPE_INT:
{
_snprintf(Value ,32, "%d", *(int *)((char *)Dsn + DsnKeys[i].DsnOffset));
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, Value, "ODBC.INI");
}
break;
case DSN_TYPE_CBOXGROUP:
{
_snprintf(Value, 32, "%hu", (short)*GET_FIELD_PTR(Dsn, &DsnKeys[i], char));
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, Value, "ODBC.INI");
}
break;
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
{
char *Val= *GET_FIELD_PTR(Dsn, &DsnKeys[i], char*);
if (Val && Val[0])
ret= SQLWritePrivateProfileString(Dsn->DSNName, DsnKeys[i].DsnKey, Val, "ODBC.INI");
}
default:
/* To avoid warning with some compilers */
break;
} /* switch */
if (!ret)
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
}
i++;
}
/* Save Options */
_snprintf(Value ,32, "%d", Dsn->Options);
if (!(ret= SQLWritePrivateProfileString(Dsn->DSNName, "OPTIONS", Value, "ODBC.INI")))
{
SQLInstallerError(1,&ErrNum, Dsn->ErrorMsg, SQL_MAX_MESSAGE_LENGTH, NULL);
return FALSE;
}
return TRUE;
}
/* }}} */
size_t ConnStringLength(const char * String, char Delimiter)
{
size_t result= strlen(String);
const char *p= String + result + 1;
if (Delimiter != '\0')
{
return result;
}
/* else - we have string with null terminated key=value pairs with additional NULL after last pair */
while (*p)
{
p+= strlen(p) + 1;
}
return p - String /* Length without ending NULL */;
}
/* {{{ MADB_ParseConnString */
my_bool MADB_ParseConnString(MADB_Dsn *Dsn, const char *String, size_t Length, char Delimiter)
{
char *Buffer, *Key, *Value;
my_bool ret;
if (!String)
return FALSE;
if (Length == SQL_NTS)
{
Length= ConnStringLength(String, Delimiter);
}
Buffer= MADB_ALLOC(Length + 1);
Buffer= memcpy(Buffer, String, Length + 1);
Key= Buffer;
while (Key && Key < ((char *)Buffer + Length))
{
int i= 0;
if (!(Value= strchr(Key, '=')))
{
ret= FALSE;
break;
}
*Value= 0;
++Value;
Key= trim(Key);
while (DsnKeys[i].DsnKey)
{
if (_stricmp(DsnKeys[i].DsnKey, Key) == 0)
{
char *p;
my_bool special= FALSE;
if (DsnKeys[i].IsAlias)
{
i= DsnKeys[i].DsnOffset; /* For aliases DsnOffset is index of aliased "main" key */
}
Value= trim(Value);
if (Value[0] == '{')
{
++Value;
if ((p = strchr(Value, '}')))
{
*p= 0;
special= TRUE;
}
}
else if ((p= strchr(Value, Delimiter)))
{
*p= 0;
}
Value= trim(Value);
/* Overwriting here - if an option repeated more than once in the string, its last entrance will determine the value */
if (!MADB_DsnStoreValue(Dsn, i, Value, TRUE))
return FALSE;
if (IS_OPTIONS_BITMAP(i))
{
MADB_DsnUpdateOptionsFields(Dsn);
}
if (p)
{
*p= (special) ? ' ' : Delimiter;
}
break;
}
++i;
}
if ((Key= strchr(Value, Delimiter)))
{
++Key;
}
}
MADB_FREE(Buffer);
return TRUE;
}
/* }}} */
/* {{{ MADB_ReadConnString */
/* Like ParseConnString, but expands DSN if needed, preserving connection string values precedence.
Or in other words - it is combination of ReadDsn and ParseConnString */
BOOL MADB_ReadConnString(MADB_Dsn *Dsn, const char *String, size_t Length, char Delimiter)
{
/* Basically at this point we need DSN name only */
if (!MADB_ParseConnString(Dsn, String, Length, Delimiter))
{
return FALSE;
}
/* "If the connection string contains the DRIVER keyword, the driver cannot retrieve information about the data source
from the system information." https://msdn.microsoft.com/en-us/library/ms715433%28v=vs.85%29.aspx */
if (Dsn->DSNName && MADB_IS_EMPTY(Dsn->Driver))
{
MADB_ReadDSN(Dsn, NULL, FALSE);
/* This redundancy is needed to be able to reset options set in the DSN, e.g. if DSN has Reconnect option selected, and
connection string has AUTO_RECONNECT=0. Connection string should have precedence */
MADB_ParseConnString(Dsn, String, Length, Delimiter);
}
return TRUE;
}
/* }}} */
/* {{{ MADB_DsnToString */
SQLULEN MADB_DsnToString(MADB_Dsn *Dsn, char *OutString, SQLULEN OutLength)
{
int i= 0;
SQLULEN TotalLength= 0;
char *p= OutString;
char *Value= NULL;
char TmpStr[1024];
char IntVal[12];
int CpyLength;
if (OutLength && OutString)
OutString[0]= '\0';
while (DsnKeys[i].DsnKey)
{
Value= NULL;
if (!DsnKeys[i].IsAlias)
{
switch (DsnKeys[i].Type) {
case DSN_TYPE_STRING:
case DSN_TYPE_COMBO:
Value= *GET_FIELD_PTR(Dsn, &DsnKeys[i], char*);
if (MADB_IS_EMPTY(Value))
{
++i;
continue;
}
break;
case DSN_TYPE_INT:
if (*GET_FIELD_PTR(Dsn, &DsnKeys[i], int))
{
_snprintf(IntVal, sizeof(IntVal), "%d", *GET_FIELD_PTR(Dsn, &DsnKeys[i], int));
Value= IntVal;
}
break;
case DSN_TYPE_BOOL:
if (*GET_FIELD_PTR(Dsn, &DsnKeys[i], my_bool))
{
Value= "1";
}
case DSN_TYPE_CBOXGROUP:
if (*GET_FIELD_PTR(Dsn, &DsnKeys[i], char))
{
_snprintf(IntVal, sizeof(IntVal), "%hu", (short)*GET_FIELD_PTR(Dsn, &DsnKeys[i], char));
Value= IntVal;
}
break;
default:
/* To avoid warning with some compilers */
break;
}
}
if (Value)
{
my_bool isSpecial= (strchr(Value, ' ') || strchr(Value, ';') || strchr(Value, '@'));
CpyLength= _snprintf(TmpStr + TotalLength, 1024 - TotalLength, "%s%s=%s%s%s", (TotalLength) ? ";" : "",
DsnKeys[i].DsnKey, isSpecial ? "{" : "", Value, isSpecial ? "}" : "");
TotalLength+= CpyLength;
}
++i;
}
if (OutLength && OutString)
{
strncpy_s(OutString, OutLength, TmpStr, TotalLength);
}
return TotalLength;
}
/* }}} */
C
1
https://gitee.com/openlookeng/hetu-odbc-driver.git
git@gitee.com:openlookeng/hetu-odbc-driver.git
openlookeng
hetu-odbc-driver
hetu-odbc-driver
master

搜索帮助