#include "stdafx.h" #include // _KANJI_CP #include // The one and only application object CWinApp theApp; int test(bool bShiftJIS) { wchar_t wstrText[1024]; char strText[1024]; // Connect PGconn *pConn = PQsetdbLogin(NULL, NULL, NULL, NULL, "DEV308", "postgres", "********"); if (!pConn) return __LINE__; if (PQstatus(pConn) != CONNECTION_OK) return __LINE__; // Set Client encoding UINT codepage; if (bShiftJIS) { if (PQsetClientEncoding(pConn, "SJIS") != 0) return __LINE__; codepage = _KANJI_CP; } else { if (PQsetClientEncoding(pConn, "UTF-8") != 0) return __LINE__; codepage = CP_UTF8; } // Create table const wchar_t wstrCommand[] = L"create table 四宗(gid serial PRIMARY KEY,説明 text,アイテムid integer,アイテムクラス text,原点x double precision,原点y double precision,原点z double precision)"; if (::WideCharToMultiByte(codepage, 0, wstrCommand, -1, strText, _countof(strText), NULL, NULL) == 0) return __LINE__; PGresult *pResult = PQexec(pConn, strText); if (!pResult) return __LINE__; int stat = PQresultStatus(pResult); if (stat != PGRES_EMPTY_QUERY && stat != PGRES_COMMAND_OK && stat != PGRES_TUPLES_OK) return __LINE__; PQclear(pResult); // Query table const wchar_t wstrCommand2[] = L"select * from 四宗"; if (::WideCharToMultiByte(codepage, 0, wstrCommand2, -1, strText, _countof(strText), NULL, NULL) == 0) return __LINE__; pResult = PQexec(pConn, strText); if (!pResult) return __LINE__; stat = PQresultStatus(pResult); if (stat != PGRES_EMPTY_QUERY && stat != PGRES_COMMAND_OK && stat != PGRES_TUPLES_OK) { if (::MultiByteToWideChar(codepage, 0, PQresultErrorMessage(pResult), -1, wstrText, _countof(wstrText)) == 0) return __LINE__; ::wprintf(L"Query failed with error:\n%s\n", wstrText); } else { for (int i = 4; i < PQnfields(pResult); ++i) { const char *strFieldName = PQfname(pResult, i); if (::MultiByteToWideChar(codepage, 0, strFieldName, -1, wstrText, _countof(wstrText)) == 0) return __LINE__; //::wprintf(L"Field name %d is %s\n", i + 1, wstrText); if (wstrText[0] != L'原') { ::printf("PQfname(pResult, %d) returned incorrect field name.\n", i); } } } PQclear(pResult); // Drop table const wchar_t wstrCommand3[] = L"drop table 四宗"; if (::WideCharToMultiByte(codepage, 0, wstrCommand3, -1, strText, _countof(strText), NULL, NULL) == 0) return __LINE__; pResult = PQexec(pConn, strText); PQclear(pResult); PQfinish(pConn); return 0; } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); nRetCode = 1; } else { // TODO: code your application's behavior here. int rv; ::printf("Testing with encoding UTF-8:\n"); rv = test(false); if (rv) ::printf("Test failed on line %d.\n", rv); ::printf("\nTesting with encoding SJIS:\n"); rv = test(true); if (rv) ::printf("Test failed on line %d.\n", rv); } ::printf("\nPress any key to exit.\n"); getchar(); return nRetCode; }