Skip to content

Commit

Permalink
#86 : Adaptation to linux platform
Browse files Browse the repository at this point in the history
  • Loading branch information
s-takeuchi committed Oct 22, 2024
1 parent e7ad2ff commit f5630d9
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions src/restapi/DbMariaDbAccessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ DbMariaDbAccessor::~DbMariaDbAccessor()

void DbMariaDbAccessor::GetDefaultConnStr(SQLTCHAR DefConnStr[Global::MAX_PARAM_LENGTH])
{
lstrcpy(DefConnStr, L"Driver={MariaDB ODBC 3.1 Driver};Server=localhost;UID=UID;PWD=PWD;DB=DATABASE_NAME;Port=3306;");
StkPlLStrCpy((wchar_t*)DefConnStr, L"Driver={MariaDB ODBC 3.1 Driver};Server=localhost;UID=UID;PWD=PWD;DB=DATABASE_NAME;Port=3306;");
}

int DbMariaDbAccessor::GetNumOfRecords(SQLTCHAR* TableName, SQLTCHAR StateMsg[10], SQLTCHAR* Msg, SQLSMALLINT MsgLen)
{
int LenOfTableName = lstrlen((wchar_t*)TableName);
size_t LenOfTableName = StkPlWcsLen((wchar_t*)TableName);
SQLTCHAR* EcdTableName = new SQLTCHAR[LenOfTableName * 4 + 2];
SqlEncoding(TableName, EcdTableName, TYPE_KEY);

Expand All @@ -32,11 +32,11 @@ int DbMariaDbAccessor::GetNumOfRecords(SQLTCHAR* TableName, SQLTCHAR StateMsg[10
bool FilterSwitch = DataAccess::GetInstance()->GetFilterSwitch();
for (int Loop = 1; Loop <= 5; Loop++) {
DataAccess::GetInstance()->GetFilterCondition(Loop, ColumnName[Loop - 1], &OpeType[Loop - 1], Value[Loop - 1]);
SqlEncoding(ColumnName[Loop - 1], ColumnNameCnv[Loop - 1], TYPE_KEY);
SqlEncoding((SQLTCHAR*)ColumnName[Loop - 1], (SQLTCHAR*)ColumnNameCnv[Loop - 1], TYPE_KEY);
if (FilterSwitch && (OpeType[Loop - 1] == 10 || OpeType[Loop - 1] == 11)) {
SqlEncoding(Value[Loop - 1], ValueCnv[Loop - 1], TYPE_LIKE_VALUE);
SqlEncoding((SQLTCHAR*)Value[Loop - 1], (SQLTCHAR*)ValueCnv[Loop - 1], TYPE_LIKE_VALUE);
} else {
SqlEncoding(Value[Loop - 1], ValueCnv[Loop - 1], TYPE_VALUE);
SqlEncoding((SQLTCHAR*)Value[Loop - 1], (SQLTCHAR*)ValueCnv[Loop - 1], TYPE_VALUE);
}
}

Expand All @@ -52,11 +52,11 @@ SQLRETURN DbMariaDbAccessor::GetTables(StkObject* Obj, SQLTCHAR StateMsg[10], SQ
wchar_t ConnStr[256];
int Init;
int DbmsType = DataAccess::GetInstance()->GetOdbcConfing(ConnStr, &Init);
Ret = OpenDatabase(ConnStr, StateMsg, Msg, MsgLen);
Ret = OpenDatabase((SQLTCHAR*)ConnStr, StateMsg, Msg, MsgLen);
if (Ret != SQL_SUCCESS) {
return Ret;
}
Ret = GetTablesCommon(L"show tables;", Obj, StateMsg, Msg, MsgLen);
Ret = GetTablesCommon((SQLTCHAR*)L"show tables;", Obj, StateMsg, Msg, MsgLen);
Ret = CloseDatabase(StateMsg, Msg, MsgLen);

return Ret;
Expand All @@ -71,17 +71,17 @@ int DbMariaDbAccessor::GetColumnInfoByTableName(SQLTCHAR* TableName, StkObject*
wchar_t ConnStr[256];
int Init;
int DbmsType = DataAccess::GetInstance()->GetOdbcConfing(ConnStr, &Init);
Ret = OpenDatabase(ConnStr, StateMsg, Msg, MsgLen);
Ret = OpenDatabase((SQLTCHAR*)ConnStr, StateMsg, Msg, MsgLen);
if (Ret != SQL_SUCCESS) {
return 0;
}

int LenOfTableName = lstrlen((wchar_t*)TableName);
size_t LenOfTableName = StkPlWcsLen((wchar_t*)TableName);
SQLTCHAR* EcdTableName = new SQLTCHAR[LenOfTableName * 4 + 2];
SqlEncoding(TableName, EcdTableName, TYPE_KEY);

SQLTCHAR SqlBuf[1024];
StkPlSwPrintf(SqlBuf, 1024, L"show full columns from %ls;", EcdTableName);
StkPlSwPrintf((wchar_t*)SqlBuf, 1024, L"show full columns from %ls;", (wchar_t*)EcdTableName);
Ret = SQLExecDirect(Hstmt, SqlBuf, SQL_NTS);
delete EcdTableName;
if (Ret != SQL_SUCCESS) {
Expand All @@ -108,16 +108,16 @@ int DbMariaDbAccessor::GetColumnInfoByTableName(SQLTCHAR* TableName, StkObject*
SQLGetDiagRec(SQL_HANDLE_STMT, Hstmt, 1, StateMsg, &Native, Msg, MsgLen, &ActualMsgLen);
return 0;
}
lstrcpyn(TmpColumnName, TmpColumnNameTmp, Global::COLUMNNAME_LENGTH); // For adaptation to the bug of MariaDB ODBC connector
lstrcpyn(TmpColumnType, TmpColumnTypeTmp, Global::COLUMNTYPE_LENGTH); // For adaptation to the bug of MariaDB ODBC connector
StkPlWcsCpy((wchar_t*)TmpColumnName, (size_t)Global::COLUMNNAME_LENGTH, (wchar_t*)TmpColumnNameTmp); // For adaptation to the bug of MariaDB ODBC connector
StkPlWcsCpy((wchar_t*)TmpColumnType, (size_t)Global::COLUMNTYPE_LENGTH, (wchar_t*)TmpColumnTypeTmp); // For adaptation to the bug of MariaDB ODBC connector
ConvertAttrType(TmpColumnType, ColTypeCnv);
StkObject* ClmObj = new StkObject(L"ColumnInfo");
ClmObj->AppendChildElement(new StkObject(L"title", TmpColumnName));
ClmObj->AppendChildElement(new StkObject(L"title", (wchar_t*)TmpColumnName));
ClmObj->AppendChildElement(new StkObject(L"width", 100));
ClmObj->AppendChildElement(new StkObject(L"dataType", ColTypeCnv));
ClmObj->AppendChildElement(new StkObject(L"dataType", (wchar_t*)ColTypeCnv));
ClmObj->AppendChildElement(new StkObject(L"dataIndx", Loop));
ClmObj->AppendChildElement(new StkObject(L"coltype", TmpColumnType));
ClmObj->AppendChildElement(new StkObject(L"isnull", TmpIsNull));
ClmObj->AppendChildElement(new StkObject(L"coltype", (wchar_t*)TmpColumnType));
ClmObj->AppendChildElement(new StkObject(L"isnull", (wchar_t*)TmpIsNull));
TblObj->AppendChildElement(ClmObj);
}
Ret = CloseDatabase(StateMsg, Msg, MsgLen);
Expand All @@ -131,19 +131,19 @@ int DbMariaDbAccessor::GetRecordsByTableName(SQLTCHAR* TableName, int NumOfCols,
wchar_t ConnStr[256];
int Init;

int LenOfTableName = lstrlen((wchar_t*)TableName);
size_t LenOfTableName = StkPlWcsLen((wchar_t*)TableName);
SQLTCHAR* EcdTableName = new SQLTCHAR[LenOfTableName * 4 + 2];
SqlEncoding(TableName, EcdTableName, TYPE_KEY);

SQLTCHAR* EcdSortTarget = NULL;
if (SortTarget != NULL && *SortTarget != L'\0') {
int LenOfSortTarget = lstrlen((wchar_t*)SortTarget);
size_t LenOfSortTarget = StkPlWcsLen((wchar_t*)SortTarget);
EcdSortTarget = new SQLTCHAR[LenOfSortTarget * 4 + 2];
SqlEncoding(SortTarget, EcdSortTarget, TYPE_KEY);
}

int DbmsType = DataAccess::GetInstance()->GetOdbcConfing(ConnStr, &Init);
Ret = OpenDatabase(ConnStr, StateMsg, Msg, MsgLen);
Ret = OpenDatabase((SQLTCHAR*)ConnStr, StateMsg, Msg, MsgLen);

wchar_t ColumnName[5][Global::COLUMNNAME_LENGTH];
wchar_t ColumnNameCnv[5][Global::COLUMNNAME_LENGTH * 4 + 2];
Expand All @@ -153,15 +153,15 @@ int DbMariaDbAccessor::GetRecordsByTableName(SQLTCHAR* TableName, int NumOfCols,
bool FilterSwitch = DataAccess::GetInstance()->GetFilterSwitch();
for (int Loop = 1; Loop <= 5; Loop++) {
DataAccess::GetInstance()->GetFilterCondition(Loop, ColumnName[Loop - 1], &OpeType[Loop - 1], Value[Loop - 1]);
SqlEncoding(ColumnName[Loop - 1], ColumnNameCnv[Loop - 1], TYPE_KEY);
SqlEncoding((SQLTCHAR*)ColumnName[Loop - 1], (SQLTCHAR*)ColumnNameCnv[Loop - 1], TYPE_KEY);
if (FilterSwitch && (OpeType[Loop - 1] == 10 || OpeType[Loop - 1] == 11)) {
SqlEncoding(Value[Loop - 1], ValueCnv[Loop - 1], TYPE_LIKE_VALUE);
SqlEncoding((SQLTCHAR*)Value[Loop - 1], (SQLTCHAR*)ValueCnv[Loop - 1], TYPE_LIKE_VALUE);
} else {
SqlEncoding(Value[Loop - 1], ValueCnv[Loop - 1], TYPE_VALUE);
SqlEncoding((SQLTCHAR*)Value[Loop - 1], (SQLTCHAR*)ValueCnv[Loop - 1], TYPE_VALUE);
}
}

int NumOfRecs = GetRecordsByTableNameCommon(EcdTableName, NumOfCols, DatObj, ColumnNameCnv, OpeType, ValueCnv, EcdSortTarget, SortOrder, Limit, Offset, StateMsg, Msg, MsgLen);
int NumOfRecs = GetRecordsByTableNameCommon(EcdTableName, NumOfCols, DatObj, ColumnNameCnv, OpeType, ValueCnv, (wchar_t*)EcdSortTarget, (wchar_t*)SortOrder, Limit, Offset, StateMsg, Msg, MsgLen);

delete EcdTableName;
if (EcdSortTarget != NULL) {
Expand All @@ -173,60 +173,60 @@ int DbMariaDbAccessor::GetRecordsByTableName(SQLTCHAR* TableName, int NumOfCols,

int DbMariaDbAccessor::ConvertAttrType(SQLTCHAR InAttr[Global::COLUMNTYPE_LENGTH], SQLTCHAR OutAttr[Global::COLUMNTYPE_LENGTH])
{
if (wcsstr(InAttr, L"bigint") != NULL ||
wcsstr(InAttr, L"int") != NULL ||
wcsstr(InAttr, L"mediumint") != NULL ||
wcsstr(InAttr, L"smallint") != NULL ||
wcsstr(InAttr, L"tinyint") != NULL) {
lstrcpy(OutAttr, L"integer");
if (StkPlWcsStr((wchar_t*)InAttr, L"bigint") != NULL ||
StkPlWcsStr((wchar_t*)InAttr, L"int") != NULL ||
StkPlWcsStr((wchar_t*)InAttr, L"mediumint") != NULL ||
StkPlWcsStr((wchar_t*)InAttr, L"smallint") != NULL ||
StkPlWcsStr((wchar_t*)InAttr, L"tinyint") != NULL) {
StkPlLStrCpy((wchar_t*)OutAttr, L"integer");
return 0;
}
if (wcsstr(InAttr, L"float") != NULL ||
wcsstr(InAttr, L"double") != NULL ||
wcsstr(InAttr, L"double precision") != NULL) {
lstrcpy(OutAttr, L"float");
if (StkPlWcsStr((wchar_t*)InAttr, L"float") != NULL ||
StkPlWcsStr((wchar_t*)InAttr, L"double") != NULL ||
StkPlWcsStr((wchar_t*)InAttr, L"double precision") != NULL) {
StkPlLStrCpy((wchar_t*)OutAttr, L"float");
return 0;
}
lstrcpy(OutAttr, L"string");
StkPlLStrCpy((wchar_t*)OutAttr, L"string");
return 0;
}

void DbMariaDbAccessor::SqlEncoding(SQLTCHAR* InSql, SQLTCHAR* OutSql, int Type)
{
int LenOfInSql = lstrlen(InSql);
size_t LenOfInSql = StkPlWcsLen((wchar_t*)InSql);
int OutSqlIndex = 0;
if (Type == TYPE_KEY) {
OutSql[0] = L'`';
OutSqlIndex++;
}
for (int Loop = 0; Loop < LenOfInSql; Loop++) {
if (InSql[Loop] == L'`' && Type == TYPE_KEY) {
lstrcpy(&OutSql[OutSqlIndex], L"``");
StkPlLStrCpy((wchar_t*) & OutSql[OutSqlIndex], L"``");
OutSqlIndex += 2;
continue;
}
if (InSql[Loop] == L'\'' && (Type == TYPE_VALUE || Type == TYPE_LIKE_VALUE)) {
lstrcpy(&OutSql[OutSqlIndex], L"\'\'");
StkPlLStrCpy((wchar_t*)&OutSql[OutSqlIndex], L"\'\'");
OutSqlIndex += 2;
continue;
}
if (InSql[Loop] == L'\\' && Type == TYPE_VALUE) {
lstrcpy(&OutSql[OutSqlIndex], L"\\\\");
StkPlLStrCpy((wchar_t*)&OutSql[OutSqlIndex], L"\\\\");
OutSqlIndex += 2;
continue;
}
if (InSql[Loop] == L'\\' && Type == TYPE_LIKE_VALUE) {
lstrcpy(&OutSql[OutSqlIndex], L"\\\\\\\\");
StkPlLStrCpy((wchar_t*)&OutSql[OutSqlIndex], L"\\\\\\\\");
OutSqlIndex += 4;
continue;
}
if (InSql[Loop] == L'%' && Type == TYPE_LIKE_VALUE) {
lstrcpy(&OutSql[OutSqlIndex], L"\\%");
StkPlLStrCpy((wchar_t*)&OutSql[OutSqlIndex], L"\\%");
OutSqlIndex += 2;
continue;
}
if (InSql[Loop] == L'_' && Type == TYPE_LIKE_VALUE) {
lstrcpy(&OutSql[OutSqlIndex], L"\\_");
StkPlLStrCpy((wchar_t*)&OutSql[OutSqlIndex], L"\\_");
OutSqlIndex += 2;
continue;
}
Expand Down

0 comments on commit f5630d9

Please sign in to comment.