fintp_udal
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ODBCParameter.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 ODBCPARAMETER_H
22 #define ODBCPARAMETER_H
23 
24 #include "../DataParameter.h"
25 #include "Trace.h"
26 
27 #include <string>
28 #include <vector>
29 #include <cstring>
30 
31 #include <sql.h>
32 
33 using namespace std;
34 
35 namespace FinTP
36 {
40  template < class T >
42  {
43  private:
45  public :
46 
47  // create DB2 parameter, implicit IN parameter
48  inline ODBCParameter( DataParameterBase::PARAMETER_DIRECTION paramDirection = DataParameterBase::PARAM_IN )
49  : DataParameter< T >( paramDirection ), m_StrLen_or_IndPtr( NULL ) {}
50 
56  inline ODBCParameter( const string& paramName, DataParameterBase::PARAMETER_DIRECTION paramDirection = DataParameterBase::PARAM_IN)
57  : DataParameter< T >( paramName, paramDirection ) {}
58 
59  inline ODBCParameter( const ODBCParameter< T >& source ) {};
60 
61  inline ~ODBCParameter() {};
62 
63  void* getIndicatorValue() { return reinterpret_cast<void *>(&m_StrLen_or_IndPtr); }
64 
65  inline void setValue( T columnValue )
66  {
67  DataParameter< T >::m_Value = columnValue;
70  }
71 
72  inline void push_back( const string& value ) { throw logic_error("Not supported by this data type"); }
73  inline const string& getElement( size_t position ) const { throw logic_error("Not supported by this data type"); }
74 
75  inline T getValue() const
76  {
77  DEBUG2( "Get value [" << DataParameter< T >::m_Dimensionm_Value << "]" );
79  }
80 
81  inline void setDimension( const unsigned int dimension )
82  {
83  DataParameterBase::setDimension( dimension );
84  }
85 
86  inline unsigned int getDimension() const
87  {
88  return DataParameterBase::getDimension();
89  }
90 
91  bool isNULLValue() const
92  {
93  return ( m_StrLen_or_IndPtr == SQL_NULL_DATA );
94  }
95  };
96 
97 
98  template<>
99  inline void ODBCParameter< vector< string > >::push_back( const string& value )
100  {
101  m_Value.push_back( value );
102  }
103 
104  template <>
106  {
107  return DataType::ARRAY;
108  }
109 
110  template <>
111  inline unsigned int ODBCParameter< vector< string > >::getDimension() const
112  {
113  return m_Value.size();
114  }
115 
116  template <>
117  inline const string& ODBCParameter< vector< string > >::getElement( size_t position ) const
118  {
119  return m_Value[position];
120  }
121 
122  template <>
123  inline void ODBCParameter< vector< string > >::setValue( vector< string > columnValue )
124  {
125  m_Value = columnValue;
126  m_Dimension= m_Value.size();
127  }
128 
129  //override for string
130  template<>
131  inline unsigned int ODBCParameter< string >::getDimension() const
132  {
133  // ODBC custom hack
134  return m_Dimension + 1;
135  }
136 
137  template<>
138  inline void ODBCParameter< string >::setDimension( const unsigned int dimension )
139  {
140  DataParameterBase::setDimension( dimension );
141 
142  delete[] m_StoragePointer;
143  m_StoragePointer = NULL;
144 
145  m_StoragePointer = new unsigned char[ m_Dimension + 1 ];
146  memset( m_StoragePointer, 0, m_Dimension + 1 );
147  }
148 
149  template<>
150  inline void ODBCParameter< string >::setValue( string columnValue )
151  {
152  ODBCParameter< string >::setDimension( columnValue.length() );
153 
154  memcpy( m_StoragePointer, columnValue.c_str(), m_Dimension );
155 
156  switch ( m_Type )
157  {
158  case DataType::CHAR_TYPE:
159  case DataType::LARGE_CHAR_TYPE:
160  case DataType::DATE_TYPE:
161  m_StrLen_or_IndPtr = SQL_NTS;
162  break;
163  case DataType::BINARY :
164  m_StrLen_or_IndPtr = m_Dimension;
165  break;
166  }
167  }
168 
169  template<>
170  inline string ODBCParameter< string >::getValue() const
171  {
172  return string( reinterpret_cast<char*>(m_StoragePointer), m_Dimension );
173  }
174 
175  template <>
177  {
178  delete[] m_StoragePointer;
179  }
180 }
181 
182 #endif // ODBCPARAMETER_H
183