aimsgui  5.1.2
qsqlgraphdatabase.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL license version 2 under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL license version 2 as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL license version 2 and that you accept its terms.
32 */
33 
34 #ifndef AIMS_GRAPH_QSQLGRAPHDATABASE_H
35 #define AIMS_GRAPH_QSQLGRAPHDATABASE_H
36 
37 #include <graph/graph/graph.h>
38 #include <qsqldatabase.h>
39 #include <qsqlquery.h>
40 #include <qsqlerror.h>
41 
42 
43 namespace aims
44 {
45 
74  {
75  public:
76  typedef std::map<std::string, std::map<std::string,
77  std::vector<std::string> > > Syntax;
78 
87  {
88  CurrentGraphData( int eid, Graph* g ) : gid( eid ), graph( g ) {}
89  bool operator == ( const CurrentGraphData & ) const;
90 
91  int gid;
93  std::map<int, int> vertexindex2eid;
94  std::map<Vertex *, int> vertexeid;
95  std::map<int, int> edgeindex2eid;
96  std::map<Edge *, int> edgeeid;
97  };
98 
101 
114  void setUrl( const std::string & url,
117  bool open();
118  bool open( const std::string & user, const std::string & password );
120  void close();
122  QSqlQuery exec( const std::string & query ) const;
124  QSqlError lastError() const;
125 
127  std::string hostname() const;
129  QSqlDatabase & database() const;
138 
143  bool readSchema();
146  void sqlAttributes( const GraphObject & el,
147  std::map<std::string, std::string> & atts,
148  bool addinherited = true,
149  const std::string & assyntax = std::string() ) const;
152  void sqlAttributesAsStrings( const GraphObject & el, std::string & names,
153  std::string & values, bool first = true,
154  bool addinherited = true ) const;
193  std::list<carto::rc_ptr<Graph> >
194  partialReadFromVertexQuery( const std::string & sqlquery,
195  std::list<CurrentGraphData> & graphsinfo,
196  bool allownewgraphs = true );
231  std::list<carto::rc_ptr<Graph> >
232  partialReadFromEdgeQuery( const std::string & sqlquery,
233  std::list<CurrentGraphData> & graphsinfo,
234  bool allownewgraphs = true );
235 
238  void updateElement( const GraphObject & el, int eid );
241  void fillGraphData( CurrentGraphData & data, Graph & g, int geid=-1 );
252  const std::list<std::string> & attributes,
253  QSqlQuery & query, int eid );
259  const std::list<std::string> & attributes,
260  const std::vector<QVariant> & query,
261  int i );
266  void readElementAttributes( GraphObject & item, QSqlQuery & query,
267  const std::map<std::string, std::vector<std::string> > & vatts,
268  int i );
269  /* * read/set *all* the attributes of the given element, but read
270  from an already filled vector of QVariant instrad of directly the SQL
271  query result
272  */
273  /*
274  void readElementAttributes( GraphObject & item, QSqlQuery & query,
275  const std::map<std::string, std::vector<std::string> > & vatts,
276  int i );
277  */
278  void readGraphAttributes( Graph & g, int eid );
279  // read all attributes of multiple graphs
280  void readGraphAttributes( std::list<CurrentGraphData> & graphsinfo );
281 
287  std::map<std::string, std::list<std::string> > syntaxInheritance;
288 
289  private:
290  struct Private;
291  Private *d;
292  std::string _hostname;
293  carto::Object _header;
294  carto::Object _parsedurl;
295  mutable QSqlDatabase *_database;
296  std::string _connectionname;
297  };
298 
299 
300  // ------
301 
302  inline QSqlDatabase & QSqlGraphDatabase::database() const
303  {
304  return *_database;
305  }
306 
307 
309  {
310  return _header;
311  }
312 
313 
315  {
316  return _parsedurl;
317  }
318 
319 
320  inline std::string QSqlGraphDatabase::hostname() const
321  {
322  return _hostname;
323  }
324 
325 
326  inline QSqlQuery QSqlGraphDatabase::exec( const std::string & query ) const
327  {
328  return database().exec( query.c_str() );
329  }
330 
331 
332  inline QSqlError QSqlGraphDatabase::lastError() const
333  {
334  return database().lastError();
335  }
336 
337 
338  inline bool QSqlGraphDatabase::CurrentGraphData::operator ==
339  ( const CurrentGraphData & other ) const
340  {
341  return gid == other.gid && graph == other.graph;
342  }
343 
344 }
345 
346 #endif
347 
Graph manipulation in SQL databases utilities.
void close()
Close the connection. After this call, queries cannot be done.
void updateVertexIndexMap(CurrentGraphData &data)
Fills the CurrentGraphData vertexindex2eid map.
std::map< std::string, std::list< std::string > > syntaxInheritance
Database schema syntax: elements types inheritance.
bool open()
Actually open the database connection.
void readGraphAttributes(Graph &g, int eid)
void sqlAttributesAsStrings(const GraphObject &el, std::string &names, std::string &values, bool first=true, bool addinherited=true) const
Same as sqlAttributes but fills two strings for attributes names and values, as needed for SQL insert...
std::list< carto::rc_ptr< Graph > > partialReadFromVertexQuery(const std::string &sqlquery, std::list< CurrentGraphData > &graphsinfo, bool allownewgraphs=true)
Reads information from a given SQL selection query, and fills graph/vertices information from it.
void updateEdgeIndexMap(CurrentGraphData &data)
Fills the CurrentGraphData edgeindex2eid map.
carto::Object header()
The header stores information about how to open the database, and possibly an initial selection query...
std::list< carto::rc_ptr< Graph > > partialReadFromEdgeQuery(const std::string &sqlquery, std::list< CurrentGraphData > &graphsinfo, bool allownewgraphs=true)
Reads information from a given SQL selection query, and fills edge information from it.
void updateVertexMap(CurrentGraphData &data)
Fills the CurrentGraphData vertexeid map.
QSqlQuery exec(const std::string &query) const
Shortcut to the Qt QSqlDatabase exec() query for SQL code.
QSqlError lastError() const
Shortcut to the Qt QSqlDatabase lastError() call for SQL code.
void readElementAttributes(GraphObject &item, QSqlQuery &query, const std::map< std::string, std::vector< std::string > > &vatts, int i)
read/set all the attributes of the given element, which must have all been queried in the attributes ...
carto::Object parsedUrl()
Decomposed URL.
carto::rc_ptr< Syntax > attributesSyntax
Database schema syntax: supported attributes (database columns) for each element type.
std::map< std::string, std::map< std::string, std::vector< std::string > > > Syntax
bool open(const std::string &user, const std::string &password)
void insertOrUpdateVertex(Vertex *vert, CurrentGraphData &data)
Store a graph vertex into the database, or updates it if it already exists.
void setHeader(carto::Object hdr)
void readGraphAttributes(std::list< CurrentGraphData > &graphsinfo)
void fillGraphData(CurrentGraphData &data, Graph &g, int geid=-1)
Fills the CurrentGraphData structure using database information (performs appropriate queries)
void readElementAttributes(GraphObject &, const std::list< std::string > &attributes, QSqlQuery &query, int eid)
Set the element attributes contained in the query result.
std::string hostname() const
Address of the database (may be a local file for SQLite)
void readElementAttributes(GraphObject &item, const std::list< std::string > &attributes, const std::vector< QVariant > &query, int i)
Set the element attributes contained in the query result, but read from an already filled vector of Q...
QSqlDatabase & database() const
Qt database.
void insertOrUpdateEdge(Edge *v, CurrentGraphData &data)
Store a graph edge into the database, or updates it if it already exists.
void sqlAttributes(const GraphObject &el, std::map< std::string, std::string > &atts, bool addinherited=true, const std::string &assyntax=std::string()) const
Build an attributes/value list from a graph element according to the database schema,...
void updateElement(const GraphObject &el, int eid)
Store in the database the attributes of an existing element (graph, vertex or edge)
bool readSchema()
Read the database schema (or syntax) from the SQL database.
void updateEdgeMap(CurrentGraphData &data)
Fills the CurrentGraphData edgeeid map.
void setUrl(const std::string &url, carto::Object header=carto::none())
Set the database address, and parse it.
Object none()
This structure stores information needed to help and speed-up graph queries in SQL databases.
bool operator==(const CurrentGraphData &) const