[QGIS Commit] r8145 - in branches/ogr-plugin-branch/src/app: . dbutilities

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue Feb 12 15:06:12 EST 2008


Author: gcontreras
Date: 2008-02-12 15:06:12 -0500 (Tue, 12 Feb 2008)
New Revision: 8145

Added:
   branches/ogr-plugin-branch/src/app/dbutilities/
   branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionparameters.h
   branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.cpp
   branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.h
   branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.cpp
   branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.h
   branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.cpp
   branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.h
   branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp
   branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h
   branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp
   branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h
Log:
Ogr database connection classes refactored and moved from
core to app

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionparameters.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionparameters.h	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionparameters.h	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,42 @@
+/***************************************************************************
+  qgsconnectionparameters.h  Struct to store connection parameters
+  ------------------------------------------------------------
+  Date                 : October 2, 2007
+  Copyright            : (C) 2007 by Godofredo Contreras
+  Email                : frdcn at hotmail dot com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+ 
+ /* $Id$ */
+
+#ifndef QGSCONNECTIONPARAMETERS_H
+#define QGSCONNECTIONPARAMETERS_H
+#include "qstring.h"
+
+struct QgsConnectionParameters
+{
+
+public:
+	QString type;
+	QString name;
+	QString host;
+	QString database;
+	QString port;
+	QString user;
+	QString password;
+	QString uri;
+	bool geometryColumnsOnly;
+	bool publicOnly;
+	QString selected;
+	bool save;
+
+};
+#endif 

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.cpp
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.cpp	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.cpp	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,219 @@
+/***************************************************************************
+ qgsconnectionregistry.cpp   Class to save and load connection parameter
+                            from qgis settings. 
+                              -------------------
+  begin                : October 2, 2007
+  copyright            : (C) 2007 by Godofredo Contreras
+  email                : frdcn at hotmail dot com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+/* $Id$ */
+
+#include "QgsConnectionRegistry.h"
+#include "qstringlist.h"
+#include "qsettings.h"
+
+
+
+
+QgsConnectionRegistry::QgsConnectionRegistry(){
+
+}
+
+
+/**
+ * Class destructor
+ */
+QgsConnectionRegistry::~QgsConnectionRegistry(){
+
+}
+
+
+/**
+ * gets connection params given a connection type and the connection name
+ */
+QgsConnectionParameters QgsConnectionRegistry::connectionParameters(QString type, QString name)
+{
+   QSettings settings;
+   QString baseKey;
+   QgsConnectionParameters conn;
+  
+   qDebug("QgsConnectionManager::getConnectionDetails : type "+type); 
+  
+   baseKey="/DatabaseConnections/"+type+"/";
+
+  
+   baseKey +=name;
+   conn.name=name;
+   conn.type=type;
+   conn.host=settings.readEntry(baseKey + "/host");
+   conn.database=settings.readEntry(baseKey + "/database");
+   conn.port=settings.readEntry(baseKey + "/port");
+   conn.user=settings.readEntry(baseKey + "/username");
+   conn.password=settings.readEntry(baseKey + "/password");
+   conn.uri=settings.readEntry(baseKey + "/uri");
+   conn.publicOnly=settings.readBoolEntry(baseKey + "/publicOnly");
+   conn.geometryColumnsOnly=settings.readBoolEntry(baseKey + "/geometryColumnsOnly");
+   conn.save=settings.readBoolEntry(baseKey + "/save");
+   qDebug("QgsConnectionManager::getConnectionDetails : host "+conn.host); 
+   return conn;  
+}
+
+
+/**
+ * returns a list of connection names of the given type
+ */
+QStringList QgsConnectionRegistry::connectionParametersList(QString type)
+{
+  QSettings settings;
+  QString baseKey;
+  
+  qDebug("QgsConnectionManager::getConnections : type "+type); 
+  
+  baseKey="/DatabaseConnections/"+type+"/";
+  
+  QStringList keys = settings.subkeyList(baseKey);
+  QStringList connections;
+  QStringList::Iterator it = keys.begin();
+  while (it != keys.end())
+  {
+    connections << *it;
+    ++it;
+  }
+  
+  return connections;
+   
+}
+
+
+/**
+ * Returns the connection params selected of the given type
+ */
+QString QgsConnectionRegistry::selected(QString type){
+  QSettings settings;
+  QString result;
+  QString basekey;
+    
+  basekey="/DatabaseConnections/"+type+"/selected";
+  result=settings.readEntry(basekey);                                      
+     
+  return result;
+}
+
+
+/**
+ * Returns the selected connection type
+ */
+QString QgsConnectionRegistry::selectedType(){
+  QSettings settings;
+  QString result;
+
+  result=settings.readEntry("/DatabaseConnections/SelectedConnectionType/selected");                                      
+  return result; 
+}
+
+
+/**
+ * Removes connection params
+ */
+bool QgsConnectionRegistry::removeConnectionParameters(QString type, QString name)
+{
+   QSettings settings; 
+   QString baseKey;
+   
+   baseKey="/DatabaseConnections/"+type+"/";
+   
+     
+   qDebug("QgsConnectionManager::removeConnection baseKey : "+baseKey);                           
+   qDebug("QgsConnectionManager::removeConnection Connection : "+name);
+   
+   baseKey += name; 
+   settings.removeEntry(baseKey + "/host");
+   settings.removeEntry(baseKey + "/database");
+   settings.removeEntry(baseKey + "/username");
+   settings.removeEntry(baseKey + "/password");
+   settings.removeEntry(baseKey + "/uri");
+   settings.removeEntry(baseKey + "/port");
+   settings.removeEntry(baseKey + "/save");
+   settings.removeEntry(baseKey + "/publicOnly");
+   settings.removeEntry(baseKey + "/geometryColumnsOnly");
+   settings.removeEntry(baseKey);
+     
+   return true;
+}
+
+
+/**
+ * Modifies the params of a certain connection
+ */
+bool QgsConnectionRegistry::modifyConnectionParameters(QgsConnectionParameters conn){
+
+	return false;
+}
+
+
+bool QgsConnectionRegistry::saveConnection(QgsConnectionParameters conn)
+{
+     QSettings settings; 
+     QString baseKey;
+   
+   baseKey="/DatabaseConnections/"+conn.type+"/";
+
+   qDebug("baseKey : "+baseKey);                           
+   qDebug("Connection : "+conn.name);
+   settings.writeEntry(baseKey + "selected", conn.selected);
+   baseKey += conn.name;
+   settings.writeEntry(baseKey + "/host", conn.host);
+   settings.writeEntry(baseKey + "/database", conn.database);
+   settings.writeEntry(baseKey + "/port", conn.port);
+   settings.writeEntry(baseKey + "/username", conn.user);
+   settings.writeEntry(baseKey + "/password", conn.password);
+   settings.writeEntry(baseKey + "/uri", conn.uri);
+   settings.writeEntry(baseKey + "/publicOnly", conn.publicOnly);
+   settings.writeEntry(baseKey + "/geometryColumnsOnly", conn.geometryColumnsOnly);
+   if (conn.save)
+     {
+      settings.writeEntry(baseKey + "/save", "true");
+     } else
+     {
+      settings.writeEntry(baseKey + "/save", "false");
+     }
+      
+
+   return true;
+}
+
+
+/**
+ * Sets the connection params selected of a given type
+ */
+void QgsConnectionRegistry::setSelected(QString type, QString name){
+   QSettings settings;
+   QString baseKey;
+    
+   baseKey="/DatabaseConnections/"+type+"/";
+   settings.writeEntry(baseKey+"selected",name);     
+
+}
+
+
+/**
+ * Sets the type selected
+ */
+void QgsConnectionRegistry::setSelectedType(QString type){
+   QSettings settings;
+   QString baseKey;
+    
+   baseKey="/DatabaseConnections/SelectedConnectionType/";                                      
+   settings.writeEntry(baseKey+"selected",type);    
+
+}

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.h	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsconnectionregistry.h	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,51 @@
+/***************************************************************************
+ qgsconnectionregistry.cpp   Class to save and load connection parameter
+                            from qgis settings. 
+                              -------------------
+  begin                : October 2, 2007
+  copyright            : (C) 2007 by Godofredo Contreras
+  email                : frdcn at hotmail dot com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+ /* $Id$ */
+ 
+#ifndef QGSCONNECTIONREGISTRY_H
+#define QGSCONNECTIONREGISTRY_H
+
+
+#include "qgsconnectionparameters.h"
+#include "qstringlist.h"
+
+/**
+ * Class to save, modify and delete qgis database connection params wich are
+ * stored in qgis registry. Also this class have methods to obtain a list of
+ * connections of certain type and to set and get the last connection used and its
+ * type.
+ */
+class QgsConnectionRegistry
+{
+
+public:
+	QgsConnectionRegistry();
+	~QgsConnectionRegistry();
+	QgsConnectionParameters connectionParameters(QString type, QString name);
+	QStringList connectionParametersList(QString type);
+	QString selected(QString type);
+	QString selectedType();
+	bool removeConnectionParameters(QString type, QString name);
+	bool modifyConnectionParameters(QgsConnectionParameters conn);
+	bool saveConnection(QgsConnectionParameters conn);
+	void setSelected(QString type, QString name);
+	void setSelectedType(QString type);
+
+};
+#endif //define QGSCONNECTIONREGISTRY_H

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.cpp
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.cpp	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.cpp	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,114 @@
+/***************************************************************************
+    qgsdatabaseconnection.cpp - Spatial Database connection interface
+     --------------------------------------
+    Date                 : 10-Oct-2007
+    Copyright            : (C) 2007 by Godofredo Contreras
+    email                : frdc at hotmail dot com
+***************************************************************************
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+*                                                                         *
+***************************************************************************/
+
+/* $Id$ */
+
+
+#include "QgsDatabaseConnection.h"
+
+
+QgsDatabaseConnection::QgsDatabaseConnection(){
+
+}
+
+
+
+
+
+QgsDatabaseConnection::QgsDatabaseConnection(QgsConnectionParameters* connectionParameters){
+  mConnectionParameters=connectionParameters;
+}
+
+
+/**
+ * Destructor for class
+ */
+QgsDatabaseConnection::~QgsDatabaseConnection(){
+
+}
+
+
+/**
+ * Connects to database
+ */
+bool QgsDatabaseConnection::connect(){
+
+	return false;
+}
+
+
+/**
+ * Returns base key for connection
+ */
+QString QgsDatabaseConnection::baseKey(){
+}
+
+
+/**
+ * Return error message.
+ */
+QString QgsDatabaseConnection::error(){
+   return mError;     
+}
+
+
+/**
+ * Returns a list of geometry tables available in the database.
+ */
+GeometryColumns QgsDatabaseConnection::geometryTables(bool searchGeometryColumnsOnly, bool searchPublicSchemaOnly){
+}
+
+
+/**
+ * Returns the geometry of a given table
+ */
+QString QgsDatabaseConnection::tableGeometry(QString tableName){
+}
+
+/**
+ * Sets error message  for connection
+ */
+void QgsDatabaseConnection::setError(QString error)
+{
+   mError=error;  
+}
+/**
+ * Sets uri string for connection
+ */
+void QgsDatabaseConnection::setUri(QString uri){
+  mUri=uri;      
+}
+
+/**
+ * Returns uri string for connection
+ */
+QString QgsDatabaseConnection::uri(){
+  return mUri;      
+}
+/**
+ * Disconnect from database
+ */
+void QgsDatabaseConnection::disconnect(){
+
+}
+
+
+/**
+ * Returns true if it is connected to database.
+ */
+bool QgsDatabaseConnection::isConnected(){
+
+	return false;
+}

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.h	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsdatabaseconnection.h	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,71 @@
+/***************************************************************************
+    qgsdatabaseconnection.h - Spatial Database connection interface
+     --------------------------------------
+    Date                 : 10-Oct-2007
+    Copyright            : (C) 2007 by Godofredo Contreras
+    Email                : frdc at hotmail dot com
+***************************************************************************
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+*                                                                         *
+***************************************************************************/
+
+/* $Id$ */
+
+
+#ifndef QGSDATABASECONNECTION_H
+#define QGSDATABASECONNECTION_H
+
+#include "qstringlist.h"
+#include "qgsconnectionparameters.h"
+
+typedef std::pair<QString, QString> GeometryPair;
+typedef std::list<GeometryPair > GeometryColumns;
+
+/**
+ * Interface for classes that will connect to spatial databases
+ */
+class QgsDatabaseConnection
+{
+
+public:
+	QgsDatabaseConnection();
+
+	QgsDatabaseConnection(QgsConnectionParameters* connectionParameters);
+	virtual ~QgsDatabaseConnection();
+	virtual bool connect();
+	QString baseKey();
+	QString error();
+	virtual GeometryColumns geometryTables(bool searchGeometryColumnsOnly, bool searchPublicSchemaOnly);
+	virtual QString tableGeometry(QString tableName);
+	void setError(QString error);
+	void setUri(QString uri);
+	QString uri();
+	void disconnect();
+	bool isConnected();
+	
+
+protected:
+	/**
+	 * Variable to store the connection QgsConnectionParams struct, passed in the
+	 * constructor or through.
+	 */
+	QgsConnectionParameters* mConnectionParameters;
+	/**
+	 * Variable to store the registry base key for the connection
+	 */
+	QString mBaseKey;
+	/**
+	 * Variable to store error messages
+	 */
+	QString mError;
+	/**
+	 * Variable to store uri string
+	 */
+	QString mUri;
+
+};
+#endif 

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.cpp
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.cpp	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.cpp	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,69 @@
+/***************************************************************************
+                             qgsgeomcolumntypethread.cpp  
+A class that determines the geometry type of a given database 
+schema.table.column, with the option of doing so in a separate
+thread.
+                              -------------------
+begin                : Sat Jun 22 2002
+copyright            : (C) 2002 by Gary E.Sherman
+email                : sherman at mrcc.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "qgsgeomcolumntypethread.h" 
+
+void QgsGeomColumnTypeThread::setConnInfo(QString s)
+{
+  mConnInfo = s;
+}
+
+void QgsGeomColumnTypeThread::setGeometryColumn(QString schema, QString table, QString column)
+{
+  schemas.push_back(schema);
+  tables.push_back(table);
+  columns.push_back(column);
+}
+
+void QgsGeomColumnTypeThread::getLayerTypes()
+{
+  PGconn *pd = PQconnectdb(mConnInfo.toLocal8Bit().data());
+  if (PQstatus(pd) == CONNECTION_OK)
+  {
+    PQsetClientEncoding(pd, "UNICODE");
+
+    for (uint i = 0; i < schemas.size(); ++i)
+    {
+      QString query = makeGeomQuery(schemas[i],
+                                    tables[i],
+                                    columns[i]);
+      PGresult* gresult = PQexec(pd, query.toLocal8Bit().data());
+      QString type;
+      if (PQresultStatus(gresult) == PGRES_TUPLES_OK)
+        type = PQgetvalue(gresult, 0, 0);
+      PQclear(gresult);
+
+      // Now tell the layer list dialog box...
+      emit setLayerType(schemas[i], tables[i], columns[i], type);
+    }
+  }
+
+  PQfinish(pd);
+}
+
+QString QgsGeomColumnTypeThread::makeGeomQuery(QString schema, 
+                                                QString table, QString column)
+{
+  QString query = "select GeometryType(\"" + column + "\") from ";
+  if (schema.length() > 0)
+    query += "\"" + schema + "\".";
+  query += "\"" + table + "\" where \"" + column + "\" is not null limit 1";
+  return query;
+}

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.h	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsgeomcolumntypethread.h	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,66 @@
+/***************************************************************************
+                             qgsgeomcolumntypethread.h  
+A class that determines the geometry type of a given database 
+schema.table.column, with the option of doing so in a separate
+thread.
+                              -------------------
+begin                : Sat Jun 22 2002
+copyright            : (C) 2002 by Gary E.Sherman
+email                : sherman at mrcc.com
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+
+#ifndef QGSGEOMCOLUMNTYPETHREAD_H
+#define QGSGEOMCOLUMNTYPETHREAD_H
+
+#include "qthread.h"
+#include "qstring.h"
+#include <vector>
+
+extern "C"
+{
+#include <libpq-fe.h>
+}
+// Perhaps this class should be in its own file??
+//
+// A class that determines the geometry type of a given database 
+// schema.table.column, with the option of doing so in a separate
+// thread.
+
+class QgsGeomColumnTypeThread : public QThread
+{
+  Q_OBJECT
+ public:
+
+  void setConnInfo(QString s);
+  void setGeometryColumn(QString schema, QString table, QString column);
+
+  // These functions get the layer types and pass that information out
+  // by emitting the setLayerType() signal. The getLayerTypes()
+  // function does the actual work, but use the run() function if you
+  // want the work to be done as a separate thread from the calling
+  // process. 
+  virtual void run() { getLayerTypes(); }
+  void getLayerTypes();
+
+  signals:
+  void setLayerType(QString schema, QString table, QString column,
+                    QString type);
+
+ private:
+  QString mConnInfo;
+  std::vector<QString> schemas, tables, columns;
+  QString makeGeomQuery(QString schema, 
+                        QString table, QString column);
+};
+
+#endif

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.cpp	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,132 @@
+/***************************************************************************
+    qgsogrdatabaseconnection.cpp - Connection class for ogr databases
+     --------------------------------------
+    Date                 : 10-Oct-2007
+    Copyright            : (C) 2007 by Godofredo Contreras
+    email                : frdc at hotmail dot com
+***************************************************************************
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+*                                                                         *
+***************************************************************************/
+
+ /* $Id$ */
+
+#include "qgsogrdatabaseconnection.h"
+#include "qgsdatasourceuri.h"
+
+
+QgsOgrDatabaseConnection::QgsOgrDatabaseConnection(){
+
+}
+
+
+
+
+
+/**
+ * Class constructor
+ */
+QgsOgrDatabaseConnection::QgsOgrDatabaseConnection(QgsConnectionParameters* connParameters)
+    : QgsDatabaseConnection(connParameters){                            
+  //generate uri according to connParameters                                
+
+}
+
+
+/**
+ * Class destructor
+ */
+QgsOgrDatabaseConnection::~QgsOgrDatabaseConnection(){
+
+}
+
+
+/**
+ * Connects to ogr database
+ */
+bool QgsOgrDatabaseConnection::connect(){
+  QString uri;   
+  bool result=true;   
+  //QgsURIManager* uriman= new QgsURIManager(mConnectionParameters);   
+  //qDebug("QgsOgrDatabaseConnection::connect: "+ mConnection.name);
+  //qDebug("QgsOgrDatabaseConnection::connect: "+ uriman->getURI());
+  if ((this->uri().isEmpty())or(this->uri().isNull()))
+   {
+     //generateURI According to connectionParameters
+     qDebug("not generic");
+     QgsDataSourceURI dsUri;
+      dsUri.setConnection(mConnectionParameters->type, 
+             mConnectionParameters->host,
+		     mConnectionParameters->port,
+		     mConnectionParameters->database,
+		     mConnectionParameters->user,
+		     mConnectionParameters->password );
+	   uri=dsUri.uri();  
+   } 
+  else
+   {
+     uri=this->uri();            
+   } 
+  //clean all previous errors
+  CPLErrorReset(); 
+  // Register all OGR-drivers
+  OGRRegisterAll();
+  OGRDataSource *poDS;
+  poDS = OGRSFDriverRegistrar::Open(uri, FALSE );
+
+  if( poDS == NULL ) {
+       QString error=QObject::tr( "Open failed.\n" );
+	   error=error + QString (CPLGetLastErrorMsg());
+	   setError(error);
+       return (false);
+    } 
+  OGRDataSource::DestroyDataSource( poDS );
+  return result;       
+}
+
+
+/**
+ * Returns the base key for ogr database type connections
+ */
+QString QgsOgrDatabaseConnection::baseKey(){
+
+	return  NULL;
+}
+
+
+/**
+ * Returns the geometry tables of OGR database
+ */
+QStringList QgsOgrDatabaseConnection::geometryTables(){
+
+	//return  NULL;
+}
+
+
+/**
+ * Returns the geometry given a table name
+ */
+QString QgsOgrDatabaseConnection::tableGeometry(QString tableName){
+
+	return  NULL;
+}
+
+
+/**
+ * Disconnects from OGR database
+ */
+void QgsOgrDatabaseConnection::disconnect(){
+
+}
+
+
+/**
+ * Returs true if connected to OGR database
+ */
+void QgsOgrDatabaseConnection::isConnected(){
+
+}

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgsogrdatabaseconnection.h	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,46 @@
+/***************************************************************************
+    qgsogrdatabaseconnection.cpp - Connection class for ogr databases
+     --------------------------------------
+    Date                 : 10-Oct-2007
+    Copyright            : (C) 2007 by Godofredo Contreras
+    email                : frdc at hotmail dot com
+***************************************************************************
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+*                                                                         *
+***************************************************************************/
+
+#ifndef QGSOGRDATABASECONNECTION_H
+#define QGSOGRDATABASECONNECTION_H
+
+#include "qgsdatabaseconnection.h"
+#include "qgsconnectionparameters.h"
+
+#include "cpl_port.h"
+#include <ogr_api.h>
+#include <ogrsf_frmts.h>
+
+/**
+ * Class to connect to databases using OGR
+ */
+class QgsOgrDatabaseConnection : public QgsDatabaseConnection
+{
+
+public:
+	QgsOgrDatabaseConnection();
+
+	QgsOgrDatabaseConnection(QgsConnectionParameters* connectionParameters);
+	~QgsOgrDatabaseConnection();
+	bool connect();
+	QString baseKey();
+	QStringList geometryTables();
+	QString tableGeometry(QString tableName);
+	void disconnect();
+	void isConnected();
+	
+
+};
+#endif // !defined(EA_55811BBB_229C_4411_8F41_521DE4B175F7__INCLUDED_)

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.cpp	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,316 @@
+/***************************************************************************
+    qgspostgresdatabaseconnection.cpp - Connection Class for Postgres
+     --------------------------------------
+    Date                 : 10-Oct-2007
+    Copyright            : (C) 2007 by Godofredo Contreras
+    email                : frdc at hotmail dot com
+***************************************************************************
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+*                                                                         *
+***************************************************************************/
+
+ /* $Id$ */
+
+#include "qgspostgresdatabaseconnection.h"
+#include "qgsdatasourceuri.h"
+#include "qstring.h"
+
+
+QgsPostgresDatabaseConnection::QgsPostgresDatabaseConnection(){
+
+}
+
+
+
+
+
+QgsPostgresDatabaseConnection::QgsPostgresDatabaseConnection(QgsConnectionParameters* conn)
+    : QgsDatabaseConnection(conn){
+
+}
+
+
+QgsPostgresDatabaseConnection::~QgsPostgresDatabaseConnection(){
+  // free pg connection resources
+  PQfinish(pd);                                                                
+}
+
+
+bool QgsPostgresDatabaseConnection::connect(){
+  // Need to escape the password to allow for single quotes and backslashes
+  qDebug("QgsPostgresDatabaseConnection::connect");
+  bool result=false;
+  QString pass = mConnectionParameters->password;
+  pass.replace('\\', "\\\\");
+  pass.replace('\'', "\\'");
+
+  QString connInfo =
+    "host=" + mConnectionParameters->host + 
+    " dbname=" + mConnectionParameters->database + 
+    " port=" + mConnectionParameters->port +
+    " user=" + mConnectionParameters->user + 
+    " password='" + pass + "'";
+  PGconn *pd = PQconnectdb(connInfo.toLocal8Bit().data());
+  //  std::cout << pd->ErrorMessage();
+  if (PQstatus(pd) == CONNECTION_OK)
+    {
+      // Database successfully opened; we can now issue SQL commands.
+      PQsetClientEncoding(pd, "UNICODE");
+      result=true;
+  } else
+    {
+      mError=QObject::tr("Connection failed - Check settings and try again.\n\nExtended error information:\n");
+      qDebug (QString(PQerrorMessage(pd)));
+      mError=mError + QString(PQerrorMessage(pd));
+      qDebug (mError);
+      result=false;
+    }
+  
+   
+  return result;     
+}
+
+
+QString QgsPostgresDatabaseConnection::fullDescription(QString schema, QString table, QString column){
+
+	return  NULL;
+}
+
+
+QString QgsPostgresDatabaseConnection::baseKey(){
+
+	return  NULL;
+}
+
+
+bool QgsPostgresDatabaseConnection::geometryColumnInformation(PGconn* pg, GeometryColumns& details, bool searchGeometryColumnsOnly, bool searchPublicOnly){
+{
+  bool ok = false;
+
+
+  QString sql = "select * from geometry_columns";
+  // where f_table_schema ='" + settings.readEntry(key + "/database") + "'";
+  sql += " order by f_table_schema,f_table_name";
+  //qDebug("Fetching tables using: " + sql);
+  PGresult *result = PQexec(pg, sql.toLocal8Bit().data());
+  if (result)
+  {
+    QString msg;
+    msg=msg+"Fetched "+(QString)PQntuples(result)+" tables from database";
+    //QTextOStream(&msg) << "Fetched " << PQntuples(result) << " tables from database";
+    //qDebug(msg);
+    for (int idx = 0; idx < PQntuples(result); idx++)
+    {
+      // Be a bit paranoid and check that the table actually
+      // exists. This is not done as a subquery in the query above
+      // because I can't get it to work correctly when there are tables
+      // with capital letters in the name.
+
+      // Take care to deal with tables with the same name but in different schema.
+      QString tableName = PQgetvalue(result, idx, PQfnumber(result, "f_table_name"));
+      QString schemaName = PQgetvalue(result, idx, PQfnumber(result, "f_table_schema"));
+      sql = "select oid from pg_class where relname = '" + tableName + "'";
+      if (schemaName.length() > 0)
+	sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
+	  schemaName + "')";
+
+      PGresult* exists = PQexec(pg, sql.toLocal8Bit().data());
+      if (PQntuples(exists) == 1)
+      {
+        QString column = PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column"));
+        QString type = PQgetvalue(result, idx, PQfnumber(result, "type"));
+        
+	QString as = "";
+	if(type=="GEOMETRY" && !searchGeometryColumnsOnly) {
+	  //fredaddSearchGeometryColumn(schemaName, tableName,  column);
+	  as=type="WAITING";
+	}
+
+	//freddetails.push_back(geomPair(fullDescription(schemaName, tableName, column, as), type));
+      }
+      PQclear(exists);
+    }
+    ok = true;
+  }
+  PQclear(result);
+
+
+  if (searchGeometryColumnsOnly)
+    return ok;
+
+  // Now have a look for geometry columns that aren't in the
+  // geometry_columns table. This code is specific to postgresql,
+  // but an equivalent query should be possible in other
+  // databases.
+  sql = "select pg_class.relname, pg_namespace.nspname, pg_attribute.attname, "
+    "pg_class.relkind from "
+    "pg_attribute, pg_class, pg_type, pg_namespace where pg_type.typname = 'geometry' and "
+    "pg_attribute.atttypid = pg_type.oid and pg_attribute.attrelid = pg_class.oid ";
+
+  if (searchPublicOnly)
+    sql += "and pg_namespace.nspname = 'public' ";
+
+  sql += "and cast(pg_class.relname as character varying) not in "
+    "(select f_table_name from geometry_columns) "
+    "and pg_namespace.oid = pg_class.relnamespace "
+    "and pg_class.relkind in ('v', 'r')"; // only from views and relations (tables)
+  
+  result = PQexec(pg, sql.toLocal8Bit().data());
+
+  for (int i = 0; i < PQntuples(result); i++)
+  {
+    // Have the column name, schema name and the table name. The concept of a
+    // catalog doesn't exist in postgresql so we ignore that, but we
+    // do need to get the geometry type.
+
+    // Make the assumption that the geometry type for the first
+    // row is the same as for all other rows. 
+
+    QString table  = PQgetvalue(result, i, 0); // relname
+    QString schema = PQgetvalue(result, i, 1); // nspname
+    QString column = PQgetvalue(result, i, 2); // attname
+    QString relkind = PQgetvalue(result, i, 3); // relation kind
+
+    //addSearchGeometryColumn(schema, table, column);
+    //details.push_back(GeometryPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
+  }
+  ok = true;
+
+  PQclear(result);
+
+  return ok;
+}
+}
+
+
+GeometryColumns QgsPostgresDatabaseConnection::geometryTables(bool searchGeometryColumnsOnly, bool searchPublicSchemaOnly){
+  GeometryColumns details;          
+  if(geometryColumnInformation(pd,details, searchGeometryColumnsOnly, searchPublicSchemaOnly))	
+      {
+       //check the thread
+      }
+  else
+    {
+      qDebug("Unable to get list of spatially enabled tables from the database");
+      qDebug(PQerrorMessage(pd));
+    }                  
+  return  details;
+}
+
+
+QString QgsPostgresDatabaseConnection::tableGeometry(QString tableName){
+
+	return  NULL;
+}
+
+
+QString QgsPostgresDatabaseConnection::makeGeometryQuery(QString schema, QString table, QString column){
+
+	return  NULL;
+}
+
+//******************************************
+
+/*bool QgsPostgresDatabaseConnection::geometryTableInformation(PGconn *pg, bool searchGeometryColumnsOnly, bool searchPublicOnly)
+{
+  bool ok = false;
+  
+  QString sql = "select * from geometry_columns";
+  sql += " order by f_table_schema,f_table_name";
+
+  PGresult *result = PQexec(pg, sql.toLocal8Bit().data());
+  if (result)
+  {
+    for (int idx = 0; idx < PQntuples(result); idx++)
+    {
+      // Be a bit paranoid and check that the table actually
+      // exists. This is not done as a subquery in the query above
+      // because I can't get it to work correctly when there are tables
+      // with capital letters in the name.
+
+      // Take care to deal with tables with the same name but in different schema.
+      QString tableName = PQgetvalue(result, idx, PQfnumber(result, "f_table_name"));
+      QString schemaName = PQgetvalue(result, idx, PQfnumber(result, "f_table_schema"));
+      sql = "select oid from pg_class where relname = '" + tableName + "'";
+      if (schemaName.length() > 0)
+	sql +=" and relnamespace = (select oid from pg_namespace where nspname = '" +
+	  schemaName + "')";
+
+      PGresult* exists = PQexec(pg, sql.toLocal8Bit().data());
+      if (PQntuples(exists) == 1)
+      {
+        QString column = PQgetvalue(result, idx, PQfnumber(result, "f_geometry_column"));
+        QString type = PQgetvalue(result, idx, PQfnumber(result, "type"));
+        
+	QString as = "";
+	if(type=="GEOMETRY" && !searchGeometryColumnsOnly) 
+	  {
+	    addSearchGeometryColumn(schemaName, tableName,  column);
+	    as=type="WAITING";
+	  }
+
+	mTableModel.addTableEntry(type, schemaName, tableName, column, "");
+      }
+      PQclear(exists);
+    }
+    ok = true;
+  }
+  PQclear(result);
+
+  //search for geometry columns in tables that are not in the geometry_columns metatable
+  
+
+  QApplication::restoreOverrideCursor();
+  if (searchGeometryColumnsOnly)
+    {
+      return ok;
+    }
+
+   // Now have a look for geometry columns that aren't in the
+  // geometry_columns table. This code is specific to postgresql,
+  // but an equivalent query should be possible in other
+  // databases.
+  sql = "select pg_class.relname, pg_namespace.nspname, pg_attribute.attname, "
+    "pg_class.relkind from "
+    "pg_attribute, pg_class, pg_type, pg_namespace where pg_type.typname = 'geometry' and "
+    "pg_attribute.atttypid = pg_type.oid and pg_attribute.attrelid = pg_class.oid ";
+
+  if (searchPublicOnly)
+    sql += "and pg_namespace.nspname = 'public' ";
+
+  sql += "and cast(pg_class.relname as character varying) not in "
+    "(select f_table_name from geometry_columns) "
+    "and pg_namespace.oid = pg_class.relnamespace "
+    "and pg_class.relkind in ('v', 'r')"; // only from views and relations (tables)
+  
+  result = PQexec(pg, sql.toLocal8Bit().data());
+
+  for (int i = 0; i < PQntuples(result); i++)
+  {
+    // Have the column name, schema name and the table name. The concept of a
+    // catalog doesn't exist in postgresql so we ignore that, but we
+    // do need to get the geometry type.
+
+    // Make the assumption that the geometry type for the first
+    // row is the same as for all other rows. 
+
+    QString table  = PQgetvalue(result, i, 0); // relname
+    QString schema = PQgetvalue(result, i, 1); // nspname
+    QString column = PQgetvalue(result, i, 2); // attname
+    QString relkind = PQgetvalue(result, i, 3); // relation kind
+
+    addSearchGeometryColumn(schema, table, column);
+    //details.push_back(geomPair(fullDescription(schema, table, column, "WAITING"), "WAITING"));
+    mTableModel.addTableEntry("Waiting", schema, table, column, "");
+  }
+  ok = true;
+
+  PQclear(result);
+  return ok;
+}
+
+*/

Added: branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h
===================================================================
--- branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h	                        (rev 0)
+++ branches/ogr-plugin-branch/src/app/dbutilities/qgspostgresdatabaseconnection.h	2008-02-12 20:06:12 UTC (rev 8145)
@@ -0,0 +1,68 @@
+/***************************************************************************
+    qgspostgresdatabaseconnection.cpp - Connection Class for Postgres
+     --------------------------------------
+    Date                 : 10-Oct-2007
+    Copyright            : (C) 2007 by Godofredo Contreras
+    email                : frdc at hotmail dot com
+***************************************************************************
+*                                                                         *
+*   This program is free software; you can redistribute it and/or modify  *
+*   it under the terms of the GNU General Public License as published by  *
+*   the Free Software Foundation; either version 2 of the License, or     *
+*   (at your option) any later version.                                   *
+*                                                                         *
+***************************************************************************/
+
+ /* $Id$ */
+
+
+
+#ifndef QGSPOSTGRESDATABASECONNECTION_H
+#define QGSPOSTGRESDATABASECONNECTION_H
+
+#include "qobject.h"
+#include "qgsgeomcolumntypethread.h"
+#include "qgsconnectionparameters.h"
+#include "qgsdatabaseconnection.h"
+#include "qstringlist.h"
+
+
+
+extern "C"
+{
+#include <libpq-fe.h>
+}
+
+/**
+ * Class to connect to Postgres spatial databases
+ */
+class QgsPostgresDatabaseConnection : public QgsDatabaseConnection
+{
+
+public:
+	typedef std::pair<QString, QString> geomPair;
+
+	typedef std::list<geomPair > geomCol;
+
+
+	QgsPostgresDatabaseConnection();
+
+	QgsPostgresDatabaseConnection(QgsConnectionParameters* conn);
+	~QgsPostgresDatabaseConnection();
+	bool connect();
+	QString baseKey();
+	GeometryColumns geometryTables(bool searchGeometryColumnsOnly, bool searchPublicSchemaOnly);
+	QString tableGeometry(QString tableName);
+
+private:
+	QgsGeomColumnTypeThread* mColumnTypeThread;
+
+	QString fullDescription(QString schema, QString table, QString column);
+//	bool geometryTableInformation(PGconn* pg, GeometryColumns& details, bool searchGeometryColumnsOnly, bool searchPublicOnly);
+	bool geometryColumnInformation(PGconn *pg,GeometryColumns& details, bool searchGeometryColumnsOnly, bool searchPublicOnly);
+	static QString makeGeometryQuery(QString schema, QString table, QString column);
+	
+	PGconn *pd;
+
+};
+#endif 



More information about the QGIS-commit mailing list