File: Database.cpp

package info (click to toggle)
simpledb 1.5-1.4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 888 kB
  • ctags: 238
  • sloc: cpp: 529; makefile: 127; perl: 23; sh: 9
file content (178 lines) | stat: -rw-r--r-- 5,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * __BEGIN_COPYRIGHT
 * SimpleDB API
 * 
 * Copyright (C) 2005 Eminence Technology Pty Ltd
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You can view the GNU Lesser General Public Licence at
 * http://www.gnu.org/licenses/lgpl.html or you can write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Eminence Technology Pty Ltd can be contacted by writing to
 * Eminence Technology, PO Box 118, Moorooka QLD 4105, Australia.
 * Alternatively, you may email opensource [at] eminence [dot] com [dot] au
 * __END_COPYRIGHT
 */

#include "SimpleDB.h"

using namespace SimpleDB ;

Database::Database(std::string dsn) {
  long return_value;
  return_value = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,
				&odbc_environment_handle);
  std::string s;
  if ((return_value != SQL_SUCCESS) &&
      (return_value != SQL_SUCCESS_WITH_INFO))
    throw Exception(std::string("Error AllocHandle"));

  return_value = SQLSetEnvAttr(odbc_environment_handle, SQL_ATTR_ODBC_VERSION, 
                               (void*)SQL_OV_ODBC3, 0); 
  if ((return_value != SQL_SUCCESS) &&
      (return_value != SQL_SUCCESS_WITH_INFO))
    throw Exception(std::string("Error SetEnv"));
	
  return_value = SQLAllocHandle(SQL_HANDLE_DBC, odbc_environment_handle,
				&connectionHandle); 
  if ((return_value != SQL_SUCCESS) &&
      (return_value != SQL_SUCCESS_WITH_INFO)) {
    SQLFreeHandle(SQL_HANDLE_ENV, odbc_environment_handle);
    throw Exception(std::string("Error AllocHDB"));
  }

  SQLSetConnectAttr(connectionHandle, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
  return_value = SQLConnect(connectionHandle,
			    (SQLCHAR*) dsn.c_str(), SQL_NTS,
			    NULL, SQL_NTS,
			    NULL, SQL_NTS);
  if ((return_value != SQL_SUCCESS) &&
      (return_value != SQL_SUCCESS_WITH_INFO)) {
    std::string errorString = "Error SQLConnect: ";
    SQLCHAR sqlState[6];
    SQLINTEGER errorCode;
    SQLSMALLINT messageLength;
    SQLCHAR errorMessage[400];
  long errValue = SQLGetDiagRec(SQL_HANDLE_DBC, connectionHandle,1,
				  (SQLCHAR *)sqlState,&errorCode,
				  (SQLCHAR *)errorMessage,
				  400,&messageLength);
    if(errValue == SQL_SUCCESS) {
      errorString += (char *)errorMessage;
    } else if (errValue == SQL_SUCCESS_WITH_INFO) {
      
      std::cerr << "Error message too long at " << messageLength <<
	" characters" <<  std::endl;
    } else if (errValue == SQL_ERROR) {
      errorString += "RecNumber was negative or 0 or BufferLength was less tha 0";
    } else if (errValue == SQL_NO_DATA) {
      errorString += "SQL no data";
    }

    SQLFreeHandle(SQL_HANDLE_DBC, connectionHandle);
    SQLFreeHandle(SQL_HANDLE_ENV, odbc_environment_handle);
    throw Exception(errorString);
  }
}

Query Database::newQuery() const {
  return Query(connectionHandle);
}

void Database::voidQuery (const std::string& query) const {
  try {
    SimpleDB::Query queryWrapper = this->newQuery();
    queryWrapper.execute(query);
  } catch(SimpleDB::Query::Exception &s)  {
    throw (SimpleDB::Database::Exception (std::string(s.what()))) ;
  }
  catch(SimpleDB::Column::UnboundException)  {
    throw (SimpleDB::Database::Exception 
	   (std::string("A Column was not bound"))) ;
  }
}

long Database::intQuery (const std::string& query) const {
  try {
    SimpleDB::Query queryWrapper = this->newQuery();
    SimpleDB::LongColumn longCol = SimpleDB::LongColumn();
    SimpleDB::Column * columns[1] = {&longCol};
    queryWrapper.bind(columns, 1);
    queryWrapper.execute(query);
    if(!queryWrapper.fetchRow())
      throw SimpleDB::Database::NoDataException();
    return longCol.value() ;
  } catch(SimpleDB::Query::Exception &s)  {
    throw SimpleDB::Database::Exception (std::string(s.what()));
  } catch(SimpleDB::Column::UnboundException)  {
    throw SimpleDB::Database::Exception (std::string("A Column was not bound"));
  }
}

std::string Database::strQuery (const std::string& query, long bufSize) const {
  try {
    SimpleDB::Query queryWrapper = this->newQuery(); 
    SimpleDB::StringColumn col = SimpleDB::StringColumn(bufSize);
    SimpleDB::Column * columns[1] = {&col};
    queryWrapper.bind(columns, sizeof(columns)/sizeof(columns[0]));
    queryWrapper.execute(query);
    queryWrapper.fetchRow();
    return col.value() ;
  }
  catch(SimpleDB::Query::Exception &s)  {
    throw (SimpleDB::Database::Exception (std::string(s.what()))) ;
  }
  catch(SimpleDB::Column::UnboundException)  {
    throw (SimpleDB::Database::Exception (std::string("A Column was not bound"))) ;
  }
  return "" ;
}

const std::string Database::escapeString(const std::string& input) {
  std::string output = "";
  char c;
  for (unsigned int i = 0; i < input.length(); i++) {
    c = input.at(i) ;
    switch (c) {
    case '\'' :
      output += "''";
      break;
    case '\\' :
      output += "\\\\";
      break ;
    default:
      output += c;
      break ;
    }
  }
  return output;
}

const std::string  Database::boolToStr (const bool in) {
  std::string rtStr = "FALSE" ;
  if (in)
    rtStr = "TRUE" ;
  return rtStr ;
}
    
const SQLHDBC Database::getConnectionHandle() const {
  return connectionHandle;
}


Database::~Database() 
{
  SQLDisconnect(connectionHandle);
  SQLFreeHandle(SQL_HANDLE_DBC, connectionHandle);
  SQLFreeHandle(SQL_HANDLE_ENV, odbc_environment_handle);
}