1 #ifndef DBAPI_DRIVER_ODBC___DBAPI_DRIVER_ODBC_UTILS__HPP
2 #define DBAPI_DRIVER_ODBC___DBAPI_DRIVER_ODBC_UTILS__HPP
3 
4 /* $Id: odbc_utils.hpp 556250 2018-01-29 14:12:00Z ucko $
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *               National Center for Biotechnology Information
9  *
10  *  This software/database is a "United States Government Work" under the
11  *  terms of the United States Copyright Act.  It was written as part of
12  *  the author's official duties as a United States Government employee and
13  *  thus cannot be copyrighted.  This software/database is freely available
14  *  to the public for use. The National Library of Medicine and the U.S.
15  *  Government have not placed any restriction on its use or reproduction.
16  *
17  *  Although all reasonable efforts have been taken to ensure the accuracy
18  *  and reliability of the software and data, the NLM and the U.S.
19  *  Government do not and cannot warrant the performance or results that
20  *  may be obtained by using this software or data. The NLM and the U.S.
21  *  Government disclaim all warranties, express or implied, including
22  *  warranties of performance, merchantability or fitness for any particular
23  *  purpose.
24  *
25  *  Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author:  Sergey Sikorskiy
30  *
31  * File Description:  Small utility classes common to the odbc driver.
32  *
33  */
34 
35 #include <dbapi/driver/impl/dbapi_driver_utils.hpp>
36 #include <dbapi/driver/odbc/interfaces.hpp>
37 
38 BEGIN_NCBI_SCOPE
39 
40 /////////////////////////////////////////////////////////////////////////////
41 class CODBCString : public CWString
42 {
43 public:
44     explicit CODBCString(SQLCHAR* str,
45                          EEncoding enc = eEncoding_Unknown);
46     explicit CODBCString(SQLCHAR* str,
47                          SQLINTEGER size,
48                          EEncoding enc = eEncoding_Unknown);
49     explicit CODBCString(const char* str,
50                          string::size_type size = string::npos,
51                          EEncoding enc = eEncoding_Unknown);
52     explicit CODBCString(const string& str, EEncoding enc = eEncoding_Unknown);
53 #ifdef HAVE_WSTRING
54     // Seconnd parameter is redundant and will be ignored,
55     // but we need it as syntactical sugar.
56     explicit CODBCString(SQLWCHAR* str,
57                          EEncoding enc = eEncoding_Unknown);
58     // Seconnd parameter is redundant and will be ignored,
59     // but we need it as syntactical sugar.
60     explicit CODBCString(const wchar_t* str,
61                          wstring::size_type size = wstring::npos,
62                          EEncoding enc = eEncoding_Unknown);
63     // Seconnd parameter is redundant and will be ignored,
64     // but we need it as syntactical sugar.
65     explicit CODBCString(const wstring& str,
66                          EEncoding enc = eEncoding_Unknown);
67 #endif
68     ~CODBCString(void);
69 
70 public:
operator LPCSTR(void) const71     operator LPCSTR(void) const
72     {
73         if (!(GetAvailableValueType() & eChar)) {
74             x_MakeString();
75         }
76 
77         return reinterpret_cast<LPCSTR>(m_Char);
78     }
operator SQLCHAR*(void) const79     operator SQLCHAR*(void) const
80     {
81         if (!(GetAvailableValueType() & eChar)) {
82             x_MakeString();
83         }
84 
85         return const_cast<SQLCHAR*>(reinterpret_cast<const SQLCHAR*>(m_Char));
86     }
operator const SQLCHAR*(void) const87     operator const SQLCHAR*(void) const
88     {
89         if (!(GetAvailableValueType() & eChar)) {
90             x_MakeString();
91         }
92 
93         return reinterpret_cast<const SQLCHAR*>(m_Char);
94     }
95 };
96 
97 /////////////////////////////////////////////////////////////////////////////
98 #if defined(UNICODE)
99 #  define _T_NCBI_ODBC(x) L ## x
100 #else
101 #  define _T_NCBI_ODBC(x) x
102 #endif
103 
104 using odbc::TSqlChar;
105 typedef CGenericSqlString<TSqlChar> TSqlString;
106 inline
x_MakeTSqlString(const CTempString & s,EEncoding enc)107 TSqlString x_MakeTSqlString(const CTempString& s, EEncoding enc)
108 {
109 #if defined(UNICODE)  ||  defined(_UNICODE)  ||  defined(UCS2)
110     return CUtf8::AsBasicString<TSqlChar>(CUtf8::AsUTF8(s, enc));
111 #else
112     if (enc == eEncoding_Unknown  ||  enc == eEncoding_UTF8) {
113         return CUtf8::AsSingleByteString(CUtf8::AsUTF8(s, enc),
114                                          eEncoding_ISO8859_1);
115     } else {
116         return CSqlString(s.data(), s.size());
117     }
118 #endif
119 }
120 
121 
122 #ifdef HAVE_WSTRING
123 inline
operator +(const wstring & str1,const string & str2)124 wstring operator+(const wstring& str1, const string& str2)
125 {
126     return str1 + CUtf8::AsBasicString<wchar_t>( CUtf8::AsUTF8(str2, eEncoding_ISO8859_1));
127 }
128 #endif
129 
130 namespace util
131 {
132     inline
strncmp(const char * str1,const char * str2,size_t count)133     int strncmp(const char* str1, const char* str2, size_t count)
134     {
135         return ::strncmp(str1, str2, count);
136     }
137 
138 #ifdef HAVE_WSTRING
139     inline
strncmp(const wchar_t * str1,const wchar_t * str2,size_t count)140     int strncmp(const wchar_t* str1, const wchar_t* str2, size_t count)
141     {
142         return ::wcsncmp(str1, str2, count);
143     }
144 #endif
145 
146     inline
strncmp(const SQLCHAR * str1,const char * str2,size_t count)147     int strncmp(const SQLCHAR* str1, const char* str2, size_t count)
148     {
149         return strncmp((const char*)str1, str2, count);
150     }
151 
152     inline
strncmp(const char * str1,const SQLCHAR * str2,size_t count)153     int strncmp(const char* str1, const SQLCHAR* str2, size_t count)
154     {
155         return strncmp(str1, (const char*)str2, count);
156     }
157 
158     ///////////////////////////////////////////////////////////////////////////
159     inline
strcmp(const char * str1,const char * str2)160     int strcmp(const char* str1, const char* str2)
161     {
162         return ::strcmp(str1, str2);
163     }
164 
165 #ifdef HAVE_WSTRING
166     inline
strcmp(const wchar_t * str1,const wchar_t * str2)167     int strcmp(const wchar_t* str1, const wchar_t* str2)
168     {
169         return ::wcscmp(str1, str2);
170     }
171 #endif
172 
173 }
174 
175 
176 extern "C"
177 {
178 
179 NCBI_DBAPIDRIVER_ODBC_EXPORT
180 void
181 NCBI_EntryPoint_xdbapi_odbc(
182     CPluginManager<I_DriverContext>::TDriverInfoList&   info_list,
183     CPluginManager<I_DriverContext>::EEntryPointRequest method);
184 
185 } // extern C
186 
187 
188 END_NCBI_SCOPE
189 
190 
191 #endif // DBAPI_DRIVER_ODBC___DBAPI_DRIVER_ODBC_UTILS__HPP
192 
193 
194