aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
nanodbcParser.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (c) 2005-2024 by Christophe GONZALES(_at_AMU) and Pierre-Henri WUILLEMIN(_at_LIP6) *
3 * info_at_agrum_dot_org *
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 2 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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
26
28
29#ifdef _ODBC
30
31#ifndef DOXYGEN_SHOULD_SKIP_THIS
32
34# ifdef GUM_NO_INLINE
36# endif /* GUM_NO_INLINE */
37
38namespace gum {
39
40 namespace learning {
42 NanodbcParser::NanodbcParser() { GUM_CONSTRUCTOR(NanodbcParser); }
43
44
46 NanodbcParser::NanodbcParser(nanodbc::connection& connexion, const std::string& query) {
47 // check if there is a connection. If so, execute the query
48 if (connexion.connected()) {
49 _result_ = nanodbc::execute(connexion, query);
50 _data_.resize(std::size_t(_result_.columns()));
51 }
52 GUM_CONSTRUCTOR(NanodbcParser);
53 }
54
55
57 NanodbcParser::~NanodbcParser() { GUM_DESTRUCTOR(NanodbcParser); }
58
59
61 bool NanodbcParser::next() {
62 /* Extract from sql.h:
63 SQL data type codes
64 #define SQL_UNKNOWN_TYPE 0
65 #define SQL_CHAR 1
66 #define SQL_NUMERIC 2
67 #define SQL_DECIMAL 3
68 #define SQL_INTEGER 4
69 #define SQL_SMALLINT 5
70 #define SQL_FLOAT 6
71 #define SQL_REAL 7
72 #define SQL_DOUBLE 8
73 #if (ODBCVER >= 0x0300)
74 #define SQL_DATETIME 9
75 #endif
76 #define SQL_VARCHAR 12 */
77 try {
78 if (_result_.next()) {
79 const std::size_t nb_cols = std::size_t(_result_.columns());
80 char str[100]; // buffer for retrieving floats
81 for (std::size_t i = 0; i < nb_cols; ++i) {
82 const short pos(i);
83 try {
84 const int type = _result_.column_datatype(pos);
85
86 // if the column contains a numeric field, we should use
87 // method get<float>, otherwise a get<string> should be ok
88 // WARNING: using a get<string> to get the content of a
89 // real-valued field will provide incorrect results
90 if ((type >= SQL_NUMERIC) && (type <= SQL_DOUBLE)) {
91 snprintf(str, 100, "%g", _result_.get< float >(pos));
92 _data_[i] = str;
93 } else {
94 _data_[i] = _result_.get< std::string >(pos);
95 }
96 } catch (nanodbc::null_access_error& e) { _data_[i] = "NULL"; }
97 }
98 ++_nb_line_;
99 return true;
100 }
101 } catch (std::runtime_error& e) { GUM_ERROR(DatabaseError, std::string(e.what())) }
102 _data_.clear();
103 return false;
104 }
105
107 void NanodbcParser::useNewQuery(nanodbc::connection& connexion, const std::string& query) {
108 _result_ = nanodbc::execute(connexion, query);
109 _data_.resize(std::size_t(_result_.columns()));
110 _nb_line_ = std::size_t(0);
111 }
112
113
114 } /* namespace learning */
115
116} /* namespace gum */
117
118#endif /* DOXYGEN_SHOULD_SKIP_THIS */
119
120#endif // _ODBC
virtual ~NanodbcParser()
destructor
bool next()
Gets the next line of the SQL stream and parses it.
NanodbcParser()
Default constructor: create a parser without being connected.
void useNewQuery(nanodbc::connection &connexion, const std::string &query)
start a new query
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Class for parsing SQL results using Nanodbc.
Class for parsing SQL results using Nanodbc.