Skip to content

Commit bff08ab

Browse files
author
g_j_m
committedJan 4, 2007
Merge r6391 from 0.8 branch to head (fix for ticket #186).
git-svn-id: http://svn.osgeo.org/qgis/trunk@6392 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 6763947 commit bff08ab

File tree

3 files changed

+111
-51
lines changed

3 files changed

+111
-51
lines changed
 

‎src/plugins/spit/qgsshapefile.cpp

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "cpl_error.h"
3535
#include "qgsshapefile.h"
3636
#include "qgis.h"
37+
#include "qgslogger.h"
3738

3839
// for htonl
3940
#ifdef WIN32
@@ -159,11 +160,11 @@ QString QgsShapeFile::getFeatureClass(){
159160
*/
160161
//geom_type = QString(geom->getGeometryName());
161162
//geom_type = "GEOMETRY";
162-
std::cerr << "Preparing to escape " << geom_type.toLocal8Bit().data() << std::endl;
163+
QgsDebugMsg("Preparing to escape " + geom_type);
163164
char * esc_str = new char[geom_type.length()*2+1];
164165
PQescapeString(esc_str, (const char *)geom_type, geom_type.length());
165166
geom_type = QString(esc_str);
166-
std::cerr << "After escaping, geom_type is : " << geom_type.toLocal8Bit().data() << std::endl;
167+
QgsDebugMsg("After escaping, geom_type is : " + geom_type);
167168
delete[] esc_str;
168169

169170
QString file(filename);
@@ -258,59 +259,73 @@ void QgsShapeFile::setColumnNames(QStringList columns)
258259
}
259260
}
260261

261-
bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col, QString srid, PGconn * conn, QProgressDialog& pro, bool &fin){
262+
bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
263+
QString srid, PGconn * conn, QProgressDialog& pro, bool &fin,
264+
QString& errorText)
265+
{
262266
connect(&pro, SIGNAL(cancelled()), this, SLOT(cancelImport()));
263267
import_cancelled = false;
264268
bool result = true;
265269
// Mangle the table name to make it PG compliant by replacing spaces with
266270
// underscores
267271
table_name = table_name.replace(" ","_");
272+
268273
QString query = "CREATE TABLE "+schema+"."+table_name+"(gid int4 PRIMARY KEY, ";
274+
269275
for(int n=0; n<column_names.size() && result; n++){
270276
if(!column_names[n][0].isLetter())
271277
result = false;
278+
272279
char * esc_str = new char[column_names[n].length()*2+1];
273-
std::cerr << "Escaping " << column_names[n].toLocal8Bit().data() << " to ";
280+
274281
PQescapeString(esc_str, (const char *)column_names[n].lower(), column_names[n].length());
275-
std::cerr << esc_str << std::endl;
282+
QgsDebugMsg("Escaped " + column_names[n] + " to " + esc_str);
276283
query += esc_str;
277-
std::cerr << query.toLocal8Bit().data() << std::endl;
278-
query += " ";
279-
std::cerr << query.toLocal8Bit().data() << std::endl;
280-
query += column_types[n];
281-
std::cerr << query.toLocal8Bit().data() << std::endl;
284+
query += " " + column_types[n];
285+
282286
if(n<column_names.size()-1)
283287
{
284288
query += ", ";
285-
std::cerr << query.toLocal8Bit().data() << std::endl;
286289
}
287290
delete[] esc_str;
288291
}
289292
query += " )";
290-
std::cerr << query.toLocal8Bit().data() << std::endl;
293+
294+
QgsDebugMsg("Query string is: " + query);
291295

292296
PGresult *res = PQexec(conn, (const char *)query);
293-
qWarning(query);
297+
294298
if(PQresultStatus(res)!=PGRES_COMMAND_OK){
295299
// flag error and send query and error message to stdout on debug
296-
result = false;
297-
qWarning(PQresultErrorMessage(res));
300+
errorText += tr("The database gave an error while executing this SQL:") + "\n";
301+
errorText += query + '\n';
302+
errorText += tr("The error was:") + "\n";
303+
errorText += PQresultErrorMessage(res) + '\n';
304+
PQclear(res);
305+
return false;
298306
}
299307
else {
300308
PQclear(res);
301309
}
302310

303-
query = "SELECT AddGeometryColumn(\'" + schema + "\', \'" + table_name + "\', \'"+geom_col+"\', " + srid +
304-
", \'" + geom_type + "\', 2)";
305-
if(result) res = PQexec(conn, (const char *)query);
311+
query = "SELECT AddGeometryColumn(\'" + schema + "\', \'" + table_name + "\', \'"+
312+
geom_col + "\', " + srid + ", \'" + geom_type + "\', 2)";
313+
314+
res = PQexec(conn, (const char *)query);
315+
306316
if(PQresultStatus(res)!=PGRES_TUPLES_OK){
307-
result = false;
317+
errorText += tr("The database gave an error while executing this SQL:") + "\n";
318+
319+
errorText += query + '\n';
320+
errorText += tr("The error was:") + "\n";
321+
errorText += PQresultErrorMessage(res) + '\n';
322+
PQclear(res);
323+
return false;
308324
}
309325
else{
310-
qWarning(query);
311-
qWarning(PQresultErrorMessage(res));
312326
PQclear(res);
313327
}
328+
314329
if(isMulti)
315330
{
316331
// drop the check constraint
@@ -319,8 +334,19 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
319334
// multiple types in the check constraint. For now, we
320335
// just drop the constraint...
321336
query = "alter table " + table_name + " drop constraint \"$2\"";
322-
// XXX tacky - we don't even check the result...
323-
PQexec(conn, (const char*)query);
337+
338+
res = PQexec(conn, (const char*)query);
339+
if(PQresultStatus(res)!=PGRES_TUPLES_OK){
340+
errorText += tr("The database gave an error while executing this SQL:") + "\n";
341+
errorText += query + '\n';
342+
errorText += tr("The error was:") + "\n";
343+
errorText += PQresultErrorMessage(res) + '\n';
344+
PQclear(res);
345+
return false;
346+
}
347+
else{
348+
PQclear(res);
349+
}
324350
}
325351

326352
//adding the data into the table
@@ -334,7 +360,8 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
334360
if(feat){
335361
OGRGeometry *geom = feat->GetGeometryRef();
336362
if(geom){
337-
query = "INSERT INTO "+schema+"."+table_name+QString(" VALUES( %1, ").arg(m);
363+
query = "INSERT INTO \"" + schema + "\".\"" + table_name + "\"" +
364+
QString(" VALUES( %1, ").arg(m);
338365

339366
int num = geom->WkbSize();
340367
char * geo_temp = new char[num*3];
@@ -343,14 +370,22 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
343370

344371
QString quotes;
345372
for(int n=0; n<column_types.size(); n++){
373+
bool numericType(false);
346374
if(column_types[n] == "int" || column_types[n] == "float")
375+
{
347376
quotes = " ";
377+
numericType = true;
378+
}
348379
else
349380
quotes = "\'";
350381
query += quotes;
351382

352-
// escape the string value
383+
// escape the string value and cope with blank data
353384
QString val = codec->toUnicode(feat->GetFieldAsString(n));
385+
if (val.isEmpty() && numericType)
386+
{
387+
val = "NULL";
388+
}
354389
val.replace("'", "''");
355390
//char * esc_str = new char[val.length()*2+1];
356391
//PQescapeString(esc_str, (const char *)val.lower().utf8(), val.length());
@@ -362,14 +397,24 @@ bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col,
362397
//delete[] esc_str;
363398
}
364399
query += QString("GeometryFromText(\'")+geometry+QString("\', ")+srid+QString("))");
365-
// std::cerr << query << std::endl;
366400

367401
if(result)
368402
res = PQexec(conn, (const char *)query.utf8());
403+
369404
if(PQresultStatus(res)!=PGRES_COMMAND_OK){
370405
// flag error and send query and error message to stdout on debug
371406
result = false;
372-
qWarning(PQresultErrorMessage(res));
407+
errorText += tr("The database gave an error while executing this SQL:") + "\n";
408+
// the query string can be quite long. Trim if necessary...
409+
if (query.count() > 100)
410+
errorText += query.left(150) +
411+
tr("... (rest of SQL trimmed)", "is appended to a truncated SQL statement") +
412+
"\n";
413+
else
414+
errorText += query + '\n';
415+
errorText += tr("The error was:") + "\n";
416+
errorText += PQresultErrorMessage(res);
417+
errorText += '\n';
373418
}
374419
else {
375420
PQclear(res);

‎src/plugins/spit/qgsshapefile.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class QgsShapeFile : public QObject
4545
~QgsShapeFile();
4646
int getFeatureCount();
4747
QString getFeatureClass();
48-
bool insertLayer(QString dbname, QString schema, QString geom_col, QString srid, PGconn * conn, QProgressDialog& pro, bool &fin);
48+
bool insertLayer(QString dbname, QString schema, QString geom_col,
49+
QString srid, PGconn * conn, QProgressDialog& pro,
50+
bool &fin, QString& errorText);
4951

5052
bool is_valid();
5153
QString getName();

‎src/plugins/spit/qgsspit.cpp

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <QPixmap>
2727
#include <QHeaderView>
2828
#include <QTextCodec>
29+
#include <QList>
30+
#include <QTableWidgetItem>
2931

3032
#include <iostream>
3133

@@ -38,6 +40,7 @@
3840
#include "qgsmessageviewer.h"
3941
#include "spiticon.xpm"
4042
#include "spit_icons.h"
43+
#include "qgslogger.h"
4144

4245
// Qt implementation of alignment() + changed the numeric types to be shown on the left as well
4346
/* Is this still needed? Numbers in Qt4 table seem to be left justified by default.
@@ -338,7 +341,9 @@ void QgsSpit::removeFile()
338341
for ( int i = temp.size()-1; i >= 0; --i )
339342
tblShapefiles->removeRow( temp[ i ] );
340343

341-
tblShapefiles->setCurrentCell( -1, 0 );
344+
QList<QTableWidgetItem*> selected = tblShapefiles->selectedItems();
345+
for (int i = 0; i < selected.count(); ++i)
346+
selected[i]->setSelected(false);
342347
}
343348

344349
void QgsSpit::removeAllFiles()
@@ -525,7 +530,9 @@ void QgsSpit::updateSchema()
525530

526531
void QgsSpit::import()
527532
{
528-
tblShapefiles->setCurrentCell( -1, 0 );
533+
QList<QTableWidgetItem*> selected = tblShapefiles->selectedItems();
534+
for (int i = 0; i < selected.count(); ++i)
535+
selected[i]->setSelected(false);
529536

530537
QString connName = cmbConnections->currentText();
531538
QSettings settings;
@@ -577,15 +584,12 @@ void QgsSpit::import()
577584

578585
// duplicate field check
579586
std::vector<QString> names_copy = fileList[ i ] ->column_names;
580-
std::cerr << "Size of names_copy before sort: " << names_copy.size() << std::endl;
581587
QString dupl = "";
582588
std::sort( names_copy.begin(), names_copy.end() );
583-
std::cerr << "Size of names_copy after sort: " << names_copy.size() << std::endl;
584589

585590
for ( int k = 1; k < names_copy.size(); k++ )
586591
{
587-
std::cerr << "USING :" << names_copy[ k ].toLocal8Bit().data() << " index " << k << std::endl;
588-
qWarning( tr("Checking to see if ") + names_copy[ k ] + " == " + names_copy[ k - 1 ] );
592+
QgsDebugMsg( tr("Checking to see if ") + names_copy[ k ] + " == " + names_copy[ k - 1 ] );
589593
if ( names_copy[ k ] == names_copy[ k - 1 ] )
590594
dupl += names_copy[ k ] + "\n";
591595
}
@@ -610,15 +614,16 @@ void QgsSpit::import()
610614
+ tblShapefiles->item( i, ColDBSCHEMA )->text() + "\'";
611615
res = PQexec( pd, ( const char * ) query );
612616
rel_exists1 = ( PQntuples( res ) > 0 );
617+
613618
if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
614619
{
615620
QString err = PQresultErrorMessage( res );
616-
qWarning( err );
617621
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
618622
tr("<p>Error while executing the SQL:</p><p>") +
619623
query + tr("</p><p>The database said:") +
620624
err + "</p>" );
621625
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
626+
PQclear(res);
622627
continue;
623628
}
624629
else
@@ -629,18 +634,19 @@ void QgsSpit::import()
629634
query = "SELECT tablename FROM pg_tables WHERE tablename=\'" + tblShapefiles->item( i, ColDBRELATIONNAME )->text() + "\' AND schemaname=\'" +
630635
tblShapefiles->item( i, ColDBSCHEMA )->text() + "\'";
631636
res = PQexec( pd, ( const char * ) query );
632-
qWarning( query );
637+
633638
rel_exists2 = ( PQntuples( res ) > 0 );
639+
634640
if ( PQresultStatus( res ) != PGRES_TUPLES_OK )
635641
{
636642
QString err = PQresultErrorMessage( res );
637-
qWarning( err );
638643
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
639644
tr("<p>Error while executing the SQL:</p><p>") +
640645
query + tr("</p><p>The database said:") +
641646
err + "</p>" );
642647

643648
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
649+
PQclear(res);
644650
continue;
645651
}
646652
else
@@ -654,12 +660,12 @@ void QgsSpit::import()
654660
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
655661
{
656662
QString err = PQresultErrorMessage( res );
657-
qWarning( err );
658663
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
659664
tr("<p>Error while executing the SQL:</p><p>") +
660665
query + tr("</p><p>The database said:") +
661666
err + "</p>" );
662667
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
668+
PQclear(res);
663669
continue;
664670
}
665671
else
@@ -673,17 +679,16 @@ void QgsSpit::import()
673679
else
674680
query += tblShapefiles->item( i, ColDBSCHEMA )->text() + "\', \'public\'";
675681
res = PQexec( pd, query );
676-
qWarning( query );
682+
677683
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
678684
{
679685
QString err = PQresultErrorMessage( res );
680-
qWarning( err );
681-
qWarning( PQresStatus( PQresultStatus( res ) ) );
682686
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
683687
tr("<p>Error while executing the SQL:</p><p>") +
684688
query + tr("</p><p>The database said:") +
685689
err + "</p>" );
686690
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
691+
PQclear(res);
687692
continue;
688693
}
689694
else
@@ -712,17 +717,17 @@ void QgsSpit::import()
712717
if ( rel_exists2 )
713718
{
714719
query = "DROP TABLE " + tblShapefiles->item( i, ColDBRELATIONNAME )->text();
715-
qWarning( query );
720+
716721
res = PQexec( pd, ( const char * ) query );
717722
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
718723
{
719724
QString err = PQresultErrorMessage( res );
720-
qWarning( err );
721725
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
722726
tr("<p>Error while executing the SQL:</p><p>") +
723727
query + tr("</p><p>The database said:") +
724728
err + "</p>" );
725729
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
730+
PQclear(res);
726731
continue;
727732
}
728733
else
@@ -737,17 +742,17 @@ void QgsSpit::import()
737742
fileList[i]->getTable()+"\', \'"+txtGeomName->text()+"')";*/
738743
query = "DELETE FROM geometry_columns WHERE f_table_schema=\'" + tblShapefiles->item( i, ColDBSCHEMA )->text() + "\' AND " +
739744
"f_table_name=\'" + tblShapefiles->item( i, ColDBRELATIONNAME )->text() + "\'";
740-
qWarning( query );
745+
741746
res = PQexec( pd, ( const char * ) query );
742747
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
743748
{
744749
QString err = PQresultErrorMessage( res );
745-
qWarning( err );
746750
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
747751
tr("<p>Error while executing the SQL:</p><p>") +
748752
query + tr("</p><p>The database said:") +
749753
err + "</p>" );
750754
pro.setValue( pro.value() + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
755+
PQclear(res);
751756
continue;
752757
}
753758
else
@@ -763,11 +768,11 @@ void QgsSpit::import()
763768
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
764769
{
765770
QString err = PQresultErrorMessage( res );
766-
qWarning( err );
767771
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
768772
tr("<p>Error while executing the SQL:</p><p>") +
769773
query + tr("</p><p>The database said:") +
770774
err + "</p>" );
775+
PQclear(res);
771776
}
772777
else
773778
{
@@ -781,19 +786,26 @@ void QgsSpit::import()
781786
// importing file here
782787
int temp_progress = pro.value();
783788
cancelled = false;
784-
if ( fileList[ i ] ->insertLayer( settings.readEntry( gl_key + connName + "/database" ), tblShapefiles->item( i, ColDBSCHEMA )->text(),
785-
txtGeomName->text(), QString( "%1" ).arg( spinSrid->value() ), pd, pro, cancelled ) && !cancelled )
789+
790+
QString dbname = settings.readEntry( gl_key + connName + "/database" );
791+
QString schema = tblShapefiles->item( i, ColDBSCHEMA )->text();
792+
QString srid = QString( "%1" ).arg( spinSrid->value() );
793+
QString errorText;
794+
795+
bool layerInserted = fileList[ i ] ->insertLayer( dbname, schema, txtGeomName->text(),
796+
srid, pd, pro, cancelled, errorText );
797+
if ( layerInserted && !cancelled )
786798
{ // if file has been imported successfully
787799
query = "COMMIT";
788800
res = PQexec( pd, query );
789801
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
790802
{
791803
QString err = PQresultErrorMessage( res );
792-
qWarning( err );
793804
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
794805
tr("<p>Error while executing the SQL:</p><p>") +
795806
query + tr("</p><p>The database said:") +
796807
err + "</p>" );
808+
PQclear(res);
797809
continue;
798810
}
799811
else
@@ -817,17 +829,18 @@ void QgsSpit::import()
817829
else if ( !cancelled )
818830
{ // if problem importing file occured
819831
pro.setValue( temp_progress + tblShapefiles->item( i, ColFEATURECOUNT )->text().toInt() );
820-
QMessageBox::warning( this, tr("Import Shapefiles"), error );
832+
QString errTxt = error + "\n" + errorText;
833+
QMessageBox::warning( this, tr("Import Shapefiles"), errTxt );
821834
query = "ROLLBACK";
822835
res = PQexec( pd, query );
823836
if ( PQresultStatus( res ) != PGRES_COMMAND_OK )
824837
{
825838
QString err = PQresultErrorMessage( res );
826-
qWarning( err );
827839
QMessageBox::warning( &pro, tr("Import Shapefiles"), error + "\n" +
828840
tr("<p>Error while executing the SQL:</p><p>") +
829841
query + tr("</p><p>The database said:") +
830842
err + "</p>" );
843+
PQclear(res);
831844
}
832845
else
833846
{

0 commit comments

Comments
 (0)
Please sign in to comment.