fintp_udal
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OracleParameter.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 ORACLEPARAMETER_H
22 #define ORACLEPARAMETER_H
23 
24 #include <cstring>
25 #include <string>
26 #include <vector>
27 
28 #include "../DataParameter.h"
29 #include "Trace.h"
30 
31 #include <oci.h>
32 
33 #ifdef WIN32
34 #define __MSXML_LIBRARY_DEFINED__
35 #include <windows.h>
36 #define sleep(x) Sleep( (x)*1000 )
37 #else
38 #include <unistd.h>
39 #endif
40 
41 using namespace std;
42 
43 namespace FinTP
44 {
48  template < class T >
50  {
51  public :
52 
57  explicit inline OracleParameter( DataParameterBase::PARAMETER_DIRECTION paramDirection = DataParameterBase::PARAM_IN )
58  : DataParameter< T >( paramDirection ), m_hBind( NULL ), m_IndicatorVariable( 0 ) {}
59 
65  explicit inline OracleParameter( const string& paramName, DataParameterBase::PARAMETER_DIRECTION paramDirection = DataParameterBase::PARAM_IN)
66  : DataParameter< T >( paramName, paramDirection ), m_hBind( NULL ), m_IndicatorVariable( 0 ) {}
67 
72  inline OracleParameter( const OracleParameter< T >& source ) {}
73 
77  inline ~OracleParameter() {}
78 
83  inline void setValue( T columnValue )
84  {
85  DEBUG2( "Set value [" << columnValue << "]" );
86  DataParameter< T >::m_Value = columnValue;
89  }
90 
95  inline T getValue() const
96  {
97  DEBUG2( "Get value [" << m_Value << "]" );
99  }
100 
105  inline void setDimension( const unsigned int dimension )
106  {
107  DataParameterBase::setDimension( dimension );
108  }
109 
114  inline unsigned int getDimension() const
115  {
116  return DataParameterBase::getDimension();
117  }
118 
119  inline void push_back( const string& value ) { throw logic_error("Not supported by this data type"); }
120  inline const string& getElement( size_t position ) const { throw logic_error("Not supported by this data type"); }
121 
126  inline void** getBindHandle()
127  {
128  return ( void ** )&m_hBind;
129  }
130 
131  void* getIndicatorValue()
132  {
133  return ( void * )&m_IndicatorVariable;
134  }
135 
136  bool isNULLValue() const
137  {
138  return ( m_IndicatorVariable == -1 );
139  }
140 
141  private :
142 
143  OCIBind* m_hBind;
145 
146  //static int getOracleParameterDirection( DataParameterBase::PARAMETER_DIRECTION paramDirection );
147  //static int getOracleParameterType( DataType::DATA_TYPE paramType );
148  };
149 
150  class ExportedUdalObject OracleArrayParameter: public OracleParameter< vector< string > >
151  {
152  mutable OCIArray* m_Array;
153  public:
154  OracleArrayParameter(): m_Array( NULL ) {}
155  inline OCIArray** getOCIArray() const { return &m_Array; }
156  };
157 
158  template <>
160  {
161  return DataType::ARRAY;
162  }
163 
164  template<>
165  inline void OracleParameter< vector< string > >::push_back( const string& value )
166  {
167  m_Value.push_back( value );
168  }
169 
170  template<>
171  inline unsigned int OracleParameter< vector< string > >::getDimension() const
172  {
173  return m_Value.size();
174  }
175 
176  template<>
177  inline const string& OracleParameter< vector< string > >::getElement( size_t position ) const
178  {
179  return m_Value[position];
180  }
181 
182  //override for string
183  template<>
184  inline unsigned int OracleParameter< string >::getDimension() const
185  {
186  // DB2 custom hack
187  return m_Dimension + 1;
188  }
189 
190  template<>
191  inline void OracleParameter< string >::setDimension( const unsigned int dimension )
192  {
193  //DEBUG( "Should be equal ( before resize : " ) << m_StoragePointer << " , " << ( void* )m_Value.data() );
194  //m_Value.resize( dimension );
195  DEBUG2( "Set dimension [" << dimension << "]" );
196  DataParameterBase::setDimension( dimension );
197 
198  if( m_StoragePointer != NULL )
199  delete[] m_StoragePointer;
200 
201  m_StoragePointer = new unsigned char[ m_Dimension + 1 ];//( void * )m_Value.data();
202  memset( m_StoragePointer, 0, m_Dimension + 1 );
203  //DEBUG( "Should be equal ( after resize : " ) << m_StoragePointer << " , " << ( void* )m_Value.data() );
204  }
205 
206  template<>
207  inline void OracleParameter< string >::setValue( string columnValue )
208  {
209  //DEBUG( "SetValue : " << columnValue );
210  string::size_type paramLength = columnValue.length();
211  DEBUG2( "Dimension : [" << m_Dimension << "] Address : [" << m_StoragePointer << "]" );
212 
213  if( m_StoragePointer == NULL )
214  dynamic_cast< OracleParameter< string > * >( this )->setDimension( paramLength );
215  DEBUG2( "Dimension : [" << m_Dimension << "] Address : [" << m_StoragePointer << "]" );
216 
217  if( m_Dimension < paramLength )
218  {
219  TRACE( "Parameter storage size is less than source. Data right truncation from " << paramLength << " chars to " << m_Dimension );
220  memcpy( m_StoragePointer, columnValue.c_str(), m_Dimension );
221  //( ( unsigned char* )m_StoragePointer )[ m_Dimension - 1 ] = 0;
222  }
223  else
224  {
225  DEBUG2( "Storage size ok." );
226  memcpy( m_StoragePointer, columnValue.c_str(), paramLength );
227  }
228  }
229 
230  template<>
232  {
233  string returnValue = string( ( char* )m_StoragePointer, m_Dimension );
234 
235  //DEBUG( "pValue : " << ( void* )m_Value.data() );
236 #ifdef WIN32
237  DEBUG2( "Get value : [" << returnValue << "]" );
238 #endif
239 
240  //DEBUG( "*Size : " << strlen( ( ( char* )m_StoragePointer ) ) );
241  //DEBUG( "Should be equal : " << m_StoragePointer << " , " << ( void* )m_Value.data() );
242 
243  return returnValue;
244  }
245 
246  template <>
248  {
249  DEBUG2( "Destructor" );
250  if( m_StoragePointer != NULL )
251  {
252  delete[] m_StoragePointer;
253  m_StoragePointer = NULL;
254  }
255  }
256 }
257 #endif //ORACLEPARAMETER_H