aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
nanodbcParser.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2005-2020 by Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@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  ***************************************************************************/
20 /**
21  * @file
22  * @brief Class for parsing SQL results using Nanodbc.
23  *
24  * @author Lionel TORTI, Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
25  */
26 
27 #ifdef ODBC_
28 
29 #ifndef GUM_NANODBC_PARSER_H
30 #define GUM_NANODBC_PARSER_H
31 
32 #include <string>
33 #include <cstdlib>
34 #include <vector>
35 #include <stdexcept>
36 #include <sql.h>
37 
38 #include <agrum/agrum.h>
39 #include <agrum/tools/external/nanodbc/nanodbc.h>
40 
41 namespace gum {
42 
43  namespace learning {
44 
45  /** @class NanodbcParser
46  * @headerfile NanodbcParser.h <agrum/tools/database/NanodbcParser.h>
47  * @brief Class for parsing SQL results using Nanodbc.
48  *
49  * This class should probably not be used by itself. It is essentially used
50  * by Class DBInitializerFromSQL.
51  * @ingroup learning_database
52  */
53  template <template<typename> class ALLOC = std::allocator>
54  class NanodbcParser {
55  public:
56 
57  /// type for the allocators passed in arguments of methods
58  using allocator_type = ALLOC<std::string>;
59 
60 
61  // ##########################################################################
62  /// @name Constructors / Destructors
63  // ##########################################################################
64  /// @{
65 
66  /// Default constructor: create a parser without being connected
67  NanodbcParser( const ALLOC<std::string>& alloc = ALLOC<std::string>() );
68 
69  /// constructor that executes an SQL query if the connection is active
70  /** @param connection a nanODBC connection to a SQL database
71  * @param query a string containing an SQL query
72  * @param alloc The allocator that will be used by all methods
73  */
74  NanodbcParser( nanodbc::connection& connection,
75  const std::string& query,
76  const ALLOC<std::string>& alloc = ALLOC<std::string> () );
77 
78  /// destructor
79  virtual ~NanodbcParser();
80 
81  /// @}
82 
83 
84  // ########################################################################
85  /// @name Accessors / Modifiers
86  // ########################################################################
87  /// @{
88 
89  /// Gets the next line of the SQL stream and parses it.
90  /** @return false if there is no next line. */
91  bool next();
92 
93  /// returns the current parsed line.
94  /** @throw NullElement is raised if there is no data. */
95  const std::vector<std::string,ALLOC<std::string>>& current() const;
96 
97  /// returns the current line number within the query
98  std::size_t nbLine() const;
99 
100  /// returns the number of columns in the query result
101  std::size_t nbColumns () const;
102 
103  /// returns the name of the ith column
104  std::string columnName ( const std::size_t i ) const;
105 
106  /// start a new query
107  void useNewQuery ( nanodbc::connection& connexion,
108  const std::string& query );
109 
110  /// @}
111 
112 
113 #ifndef DOXYGEN_SHOULD_SKIP_THIS
114 
115  private:
116 
117  // the result of the last SQL query performed
118  nanodbc::result _result_;
119 
120  // the line number within the current query
121  std::size_t _nb_line_ { std::size_t(0) };
122 
123  // a vector that will contain the content of the current line of result
124  std::vector<std::string,ALLOC<std::string>> _data_;
125 
126 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
127 
128  };
129 
130  } // namespace learning
131 
132 } // namespace gum
133 
134 #include <agrum/tools/database/nanodbcParser_tpl.h>
135 
136 #endif // GUM_NANODBC_PARSER_H
137 
138 #endif // ODBC_