Skip to content

Commit

Permalink
Initialize qgis.db in server. Fixes ticket #8172
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jun 28, 2013
1 parent e491acb commit e00f0c0
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 102 deletions.
106 changes: 5 additions & 101 deletions src/app/qgisapp.cpp
Expand Up @@ -458,7 +458,11 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
mSplash->showMessage( tr( "Checking database" ), Qt::AlignHCenter | Qt::AlignBottom );
qApp->processEvents();
// Do this early on before anyone else opens it and prevents us copying it
createDB();
QString dbError;
if ( !QgsApplication::createDB( &dbError ) )
{
QMessageBox::critical( this, tr( "Private qgis.db" ), dbError );
}

mSplash->showMessage( tr( "Reading settings" ), Qt::AlignHCenter | Qt::AlignBottom );
qApp->processEvents();
Expand Down Expand Up @@ -2118,106 +2122,6 @@ void QgisApp::initLegend()
return;
}

bool QgisApp::createDB()
{
// Check qgis.db and make private copy if necessary
QFile qgisPrivateDbFile( QgsApplication::qgisUserDbFilePath() );

// first we look for ~/.qgis/qgis.db
if ( !qgisPrivateDbFile.exists() )
{
// if it doesnt exist we copy it in from the global resources dir
QString qgisMasterDbFileName = QgsApplication::qgisMasterDbFilePath();
QFile masterFile( qgisMasterDbFileName );

// Must be sure there is destination directory ~/.qgis
QDir().mkpath( QgsApplication::qgisSettingsDirPath() );

//now copy the master file into the users .qgis dir
bool isDbFileCopied = masterFile.copy( qgisPrivateDbFile.fileName() );

if ( !isDbFileCopied )
{
QgsMessageLog::logMessage( tr( "[ERROR] Can not make qgis.db private copy" ) );
return false;
}
}
else
{
// migrate if necessary
sqlite3 *db;
if ( sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().constData(), &db ) != SQLITE_OK )
{
QMessageBox::critical( this, tr( "Private qgis.db" ), tr( "Could not open qgis.db" ) );
return false;
}

char *errmsg;
int res = sqlite3_exec( db, "SELECT epsg FROM tbl_srs LIMIT 0", 0, 0, &errmsg );
if ( res == SQLITE_OK )
{
// epsg column exists => need migration
if ( sqlite3_exec( db,
"ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
"CREATE TABLE tbl_srs ("
"srs_id INTEGER PRIMARY KEY,"
"description text NOT NULL,"
"projection_acronym text NOT NULL,"
"ellipsoid_acronym NOT NULL,"
"parameters text NOT NULL,"
"srid integer,"
"auth_name varchar,"
"auth_id varchar,"
"is_geo integer NOT NULL,"
"deprecated boolean);"
"CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
"INSERT INTO tbl_srs(srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated) SELECT srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,'','',is_geo,0 FROM tbl_srs_bak;"
"DROP TABLE tbl_srs_bak", 0, 0, &errmsg ) != SQLITE_OK
)
{
QMessageBox::critical( this, tr( "Private qgis.db" ), tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) ) );
sqlite3_free( errmsg );
sqlite3_close( db );
return false;
}
}
else
{
sqlite3_free( errmsg );
}

if ( sqlite3_exec( db, "DROP VIEW vw_srs", 0, 0, &errmsg ) != SQLITE_OK )
{
QgsDebugMsg( QString( "vw_srs didn't exists in private qgis.db: %1" ).arg( errmsg ) );
}

if ( sqlite3_exec( db,
"CREATE VIEW vw_srs AS"
" SELECT"
" a.description AS description"
",a.srs_id AS srs_id"
",a.is_geo AS is_geo"
",coalesce(b.name,a.projection_acronym) AS name"
",a.parameters AS parameters"
",a.auth_name AS auth_name"
",a.auth_id AS auth_id"
",a.deprecated AS deprecated"
" FROM tbl_srs a"
" LEFT OUTER JOIN tbl_projection b ON a.projection_acronym=b.acronym"
" ORDER BY coalesce(b.name,a.projection_acronym),a.description", 0, 0, &errmsg ) != SQLITE_OK
)
{
QMessageBox::critical( this, tr( "Private qgis.db" ), tr( "Update of view in private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) ) );
sqlite3_free( errmsg );
sqlite3_close( db );
return false;
}

sqlite3_close( db );
}
return true;
}

void QgisApp::createMapTips()
{
// Set up the timer for maptips. The timer is reset everytime the mouse is moved
Expand Down
1 change: 0 additions & 1 deletion src/app/qgisapp.h
Expand Up @@ -1226,7 +1226,6 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
void initLegend();
void createOverview();
void createCanvasTools();
bool createDB();
void createMapTips();
void updateCRSStatusBar();
void createDecorations();
Expand Down
113 changes: 113 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -41,6 +41,7 @@
#include <gdal.h>
#include <ogr_api.h>
#include <cpl_conv.h> // for setting gdal options
#include <sqlite3.h>

QObject * ABISYM( QgsApplication::mFileOpenEventReceiver );
QStringList ABISYM( QgsApplication::mFileOpenEventList );
Expand Down Expand Up @@ -885,4 +886,116 @@ void QgsApplication::applyGdalSkippedDrivers()
GDALAllRegister(); //to update driver list and skip missing ones
}

bool QgsApplication::createDB( QString* errorMessage )
{
// Check qgis.db and make private copy if necessary
QFile qgisPrivateDbFile( QgsApplication::qgisUserDbFilePath() );

// first we look for ~/.qgis/qgis.db
if ( !qgisPrivateDbFile.exists() )
{
// if it doesnt exist we copy it in from the global resources dir
QString qgisMasterDbFileName = QgsApplication::qgisMasterDbFilePath();
QFile masterFile( qgisMasterDbFileName );

// Must be sure there is destination directory ~/.qgis
QDir().mkpath( QgsApplication::qgisSettingsDirPath() );

//now copy the master file into the users .qgis dir
bool isDbFileCopied = masterFile.copy( qgisPrivateDbFile.fileName() );

if ( !isDbFileCopied )
{
if ( errorMessage )
{
*errorMessage = tr( "[ERROR] Can not make qgis.db private copy" );
}
return false;
}
}
else
{
// migrate if necessary
sqlite3 *db;
if ( sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().constData(), &db ) != SQLITE_OK )
{
if ( errorMessage )
{
*errorMessage = tr( "Could not open qgis.db" );
}
return false;
}

char *errmsg;
int res = sqlite3_exec( db, "SELECT epsg FROM tbl_srs LIMIT 0", 0, 0, &errmsg );
if ( res == SQLITE_OK )
{
// epsg column exists => need migration
if ( sqlite3_exec( db,
"ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
"CREATE TABLE tbl_srs ("
"srs_id INTEGER PRIMARY KEY,"
"description text NOT NULL,"
"projection_acronym text NOT NULL,"
"ellipsoid_acronym NOT NULL,"
"parameters text NOT NULL,"
"srid integer,"
"auth_name varchar,"
"auth_id varchar,"
"is_geo integer NOT NULL,"
"deprecated boolean);"
"CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
"INSERT INTO tbl_srs(srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,auth_name,auth_id,is_geo,deprecated) SELECT srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,'','',is_geo,0 FROM tbl_srs_bak;"
"DROP TABLE tbl_srs_bak", 0, 0, &errmsg ) != SQLITE_OK
)
{
if ( errorMessage )
{
*errorMessage = tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
}
sqlite3_free( errmsg );
sqlite3_close( db );
return false;
}
}
else
{
sqlite3_free( errmsg );
}

if ( sqlite3_exec( db, "DROP VIEW vw_srs", 0, 0, &errmsg ) != SQLITE_OK )
{
QgsDebugMsg( QString( "vw_srs didn't exists in private qgis.db: %1" ).arg( errmsg ) );
}

if ( sqlite3_exec( db,
"CREATE VIEW vw_srs AS"
" SELECT"
" a.description AS description"
",a.srs_id AS srs_id"
",a.is_geo AS is_geo"
",coalesce(b.name,a.projection_acronym) AS name"
",a.parameters AS parameters"
",a.auth_name AS auth_name"
",a.auth_id AS auth_id"
",a.deprecated AS deprecated"
" FROM tbl_srs a"
" LEFT OUTER JOIN tbl_projection b ON a.projection_acronym=b.acronym"
" ORDER BY coalesce(b.name,a.projection_acronym),a.description", 0, 0, &errmsg ) != SQLITE_OK
)
{
if ( errorMessage )
{
*errorMessage = tr( "Update of view in private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
}
sqlite3_free( errmsg );
sqlite3_close( db );
return false;
}

sqlite3_close( db );
}
return true;
}


3 changes: 3 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -183,6 +183,9 @@ class CORE_EXPORT QgsApplication: public QApplication
//! loads providers
static void initQgis();

//! initialise qgis.db
static bool createDB( QString* errorMessage = 0 );

//! deletes provider registry and map layer registry
static void exitQgis();

Expand Down
1 change: 1 addition & 0 deletions src/mapserver/qgis_map_serv.cpp
Expand Up @@ -195,6 +195,7 @@ int main( int argc, char * argv[] )
QgsDebugMsg( "User DB PATH: " + QgsApplication::qgisUserDbFilePath() );

QgsDebugMsg( qgsapp.applicationDirPath() + "/qgis_wms_server.log" );
QgsApplication::createDB(); //init qgis.db (e.g. necessary for user crs)

//create config cache and search for config files in the current directory.
//These configurations are used if no mapfile parameter is present in the request
Expand Down

0 comments on commit e00f0c0

Please sign in to comment.