Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
support for GEOMETRY column, fixes #818
git-svn-id: http://svn.osgeo.org/qgis/trunk@7611 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 19, 2007
1 parent 3117592 commit 6ba8b2a
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 38 deletions.
127 changes: 95 additions & 32 deletions src/app/qgsdbsourceselect.cpp
Expand Up @@ -20,6 +20,7 @@ email : sherman at mrcc.com
#include "qgsdbsourceselect.h"

#include "qgisapp.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgscontexthelp.h"
#include "qgsnewconnection.h"
Expand Down Expand Up @@ -105,9 +106,11 @@ QgsDbSourceSelect::QgsDbSourceSelect(QgisApp *app, Qt::WFlags fl)

// Do some things that couldn't be done in designer
lstTables->horizontalHeader()->setStretchLastSection(true);
// Set the column count to 3 for the type, name, and sql
lstTables->setColumnCount(3);
// Set the column count
lstTables->setColumnCount(dbssColumns);
mColumnLabels += "";
mColumnLabels += tr("Type");
mColumnLabels += tr("Import As");
mColumnLabels += tr("Name");
mColumnLabels += tr("Sql");
lstTables->setHorizontalHeaderLabels(mColumnLabels);
Expand Down Expand Up @@ -155,16 +158,41 @@ void QgsDbSourceSelect::on_cmbConnections_activated(int)
dbChanged();
}

void QgsDbSourceSelect::updateImportAsInfo(int row, const QString &type)
{
if(type=="WAITING")
return;

QComboBox *cb = static_cast<QComboBox *>(lstTables->cellWidget(row,dbssImport));
if(cb)
lstTables->removeCellWidget(row, dbssImportAs);
else
{
QTableWidgetItem *item = lstTables->takeItem(row,dbssImportAs);
delete item;
}

if( type=="GEOMETRY")
{
cb = new QComboBox(lstTables);
cb->addItem( mLayerIcons.value("POINT").second, mLayerIcons.value("POINT").first);
cb->addItem( mLayerIcons.value("LINESTRING").second, mLayerIcons.value("LINESTRING").first);
cb->addItem( mLayerIcons.value("POLYGON").second, mLayerIcons.value("POLYGON").first);
cb->setCurrentIndex(0);
cb->setToolTip( tr("select import type for multi type layer") );
lstTables->setCellWidget(row, dbssImportAs, cb);
}
else
{
lstTables->setItem(row, dbssImportAs, new QTableWidgetItem(*lstTables->item(row,dbssType)));
}
}

void QgsDbSourceSelect::setLayerType(QString schema,
QString table, QString column,
QString type)
{
#ifdef QGISDEBUG
std::cerr << "Received layer type of " << type.toLocal8Bit().data()
<< " for "
<< (schema+'.'+table+'.'+column).toLocal8Bit().data()
<< '\n';
#endif
QgsDebugMsg("Received layer type of " + type + " for " + schema + '.' + table + '.' + column);

// Find the right row in the table by searching for the text that
// was put into the Name column.
Expand All @@ -176,7 +204,7 @@ void QgsDbSourceSelect::setLayerType(QString schema,
if (ii.count() > 0)
{
int row = lstTables->row(ii.at(0)); // just use the first one
QTableWidgetItem* iconItem = lstTables->item(row, 0);
QTableWidgetItem* iconItem = lstTables->item(row, dbssType);

// Get the icon and tooltip text
const QIcon* p;
Expand All @@ -188,13 +216,15 @@ void QgsDbSourceSelect::setLayerType(QString schema,
}
else
{
qDebug(("Unknown geometry type of '" + type + "'.").toLocal8Bit().data());
QgsDebugMsg("Unknown geometry type of '" + type + "'.");
p = &(mLayerIcons.value("UNKNOWN").second);
toolTipText = mLayerIcons.value("UNKNOWN").first;
}

iconItem->setIcon(*p);
iconItem->setToolTip(toolTipText);

updateImportAsInfo(row, type);
}
}

Expand Down Expand Up @@ -275,13 +305,40 @@ void QgsDbSourceSelect::addTables()

for (int i = 0; i < lstTables->rowCount(); ++i)
{
if (lstTables->isItemSelected(lstTables->item(i, 0)))
if ( static_cast<QCheckBox *>(lstTables->cellWidget(i, dbssImport))->isChecked() )
{
QString table = lstTables->item(i,1)->text() + " sql=";
QTableWidgetItem* sqlItem = lstTables->item(i,2);
if (sqlItem)
table += sqlItem->text();
m_selectedTables += table;
QString table = lstTables->item(i,dbssDetail)->text();
QString query = table + " sql=";

QComboBox *cb = static_cast<QComboBox *>( lstTables->cellWidget(i, dbssImportAs) );
if(cb) {
int i = table.find("(");
int j = table.find(")");
QString column = table.mid(i+1,j-i-1);

switch(cb->currentIndex() ) {
case 0:
query += QString("GeometryType(\"%1\") IN ('POINT','MULTIPOINT')").arg(column);
break;
case 1:
query += QString("GeometryType(\"%1\") IN ('LINESTRING','MULTILINESTRING')").arg(column);
break;
case 2:
query += QString("GeometryType(\"%1\") IN ('POLYGON','MULTIPOLYGON')").arg(column);
break;
}
}

QTableWidgetItem *sqlItem = lstTables->item(i, dbssSql);
if (sqlItem && sqlItem->text()!="" )
{
if(cb)
query += QString(" AND (%1)").arg( sqlItem->text() );
else
query += sqlItem->text();
}

m_selectedTables += query;
}
}

Expand All @@ -306,7 +363,8 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
QString database = settings.readEntry(key + "/database");
connString += database + " port=";
QString port = settings.readEntry(key + "/port");
if(port.length() == 0){
if(port.length() == 0)
{
port = "5432";
}
connString += port + " user=";
Expand All @@ -330,9 +388,9 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
password.replace('\\', "\\\\");
password.replace('\'', "\\'");
connString += " password='" + password + "'";
#ifdef QGISDEBUG
std::cout << "Connection info: " << connString.toLocal8Bit().data() << std::endl;
#endif

QgsDebugMsg("Connection info: " + connString);

if (makeConnection)
{
m_connInfo = connString; //host + " " + database + " " + username + " " + password;
Expand Down Expand Up @@ -415,21 +473,30 @@ void QgsDbSourceSelect::on_btnConnect_clicked()
}
else
{
qDebug(("Unknown geometry type of '" + iter->second + "'.").toLocal8Bit().data());
QgsDebugMsg("Unknown geometry type of '" + iter->second + "'.");
p = &(mLayerIcons.value("UNKNOWN").second);
toolTipText = mLayerIcons.value("UNKNOWN").first;
}

if (p != 0)
{
int row = lstTables->rowCount();
lstTables->setRowCount(row+1);

QCheckBox *cb = new QCheckBox();
cb->setToolTip(tr("check to import layer"));
lstTables->setCellWidget(row, dbssImport, cb);

QTableWidgetItem *iconItem = new QTableWidgetItem();
iconItem->setIcon(*p);
iconItem->setToolTip(toolTipText);
lstTables->setItem(row, dbssType, iconItem);

QTableWidgetItem *textItem = new QTableWidgetItem(iter->first);
int row = lstTables->rowCount();
lstTables->setRowCount(row+1);
lstTables->setItem(row, 0, iconItem);
lstTables->setItem(row, 1, textItem);
textItem->setToolTip( tr("double click to open PostgreSQL query builder") );
lstTables->setItem(row, dbssDetail, textItem);

updateImportAsInfo(row, iter->second);
}
}

Expand Down Expand Up @@ -487,9 +554,9 @@ QString QgsDbSourceSelect::connInfo()
void QgsDbSourceSelect::setSql(QTableWidgetItem *item)
{
int row = lstTables->row(item);
QString tableText = lstTables->item(row, 1)->text();
QString tableText = lstTables->item(row, dbssDetail)->text();

QTableWidgetItem* sqlItem = lstTables->item(row, 2);
QTableWidgetItem* sqlItem = lstTables->item(row, dbssSql);
QString sqlText;
if (sqlItem)
sqlText = sqlItem->text();
Expand All @@ -511,13 +578,9 @@ void QgsDbSourceSelect::setSql(QTableWidgetItem *item)
if (!sqlItem)
{
sqlItem = new QTableWidgetItem();
lstTables->setItem(row, 2, sqlItem);
lstTables->setItem(row, dbssSql, sqlItem);
}
sqlItem->setText(pgb->sql());
// Ensure that the current row remains selected
lstTables->setItemSelected(lstTables->item(row,0), true);
lstTables->setItemSelected(lstTables->item(row,1), true);
lstTables->setItemSelected(lstTables->item(row,2), true);
}
// delete the query builder object
delete pgb;
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgsdbsourceselect.h
Expand Up @@ -92,6 +92,14 @@ class QgsDbSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
void setLayerType(QString schema, QString table, QString column,
QString type);
private:
enum columns {
dbssImport=0,
dbssType,
dbssImportAs,
dbssDetail,
dbssSql,
dbssColumns,
};

typedef std::pair<QString, QString> geomPair;
typedef std::list<geomPair > geomCol;
Expand All @@ -105,6 +113,8 @@ class QgsDbSourceSelect : public QDialog, private Ui::QgsDbSourceSelectBase
void setConnectionListPosition();
// Show the context help for the dialog
void showHelp();
// update 'import as column'
void updateImportAsInfo(int row, const QString &type);
// Combine the schema, table and column data into a single string
// useful for display to the user
QString fullDescription(QString schema, QString table, QString column);
Expand Down
21 changes: 21 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2525,6 +2525,27 @@ bool QgsPostgresProvider::getGeometryDetails()
if (!srid.isEmpty() && !fType.isEmpty())
{
valid = true;
if(fType == "GEOMETRY")
{
// check to see if there is a unique geometry type
sql = QString("select distinct "
"case"
" when geometrytype(%1) IN ('POINT','MULTIPOINT') THEN 'POINT'"
" when geometrytype(%1) IN ('LINESTRING','MULTILINESTRING') THEN 'LINESTRING'"
" when geometrytype(%1) IN ('POLYGON','MULTIPOLYGON') THEN 'POLYGON'"
" end "
"from %2").arg(geometryColumn).arg(mSchemaTableName);
if(mUri.sql!="")
sql += " where " + mUri.sql;

result = executeDbCommand(connection, sql);

if (PQntuples(result)==1)
{
fType = PQgetvalue(result, 0, 0);
}
PQclear(result);
}
if (fType == "POINT")
{
geomType = QGis::WKBPoint;
Expand Down
6 changes: 0 additions & 6 deletions src/ui/qgsdbsourceselectbase.ui
Expand Up @@ -129,12 +129,6 @@
<property name="editTriggers" >
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="selectionMode" >
<enum>QAbstractItemView::MultiSelection</enum>
</property>
<property name="selectionBehavior" >
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid" >
<bool>false</bool>
</property>
Expand Down

0 comments on commit 6ba8b2a

Please sign in to comment.