Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

FirebirdParameter.cpp

Go to the documentation of this file.
00001 #include "../include/FirebirdParameter.h"
00002 #include "../include/FirebirdDatabaseLayer.h"
00003 #include "../include/DatabaseLayerException.h"
00004 
00005 // ctor
00006 FirebirdParameter::FirebirdParameter(XSQLVAR* pVar)
00007 {
00008   m_pParameter = pVar;
00009   m_nNullFlag = -1;
00010   m_pParameter->sqlind = &m_nNullFlag; // NULL indicator
00011   m_nParameterType = FirebirdParameter::PARAM_NULL;
00012   m_pDatabase = NULL;
00013   m_pTransaction = NULL;
00014 }
00015 
00016 FirebirdParameter::FirebirdParameter(XSQLVAR* pVar, const wxString& strValue, const wxCSConv* conv)
00017 {
00018   m_pParameter = pVar;
00019   m_strValue = strValue;
00020 
00021   SetEncoding(conv);
00022 
00023   // Set to SQL_TEXT manually
00024   m_pParameter->sqltype = SQL_TEXT;
00025   wxCharBuffer valueBuffer = ConvertToUnicodeStream(m_strValue);
00026   size_t length = GetEncodedStreamLength(m_strValue);
00027   wxStrncpy((wxChar*)m_pParameter->sqldata, (wxChar*)(const char*)valueBuffer, length);
00028   //(char*)(m_pParameter->sqldata) = valueBuffer;
00029   m_pParameter->sqllen = length;
00030 
00031   m_nNullFlag = 0;
00032   m_pParameter->sqlind = &m_nNullFlag; // NULL indicator
00033 }
00034 
00035 FirebirdParameter::FirebirdParameter(XSQLVAR* pVar, int nValue)
00036 {
00037   m_pParameter = pVar;
00038   m_nParameterType = FirebirdParameter::PARAM_INT;
00039   m_nValue = nValue;
00040   
00041   m_pParameter->sqldata = (char*)&m_nValue;
00042 
00043   m_nNullFlag = 0;
00044   m_pParameter->sqlind = &m_nNullFlag; // NULL indicator
00045 }
00046 
00047 FirebirdParameter::FirebirdParameter(XSQLVAR* pVar, double dblValue)
00048 {
00049   m_pParameter = pVar;
00050   m_nParameterType = FirebirdParameter::PARAM_DOUBLE;
00051   int nType = (m_pParameter->sqltype & ~1);
00052   if (nType == SQL_FLOAT)
00053   {
00054     m_fValue = dblValue;
00055     m_pParameter->sqldata = (char*)&m_fValue;
00056   }
00057   else if (nType == SQL_DOUBLE)
00058   {
00059     m_dblValue = dblValue;
00060     m_pParameter->sqldata = (char*)&m_dblValue;
00061   }
00062   else
00063   {
00064     // Error?
00065     wxLogError(_("Parameter type is not compatible with parameter of type double\n"));
00066   }
00067   m_nNullFlag = 0;
00068   m_pParameter->sqlind = &m_nNullFlag; // NULL indicator
00069 }
00070 
00071 FirebirdParameter::FirebirdParameter(XSQLVAR* pVar, bool bValue)
00072 {
00073   m_pParameter = pVar;
00074   m_nParameterType = FirebirdParameter::PARAM_BOOL;
00075   m_bValue = bValue;
00076   m_nValue = (m_bValue) ? 1 : 0;
00077   
00078   m_pParameter->sqldata = (char*)&m_nValue;
00079 
00080   m_nNullFlag = 0;
00081   m_pParameter->sqlind = &m_nNullFlag; // NULL indicator
00082 }
00083 
00084 FirebirdParameter::FirebirdParameter(XSQLVAR* pVar, const wxDateTime& dateValue)
00085 {
00086   m_pParameter = pVar;
00087   m_nParameterType = FirebirdParameter::PARAM_DATETIME;
00088 
00089   struct tm dateAsTm;
00090   wxDateTime::Tm tm = dateValue.GetTm();
00091   dateAsTm.tm_sec = tm.sec;
00092   dateAsTm.tm_min = tm.min;
00093   dateAsTm.tm_hour = tm.hour;
00094   dateAsTm.tm_mday = tm.mday;
00095   dateAsTm.tm_mon = tm.mon;
00096   dateAsTm.tm_year = tm.year;
00097   isc_encode_timestamp(&dateAsTm, &m_Date);
00098 
00099   m_nBufferLength = sizeof(ISC_TIMESTAMP);
00100   
00101   m_pParameter->sqldata = (char*)&m_Date;
00102 
00103   m_nNullFlag = 0;
00104   m_pParameter->sqlind = &m_nNullFlag; // NULL indicator
00105 }
00106 
00107 FirebirdParameter::FirebirdParameter(XSQLVAR* pVar, isc_db_handle pDatabase, isc_tr_handle pTransaction, const void* pData, long nDataLength)
00108 {
00109   m_pParameter = pVar;
00110   m_pDatabase = pDatabase;
00111   m_pTransaction = pTransaction;
00112 
00113   // Just copy the data into the memory buffer for now.  We'll move the data over to the blob in the call to ResetBlob
00114   void* pBuffer = m_BufferValue.GetWriteBuf(nDataLength);
00115   memcpy(pBuffer, pData, nDataLength);
00116   m_nBufferLength = nDataLength;
00117 }
00118 
00119 void FirebirdParameter::ResetBlob()
00120 {
00121   // If the databaes and transaction handles aren't valid then don't try to do anything
00122   if ((m_pDatabase == NULL) || (m_pTransaction == NULL))
00123     return;
00124 
00125   //m_BlobId = NULL;
00126   m_pBlob = NULL;
00127   ISC_STATUS_ARRAY    status;              /* status vector */
00128   void* pData = m_BufferValue.GetData();
00129   int nDataLength = m_nBufferLength;//m_BufferValue.GetDataLen();
00130   
00131   memset(&m_BlobId, 0, sizeof(m_BlobId));
00132   int nReturn = isc_create_blob2(status, &m_pDatabase, &m_pTransaction, &m_pBlob, &m_BlobId, 0, NULL);
00133   if (nReturn != 0)
00134   {
00135 #ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
00136     long nSqlCode = isc_sqlcode(status);
00137     DatabaseLayerException error(FirebirdDatabaseLayer::TranslateErrorCode(nSqlCode), 
00138         FirebirdDatabaseLayer::TranslateErrorCodeToString(nSqlCode, status));
00139 
00140     throw error;
00141 #endif
00142 
00143     //isc_print_status(status);
00144     return;
00145   }
00146 
00147   nReturn = isc_put_segment(status, &m_pBlob, nDataLength, (char*)pData);
00148   if (nReturn != 0)
00149   {
00150 #ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
00151     long nSqlCode = isc_sqlcode(status);
00152     DatabaseLayerException error(FirebirdDatabaseLayer::TranslateErrorCode(nSqlCode),
00153         FirebirdDatabaseLayer::TranslateErrorCodeToString(nSqlCode, status));
00154 
00155     throw error;
00156 #endif
00157 
00158     //isc_print_status(status);
00159     return;
00160   }
00161 
00162   nReturn = isc_close_blob(status, &m_pBlob);
00163   if (nReturn != 0)
00164   {
00165 #ifndef DONT_USE_DATABASE_LAYER_EXCEPTIONS
00166     long nSqlCode = isc_sqlcode(status);
00167     DatabaseLayerException error(FirebirdDatabaseLayer::TranslateErrorCode(nSqlCode),
00168         FirebirdDatabaseLayer::TranslateErrorCodeToString(nSqlCode, status));
00169 
00170     throw error;
00171 #endif
00172 
00173     //isc_print_status(status);
00174     return;
00175   }
00176 
00177   m_pParameter->sqldata = (char*)&m_BlobId;
00178 
00179   m_nNullFlag = 0;
00180   m_pParameter->sqlind = &m_nNullFlag; // NULL indicator
00181 }
00182 
00183 FirebirdParameter::~FirebirdParameter()
00184 {
00185 }
00186 

Generated on Sat May 13 17:31:34 2006 for databaselayer by  doxygen 1.4.1