Skip to content

Commit bff08ab

Browse files
author
g_j_m
committed
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();

0 commit comments

Comments
 (0)