2002-07-26 16:48:31 +00:00
|
|
|
/***************************************************************************
|
|
|
|
qgsdbsourceselect.h - description
|
|
|
|
-------------------
|
|
|
|
begin : Sat Jun 22 2002
|
|
|
|
copyright : (C) 2002 by Gary E.Sherman
|
|
|
|
email : sherman@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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
2002-07-06 00:33:05 +00:00
|
|
|
#include <libpq++.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <qsettings.h>
|
2002-07-06 17:22:01 +00:00
|
|
|
#include <qpixmap.h>
|
2002-07-06 00:33:05 +00:00
|
|
|
#include <qlistbox.h>
|
|
|
|
#include <qstringlist.h>
|
|
|
|
#include <qcombobox.h>
|
2002-07-12 04:04:29 +00:00
|
|
|
#include "xpm/point_layer.xpm"
|
|
|
|
#include "xpm/line_layer.xpm"
|
|
|
|
#include "xpm/polygon_layer.xpm"
|
2002-07-06 00:33:05 +00:00
|
|
|
#include "qgsdbsourceselect.h"
|
|
|
|
#include "qgsnewconnection.h"
|
|
|
|
|
|
|
|
QgsDbSourceSelect::QgsDbSourceSelect():QgsDbSourceSelectBase(){
|
|
|
|
QSettings settings;
|
|
|
|
QStringList keys = settings.subkeyList("/Qgis/connections");
|
|
|
|
QStringList::Iterator it = keys.begin();
|
|
|
|
while( it != keys.end() ) {
|
|
|
|
cmbConnections->insertItem(*it);
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
QgsDbSourceSelect::~QgsDbSourceSelect(){
|
|
|
|
}
|
|
|
|
void QgsDbSourceSelect::addNewConnection(){
|
|
|
|
|
|
|
|
QgsNewConnection *nc = new QgsNewConnection();
|
|
|
|
|
|
|
|
if(nc->exec()){
|
|
|
|
}
|
|
|
|
}
|
2002-07-06 17:22:01 +00:00
|
|
|
void QgsDbSourceSelect::editConnection(){
|
|
|
|
|
2002-07-20 22:09:32 +00:00
|
|
|
QgsNewConnection *nc = new QgsNewConnection(cmbConnections->currentText());
|
2002-07-06 17:22:01 +00:00
|
|
|
|
|
|
|
if(nc->exec()){
|
2002-07-20 22:09:32 +00:00
|
|
|
nc->saveConnection();
|
2002-07-06 17:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-07 03:48:55 +00:00
|
|
|
void QgsDbSourceSelect::addTables(){
|
|
|
|
//store the table info
|
|
|
|
for(int idx=0; idx <lstTables->numRows(); idx++){
|
|
|
|
if(lstTables->isSelected(idx))
|
|
|
|
m_selectedTables += lstTables->text(idx);
|
|
|
|
}
|
2002-07-10 04:26:58 +00:00
|
|
|
accept();
|
2002-07-07 03:48:55 +00:00
|
|
|
}
|
2002-07-06 00:33:05 +00:00
|
|
|
void QgsDbSourceSelect::dbConnect(){
|
|
|
|
// populate the table list
|
|
|
|
QSettings settings;
|
2002-07-20 22:09:32 +00:00
|
|
|
|
2002-07-06 00:33:05 +00:00
|
|
|
QString key = "/Qgis/connections/" + cmbConnections->currentText();
|
|
|
|
QString host = "host="+settings.readEntry(key+"/host");
|
|
|
|
QString database = "dbname="+settings.readEntry(key+"/database");
|
|
|
|
QString username = "user="+settings.readEntry(key+"/username");
|
|
|
|
QString password = "password="+settings.readEntry(key+"/password");
|
2002-07-07 03:48:55 +00:00
|
|
|
m_connInfo = host +" " + database + " " + username + " " + password;
|
2002-07-08 03:20:50 +00:00
|
|
|
qDebug(m_connInfo);
|
2002-07-07 03:48:55 +00:00
|
|
|
PgDatabase *pd = new PgDatabase((const char *)m_connInfo);
|
2002-07-30 05:40:02 +00:00
|
|
|
std::cout << pd->ErrorMessage();
|
2002-07-06 00:33:05 +00:00
|
|
|
if(pd->Status()==CONNECTION_OK){
|
2002-07-20 22:09:32 +00:00
|
|
|
// clear the existing entries
|
|
|
|
lstTables->clear();
|
2002-07-06 17:22:01 +00:00
|
|
|
// create the pixmaps for the layer types
|
|
|
|
QPixmap pxPoint;
|
2002-07-12 04:04:29 +00:00
|
|
|
pxPoint = QPixmap(point_layer_xpm);
|
2002-07-06 17:22:01 +00:00
|
|
|
QPixmap pxLine;
|
2002-07-12 04:04:29 +00:00
|
|
|
pxLine = QPixmap(line_layer_xpm);
|
2002-07-06 17:22:01 +00:00
|
|
|
QPixmap pxPoly;
|
2002-07-12 04:04:29 +00:00
|
|
|
pxPoly = QPixmap(polygon_layer_xpm);
|
2002-07-06 00:33:05 +00:00
|
|
|
qDebug("Connection succeeded");
|
|
|
|
// get the list of tables
|
2002-07-06 17:22:01 +00:00
|
|
|
QString sql = "select * from geometry_columns where f_table_schema ='"
|
2002-07-06 00:33:05 +00:00
|
|
|
+ settings.readEntry(key+"/database") + "'";
|
2002-07-11 05:00:18 +00:00
|
|
|
sql += " order by f_table_name";
|
2002-07-06 00:33:05 +00:00
|
|
|
qDebug("Fetching tables using: " + sql);
|
|
|
|
int result = pd->ExecTuplesOk((const char *)sql);
|
|
|
|
if(result){
|
|
|
|
QString msg;
|
|
|
|
QTextOStream(&msg) << "Fetched " << pd->Tuples() << " tables from database";
|
|
|
|
qDebug( msg);
|
|
|
|
for(int idx = 0; idx < pd->Tuples(); idx++){
|
|
|
|
QString v = pd->GetValue(idx,"f_table_name");
|
2002-07-06 17:22:01 +00:00
|
|
|
QString type = pd->GetValue(idx,"type");
|
|
|
|
QPixmap *p;
|
|
|
|
if(type == "POINT" || type == "MULTIPOINT")
|
|
|
|
p = &pxPoint;
|
|
|
|
else
|
|
|
|
if(type == "MULTIPOLYGON" || type == "POLYGON")
|
|
|
|
p = &pxPoly;
|
|
|
|
else
|
|
|
|
if(type == "LINESTRING" || type == "MULTILINESTRING")
|
|
|
|
p = &pxLine;
|
|
|
|
else
|
|
|
|
p = 0;
|
|
|
|
lstTables->insertItem(*p,v);
|
2002-07-06 00:33:05 +00:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
qDebug( "Unable to get list of spatially enabled tables from geometry_columns table");
|
|
|
|
qDebug( pd->ErrorMessage());
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
qDebug( "Connection failed");
|
|
|
|
}
|
|
|
|
}
|
2002-07-07 03:48:55 +00:00
|
|
|
QStringList QgsDbSourceSelect::selectedTables(){
|
|
|
|
return m_selectedTables;
|
|
|
|
}
|
|
|
|
QString QgsDbSourceSelect::connInfo(){
|
|
|
|
return m_connInfo;
|
|
|
|
}
|