aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBInitializerFromSQL.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
50
51#ifdef _ODBC
52
53# ifndef DOXYGEN_SHOULD_SKIP_THIS
54
56# ifdef GUM_NO_INLINE
58# endif /* GUM_NO_INLINE */
59
60namespace gum {
61
62 namespace learning {
63
65 void DBInitializerFromSQL::_connect_(const std::string& connection_string, long timeout) {
66 // analyze the connection string: either this is a user-defined connection
67 // string or this is an aGrUM-constructed one derived from a datasource,
68 // a login and a password
69 bool agrum_connection
70 = (connection_string.size() > 4) && (connection_string.compare(0, 4, "gum ") == 0);
71
72 // perform the connection to the database
73 if (!agrum_connection) {
74 _connection_.connect(connection_string, timeout);
75 } else {
76 std::size_t deb_index, end_index;
77 const std::string delimiter = "|";
78
79 deb_index = connection_string.find(delimiter, 0);
80 if (deb_index == std::string::npos)
81 GUM_ERROR(DatabaseError,
82 "could not determine the datasource from string " << connection_string);
83 deb_index += std::size_t(1);
84 end_index = connection_string.find(delimiter, deb_index);
85 if (end_index == std::string::npos)
86 GUM_ERROR(DatabaseError,
87 "could not determine the datasource from string " << connection_string);
88 std::string dataSource = connection_string.substr(deb_index, end_index - deb_index);
89
90 deb_index = connection_string.find(delimiter, end_index + std::size_t(1));
91 if (deb_index == std::string::npos)
92 GUM_ERROR(DatabaseError,
93 "could not determine the database login from string " << connection_string);
94 deb_index += std::size_t(1);
95 end_index = connection_string.find(delimiter, deb_index);
96 if (end_index == std::string::npos)
97 GUM_ERROR(DatabaseError,
98 "could not determine the database login from string " << connection_string);
99 std::string login = connection_string.substr(deb_index, end_index - deb_index);
100
101 deb_index = connection_string.find(delimiter, end_index + std::size_t(1));
102 if (deb_index == std::string::npos)
103 GUM_ERROR(DatabaseError,
104 "could not determine the database password from string " << connection_string);
105 deb_index += std::size_t(1);
106 end_index = connection_string.find(delimiter, deb_index);
107 if (end_index == std::string::npos)
108 GUM_ERROR(DatabaseError,
109 "could not determine the database password from string " << connection_string);
110 std::string password = connection_string.substr(deb_index, end_index - deb_index);
111
112 _connection_.connect(dataSource, login, password, timeout);
113 }
114 }
115
117 DBInitializerFromSQL::DBInitializerFromSQL(const std::string& connection_string,
118 const std::string& query,
119 long timeout) :
120 IDBInitializer(IDBInitializer::InputType::STRING), _connection_string_(connection_string),
121 _query_(query), _timeout_(timeout) {
122 // save the current locale because the connection to the database
123 // will change it
124 const std::string current_locale = std::setlocale(LC_NUMERIC, NULL);
125
126 // perform the connection
127 _connect_(connection_string, timeout);
128
129 // restore the locale
130 std::setlocale(LC_NUMERIC, current_locale.c_str());
131
132 // ask the parser to execute the query
133 _parser_.useNewQuery(_connection_, _query_);
134
135 // store the names of the columns into the intializer
136 const std::size_t nb_cols = std::size_t(_parser_.nbColumns());
137 for (std::size_t i = 0; i < nb_cols; ++i) {
138 _var_names_.push_back(_parser_.columnName(i));
139 }
140
141 GUM_CONSTRUCTOR(DBInitializerFromSQL);
142 }
143
145 DBInitializerFromSQL::DBInitializerFromSQL(const std::string& dataSource,
146 const std::string& login,
147 const std::string& password,
148 const std::string& query,
149 long timeout) :
150 DBInitializerFromSQL("gum datasource=|" + dataSource + "|; login=|" + login
151 + "|; password=|" + password + "|",
152 query,
153 timeout) {}
154
156 DBInitializerFromSQL::DBInitializerFromSQL(const DBInitializerFromSQL& from) :
157 DBInitializerFromSQL(from._connection_string_, from._query_, from._timeout_) {}
158
160 DBInitializerFromSQL::DBInitializerFromSQL(DBInitializerFromSQL&& from) :
161 DBInitializerFromSQL(from._connection_string_, from._query_, from._timeout_) {}
162
164 DBInitializerFromSQL* DBInitializerFromSQL::clone() const {
165 return new DBInitializerFromSQL(*this);
166 }
167
169 DBInitializerFromSQL::~DBInitializerFromSQL() { GUM_DESTRUCTOR(DBInitializerFromSQL); }
170
172 DBInitializerFromSQL& DBInitializerFromSQL::operator=(const DBInitializerFromSQL& from) {
173 if (this != &from) {
174 IDBInitializer::operator=(from);
175 // check if the connection parameters have changed
176 const bool connexion_changed = (_connection_string_ != from._connection_string_);
177
178 // save the new connection parameters
179 _connection_string_ = from._connection_string_;
180 _query_ = from._query_;
181 _timeout_ = from._timeout_;
182
183 // recreate the connection if needed
184 if (connexion_changed) {
185 if (_connection_.connected()) _connection_.disconnect();
186 _connect_(_connection_string_, _timeout_);
187 }
188
189 // initiate the SQL parser
190 _parser_.useNewQuery(_connection_, _query_);
191
192 // store the names of the columns into the intializer
193 _var_names_.clear();
194 const std::size_t nb_cols = std::size_t(_parser_.nbColumns());
195 for (std::size_t i = 0; i < nb_cols; ++i) {
196 _var_names_.push_back(_parser_.columnName(i));
197 }
198 }
199
200 return *this;
201 }
202
204 DBInitializerFromSQL& DBInitializerFromSQL::operator=(DBInitializerFromSQL&& from) {
205 return operator=(from);
206 }
207
208
209 } /* namespace learning */
210
211} /* namespace gum */
212
213# endif /* DOXYGEN_SHOULD_SKIP_THIS */
214
215#endif // _ODBC
The class for initializing DatabaseTable and RawDatabaseTable instances from SQL databases.
The class for initializing DatabaseTable and RawDatabaseTable instances from SQL databases.
DBInitializerFromSQL(const std::string &dataSource, const std::string &login, const std::string &password, const std::string &query, long timeout=0L)
default constructor, especially for postgresql databases
The base class for initializing DatabaseTable and RawDatabaseTable instances from CSV files or SQL da...
#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