30 # ifndef DOXYGEN_SHOULD_SKIP_THIS 41 template <
template <
typename >
class ALLOC >
42 void DBInitializerFromSQL< ALLOC >::__connect(
43 const std::string& connection_string,
long timeout) {
47 bool agrum_connection = (connection_string.size() > 4)
48 && (connection_string.compare(0, 4,
"gum ") == 0);
51 if (!agrum_connection) {
52 __connection.connect(connection_string, timeout);
54 std::size_t deb_index, end_index;
55 const std::string delimiter =
"|";
57 deb_index = connection_string.find(delimiter, 0);
58 if (deb_index == std::string::npos)
60 "could not determine the datasource from string " 61 << connection_string);
62 deb_index += std::size_t(1);
63 end_index = connection_string.find(delimiter, deb_index);
64 if (end_index == std::string::npos)
66 "could not determine the datasource from string " 67 << connection_string);
68 std::string dataSource =
69 connection_string.substr(deb_index, end_index - deb_index);
71 deb_index = connection_string.find(delimiter, end_index + std::size_t(1));
72 if (deb_index == std::string::npos)
74 "could not determine the database login from string " 75 << connection_string);
76 deb_index += std::size_t(1);
77 end_index = connection_string.find(delimiter, deb_index);
78 if (end_index == std::string::npos)
80 "could not determine the database login from string " 81 << connection_string);
83 connection_string.substr(deb_index, end_index - deb_index);
85 deb_index = connection_string.find(delimiter, end_index + std::size_t(1));
86 if (deb_index == std::string::npos)
88 "could not determine the database password from string " 89 << connection_string);
90 deb_index += std::size_t(1);
91 end_index = connection_string.find(delimiter, deb_index);
92 if (end_index == std::string::npos)
94 "could not determine the database password from string " 95 << connection_string);
96 std::string password =
97 connection_string.substr(deb_index, end_index - deb_index);
99 __connection.connect(dataSource, login, password, timeout);
105 template <
template <
typename >
class ALLOC >
107 const std::string& connection_string,
108 const std::string& query,
111 IDBInitializer< ALLOC >(IDBInitializer< ALLOC >::InputType::STRING, alloc),
112 __connection_string(connection_string), __query(query), __timeout(timeout),
113 __var_names(alloc), __parser(alloc) {
116 const std::string current_locale = std::setlocale(LC_NUMERIC, NULL);
119 __connect(connection_string, timeout);
122 std::setlocale(LC_NUMERIC, current_locale.c_str());
125 __parser.useNewQuery(__connection, __query);
128 const std::size_t nb_cols = std::size_t(__parser.nbColumns());
129 for (std::size_t i = 0; i < nb_cols; ++i) {
130 __var_names.push_back(__parser.columnName(i));
138 template <
template <
typename >
class ALLOC >
140 const std::string& dataSource,
141 const std::string& login,
142 const std::string& password,
143 const std::string& query,
147 +
"|; login=|" + login +
"|; password=|" 155 template <
template <
typename >
class ALLOC >
157 const DBInitializerFromSQL< ALLOC >& from,
160 from.__connection_string, from.__query, from.__timeout, alloc) {}
164 template <
template <
typename >
class ALLOC >
166 const DBInitializerFromSQL< ALLOC >& from) :
171 template <
template <
typename >
class ALLOC >
173 DBInitializerFromSQL< ALLOC >&& from,
176 from.__connection_string, from.__query, from.__timeout, alloc) {}
179 template <
template <
typename >
class ALLOC >
181 DBInitializerFromSQL< ALLOC >&& from) :
186 template <
template <
typename >
class ALLOC >
189 ALLOC< DBInitializerFromSQL< ALLOC > > allocator(alloc);
190 DBInitializerFromSQL< ALLOC >* new_initializer = allocator.allocate(1);
192 allocator.construct(new_initializer, *
this, alloc);
194 allocator.deallocate(new_initializer, 1);
198 return new_initializer;
203 template <
template <
typename >
class ALLOC >
210 template <
template <
typename >
class ALLOC >
217 template <
template <
typename >
class ALLOC >
219 operator=(
const DBInitializerFromSQL< ALLOC >& from) {
223 const bool connexion_changed =
224 (__connection_string != from.__connection_string);
227 __connection_string = from.__connection_string;
228 __query = from.__query;
229 __timeout = from.__timeout;
232 if (connexion_changed) {
233 if (__connection.connected()) __connection.disconnect();
234 __connect(__connection_string, __timeout);
238 __parser.useNewQuery(__connection, __query);
242 const std::size_t nb_cols = std::size_t(__parser.nbColumns());
243 for (std::size_t i = 0; i < nb_cols; ++i) {
244 __var_names.push_back(__parser.columnName(i));
253 template <
template <
typename >
class ALLOC >
255 operator=(DBInitializerFromSQL< ALLOC >&& from) {
261 template <
template <
typename >
class ALLOC >
262 INLINE std::vector< std::string, ALLOC< std::string > >
269 template <
template <
typename >
class ALLOC >
270 INLINE
const std::vector< std::string, ALLOC< std::string > >&
272 return __parser.current();
277 template <
template <
typename >
class ALLOC >
279 return __parser.next();
virtual DBInitializerFromSQL< ALLOC > * clone() const
virtual copy constructor
virtual bool _nextRow() final
indicates whether there is a next row to read (and point on it)
DBInitializerFromSQL(const std::string &dataSource, const std::string &login, const std::string &password, const std::string &query, long timeout=0L, const allocator_type &alloc=allocator_type())
default constructor, especially for postgresql databases
ALLOC< std::string > allocator_type
type for the allocators passed in arguments of methods
allocator_type getAllocator() const
returns the allocator used
gum is the global namespace for all aGrUM entities
IDBInitializer< ALLOC > & operator=(const IDBInitializer< ALLOC > &from)
copy operator
virtual std::vector< std::string, ALLOC< std::string > > _variableNames() final
returns the names of the variables
The class for initializing DatabaseTable and RawDatabaseTable instances from SQL databases.
virtual ~DBInitializerFromSQL()
destructor
DBInitializerFromSQL< ALLOC > & operator=(const DBInitializerFromSQL< ALLOC > &from)
copy operator
virtual const std::vector< std::string, ALLOC< std::string > > & _currentStringRow() final
returns the content of the current row using strings
#define GUM_ERROR(type, msg)