1 /*  $Id: dbapi_unit_test_pch.hpp 583697 2019-04-01 15:00:36Z ucko $
2  * ===========================================================================
3  *
4  *                            PUBLIC DOMAIN NOTICE
5  *               National Center for Biotechnology Information
6  *
7  *  This software/database is a "United States Government Work" under the
8  *  terms of the United States Copyright Act.  It was written as part of
9  *  the author's official duties as a United States Government employee and
10  *  thus cannot be copyrighted.  This software/database is freely available
11  *  to the public for use. The National Library of Medicine and the U.S.
12  *  Government have not placed any restriction on its use or reproduction.
13  *
14  *  Although all reasonable efforts have been taken to ensure the accuracy
15  *  and reliability of the software and data, the NLM and the U.S.
16  *  Government do not and cannot warrant the performance or results that
17  *  may be obtained by using this software or data. The NLM and the U.S.
18  *  Government disclaim all warranties, express or implied, including
19  *  warranties of performance, merchantability or fitness for any particular
20  *  purpose.
21  *
22  *  Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Sergey Sikorskiy
27  *
28  * File Description: DBAPI unit-test
29  *
30  * ===========================================================================
31  */
32 
33 #ifndef DBAPI_UNIT_TEST_H
34 #define DBAPI_UNIT_TEST_H
35 
36 #include <corelib/ncbiapp.hpp>
37 #include <corelib/ncbiargs.hpp>
38 #include <corelib/ncbienv.hpp>
39 
40 #include <corelib/test_boost.hpp>
41 
42 #include <dbapi/dbapi.hpp>
43 #include <corelib/impl/ncbi_dbsvcmapper.hpp>
44 
45 #include <dbapi/driver/dbapi_driver_conn_params.hpp>
46 
47 
48 // Note: there are a few important details in case of a CDB_TimeoutEx for the
49 // DBAPI_BOOST_FAIL() macro below.
50 // - It is not thread safe because the error message is saved in a static
51 //   buffer. This is however a rare case.
52 // - The static buffer is required to avoid illegal reading detected by
53 //   valgrind in case if BOOST_TIMEOUT(string(ex.what())); is used.
54 //   The BOOST_TIMEOUT macro is eventually expanded as
55 //   throw boost::execution_exception(...); and this particular boost exception
56 //   works well only with constants or a some kind of persistent memory.
57 //   See here:
58 //   http://www.boost.org/doc/libs/1_61_0/libs/test/doc/html/boost/execution_exception.html
59 //   So a static buffer looks good enough to avoid unnecessary valgrind
60 //   complains.
61 #define DBAPI_BOOST_FAIL(ex)                                                \
62     do {                                                                    \
63         const CDB_TimeoutEx* to = dynamic_cast<const CDB_TimeoutEx*>(&ex);  \
64         if (to) {                                                           \
65             const size_t    buf_size = 8192;                                \
66             static char     buf[buf_size];                                  \
67             strncpy(buf, ex.what(), buf_size - 1);                          \
68             BOOST_TIMEOUT(buf);                                             \
69         }                                                                   \
70         else                                                                \
71             BOOST_FAIL(string(ex.what()));                                  \
72     } while (0)                                                             \
73 /**/
74 
75 #define CONN_OWNERSHIP  eTakeOwnership
76 
77 BEGIN_NCBI_SCOPE
78 
79 // Forward declaration
80 class CDriverManager;
81 class IDataSource;
82 class IConnection;
83 class IStatement;
84 class IDBConnectionFactory;
85 
86 enum ETransBehavior { eNoTrans, eTransCommit, eTransRollback };
87 enum { max_text_size = 8000 };
88 
89 class CTestTransaction
90 {
91 public:
92     CTestTransaction(IConnection& conn, ETransBehavior tb = eTransCommit);
93     ~CTestTransaction(void);
94 
95 public:
GetConnection(void)96     IConnection& GetConnection(void)
97     {
98         _ASSERT(m_Conn);
99         return *m_Conn;
100     }
101 
102 private:
103     IConnection* m_Conn;
104     ETransBehavior  m_TransBehavior;
105 };
106 
107 /////////////////////////////////////////////////////////////////////////////
108 class CUnitTestParams : public CDBConnParamsDelegate
109 {
110 public:
111     CUnitTestParams(const CDBConnParams& other);
112     virtual ~CUnitTestParams(void);
113 
114 public:
115     virtual string GetServerName(void) const;
116     virtual EServerType GetServerType(void) const;
117 
118 private:
119     // Non-copyable.
120     CUnitTestParams(const CUnitTestParams& other);
121     CUnitTestParams& operator =(const CUnitTestParams& other);
122 };
123 
124 
125 ///////////////////////////////////////////////////////////////////////////
126 class CTestArguments : public CObject
127 {
128 public:
129     CTestArguments(void);
130 
131 public:
132     typedef map<string, string> TDatabaseParameters;
133 
134     enum ETestConfiguration {
135         eWithExceptions,
136         eWithoutExceptions,
137         eFast
138     };
139 
GetDriverName(void) const140     string GetDriverName(void) const
141     {
142         return GetTestParams().GetDriverName();
143     }
144 
GetServerName(void) const145     string GetServerName(void) const
146     {
147         return GetTestParams().GetServerName();
148     }
149 
GetUserName(void) const150     string GetUserName(void) const
151     {
152         return GetTestParams().GetUserName();
153     }
154 
GetUserPassword(void) const155     string GetUserPassword(void) const
156     {
157         return GetTestParams().GetPassword();
158     }
159 
GetDatabaseName(void) const160     string GetDatabaseName(void) const
161     {
162         return GetTestParams().GetDatabaseName();
163     }
164 
GetServerType(void) const165     CDBConnParams::EServerType GetServerType(void) const
166     {
167         return GetTestParams().GetServerType();
168     }
169 
170     string GetProgramBasename(void) const;
171 
172     bool IsBCPAvailable(void) const;
173 
174     bool IsFreeTDS(void) const;
175     bool IsODBCBased(void) const;
176     bool DriverAllowsMultipleContexts(void) const;
177 
UseGateway(void) const178     bool UseGateway(void) const
179     {
180         return !m_GatewayHost.empty();
181     }
182 
GetTestConfiguration(void) const183     ETestConfiguration GetTestConfiguration(void) const
184     {
185         return m_TestConfiguration;
186     }
187 
188     void PutMsgDisabled(const char* msg) const;
189     void PutMsgExpected(const char* msg, const char* replacement) const;
190 
GetConnParams(void) const191     const CDBConnParams& GetConnParams(void) const
192     {
193         return m_ConnParams;
194     }
195 
196 private:
197     void SetDatabaseParameters(void);
198 
GetTestParams(void) const199     const CDBConnParams& GetTestParams(void) const
200     {
201         return m_ConnParams2;
202     }
203 
204 private:
205     string m_GatewayHost;
206     string m_GatewayPort;
207 
208     ETestConfiguration  m_TestConfiguration;
209 
210     mutable unsigned int m_NumOfDisabled;
211     bool m_ReportDisabled;
212     bool m_ReportExpected;
213 
214     CDBConnParamsBase m_ParamBase;
215     CDBConnParamsBase m_ParamBase2;
216     CUnitTestParams m_ConnParams;
217     CCPPToolkitConnParams m_CPPParams;
218     CUnitTestParams m_ConnParams2;
219     // CCPPToolkitConnParams m_CPPParams;
220     // CUnitTestParams m_ConnParams;
221 };
222 
223 
224 const string& GetTableName(void);
225 IConnection& GetConnection(void);
226 IDataSource& GetDS(void);
227 const CTestArguments& GetArgs(void);
228 int GetMaxVarcharSize(void);
229 
230 inline
GetDM(void)231 CDriverManager& GetDM(void)
232 {
233     return CDriverManager::GetInstance();
234 }
235 
236 void DumpResults(IStatement* const stmt);
237 size_t GetNumOfRecords(const unique_ptr<IStatement>& auto_stmt,
238                        const string& table_name);
239 size_t GetNumOfRecords(const unique_ptr<ICallableStatement>& auto_stmt);
240 Int8 GetIdentity(const unique_ptr<IStatement>& auto_stmt);
241 
242 
243 ///////////////////////////////////////////////////////////////////////////
244 class CDBSetConnParams : public CDBConnParamsDelegate
245 {
246 public:
247     CDBSetConnParams(
248         const string& server_name,
249         const string& user_name,
250         const string& password,
251         Uint4 tds_version,
252         const CDBConnParams& other);
253     virtual ~CDBSetConnParams(void);
254 
255 public:
256     virtual Uint4  GetProtocolVersion(void) const;
257 
258     virtual string GetServerName(void) const;
259     virtual string GetUserName(void) const;
260     virtual string GetPassword(void) const;
261 
262 private:
263     // Non-copyable.
264     CDBSetConnParams(const CDBSetConnParams& other);
265     CDBSetConnParams& operator =(const CDBSetConnParams& other);
266 
267 private:
268     const Uint4     m_ProtocolVersion;
269     const string    m_ServerName;
270     const string    m_UserName;
271     const string    m_Password;
272 };
273 
274 
275 ///////////////////////////////////////////////////////////////////////////
276 extern const char* ftds95_driver;
277 extern const char* ftds100_driver;
278 extern const char* ftds_driver;
279 
280 extern const char* odbc_driver;
281 extern const char* ctlib_driver;
282 
283 extern const char* msg_record_expected;
284 
285 static const TBlobOStreamFlags kBOSFlags = (fBOS_UseTransaction |
286                                             fBOS_SkipLogging);
287 
288 string GetSybaseClientVersion(void);
289 
290 END_NCBI_SCOPE
291 
292 #endif  // DBAPI_UNIT_TEST_H
293 
294 
295