aimsgui  5.1.2
aims::QSqlGraphDatabase Class Reference

Graph manipulation in SQL databases utilities. More...

#include <aims/graph/qsqlgraphdatabase.h>

Collaboration diagram for aims::QSqlGraphDatabase:

Classes

struct  CurrentGraphData
 This structure stores information needed to help and speed-up graph queries in SQL databases. More...
 

Public Types

typedef std::map< std::string, std::map< std::string, std::vector< std::string > > > Syntax
 

Public Member Functions

 QSqlGraphDatabase ()
 
 ~QSqlGraphDatabase ()
 
void setUrl (const std::string &url, carto::Object header=carto::none())
 Set the database address, and parse it. More...
 
bool open ()
 Actually open the database connection. More...
 
bool open (const std::string &user, const std::string &password)
 
void close ()
 Close the connection. After this call, queries cannot be done. More...
 
QSqlQuery exec (const std::string &query) const
 Shortcut to the Qt QSqlDatabase exec() query for SQL code. More...
 
QSqlError lastError () const
 Shortcut to the Qt QSqlDatabase lastError() call for SQL code. More...
 
std::string hostname () const
 Address of the database (may be a local file for SQLite) More...
 
QSqlDatabase & database () const
 Qt database. More...
 
carto::Object header ()
 The header stores information about how to open the database, and possibly an initial selection query, to restrict to a given single graph, especially for Reader<Graph> operations. More...
 
carto::Object parsedUrl ()
 Decomposed URL. More...
 
void setHeader (carto::Object hdr)
 
bool readSchema ()
 Read the database schema (or syntax) from the SQL database. More...
 
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, in a SQL-compatible way. More...
 
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 queries. More...
 
void insertOrUpdateVertex (Vertex *vert, CurrentGraphData &data)
 Store a graph vertex into the database, or updates it if it already exists. More...
 
void insertOrUpdateEdge (Edge *v, CurrentGraphData &data)
 Store a graph edge into the database, or updates it if it already exists. More...
 
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. More...
 
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. More...
 
void updateElement (const GraphObject &el, int eid)
 Store in the database the attributes of an existing element (graph, vertex or edge) More...
 
void fillGraphData (CurrentGraphData &data, Graph &g, int geid=-1)
 Fills the CurrentGraphData structure using database information (performs appropriate queries) More...
 
void updateVertexIndexMap (CurrentGraphData &data)
 Fills the CurrentGraphData vertexindex2eid map. More...
 
void updateEdgeIndexMap (CurrentGraphData &data)
 Fills the CurrentGraphData edgeindex2eid map. More...
 
void updateVertexMap (CurrentGraphData &data)
 Fills the CurrentGraphData vertexeid map. More...
 
void updateEdgeMap (CurrentGraphData &data)
 Fills the CurrentGraphData edgeeid map. More...
 
void readElementAttributes (GraphObject &, const std::list< std::string > &attributes, QSqlQuery &query, int eid)
 Set the element attributes contained in the query result. More...
 
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 QVariant instrad of directly the SQL query result. More...
 
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 map order. More...
 
void readGraphAttributes (Graph &g, int eid)
 
void readGraphAttributes (std::list< CurrentGraphData > &graphsinfo)
 

Public Attributes

carto::rc_ptr< SyntaxattributesSyntax
 Database schema syntax: supported attributes (database columns) for each element type. More...
 
std::map< std::string, std::list< std::string > > syntaxInheritance
 Database schema syntax: elements types inheritance. More...
 

Detailed Description

Graph manipulation in SQL databases utilities.

A QSqlGraphDatabase instance stores the database connection, and caches several needed information on the database, amongst which the database schema (elements inheritance and attributes).

It is useful to facilitate IO for graphs in SQL databases. It is used by Reader<Graph> and Writer<Graph> for database formats. It can also be used to perform partial reading on multiple graphs:

(the example is in python language, it is just a bit clearer)

from soma import aims
from soma.aims import aimsgui
db.setUrl( '/tmp/morphee2.sqlite' )
db.open()
db.readSchema()
lg = aims.aimsguisip.list_CurrentGraphData()
ng = db.partialReadFromVertexQuery( 'SELECT fold.eid, fold.graph, ' \
'fold.graph_index, fold.name, fold.label, class.class_name FROM fold ' \
'JOIN class ON class.eid=fold.eid WHERE fold.name=\'ventricle_left\'',
lg )
Graph manipulation in SQL databases utilities.
void setUrl(const std::string &url, carto::Object header=carto::none())
Set the database address, and parse it.

Definition at line 73 of file qsqlgraphdatabase.h.

Member Typedef Documentation

◆ Syntax

typedef std::map<std::string, std::map<std::string, std::vector<std::string> > > aims::QSqlGraphDatabase::Syntax

Definition at line 77 of file qsqlgraphdatabase.h.

Constructor & Destructor Documentation

◆ QSqlGraphDatabase()

aims::QSqlGraphDatabase::QSqlGraphDatabase ( )

◆ ~QSqlGraphDatabase()

aims::QSqlGraphDatabase::~QSqlGraphDatabase ( )

Member Function Documentation

◆ close()

void aims::QSqlGraphDatabase::close ( )

Close the connection. After this call, queries cannot be done.

◆ database()

QSqlDatabase & aims::QSqlGraphDatabase::database ( ) const
inline

Qt database.

Definition at line 302 of file qsqlgraphdatabase.h.

Referenced by exec(), and lastError().

◆ exec()

QSqlQuery aims::QSqlGraphDatabase::exec ( const std::string &  query) const
inline

Shortcut to the Qt QSqlDatabase exec() query for SQL code.

Definition at line 326 of file qsqlgraphdatabase.h.

References database().

◆ fillGraphData()

void aims::QSqlGraphDatabase::fillGraphData ( CurrentGraphData data,
Graph g,
int  geid = -1 
)

Fills the CurrentGraphData structure using database information (performs appropriate queries)

◆ header()

carto::Object aims::QSqlGraphDatabase::header ( )
inline

The header stores information about how to open the database, and possibly an initial selection query, to restrict to a given single graph, especially for Reader<Graph> operations.

Definition at line 308 of file qsqlgraphdatabase.h.

◆ hostname()

std::string aims::QSqlGraphDatabase::hostname ( ) const
inline

Address of the database (may be a local file for SQLite)

Definition at line 320 of file qsqlgraphdatabase.h.

◆ insertOrUpdateEdge()

void aims::QSqlGraphDatabase::insertOrUpdateEdge ( Edge v,
CurrentGraphData data 
)

Store a graph edge into the database, or updates it if it already exists.

◆ insertOrUpdateVertex()

void aims::QSqlGraphDatabase::insertOrUpdateVertex ( Vertex vert,
CurrentGraphData data 
)

Store a graph vertex into the database, or updates it if it already exists.

◆ lastError()

QSqlError aims::QSqlGraphDatabase::lastError ( ) const
inline

Shortcut to the Qt QSqlDatabase lastError() call for SQL code.

Definition at line 332 of file qsqlgraphdatabase.h.

References database().

◆ open() [1/2]

bool aims::QSqlGraphDatabase::open ( )

Actually open the database connection.

◆ open() [2/2]

bool aims::QSqlGraphDatabase::open ( const std::string &  user,
const std::string &  password 
)

◆ parsedUrl()

carto::Object aims::QSqlGraphDatabase::parsedUrl ( )
inline

Decomposed URL.

Definition at line 314 of file qsqlgraphdatabase.h.

◆ partialReadFromEdgeQuery()

std::list<carto::rc_ptr<Graph> > aims::QSqlGraphDatabase::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.

The SQL query is executed as is, and must include eid, vertex1 and vertex2 from edges.

It is also strongly advised to also get the class_name column from the class table (via a JOIN in the SQL query) because it avoids individual queries for it in each edge, and if possible the graph eid (via a JOIN on the vertices in ths SQL query).

Referenced graphs /vertices are queried and added to the CurrentGraphData list if allownewgraphs is true, otherwise edges belonging to an "unknown" graph are dropped.

Returns
a list of new graphs which have been allocated if allownewgraphs is true. New graphs must be deleted when not needed any longer.

for instance:

// graphs will be queried separately
ng = db.partialReadFromEdgeQuery( 'SELECT cortical.eid,
cortical.vertex1, cortical.vertex2, cortical.size, class.class_name FROM
cortical JOIN class ON class.eid=cortical.eid WHERE cortical.size > 50',
lg )
// here graph info is part of the same query
ng = db.partialReadFromEdgeQuery( 'SELECT cortical.eid,
cortical.vertex1, cortical.vertex2, cortical.size, class.class_name,
_Vertex.graph FROM cortical JOIN class, _Vertex ON
class.eid=cortical.eid AND _Vertex.eid=cortical.vertex1 WHERE
cortical.size > 50', lg )

◆ partialReadFromVertexQuery()

std::list<carto::rc_ptr<Graph> > aims::QSqlGraphDatabase::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.

The SQL query is executed as is, and must include eid and graph columns from vertices.

It is also strongly advised to also get the class_name column from the class table (via a JOIN in the SQL query) because it avoids individual queries for it in each vertex.

Referenced graphs are queried and added to the CurrentGraphData list if allownewgraphs is true, otherwise vertices belonging to an "unknown" graph are dropped.

Returns
a list of new graphs which have been allocated if allownewgraphs is true. New graphs must be deleted when not needed any longer.

for instance:

// classes will be queried separately
ng = db.partialReadFromVertexQuery( 'SELECT fold.eid, fold.graph,
fold.graph_index, fold.name, fold.label, class.class_name FROM fold
JOIN class ON class.eid=fold.eid WHERE fold.name=\'ventricle_left\'',
lg );
// here classes are part of the same query, which will avoid
// additional queries
ng = db.partialReadFromVertexQuery( 'SELECT fold.eid, fold.graph,
fold.graph_index, fold.name, fold.label, class.class_name FROM fold
JOIN class ON class.eid=fold.eid', lg );

◆ readElementAttributes() [1/3]

void aims::QSqlGraphDatabase::readElementAttributes ( GraphObject ,
const std::list< std::string > &  attributes,
QSqlQuery &  query,
int  eid 
)

Set the element attributes contained in the query result.

◆ readElementAttributes() [2/3]

void aims::QSqlGraphDatabase::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 QVariant instrad of directly the SQL query result.

◆ readElementAttributes() [3/3]

void aims::QSqlGraphDatabase::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 map order.

Parameters
iis the index in the query of the first attribute

◆ readGraphAttributes() [1/2]

void aims::QSqlGraphDatabase::readGraphAttributes ( Graph g,
int  eid 
)

◆ readGraphAttributes() [2/2]

void aims::QSqlGraphDatabase::readGraphAttributes ( std::list< CurrentGraphData > &  graphsinfo)

◆ readSchema()

bool aims::QSqlGraphDatabase::readSchema ( )

Read the database schema (or syntax) from the SQL database.

This fills the attributesSyntax and syntaxInheritance fields.

◆ setHeader()

void aims::QSqlGraphDatabase::setHeader ( carto::Object  hdr)

◆ setUrl()

void aims::QSqlGraphDatabase::setUrl ( const std::string &  url,
carto::Object  header = carto::none() 
)

Set the database address, and parse it.

Or it can be already parsed in an Object. Only after using this method, hostname(), header(), parsedUrl() and open() methods may be used.

The url may contain an initial selection query, to restrict to a given single graph, especially for Reader<Graph> operations.

db.setUrl( "/home/totor/mysqlgraphs.sqlite?Graph.uuid="
"'259049f8-e93c-095d-064f-fad19a73dc0a'" );

◆ sqlAttributes()

void aims::QSqlGraphDatabase::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, in a SQL-compatible way.

◆ sqlAttributesAsStrings()

void aims::QSqlGraphDatabase::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 queries.

◆ updateEdgeIndexMap()

void aims::QSqlGraphDatabase::updateEdgeIndexMap ( CurrentGraphData data)

Fills the CurrentGraphData edgeindex2eid map.

◆ updateEdgeMap()

void aims::QSqlGraphDatabase::updateEdgeMap ( CurrentGraphData data)

Fills the CurrentGraphData edgeeid map.

◆ updateElement()

void aims::QSqlGraphDatabase::updateElement ( const GraphObject el,
int  eid 
)

Store in the database the attributes of an existing element (graph, vertex or edge)

◆ updateVertexIndexMap()

void aims::QSqlGraphDatabase::updateVertexIndexMap ( CurrentGraphData data)

Fills the CurrentGraphData vertexindex2eid map.

◆ updateVertexMap()

void aims::QSqlGraphDatabase::updateVertexMap ( CurrentGraphData data)

Fills the CurrentGraphData vertexeid map.

Member Data Documentation

◆ attributesSyntax

carto::rc_ptr<Syntax> aims::QSqlGraphDatabase::attributesSyntax

Database schema syntax: supported attributes (database columns) for each element type.

Definition at line 285 of file qsqlgraphdatabase.h.

◆ syntaxInheritance

std::map<std::string, std::list<std::string> > aims::QSqlGraphDatabase::syntaxInheritance

Database schema syntax: elements types inheritance.

Definition at line 287 of file qsqlgraphdatabase.h.


The documentation for this class was generated from the following file: