Skip to content

Commit f289935

Browse files
author
rblazek
committed
added openDb
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@4936 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent a91787b commit f289935

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

src/gui/qgsspatialrefsys.cpp

+38-40
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <qdir.h>
1111
#include <projects.h>
1212
#include <qdom.h>
13+
#include <QMessageBox>
1314

1415
#include <qgsapplication.h>
1516
#include <qgslayerprojectionselector.h>
@@ -200,13 +201,10 @@ bool QgsSpatialRefSys::createFromSrid(long theSrid)
200201
sqlite3_stmt *myPreparedStatement;
201202
int myResult;
202203
//check the db is available
203-
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
204+
myResult = openDb(myDatabaseFileName, &myDatabase);
204205
if(myResult)
205206
{
206-
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
207-
// XXX This will likely never happen since on open, sqlite creates the
208-
// database if it does not exist.
209-
assert(myResult == 0);
207+
return false;
210208
}
211209

212210
/*
@@ -319,13 +317,10 @@ bool QgsSpatialRefSys::createFromEpsg(long theEpsg)
319317
sqlite3_stmt *myPreparedStatement;
320318
int myResult;
321319
//check the db is available
322-
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
320+
myResult = openDb(myDatabaseFileName, &myDatabase);
323321
if(myResult)
324322
{
325-
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
326-
// XXX This will likely never happen since on open, sqlite creates the
327-
// database if it does not exist.
328-
assert(myResult == 0);
323+
return false;
329324
}
330325

331326
/*
@@ -404,13 +399,10 @@ bool QgsSpatialRefSys::createFromSrsId (long theSrsId)
404399
sqlite3_stmt *myPreparedStatement;
405400
int myResult;
406401
//check the db is available
407-
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
402+
myResult = openDb(myDatabaseFileName, &myDatabase);
408403
if(myResult)
409404
{
410-
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
411-
// XXX This will likely never happen since on open, sqlite creates the
412-
// database if it does not exist.
413-
assert(myResult == 0);
405+
return false;
414406
}
415407

416408
/*
@@ -601,18 +593,13 @@ QgsSpatialRefSys::RecordMap QgsSpatialRefSys::getRecord(QString theSql)
601593
// Get the full path name to the sqlite3 spatial reference database.
602594
myDatabaseFileName = QgsApplication::srsDbFilePath();
603595

604-
605596
//check the db is available
606-
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
597+
myResult = openDb(myDatabaseFileName, &myDatabase);
607598
if(myResult)
608599
{
609-
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
610-
// XXX This will likely never happen since on open, sqlite creates the
611-
// database if it does not exist.
612-
assert(myResult == 0);
600+
return myMap;
613601
}
614602

615-
616603
myResult = sqlite3_prepare(myDatabase, theSql.utf8(), theSql.length(), &myPreparedStatement, &myTail);
617604
// XXX Need to free memory from the error msg if one is set
618605
if(myResult == SQLITE_OK && sqlite3_step(myPreparedStatement) == SQLITE_ROW)
@@ -644,16 +631,12 @@ QgsSpatialRefSys::RecordMap QgsSpatialRefSys::getRecord(QString theSql)
644631
}
645632

646633
//check the db is available
647-
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
634+
myResult = openDb(myDatabaseFileName, &myDatabase);
648635
if(myResult)
649636
{
650-
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
651-
// XXX This will likely never happen since on open, sqlite creates the
652-
// database if it does not exist.
653-
assert(myResult == 0);
637+
return myMap;
654638
}
655639

656-
657640
myResult = sqlite3_prepare(myDatabase, theSql.utf8(), theSql.length(), &myPreparedStatement, &myTail);
658641
// XXX Need to free memory from the error msg if one is set
659642
if(myResult == SQLITE_OK && sqlite3_step(myPreparedStatement) == SQLITE_ROW)
@@ -937,13 +920,10 @@ long QgsSpatialRefSys::findMatchingProj()
937920

938921

939922
//check the db is available
940-
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
923+
myResult = openDb(myDatabaseFileName, &myDatabase);
941924
if(myResult)
942925
{
943-
std::cout << "QgsSpatialRefSys::findMatchingProj Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
944-
// XXX This will likely never happen since on open, sqlite creates the
945-
// database if it does not exist.
946-
assert(myResult == 0);
926+
return 0;
947927
}
948928

949929
myResult = sqlite3_prepare(myDatabase, mySql.utf8(), mySql.length(), &myPreparedStatement, &myTail);
@@ -979,10 +959,9 @@ long QgsSpatialRefSys::findMatchingProj()
979959

980960
myDatabaseFileName = QDir::homeDirPath () + "/.qgis/qgis.db";
981961
//check the db is available
982-
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
962+
myResult = openDb(myDatabaseFileName, &myDatabase);
983963
if(myResult)
984964
{
985-
std::cout << "QgsSpatialRefSys::findMatchingProj Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
986965
std::cout << "This is a non critical error" << std::endl;
987966
return 0;
988967
}
@@ -1258,13 +1237,10 @@ QString QgsSpatialRefSys::getProj4FromSrsId(const int theSrsId)
12581237

12591238
sqlite3 *db;
12601239
int rc;
1261-
rc = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &db);
1240+
rc = openDb(myDatabaseFileName, &db);
12621241
if(rc)
12631242
{
1264-
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
1265-
// XXX This will likely never happen since on open, sqlite creates the
1266-
// database if it does not exist.
1267-
assert(rc == 0);
1243+
return QString();
12681244
}
12691245
// prepare the sql statement
12701246
const char *pzTail;
@@ -1288,3 +1264,25 @@ QString QgsSpatialRefSys::getProj4FromSrsId(const int theSrsId)
12881264
//assert(myProjString.length() > 0);
12891265
return myProjString;
12901266
}
1267+
1268+
int QgsSpatialRefSys::openDb(QString path, sqlite3 **db)
1269+
{
1270+
#ifdef QGISDEBUG
1271+
std::cout << "QgsSpatialRefSys::openDb path = " << path.toLocal8Bit().data() << std::endl;
1272+
#endif
1273+
int myResult = sqlite3_open(path.toLocal8Bit().data(), db);
1274+
1275+
if(myResult)
1276+
{
1277+
std::cout << "Can't open database: " << sqlite3_errmsg(*db) << std::endl;
1278+
1279+
// XXX This will likely never happen since on open, sqlite creates the
1280+
// database if it does not exist.
1281+
// ... unfortunately it happens on Windows
1282+
QMessageBox::warning(0,"Error","Could not open SRS database "
1283+
+ path + "<br>Error(" + QString::number(myResult)
1284+
+ "): " + QString(sqlite3_errmsg(*db)) );
1285+
1286+
}
1287+
return myResult;
1288+
}

src/gui/qgsspatialrefsys.h

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
class QDomNode;
1414
class QDomDocument;
1515

16+
#include <sqlite3.h>
17+
1618
//qgis includes
1719
#include <qgis.h>
1820

@@ -305,6 +307,10 @@ class QgsSpatialRefSys
305307
*/
306308
void setEllipsoidAcronym(QString theEllipsoidAcronym);
307309
private:
310+
// Open SQLite db and show message if ccannot be opened
311+
// returns the same code as sqlite3_open
312+
static int openDb ( QString path, sqlite3 **db );
313+
308314
//!The internal sqlite3 srs.db primary key for this srs
309315
long mSrsId;
310316
//!A textual description of the srs.

0 commit comments

Comments
 (0)