Skip to content

Commit e00f0c0

Browse files
committed
Initialize qgis.db in server. Fixes ticket #8172
1 parent e491acb commit e00f0c0

File tree

5 files changed

+122
-102
lines changed

5 files changed

+122
-102
lines changed

src/app/qgisapp.cpp

+5-101
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,11 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
458458
mSplash->showMessage( tr( "Checking database" ), Qt::AlignHCenter | Qt::AlignBottom );
459459
qApp->processEvents();
460460
// Do this early on before anyone else opens it and prevents us copying it
461-
createDB();
461+
QString dbError;
462+
if ( !QgsApplication::createDB( &dbError ) )
463+
{
464+
QMessageBox::critical( this, tr( "Private qgis.db" ), dbError );
465+
}
462466

463467
mSplash->showMessage( tr( "Reading settings" ), Qt::AlignHCenter | Qt::AlignBottom );
464468
qApp->processEvents();
@@ -2118,106 +2122,6 @@ void QgisApp::initLegend()
21182122
return;
21192123
}
21202124

2121-
bool QgisApp::createDB()
2122-
{
2123-
// Check qgis.db and make private copy if necessary
2124-
QFile qgisPrivateDbFile( QgsApplication::qgisUserDbFilePath() );
2125-
2126-
// first we look for ~/.qgis/qgis.db
2127-
if ( !qgisPrivateDbFile.exists() )
2128-
{
2129-
// if it doesnt exist we copy it in from the global resources dir
2130-
QString qgisMasterDbFileName = QgsApplication::qgisMasterDbFilePath();
2131-
QFile masterFile( qgisMasterDbFileName );
2132-
2133-
// Must be sure there is destination directory ~/.qgis
2134-
QDir().mkpath( QgsApplication::qgisSettingsDirPath() );
2135-
2136-
//now copy the master file into the users .qgis dir
2137-
bool isDbFileCopied = masterFile.copy( qgisPrivateDbFile.fileName() );
2138-
2139-
if ( !isDbFileCopied )
2140-
{
2141-
QgsMessageLog::logMessage( tr( "[ERROR] Can not make qgis.db private copy" ) );
2142-
return false;
2143-
}
2144-
}
2145-
else
2146-
{
2147-
// migrate if necessary
2148-
sqlite3 *db;
2149-
if ( sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().constData(), &db ) != SQLITE_OK )
2150-
{
2151-
QMessageBox::critical( this, tr( "Private qgis.db" ), tr( "Could not open qgis.db" ) );
2152-
return false;
2153-
}
2154-
2155-
char *errmsg;
2156-
int res = sqlite3_exec( db, "SELECT epsg FROM tbl_srs LIMIT 0", 0, 0, &errmsg );
2157-
if ( res == SQLITE_OK )
2158-
{
2159-
// epsg column exists => need migration
2160-
if ( sqlite3_exec( db,
2161-
"ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
2162-
"CREATE TABLE tbl_srs ("
2163-
"srs_id INTEGER PRIMARY KEY,"
2164-
"description text NOT NULL,"
2165-
"projection_acronym text NOT NULL,"
2166-
"ellipsoid_acronym NOT NULL,"
2167-
"parameters text NOT NULL,"
2168-
"srid integer,"
2169-
"auth_name varchar,"
2170-
"auth_id varchar,"
2171-
"is_geo integer NOT NULL,"
2172-
"deprecated boolean);"
2173-
"CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
2174-
"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;"
2175-
"DROP TABLE tbl_srs_bak", 0, 0, &errmsg ) != SQLITE_OK
2176-
)
2177-
{
2178-
QMessageBox::critical( this, tr( "Private qgis.db" ), tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) ) );
2179-
sqlite3_free( errmsg );
2180-
sqlite3_close( db );
2181-
return false;
2182-
}
2183-
}
2184-
else
2185-
{
2186-
sqlite3_free( errmsg );
2187-
}
2188-
2189-
if ( sqlite3_exec( db, "DROP VIEW vw_srs", 0, 0, &errmsg ) != SQLITE_OK )
2190-
{
2191-
QgsDebugMsg( QString( "vw_srs didn't exists in private qgis.db: %1" ).arg( errmsg ) );
2192-
}
2193-
2194-
if ( sqlite3_exec( db,
2195-
"CREATE VIEW vw_srs AS"
2196-
" SELECT"
2197-
" a.description AS description"
2198-
",a.srs_id AS srs_id"
2199-
",a.is_geo AS is_geo"
2200-
",coalesce(b.name,a.projection_acronym) AS name"
2201-
",a.parameters AS parameters"
2202-
",a.auth_name AS auth_name"
2203-
",a.auth_id AS auth_id"
2204-
",a.deprecated AS deprecated"
2205-
" FROM tbl_srs a"
2206-
" LEFT OUTER JOIN tbl_projection b ON a.projection_acronym=b.acronym"
2207-
" ORDER BY coalesce(b.name,a.projection_acronym),a.description", 0, 0, &errmsg ) != SQLITE_OK
2208-
)
2209-
{
2210-
QMessageBox::critical( this, tr( "Private qgis.db" ), tr( "Update of view in private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) ) );
2211-
sqlite3_free( errmsg );
2212-
sqlite3_close( db );
2213-
return false;
2214-
}
2215-
2216-
sqlite3_close( db );
2217-
}
2218-
return true;
2219-
}
2220-
22212125
void QgisApp::createMapTips()
22222126
{
22232127
// Set up the timer for maptips. The timer is reset everytime the mouse is moved

src/app/qgisapp.h

-1
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,6 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
12261226
void initLegend();
12271227
void createOverview();
12281228
void createCanvasTools();
1229-
bool createDB();
12301229
void createMapTips();
12311230
void updateCRSStatusBar();
12321231
void createDecorations();

src/core/qgsapplication.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <gdal.h>
4242
#include <ogr_api.h>
4343
#include <cpl_conv.h> // for setting gdal options
44+
#include <sqlite3.h>
4445

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

889+
bool QgsApplication::createDB( QString* errorMessage )
890+
{
891+
// Check qgis.db and make private copy if necessary
892+
QFile qgisPrivateDbFile( QgsApplication::qgisUserDbFilePath() );
893+
894+
// first we look for ~/.qgis/qgis.db
895+
if ( !qgisPrivateDbFile.exists() )
896+
{
897+
// if it doesnt exist we copy it in from the global resources dir
898+
QString qgisMasterDbFileName = QgsApplication::qgisMasterDbFilePath();
899+
QFile masterFile( qgisMasterDbFileName );
900+
901+
// Must be sure there is destination directory ~/.qgis
902+
QDir().mkpath( QgsApplication::qgisSettingsDirPath() );
903+
904+
//now copy the master file into the users .qgis dir
905+
bool isDbFileCopied = masterFile.copy( qgisPrivateDbFile.fileName() );
906+
907+
if ( !isDbFileCopied )
908+
{
909+
if ( errorMessage )
910+
{
911+
*errorMessage = tr( "[ERROR] Can not make qgis.db private copy" );
912+
}
913+
return false;
914+
}
915+
}
916+
else
917+
{
918+
// migrate if necessary
919+
sqlite3 *db;
920+
if ( sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().constData(), &db ) != SQLITE_OK )
921+
{
922+
if ( errorMessage )
923+
{
924+
*errorMessage = tr( "Could not open qgis.db" );
925+
}
926+
return false;
927+
}
928+
929+
char *errmsg;
930+
int res = sqlite3_exec( db, "SELECT epsg FROM tbl_srs LIMIT 0", 0, 0, &errmsg );
931+
if ( res == SQLITE_OK )
932+
{
933+
// epsg column exists => need migration
934+
if ( sqlite3_exec( db,
935+
"ALTER TABLE tbl_srs RENAME TO tbl_srs_bak;"
936+
"CREATE TABLE tbl_srs ("
937+
"srs_id INTEGER PRIMARY KEY,"
938+
"description text NOT NULL,"
939+
"projection_acronym text NOT NULL,"
940+
"ellipsoid_acronym NOT NULL,"
941+
"parameters text NOT NULL,"
942+
"srid integer,"
943+
"auth_name varchar,"
944+
"auth_id varchar,"
945+
"is_geo integer NOT NULL,"
946+
"deprecated boolean);"
947+
"CREATE INDEX idx_srsauthid on tbl_srs(auth_name,auth_id);"
948+
"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;"
949+
"DROP TABLE tbl_srs_bak", 0, 0, &errmsg ) != SQLITE_OK
950+
)
951+
{
952+
if ( errorMessage )
953+
{
954+
*errorMessage = tr( "Migration of private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
955+
}
956+
sqlite3_free( errmsg );
957+
sqlite3_close( db );
958+
return false;
959+
}
960+
}
961+
else
962+
{
963+
sqlite3_free( errmsg );
964+
}
965+
966+
if ( sqlite3_exec( db, "DROP VIEW vw_srs", 0, 0, &errmsg ) != SQLITE_OK )
967+
{
968+
QgsDebugMsg( QString( "vw_srs didn't exists in private qgis.db: %1" ).arg( errmsg ) );
969+
}
970+
971+
if ( sqlite3_exec( db,
972+
"CREATE VIEW vw_srs AS"
973+
" SELECT"
974+
" a.description AS description"
975+
",a.srs_id AS srs_id"
976+
",a.is_geo AS is_geo"
977+
",coalesce(b.name,a.projection_acronym) AS name"
978+
",a.parameters AS parameters"
979+
",a.auth_name AS auth_name"
980+
",a.auth_id AS auth_id"
981+
",a.deprecated AS deprecated"
982+
" FROM tbl_srs a"
983+
" LEFT OUTER JOIN tbl_projection b ON a.projection_acronym=b.acronym"
984+
" ORDER BY coalesce(b.name,a.projection_acronym),a.description", 0, 0, &errmsg ) != SQLITE_OK
985+
)
986+
{
987+
if ( errorMessage )
988+
{
989+
*errorMessage = tr( "Update of view in private qgis.db failed.\n%1" ).arg( QString::fromUtf8( errmsg ) );
990+
}
991+
sqlite3_free( errmsg );
992+
sqlite3_close( db );
993+
return false;
994+
}
995+
996+
sqlite3_close( db );
997+
}
998+
return true;
999+
}
1000+
8881001

src/core/qgsapplication.h

+3
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ class CORE_EXPORT QgsApplication: public QApplication
183183
//! loads providers
184184
static void initQgis();
185185

186+
//! initialise qgis.db
187+
static bool createDB( QString* errorMessage = 0 );
188+
186189
//! deletes provider registry and map layer registry
187190
static void exitQgis();
188191

src/mapserver/qgis_map_serv.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ int main( int argc, char * argv[] )
195195
QgsDebugMsg( "User DB PATH: " + QgsApplication::qgisUserDbFilePath() );
196196

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

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

0 commit comments

Comments
 (0)