Version: 1.0.0
postgresql_preparedstatement_wrapper.cpp
Go to the documentation of this file.
1 #include "wx/database/wxprec.h"
2 
3 #if wxUSE_DATABASE_POSTGRESQL
4 
5 wxPostgresPreparedStatementWrapper::wxPostgresPreparedStatementWrapper(wxDynamicPostgresInterface* pInterface, PGconn* pDatabase, const wxString& strSQL, const wxString& strStatementName)
7 {
8  m_pInterface = pInterface;
9  m_pDatabase = pDatabase;
10  m_strSQL = strSQL;
11  m_strStatementName = strStatementName;
12 }
13 
15 {
16 }
17 
18 // set field
19 void wxPostgresPreparedStatementWrapper::SetParam(int nPosition, int nValue)
20 {
21  m_Parameters.SetParam(nPosition, nValue);
22 }
23 
24 void wxPostgresPreparedStatementWrapper::SetParam(int nPosition, double dblValue)
25 {
26  m_Parameters.SetParam(nPosition, dblValue);
27 }
28 
29 void wxPostgresPreparedStatementWrapper::SetParam(int nPosition, const wxString& strValue)
30 {
31  m_Parameters.SetParam(nPosition, strValue);
32 }
33 
35 {
36  m_Parameters.SetParam(nPosition);
37 }
38 
39 void wxPostgresPreparedStatementWrapper::SetParam(int nPosition, const void* pData, long nDataLength)
40 {
41  m_Parameters.SetParam(nPosition, pData, nDataLength);
42 }
43 
44 void wxPostgresPreparedStatementWrapper::SetParam(int nPosition, const wxDateTime& dateValue)
45 {
46  m_Parameters.SetParam(nPosition, dateValue);
47 }
48 
49 void wxPostgresPreparedStatementWrapper::SetParam(int nPosition, bool bValue)
50 {
51  m_Parameters.SetParam(nPosition, bValue);
52 }
53 
55 {
56  int nParameterCount = 0;
57  bool bInStringLiteral = false;
58  size_t len = m_strSQL.length();
59  for (size_t i = 0; i < len; i++)
60  {
61  wxChar character = m_strSQL[i];
62  if ('\'' == character)
63  {
64  // Signify that we are inside a string literal inside the SQL
65  bInStringLiteral = !bInStringLiteral;
66  }
67  else if (('?' == character) && !bInStringLiteral)
68  {
69  nParameterCount++;
70  }
71  }
72  return nParameterCount;
73 }
74 
76 {
77  long nRows = -1;
78  int nParameters = m_Parameters.GetSize();
79  char** paramValues = m_Parameters.GetParamValues();
80  int* paramLengths = m_Parameters.GetParamLengths();
81  int* paramFormats = m_Parameters.GetParamFormats();
82  int nResultFormat = 0; // 0 = text, 1 = binary (all or none on the result set, not column based)
83  wxCharBuffer statementNameBuffer = ConvertToUnicodeStream(m_strStatementName);
84  PGresult* pResult = m_pInterface->GetPQexecPrepared()(m_pDatabase, statementNameBuffer, nParameters, paramValues, paramLengths, paramFormats, nResultFormat);
85  if (pResult != NULL)
86  {
87  ExecStatusType status = m_pInterface->GetPQresultStatus()(pResult);
88  if ((status != PGRES_COMMAND_OK) && (status != PGRES_TUPLES_OK))
89  {
92  }
93 
94  if (GetErrorCode() == wxDATABASE_OK)
95  {
96  wxString rowsAffected = ConvertFromUnicodeStream(m_pInterface->GetPQcmdTuples()(pResult));
97  rowsAffected.ToLong(&nRows);
98  }
99  m_pInterface->GetPQclear()(pResult);
100  }
101  delete []paramValues;
102  delete []paramLengths;
103  delete []paramFormats;
104 
105  if (GetErrorCode() != wxDATABASE_OK)
106  {
109  }
110 
111  return (int)nRows;
112 }
113 
115 {
116  int nParameters = m_Parameters.GetSize();
117  char** paramValues = m_Parameters.GetParamValues();
118  int* paramLengths = m_Parameters.GetParamLengths();
119  int* paramFormats = m_Parameters.GetParamFormats();
120  int nResultFormat = 0; // 0 = text, 1 = binary (all or none on the result set, not column based)
121  wxCharBuffer statementNameBuffer = ConvertToUnicodeStream(m_strStatementName);
122  PGresult* pResult = m_pInterface->GetPQexecPrepared()(m_pDatabase, statementNameBuffer, nParameters, paramValues, paramLengths, paramFormats, nResultFormat);
123  if (pResult != NULL)
124  {
125  ExecStatusType status = m_pInterface->GetPQresultStatus()(pResult);
126  if ((status != PGRES_COMMAND_OK) && (status != PGRES_TUPLES_OK))
127  {
130  }
131  else
132  {
133  delete []paramValues;
134  delete []paramLengths;
135  delete []paramFormats;
136 
137  wxPostgresResultSet* pResultSet = new wxPostgresResultSet(m_pInterface, pResult);
138  pResultSet->SetEncoding(GetEncoding());
139  return pResultSet;
140  }
141  m_pInterface->GetPQclear()(pResult);
142  }
143  delete []paramValues;
144  delete []paramLengths;
145  delete []paramFormats;
146 
148 
149  return NULL;
150 }
151 
152 #endif//wxUSE_DATABASE_POSTGRESQL
153 
wxPostgresPreparedStatementWrapper(wxDynamicPostgresInterface *pInterface, PGconn *pDatabase, const wxString &strSQL, const wxString &strStatementName)
wxDatabaseResultSet * RunQueryWithResults()
static int TranslateErrorCode(int nCode)
#define wxDATABASE_OK
Definition: errorcodes.h:4
void SetParam(int nPosition, int nValue)
wxPostgresPreparedStatementParameterCollection m_Parameters
void SetErrorMessage(const wxString &strErrorMessage)
PQresultErrorMessageType GetPQresultErrorMessage()
void SetEncoding(wxFontEncoding encoding)
#define wxDATABASE_QUERY_RESULT_ERROR
Definition: errorcodes.h:21
PQexecPreparedType GetPQexecPrepared()
void SetErrorCode(int nErrorCode)
virtual wxString ConvertFromUnicodeStream(const char *inputBuffer)
PQresultStatusType GetPQresultStatus()
const wxCSConv * GetEncoding()
virtual const wxCharBuffer ConvertToUnicodeStream(const wxString &inputString)
void SetParam(int nPosition, int nValue)