fintp_udal
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ODBCDatabase.h
Go to the documentation of this file.
1 /*
2 * FinTP - Financial Transactions Processing Application
3 * Copyright (C) 2013 Business Information Systems (Allevo) S.R.L.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 * or contact Allevo at : 031281 Bucuresti, 23C Calea Vitan, Romania,
18 * phone +40212554577, office@allevo.ro <mailto:office@allevo.ro>, www.allevo.ro.
19 */
20 
21 #ifndef ODBCDATABASE_H
22 #define ODBCDATABASE_H
23 
24 #include "../Database.h"
25 #include "../ConnectionString.h"
26 #include "ODBCParameter.h"
27 #include "ODBCColumn.h"
28 #include <pthread.h>
29 
30 #include <map>
31 #include <string>
32 
33 #ifdef DB2_ONLY
34 #include <SQLLIB\include\sqlcli.h>
35 #include <SQLLIB\include\sqlext.h>
36 #include <SQLLIB\include\sqlcli1.h>
37 #elif defined INFORMIX_ONLY
38 #ifdef WIN32
39 #include <Informix\Client-SDK\incl\cli\sql.h>
40 #include <Informix\Client-SDK\incl\cli\sqlext.h>
41 #else
42 #include <infxcli.h>
43 #include <infxsql.h>
44 #endif
45 #else
46 #include <sql.h>
47 #include <sqlext.h>
48 #endif
49 
50 using namespace std;
51 
52 namespace FinTP
53 {
75  {
76  public :
77 
78  ODBCDatabase();
79  virtual ~ODBCDatabase();
80 
81  // ODBC overrides for transaction support
82  virtual void BeginTransaction( const bool readonly );
83  virtual void EndTransaction( const TransactionType::TRANSACTION_TYPE transactionType, const bool throwOnError );
84 
89  void Connect( const ConnectionString& connectionString );
90 
94  void Disconnect();
95 
96  // Gets the connected/disconnected status
97  virtual bool IsConnected() {
98  return (isDbcAllocated() & m_IsConnected);
99  };
100 
107  void ExecuteNonQuery( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, bool onCursor )
108  {
109  DataCommand nonquery( commType, DataCommand::NONQUERY, stringStatement, false );
110  ParametersVector tempParams;
111  (void)innerExecuteCommand( nonquery, tempParams, onCursor, 0 );
112  }
113 
120  void ExecuteNonQueryCached( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, bool onCursor )
121  {
122  DataCommand nonquery( commType, DataCommand::NONQUERY, stringStatement, true );
123  ParametersVector tempParams;
124  (void)innerExecuteCommand( nonquery, tempParams, onCursor, 0 );
125  }
126 
134  void ExecuteNonQuery( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, const ParametersVector& vectorOfParameters, bool onCursor )
135  {
136  DataCommand nonquery( commType, DataCommand::NONQUERY, stringStatement, false );
137  (void)innerExecuteCommand( nonquery, vectorOfParameters, onCursor, 0 );
138  }
139 
147  void ExecuteNonQueryCached( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, const ParametersVector& vectorOfParameters, bool onCursor )
148  {
149  DataCommand nonquery( commType, DataCommand::NONQUERY, stringStatement, true );
150  (void)innerExecuteCommand( nonquery, vectorOfParameters, onCursor, 0 );
151  }
152 
160  DataSet* ExecuteQuery( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, bool onCursor, const unsigned int fetchRows )
161  {
162  DataCommand query( commType, DataCommand::QUERY, stringStatement, false );
163  ParametersVector tempParams;
164  return innerExecuteCommand( query, tempParams, onCursor, fetchRows );
165  }
166 
175  DataSet* ExecuteQuery( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, const ParametersVector& vectorOfParameters, bool onCursor, const unsigned int fetchRows )
176  {
177  DataCommand query( commType, DataCommand::QUERY, stringStatement, false );
178  return innerExecuteCommand( query, vectorOfParameters, onCursor, fetchRows );
179  }
180 
188  DataSet* ExecuteQueryCached( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, const bool onCursor, const unsigned int fetchRows )
189  {
190  DataCommand queryCached( commType, DataCommand::QUERY, stringStatement, true );
191  ParametersVector tempParams;
192  return innerExecuteCommand( queryCached, tempParams, onCursor, fetchRows );
193  }
194 
203  DataSet* ExecuteQueryCached( const DataCommand::COMMAND_TYPE commType, const string& stringStatement, const ParametersVector& vectorOfParameters, const bool onCursor, const unsigned int fetchRows )
204  {
205  DataCommand queryCached( commType, DataCommand::QUERY, stringStatement, true );
206  return innerExecuteCommand( queryCached, vectorOfParameters, onCursor, fetchRows );
207  }
208 
209  void ReleaseCursor( const bool checkConn );
210  void RewindCursor();
211  bool CursorHeld() const {
212  return m_HoldCursorInvoked;
213  }
214 
215  protected :
216 
217  // ODBC specific Environment helper functions
218 
219  SQLHANDLE m_Henv;
221  bool isEnvAllocated() const {
222  return m_HenvAllocated;
223  }
225 
226  SQLHANDLE m_Hdbc;
228  SQLHANDLE m_Hstmt;
232 
233  SQLRETURN AllocateEnv();
234  bool isDbcAllocated() const {
235  return m_HdbcAllocated;
236  }
237 
238  void BindParams( const ParametersVector& vectorOfParameters, SQLHANDLE* statementHandle = NULL, const unsigned int startIndex = 1 );
239 
240  DataSet* innerExecuteCommand( const DataCommand& command, const ParametersVector& vectorOfParameters, const bool useCursor = false, const unsigned int fetchRows = 0 );
241 
242  // fetches data from the database, using an already created statement handle
243  DataSet* executeQuery( DataCommand& command, const bool isCommandCached, SQLHANDLE* statementHandle, const bool useCursor, const unsigned int fetchRows = 0 );
244  DataSet* getDataSet( DataCommand& command, const bool isCommandCached, SQLHANDLE* statementHandle, const bool useCursor, const unsigned int fetchRows = 0 );
245 
246  void executeNonQuery( DataCommand& command, const bool isCommandCached, SQLHANDLE* statementHandle, const bool useCursor );
247 
248  // translates transaction type to a DB2 specific value
249  static int getODBCTransactionType( TransactionType::TRANSACTION_TYPE type );
250 
251  // get detailed error information
252  virtual string getErrorInformation( SQLSMALLINT htype, SQLHANDLE handle );
253 
254  virtual void setSpecificConnAttr() {};
255  virtual void setSpecificEnvAttr();
256  virtual string callFormating( const string& statementString, const ParametersVector& vectorOfParameters );
257  };
258 }
259 
260 #endif