fix for handling special characters

git-svn-id: http://svn.osgeo.org/qgis/trunk@664 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-02-01 02:11:21 +00:00
parent b04f750d66
commit f152f1643b
4 changed files with 31 additions and 32 deletions

View File

@ -23,10 +23,6 @@
#include <iostream>
#include <fstream>
#include <cstdio>
extern "C"
{
#include <libpq-fe.h>
}
#include "qgsdbfbase.h"
#include "cpl_error.h"
@ -125,6 +121,8 @@ QString QgsShapeFile::getTable(){
}
void QgsShapeFile::setTable(QString new_table){
new_table.replace("\'","\\'");
new_table.replace("\\","\\\\");
table_name = new_table;
}
@ -141,14 +139,17 @@ bool QgsShapeFile::insertLayer(QString dbname, QString geom_col, QString srid, P
QString message;
QString query = "CREATE TABLE "+table_name+"(gid int4, ";
for(int n=0; n<column_names.size(); n++){
for(int n=0; n<column_names.size() && result; n++){
if(!column_names[n][0].isLetter())
result = false;
query += column_names[n].lower();
query += " ";
query += column_types[n];
if(n < column_names.size() -1)
query += ", ";
}
query += ")";
query += " PRIMARY KEY (gid))";
PGresult *res = PQexec(conn, (const char *)query);
message = PQresultErrorMessage(res);
if(message != ""){
@ -192,8 +193,9 @@ bool QgsShapeFile::insertLayer(QString dbname, QString geom_col, QString srid, P
// escape single quotes to prevent sql syntax error (no effect for numerics)
QString val = feat->GetFieldAsString(n);
val.replace("\'","\\\'");
val.replace("\'","\\\'");
val.replace("\'","\\'");
val.replace("\\","\\\\");
// add escaped value to the query
query += val;
query += QString(quotes + ", ");

View File

@ -24,15 +24,15 @@
#include <qobject.h>
#include <ogrsf_frmts.h>
#include <qprogressdialog.h>
class OGRLayer;
class OGRDataSource;
extern "C"
{
#include <libpq-fe.h>
}
class OGRLayer;
class OGRDataSource;
class QgsShapeFile : public QObject
{
Q_OBJECT
@ -42,7 +42,7 @@ class QgsShapeFile : public QObject
~QgsShapeFile();
int getFeatureCount();
QString getFeatureClass();
bool insertLayer(QString dbname, QString geom_col, QString srid, struct pg_conn* conn, QProgressDialog * pro, bool &fin);
bool insertLayer(QString dbname, QString geom_col, QString srid, PGconn * conn, QProgressDialog * pro, bool &fin);
bool is_valid();
QString getName();

View File

@ -30,6 +30,7 @@
#include <qprogressdialog.h>
#include <qmemarray.h>
#include <qapplication.h>
#include <iostream>
extern "C"
{
#include <libpq-fe.h>
@ -252,10 +253,17 @@ void QgsSpit::import(){
pro->setAutoClose(true);
qApp->processEvents();
//pro->setAutoReset(true);
PQexec(pd, "BEGIN");
for(int i=0; i<fileList.size() ; i++){
// if a name starts with invalid character
if(!(fileList[i]->getTable()[0]).isLetter()){
QMessageBox::warning(pro, "Import Shapefiles",
"Problem inserting file:\n"+fileList[i]->getName()+"\nInvalid table name.");
std::cout<<i<<std::endl;
continue;
}
PQexec(pd, "BEGIN");
fileList[i]->setTable(tblShapefiles->text(i, 3));
pro->show();
pro->setLabelText("Importing files\n"+fileList[i]->getName());
@ -280,7 +288,7 @@ void QgsSpit::import(){
QMessageBox::Warning,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No | QMessageBox::Escape,
QMessageBox::NoButton, this, "Relation Exists");
QMessageBox::NoButton, pro, "Relation Exists");
}
if ((!rel_exists1 && !rel_exists2) || del_confirm->exec() == QMessageBox::Yes){
if(rel_exists1){
@ -298,18 +306,13 @@ void QgsSpit::import(){
if(!fileList[i]->insertLayer(settings.readEntry(key + "/database"), txtGeomName->text(),
QString("%1").arg(spinSrid->value()), pd, pro, finished)){
if(!finished){
pro->close();
QMessageBox::warning(this, "Import Shapefiles",
"Problem inserting features\nOne or more of your shapefiles may be corrupted");
QMessageBox::warning(pro, "Import Shapefiles",
"Problem inserting features from file:\n"+fileList[i]->getName());
PQexec(pd, "ROLLBACK");
}
finished = true;
break;
}
else if(finished){
pro->close();
break;
}
else{ // if file has been imported, remove it from the list
PQexec(pd, "COMMIT");
for(int j=0; j<tblShapefiles->numRows(); j++)
if(tblShapefiles->text(j,0)==QString(fileList[i]->getName())){
tblShapefiles->selectRow(j);
@ -321,13 +324,7 @@ void QgsSpit::import(){
}
else{
pro->setProgress(pro->progress()+fileList[i]->getFeatureCount());
}
}
if(finished)
PQexec(pd, "ROLLBACK");
else{
PQexec(pd, "COMMIT");
}
}
}
else

View File

@ -88,7 +88,7 @@ void QgsSpitPlugin::initGui()
QPopupMenu *pluginMenu = new QPopupMenu(qgisMainWindow);
pluginMenu->insertItem("&Import Shapefiles to PostgreSQL", this, SLOT(spit()));
pluginMenu->insertItem("&Unload SPTI Plugin", this, SLOT(unload()));
pluginMenu->insertItem("&Unload SPIT Plugin", this, SLOT(unload()));
menu = ((QMainWindow *) qgisMainWindow)->menuBar();