00001 // 00002 // File: src/environment.cpp 00003 // Object: Environment object encapsulation 00004 // Project: http://www.m2osw.com/odbcpp 00005 // Author: alexis_wilke@sourceforge.net 00006 // 00007 // Copyright (C) 2008 Made to Order Software Corp. 00008 // 00009 // This program is free software: you can redistribute it and/or modify 00010 // it under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 3 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // This program is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with this program. If not, see <http://www.gnu.org/licenses/> 00021 // or <http://gpl3.m2osw.com/>. 00022 // 00023 00024 #include "odbcpp/odbcpp.h" 00025 00026 namespace odbcpp 00027 { 00028 00029 00030 00101 environment::environment(SQLUINTEGER version) : 00102 handle(SQL_HANDLE_ENV) 00103 { 00104 // we right away allocate an environment 00105 // throw if it fails 00106 check(SQLAllocHandle(f_handle_type, SQL_NULL_HANDLE, &f_handle)); 00107 00108 // setup the current version, without that we run in problems 00109 // when trying to create the connection in a constructor 00110 set_attr(SQL_ATTR_ODBC_VERSION, version); 00111 } 00112 00113 00114 00125 void environment::set_attr(SQLINTEGER attr, SQLINTEGER integer) 00126 { 00127 // Note: the length parameter is ignore in this case 00128 check(SQLSetEnvAttr(f_handle, attr, int_to_ptr(integer), 0)); 00129 } 00130 00131 00143 void environment::set_attr(SQLINTEGER attr, SQLPOINTER ptr, SQLINTEGER length) 00144 { 00145 check(SQLSetEnvAttr(f_handle, attr, ptr, length)); 00146 } 00147 00148 00169 void environment::get_data_source(data_source_type_t type, data_source_vector_t& sources) 00170 { 00171 SQLRETURN return_code; 00172 SQLUSMALLINT direction; 00173 SQLCHAR server_str[256], description_str[1024]; 00174 data_source_t src; 00175 00176 sources.clear(); 00177 00178 switch(type) { 00179 default: //case DATA_SOURCE_TYPE_ALL: 00180 direction = SQL_FETCH_FIRST; 00181 break; 00182 00183 case DATA_SOURCE_TYPE_USER: 00184 direction = SQL_FETCH_FIRST_USER; 00185 break; 00186 00187 case DATA_SOURCE_TYPE_SYSTEM: 00188 direction = SQL_FETCH_FIRST_SYSTEM; 00189 break; 00190 00191 } 00192 00193 for(;;) { 00194 return_code = SQLDataSources(f_handle, direction, 00195 server_str, sizeof(server_str), NULL, 00196 description_str, sizeof(description_str), NULL); 00197 if(return_code == SQL_NO_DATA) { 00198 // done 00199 return; 00200 } 00201 check(return_code); 00202 00203 src.f_server = reinterpret_cast<char *>(server_str); 00204 src.f_description = reinterpret_cast<char *>(description_str); 00205 00206 sources.push_back(src); 00207 00208 direction = SQL_FETCH_NEXT; 00209 } 00210 } 00211 00212 00223 void environment::commit() 00224 { 00225 check(SQLEndTran(f_handle_type, f_handle, SQL_COMMIT)); 00226 } 00227 00228 00229 00240 void environment::rollback() 00241 { 00242 check(SQLEndTran(f_handle_type, f_handle, SQL_ROLLBACK)); 00243 } 00244 00245 00246 00277 } // namespace odbcpp 00278 00279