Skip to content

Commit

Permalink
update spit plugin, fixes #593
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@7457 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Nov 18, 2007
1 parent aaa9ddf commit bdc159e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
60 changes: 37 additions & 23 deletions src/plugins/spit/qgsshapefile.cpp
Expand Up @@ -29,6 +29,7 @@
#include <QString>
#include <QLabel>
#include <QTextCodec>
#include <QFileInfo>

#include "qgsdbfbase.h"
#include "cpl_error.h"
Expand Down Expand Up @@ -255,9 +256,8 @@ void QgsShapeFile::setTable(QString new_table){
}

void QgsShapeFile::setDefaultTable(){
QString name(filename);
name = name.section('/', -1);
table_name = name.section('.', 0, 0);
QFileInfo fi(filename);
table_name = fi.baseName();
}

void QgsShapeFile::setColumnNames(QStringList columns)
Expand All @@ -273,8 +273,8 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
QString srid, PGconn * conn, QProgressDialog& pro, bool &fin,
QString& errorText)
{
connect(&pro, SIGNAL(cancelled()), this, SLOT(cancelImport()));
import_cancelled = false;
connect(&pro, SIGNAL(canceled()), this, SLOT(cancelImport()));
import_canceled = false;
bool result = true;
// Mangle the table name to make it PG compliant by replacing spaces with
// underscores
Expand Down Expand Up @@ -338,30 +338,44 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,

if(isMulti)
{
// drop the check constraint
// TODO This whole concept needs to be changed to either
// convert the geometries to the same type or allow
// multiple types in the check constraint. For now, we
// just drop the constraint...
query = "alter table " + table_name + " drop constraint \"$2\"";

res = PQexec(conn, (const char*)query);
if(PQresultStatus(res)!=PGRES_TUPLES_OK){
errorText += tr("The database gave an error while executing this SQL:") + "\n";
errorText += query + '\n';
errorText += tr("The error was:") + "\n";
errorText += PQresultErrorMessage(res) + '\n';
PQclear(res);
return false;
query = QString("select constraint_name from information_schema.table_constraints where table_schema='%1' and table_name='%2' and constraint_name in ('$2','enforce_geotype_the_geom')")
.arg( schema ).arg( table_name );

QStringList constraints;
res = PQexec( conn, query );
if( PQresultStatus( res ) == PGRES_TUPLES_OK )
{
for(int i=0; i<PQntuples(res); i++)
constraints.append( PQgetvalue(res, i, 0) );
}
else{
PQclear(res);

if( constraints.size()>0 ) {
// drop the check constraint
// TODO This whole concept needs to be changed to either
// convert the geometries to the same type or allow
// multiple types in the check constraint. For now, we
// just drop the constraint...
query = "alter table " + table_name + " drop constraint \"" + constraints[0] + "\"";

res = PQexec(conn, (const char*)query);
if(PQresultStatus(res)!=PGRES_COMMAND_OK) {
errorText += tr("The database gave an error while executing this SQL:") + "\n";
errorText += query + '\n';
errorText += tr("The error was:") + "\n";
errorText += PQresultErrorMessage(res) + '\n';
PQclear(res);
return false;
}

PQclear(res);
}

}

//adding the data into the table
for(int m=0;m<features && result; m++){
if(import_cancelled){
if(import_canceled){
fin = true;
break;
}
Expand Down Expand Up @@ -452,5 +466,5 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
}

void QgsShapeFile::cancelImport(){
import_cancelled = true;
import_canceled = true;
}
2 changes: 1 addition & 1 deletion src/plugins/spit/qgsshapefile.h
Expand Up @@ -64,7 +64,7 @@ class QgsShapeFile : public QObject
QString table_name;
OGRDataSource *ogrDataSource;
OGRLayer * ogrLayer;
bool import_cancelled;
bool import_canceled;
bool valid;
//! Flag to indicate the file contains multiple geometry types
bool isMulti;
Expand Down
94 changes: 48 additions & 46 deletions src/plugins/spit/qgsspit.cpp
Expand Up @@ -537,7 +537,7 @@ void QgsSpit::import()

QString connName = cmbConnections->currentText();
QSettings settings;
bool cancelled = false;
bool canceled = false;
PGconn* pd = checkConnection();
QString query;

Expand Down Expand Up @@ -627,10 +627,8 @@ void QgsSpit::import()
PQclear(res);
continue;
}
else
{
PQclear( res );
}

PQclear( res );

query = "SELECT tablename FROM pg_tables WHERE tablename=\'" + tblShapefiles->item( i, ColDBRELATIONNAME )->text() + "\' AND schemaname=\'" +
tblShapefiles->item( i, ColDBSCHEMA )->text() + "\'";
Expand Down Expand Up @@ -733,28 +731,40 @@ void QgsSpit::import()

if ( rel_exists1 )
{
/*query = "SELECT DropGeometryColumn(\'"+QString(settings.readEntry(key + "/database"))+"\', \'"+
fileList[i]->getTable()+"\', \'"+txtGeomName->text()+"')";*/
query = "DELETE FROM geometry_columns WHERE f_table_schema=\'" + tblShapefiles->item( i, ColDBSCHEMA )->text() + "\' AND " +
"f_table_name=\'" + tblShapefiles->item( i, ColDBRELATIONNAME )->text() + "\'";
query = QString("SELECT f_geometry_column FROM geometry_columns WHERE f_table_schema='%1' AND f_table_name='%2'")
.arg( tblShapefiles->item( i, ColDBSCHEMA )->text() )
.arg( tblShapefiles->item( i, ColDBRELATIONNAME )->text() );

QStringList columns;
res = PQexec( pd, query );
if( PQresultStatus( res ) != PGRES_TUPLES_OK )
{
for(int i=0; i<PQntuples(res); i++)
columns.append( PQgetvalue(res, i, 0) );
}
PQclear(res);

res = PQexec( pd, ( const char * ) query );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
tr("<p>Error while executing the SQL:</p><p>") +
query + tr("</p><p>The database said:") +
err + "</p>" );
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
PQclear(res);
continue;
for(int i=0; i<columns.size(); i++)
{
query = QString("SELECT DropGeometryColumn('%1','%2','%3')")
.arg( tblShapefiles->item( i, ColDBSCHEMA )->text() )
.arg( tblShapefiles->item( i, ColDBRELATIONNAME )->text() )
.arg( columns[i] );

res = PQexec( pd, ( const char * ) query );
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
{
QString err = PQresultErrorMessage( res );
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
tr("<p>Error while executing the SQL:</p><p>") +
query + tr("</p><p>The database said:") +
err + "</p>" );
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
}

PQclear(res);
}
else
{
PQclear( res );
}
}
}
}
else
{
Expand All @@ -767,29 +777,26 @@ void QgsSpit::import()
tr("<p>Error while executing the SQL:</p><p>") +
query + tr("</p><p>The database said:") +
err + "</p>" );
PQclear(res);
}
else
{
PQclear( res );
}
pro.setValue( pro.value() + tblShapefiles->item( i, 2 )->text().toInt() );
}
PQclear(res);

pro.setValue( pro.value() + tblShapefiles->item( i, 2 )->text().toInt() );
continue;
}
}

// importing file here
int temp_progress = pro.value();
cancelled = false;
canceled = false;

QString dbname = settings.readEntry( gl_key + connName + "/database" );
QString schema = tblShapefiles->item( i, ColDBSCHEMA )->text();
QString srid = QString( "%1" ).arg( spinSrid->value() );
QString errorText;

bool layerInserted = fileList[ i ] ->insertLayer( dbname, schema, txtGeomName->text(),
srid, pd, pro, cancelled, errorText );
if ( layerInserted && !cancelled )
srid, pd, pro, canceled, errorText );
if ( layerInserted && !canceled )
{ // if file has been imported successfully
query = "COMMIT";
res = PQexec( pd, query );
Expand All @@ -803,10 +810,8 @@ void QgsSpit::import()
PQclear(res);
continue;
}
else
{
PQclear( res );
}

PQclear( res );

// remove file
for ( int j = 0; j < tblShapefiles->rowCount(); j++ )
Expand All @@ -821,7 +826,7 @@ void QgsSpit::import()
}

}
else if ( !cancelled )
else if ( !canceled )
{ // if problem importing file occured
pro.setValue( temp_progress + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
QString errTxt = error + "\n" + errorText;
Expand All @@ -835,15 +840,12 @@ void QgsSpit::import()
tr("<p>Error while executing the SQL:</p><p>") +
query + tr("</p><p>The database said:") +
err + "</p>" );
PQclear(res);
}
else
{
PQclear( res );
}

PQclear( res );
}
else
{ // if import was actually cancelled
{ // if import was actually canceled
break;
}
}
Expand Down

0 comments on commit bdc159e

Please sign in to comment.