Skip to content

Commit

Permalink
added openDb
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@4936 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed Mar 2, 2006
1 parent a91787b commit f289935
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
78 changes: 38 additions & 40 deletions src/gui/qgsspatialrefsys.cpp
Expand Up @@ -10,6 +10,7 @@
#include <qdir.h>
#include <projects.h>
#include <qdom.h>
#include <QMessageBox>

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

/*
Expand Down Expand Up @@ -319,13 +317,10 @@ bool QgsSpatialRefSys::createFromEpsg(long theEpsg)
sqlite3_stmt *myPreparedStatement;
int myResult;
//check the db is available
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(myResult == 0);
return false;
}

/*
Expand Down Expand Up @@ -404,13 +399,10 @@ bool QgsSpatialRefSys::createFromSrsId (long theSrsId)
sqlite3_stmt *myPreparedStatement;
int myResult;
//check the db is available
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(myResult == 0);
return false;
}

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


//check the db is available
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(myResult == 0);
return myMap;
}


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

//check the db is available
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
std::cout << "Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(myResult == 0);
return myMap;
}


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


//check the db is available
myResult = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &myDatabase);
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
std::cout << "QgsSpatialRefSys::findMatchingProj Can't open database: " << sqlite3_errmsg(myDatabase) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(myResult == 0);
return 0;
}

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

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

sqlite3 *db;
int rc;
rc = sqlite3_open(myDatabaseFileName.toLocal8Bit().data(), &db);
rc = openDb(myDatabaseFileName, &db);
if(rc)
{
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
assert(rc == 0);
return QString();
}
// prepare the sql statement
const char *pzTail;
Expand All @@ -1288,3 +1264,25 @@ QString QgsSpatialRefSys::getProj4FromSrsId(const int theSrsId)
//assert(myProjString.length() > 0);
return myProjString;
}

int QgsSpatialRefSys::openDb(QString path, sqlite3 **db)
{
#ifdef QGISDEBUG
std::cout << "QgsSpatialRefSys::openDb path = " << path.toLocal8Bit().data() << std::endl;
#endif
int myResult = sqlite3_open(path.toLocal8Bit().data(), db);

if(myResult)
{
std::cout << "Can't open database: " << sqlite3_errmsg(*db) << std::endl;

// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
// ... unfortunately it happens on Windows
QMessageBox::warning(0,"Error","Could not open SRS database "
+ path + "<br>Error(" + QString::number(myResult)
+ "): " + QString(sqlite3_errmsg(*db)) );

}
return myResult;
}
6 changes: 6 additions & 0 deletions src/gui/qgsspatialrefsys.h
Expand Up @@ -13,6 +13,8 @@
class QDomNode;
class QDomDocument;

#include <sqlite3.h>

//qgis includes
#include <qgis.h>

Expand Down Expand Up @@ -305,6 +307,10 @@ class QgsSpatialRefSys
*/
void setEllipsoidAcronym(QString theEllipsoidAcronym);
private:
// Open SQLite db and show message if ccannot be opened
// returns the same code as sqlite3_open
static int openDb ( QString path, sqlite3 **db );

//!The internal sqlite3 srs.db primary key for this srs
long mSrsId;
//!A textual description of the srs.
Expand Down

0 comments on commit f289935

Please sign in to comment.