26
26
#include < QPixmap>
27
27
#include < QHeaderView>
28
28
#include < QTextCodec>
29
+ #include < QList>
30
+ #include < QTableWidgetItem>
29
31
30
32
#include < iostream>
31
33
38
40
#include " qgsmessageviewer.h"
39
41
#include " spiticon.xpm"
40
42
#include " spit_icons.h"
43
+ #include " qgslogger.h"
41
44
42
45
// Qt implementation of alignment() + changed the numeric types to be shown on the left as well
43
46
/* Is this still needed? Numbers in Qt4 table seem to be left justified by default.
@@ -338,7 +341,9 @@ void QgsSpit::removeFile()
338
341
for ( int i = temp.size ()-1 ; i >= 0 ; --i )
339
342
tblShapefiles->removeRow ( temp[ i ] );
340
343
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 );
342
347
}
343
348
344
349
void QgsSpit::removeAllFiles ()
@@ -525,7 +530,9 @@ void QgsSpit::updateSchema()
525
530
526
531
void QgsSpit::import ()
527
532
{
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 );
529
536
530
537
QString connName = cmbConnections->currentText ();
531
538
QSettings settings;
@@ -577,15 +584,12 @@ void QgsSpit::import()
577
584
578
585
// duplicate field check
579
586
std::vector<QString> names_copy = fileList[ i ] ->column_names ;
580
- std::cerr << " Size of names_copy before sort: " << names_copy.size () << std::endl;
581
587
QString dupl = " " ;
582
588
std::sort ( names_copy.begin (), names_copy.end () );
583
- std::cerr << " Size of names_copy after sort: " << names_copy.size () << std::endl;
584
589
585
590
for ( int k = 1 ; k < names_copy.size (); k++ )
586
591
{
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 ] );
589
593
if ( names_copy[ k ] == names_copy[ k - 1 ] )
590
594
dupl += names_copy[ k ] + " \n " ;
591
595
}
@@ -610,15 +614,16 @@ void QgsSpit::import()
610
614
+ tblShapefiles->item ( i, ColDBSCHEMA )->text () + " \' " ;
611
615
res = PQexec ( pd, ( const char * ) query );
612
616
rel_exists1 = ( PQntuples ( res ) > 0 );
617
+
613
618
if ( PQresultStatus ( res ) != PGRES_TUPLES_OK )
614
619
{
615
620
QString err = PQresultErrorMessage ( res );
616
- qWarning ( err );
617
621
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
618
622
tr (" <p>Error while executing the SQL:</p><p>" ) +
619
623
query + tr (" </p><p>The database said:" ) +
620
624
err + " </p>" );
621
625
pro.setValue ( pro.value () + tblShapefiles->item ( i, ColFEATURECOUNT )->text ().toInt () );
626
+ PQclear (res);
622
627
continue ;
623
628
}
624
629
else
@@ -629,18 +634,19 @@ void QgsSpit::import()
629
634
query = " SELECT tablename FROM pg_tables WHERE tablename=\' " + tblShapefiles->item ( i, ColDBRELATIONNAME )->text () + " \' AND schemaname=\' " +
630
635
tblShapefiles->item ( i, ColDBSCHEMA )->text () + " \' " ;
631
636
res = PQexec ( pd, ( const char * ) query );
632
- qWarning ( query );
637
+
633
638
rel_exists2 = ( PQntuples ( res ) > 0 );
639
+
634
640
if ( PQresultStatus ( res ) != PGRES_TUPLES_OK )
635
641
{
636
642
QString err = PQresultErrorMessage ( res );
637
- qWarning ( err );
638
643
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
639
644
tr (" <p>Error while executing the SQL:</p><p>" ) +
640
645
query + tr (" </p><p>The database said:" ) +
641
646
err + " </p>" );
642
647
643
648
pro.setValue ( pro.value () + tblShapefiles->item ( i, ColFEATURECOUNT )->text ().toInt () );
649
+ PQclear (res);
644
650
continue ;
645
651
}
646
652
else
@@ -654,12 +660,12 @@ void QgsSpit::import()
654
660
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
655
661
{
656
662
QString err = PQresultErrorMessage ( res );
657
- qWarning ( err );
658
663
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
659
664
tr (" <p>Error while executing the SQL:</p><p>" ) +
660
665
query + tr (" </p><p>The database said:" ) +
661
666
err + " </p>" );
662
667
pro.setValue ( pro.value () + tblShapefiles->item ( i, ColFEATURECOUNT )->text ().toInt () );
668
+ PQclear (res);
663
669
continue ;
664
670
}
665
671
else
@@ -673,17 +679,16 @@ void QgsSpit::import()
673
679
else
674
680
query += tblShapefiles->item ( i, ColDBSCHEMA )->text () + " \' , \' public\' " ;
675
681
res = PQexec ( pd, query );
676
- qWarning ( query );
682
+
677
683
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
678
684
{
679
685
QString err = PQresultErrorMessage ( res );
680
- qWarning ( err );
681
- qWarning ( PQresStatus ( PQresultStatus ( res ) ) );
682
686
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
683
687
tr (" <p>Error while executing the SQL:</p><p>" ) +
684
688
query + tr (" </p><p>The database said:" ) +
685
689
err + " </p>" );
686
690
pro.setValue ( pro.value () + tblShapefiles->item ( i, ColFEATURECOUNT )->text ().toInt () );
691
+ PQclear (res);
687
692
continue ;
688
693
}
689
694
else
@@ -712,17 +717,17 @@ void QgsSpit::import()
712
717
if ( rel_exists2 )
713
718
{
714
719
query = " DROP TABLE " + tblShapefiles->item ( i, ColDBRELATIONNAME )->text ();
715
- qWarning ( query );
720
+
716
721
res = PQexec ( pd, ( const char * ) query );
717
722
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
718
723
{
719
724
QString err = PQresultErrorMessage ( res );
720
- qWarning ( err );
721
725
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
722
726
tr (" <p>Error while executing the SQL:</p><p>" ) +
723
727
query + tr (" </p><p>The database said:" ) +
724
728
err + " </p>" );
725
729
pro.setValue ( pro.value () + tblShapefiles->item ( i, ColFEATURECOUNT )->text ().toInt () );
730
+ PQclear (res);
726
731
continue ;
727
732
}
728
733
else
@@ -737,17 +742,17 @@ void QgsSpit::import()
737
742
fileList[i]->getTable()+"\', \'"+txtGeomName->text()+"')";*/
738
743
query = " DELETE FROM geometry_columns WHERE f_table_schema=\' " + tblShapefiles->item ( i, ColDBSCHEMA )->text () + " \' AND " +
739
744
" f_table_name=\' " + tblShapefiles->item ( i, ColDBRELATIONNAME )->text () + " \' " ;
740
- qWarning ( query );
745
+
741
746
res = PQexec ( pd, ( const char * ) query );
742
747
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
743
748
{
744
749
QString err = PQresultErrorMessage ( res );
745
- qWarning ( err );
746
750
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
747
751
tr (" <p>Error while executing the SQL:</p><p>" ) +
748
752
query + tr (" </p><p>The database said:" ) +
749
753
err + " </p>" );
750
754
pro.setValue ( pro.value () + tblShapefiles->item ( i, ColFEATURECOUNT )->text ().toInt () );
755
+ PQclear (res);
751
756
continue ;
752
757
}
753
758
else
@@ -763,11 +768,11 @@ void QgsSpit::import()
763
768
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
764
769
{
765
770
QString err = PQresultErrorMessage ( res );
766
- qWarning ( err );
767
771
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
768
772
tr (" <p>Error while executing the SQL:</p><p>" ) +
769
773
query + tr (" </p><p>The database said:" ) +
770
774
err + " </p>" );
775
+ PQclear (res);
771
776
}
772
777
else
773
778
{
@@ -781,19 +786,26 @@ void QgsSpit::import()
781
786
// importing file here
782
787
int temp_progress = pro.value ();
783
788
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 )
786
798
{ // if file has been imported successfully
787
799
query = " COMMIT" ;
788
800
res = PQexec ( pd, query );
789
801
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
790
802
{
791
803
QString err = PQresultErrorMessage ( res );
792
- qWarning ( err );
793
804
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
794
805
tr (" <p>Error while executing the SQL:</p><p>" ) +
795
806
query + tr (" </p><p>The database said:" ) +
796
807
err + " </p>" );
808
+ PQclear (res);
797
809
continue ;
798
810
}
799
811
else
@@ -817,17 +829,18 @@ void QgsSpit::import()
817
829
else if ( !cancelled )
818
830
{ // if problem importing file occured
819
831
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 );
821
834
query = " ROLLBACK" ;
822
835
res = PQexec ( pd, query );
823
836
if ( PQresultStatus ( res ) != PGRES_COMMAND_OK )
824
837
{
825
838
QString err = PQresultErrorMessage ( res );
826
- qWarning ( err );
827
839
QMessageBox::warning ( &pro, tr (" Import Shapefiles" ), error + " \n " +
828
840
tr (" <p>Error while executing the SQL:</p><p>" ) +
829
841
query + tr (" </p><p>The database said:" ) +
830
842
err + " </p>" );
843
+ PQclear (res);
831
844
}
832
845
else
833
846
{
0 commit comments