Index: src/core/qgscoordinatereferencesystem.cpp =================================================================== --- src/core/qgscoordinatereferencesystem.cpp (revision 11794) +++ src/core/qgscoordinatereferencesystem.cpp (working copy) @@ -25,6 +25,10 @@ #include #include #include +#include +#include +#include +#include #include "qgsapplication.h" #include "qgslogger.h" @@ -32,7 +36,6 @@ #include "qgis.h" //const vals declared here #include -#include //gdal and ogr includes (needed for == operator) #include @@ -169,6 +172,7 @@ createFromProj4( GEOPROJ4 ); } +//srid is a postgis sys ref no bool QgsCoordinateReferenceSystem::createFromSrid( long id ) { return loadFromDb( QgsApplication::srsDbFilePath(), "srid", id ); @@ -179,9 +183,10 @@ return loadFromDb( QgsApplication::srsDbFilePath(), "epsg", id ); } +//qgis internal sys ref now bool QgsCoordinateReferenceSystem::createFromSrsId( long id ) { - return loadFromDb( id < 100000 ? QgsApplication::srsDbFilePath() : + return loadFromDb( id < USER_CRS_START_ID ? QgsApplication::srsDbFilePath() : QgsApplication::qgisUserDbFilePath(), "srs_id", id ); } @@ -197,16 +202,12 @@ return mIsValidFlag; } - sqlite3 *myDatabase; - const char *myTail; - sqlite3_stmt *myPreparedStatement; - int myResult; - //check the db is available - myResult = openDb( db, &myDatabase ); - if ( myResult ) + QSqlDatabase myDb = QSqlDatabase::addDatabase ( "QSQLITE" ); + myDb.setDatabaseName ( db ); + if (!myDb.open()) { QgsDebugMsg( "failed : " + db + " could not be opened!" ); - return mIsValidFlag; + return false; } /* @@ -220,19 +221,23 @@ is_geo integer NOT NULL); */ + QSqlQuery myQuery; QString mySql = "select srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,epsg,is_geo from tbl_srs where " + field + "='" + QString::number( id ) + "'"; - myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().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 ) + if (!myQuery.exec( mySql ) ) { - mSrsId = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) ).toLong(); - mDescription = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 1 ) ); - mProjectionAcronym = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 2 ) ); - mEllipsoidAcronym = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 3 ) ); - QString toProj4 = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 4 ) ); - mSRID = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 5 ) ).toLong(); - mEpsg = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 6 ) ).toLong(); - int geo = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 7 ) ).toInt(); + QgsDebugMsg( "failed : " + mySql ); + return false; + } + if (myQuery.next()) //there should be only one... + { + mSrsId = myQuery.value(myQuery.record().indexOf("srs_id")).toLongLong(); + mDescription = myQuery.value(myQuery.record().indexOf("description")).toString(); + mProjectionAcronym = myQuery.value(myQuery.record().indexOf("projection_acronym")).toString(); + mEllipsoidAcronym = myQuery.value(myQuery.record().indexOf("ellipsoid_acronym")).toString(); + QString toProj4 = myQuery.value(myQuery.record().indexOf("parameters")).toString(); + mSRID = myQuery.value(myQuery.record().indexOf("srid")).toLongLong(); + mEpsg = myQuery.value(myQuery.record().indexOf("epsg")).toLongLong(); + int geo = myQuery.value(myQuery.record().indexOf("is_geo")).toInt(); mGeoFlag = ( geo == 0 ? false : true ); setProj4String( toProj4 ); setMapUnits(); @@ -241,8 +246,6 @@ { QgsDebugMsg( "failed : " + mySql ); } - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); return mIsValidFlag; } @@ -481,10 +484,6 @@ QgsCoordinateReferenceSystem::RecordMap myMap; QString myFieldName; QString myFieldValue; - sqlite3 *myDatabase; - const char *myTail; - sqlite3_stmt *myPreparedStatement; - int myResult; QgsDebugMsg( "running query: " + theSql ); // Get the full path name to the sqlite3 spatial reference database. @@ -496,33 +495,34 @@ " does not exist!" ); return myMap; } - - //check the db is available - myResult = openDb( myDatabaseFileName, &myDatabase ); - if ( myResult ) + QSqlDatabase myDb = QSqlDatabase::addDatabase ( "QSQLITE" ); + myDb.setDatabaseName ( myDatabaseFileName ); + if (!myDb.open()) { + QgsDebugMsg( "failed : " + myDatabaseFileName + " could not be opened!" ); return myMap; } - - myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().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 ) + QSqlQuery myQuery; + if (!myQuery.exec( theSql ) ) { + QgsDebugMsg( "failed : " + theSql ); + return myMap; + } + if (myQuery.next()) //there should be only one... + { QgsDebugMsg( "trying system srs.db" ); - int myColumnCount = sqlite3_column_count( myPreparedStatement ); + int myColumnCount = myQuery.record().count(); //loop through each column in the record adding its field name and vvalue to the map for ( int myColNo = 0; myColNo < myColumnCount; myColNo++ ) { - myFieldName = QString::fromUtf8(( char * )sqlite3_column_name( myPreparedStatement, myColNo ) ); - myFieldValue = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, myColNo ) ); + myFieldName = myQuery.record().fieldName( myColNo ); + myFieldValue = myQuery.record().indexOf( myFieldName ); myMap[myFieldName] = myFieldValue; } } else { - QgsDebugMsg( "trying system qgis.db" ); - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); + QgsDebugMsg( "trying user qgis.db" ); myDatabaseFileName = QgsApplication::qgisUserDbFilePath(); QFileInfo myFileInfo; @@ -532,24 +532,27 @@ QgsDebugMsg( "users qgis.db not found" ); return myMap; } - - //check the db is available - myResult = openDb( myDatabaseFileName, &myDatabase ); - if ( myResult ) + myDb.setDatabaseName ( myDatabaseFileName ); + if (!myDb.open()) { + QgsDebugMsg( "failed : " + myDatabaseFileName + " could not be opened!" ); return myMap; } - - myResult = sqlite3_prepare( myDatabase, theSql.toUtf8(), theSql.toUtf8().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 ) + QSqlQuery myQuery; + if (!myQuery.exec( theSql ) ) { - int myColumnCount = sqlite3_column_count( myPreparedStatement ); + QgsDebugMsg( "failed : " + theSql ); + return myMap; + } + if (myQuery.next()) //there should be only one... + { + QgsDebugMsg( "trying system srs.db" ); + int myColumnCount = myQuery.record().count(); //loop through each column in the record adding its field name and vvalue to the map for ( int myColNo = 0; myColNo < myColumnCount; myColNo++ ) { - myFieldName = QString::fromUtf8(( char * )sqlite3_column_name( myPreparedStatement, myColNo ) ); - myFieldValue = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, myColNo ) ); + myFieldName = myQuery.record().fieldName( myColNo ); + myFieldValue = myQuery.value( myQuery.record().indexOf( myFieldName ) ).toString(); myMap[myFieldName] = myFieldValue; } } @@ -559,8 +562,6 @@ } } - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); #ifdef QGISDEBUG QgsDebugMsg( "retrieved: " + theSql ); @@ -782,11 +783,6 @@ return 0; } - sqlite3 *myDatabase; - const char *myTail; - sqlite3_stmt *myPreparedStatement; - int myResult; - // Set up the query to retrieve the projection information needed to populate the list QString mySql = QString( "select srs_id,parameters from tbl_srs where projection_acronym='" + mProjectionAcronym + "' and ellipsoid_acronym='" + mEllipsoidAcronym + "'" ); @@ -794,79 +790,67 @@ QString myDatabaseFileName = QgsApplication::srsDbFilePath(); //check the db is available - myResult = openDb( myDatabaseFileName, &myDatabase ); - if ( myResult ) + bool myResult = openDb( myDatabaseFileName ); + if ( !myResult ) { return 0; } - - myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); - // XXX Need to free memory from the error msg if one is set - if ( myResult == SQLITE_OK ) + QSqlQuery myQuery; + if (!myQuery.exec( mySql ) ) { + QgsDebugMsg( "failed : " + mySql ); + return false; + } - while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW ) + + while ( myQuery.next() ) + { + QString mySrsId = myQuery.value(myQuery.record().indexOf("srs_id")).toString(); + QString myProj4String = myQuery.value(myQuery.record().indexOf("parameters")).toString(); + if ( equals( myProj4String ) ) { - QString mySrsId = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) ); - QString myProj4String = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 1 ) ); - if ( equals( myProj4String ) ) - { - QgsDebugMsg( "-------> MATCH FOUND in srs.db srsid: " + mySrsId ); - // close the sqlite3 statement - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); - return mySrsId.toLong(); - } - else - { -// QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String)); - } + QgsDebugMsg( "-------> MATCH FOUND in srs.db srsid: " + mySrsId ); + return mySrsId.toLong(); } + else + { + QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String)); + } } QgsDebugMsg( "no match found in srs.db, trying user db now!" ); - // close the sqlite3 statement - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); + // // Try the users db now // myDatabaseFileName = QgsApplication::qgisUserDbFilePath(); //check the db is available - myResult = openDb( myDatabaseFileName, &myDatabase ); - if ( myResult ) + myResult = openDb( myDatabaseFileName ); + if ( !myResult ) { return 0; } - - myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); - // XXX Need to free memory from the error msg if one is set - if ( myResult == SQLITE_OK ) + if (!myQuery.exec( mySql ) ) { - - while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW ) + QgsDebugMsg( "failed : " + mySql ); + return false; + } + while ( myQuery.next() ) + { + QString mySrsId = myQuery.value(myQuery.record().indexOf("srs_id")).toString(); + QString myProj4String = myQuery.value(myQuery.record().indexOf("parameters")).toString(); + if ( equals( myProj4String ) ) { - QString mySrsId = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) ); - QString myProj4String = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 1 ) ); - if ( equals( myProj4String ) ) - { - QgsDebugMsg( "-------> MATCH FOUND in user qgis.db srsid: " + mySrsId ); - // close the sqlite3 statement - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); - return mySrsId.toLong(); - } - else - { -// QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String)); - } + QgsDebugMsg( "-------> MATCH FOUND in user qgis.db srsid: " + mySrsId ); + return mySrsId.toLong(); } + else + { + QgsDebugMsg(QString(" Not matched : %1").arg(myProj4String)); + } } QgsDebugMsg( "no match found in user db" ); - // close the sqlite3 statement - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); return 0; } @@ -1082,7 +1066,7 @@ // // Determine if this is a user projection or a system on - // user projection defs all have srs_id >= 100000 + // user projection defs all have srs_id >= USER_CRS_START_ID // if ( theSrsId >= USER_CRS_START_ID ) { @@ -1101,55 +1085,44 @@ } QgsDebugMsg( "db = " + myDatabaseFileName ); - sqlite3 *db; - int rc; - rc = openDb( myDatabaseFileName, &db ); - if ( rc ) + bool myResult = openDb( myDatabaseFileName ); + if ( !myResult ) { return QString(); } - // prepare the sql statement - const char *pzTail; - sqlite3_stmt *ppStmt; - rc = sqlite3_prepare( db, mySql.toUtf8(), mySql.toUtf8().length(), &ppStmt, &pzTail ); - // XXX Need to free memory from the error msg if one is set - - if ( rc == SQLITE_OK ) + QSqlQuery myQuery; + if ( !myQuery.exec( mySql ) ) { - if ( sqlite3_step( ppStmt ) == SQLITE_ROW ) - { - myProjString = QString::fromUtf8(( char* )sqlite3_column_text( ppStmt, 0 ) ); - } + QgsDebugMsg( "failed : " + mySql ); + return false; } - // close the statement - sqlite3_finalize( ppStmt ); - // close the database - sqlite3_close( db ); + if ( myQuery.next() ) //there should be only one... + { + myProjString = myQuery.value(myQuery.record().indexOf( "parameters" ) ).toString(); + } //assert(myProjString.length() > 0); return myProjString; } -int QgsCoordinateReferenceSystem::openDb( QString path, sqlite3 **db ) +bool QgsCoordinateReferenceSystem::openDb( QString db ) { - QgsDebugMsg( "path = " + path ); - int myResult = sqlite3_open( path.toUtf8().data(), db ); + QFileInfo myInfo( db ); + if ( !myInfo.exists() ) + { + QgsDebugMsg( "failed : " + db + " does not exist!" ); + return false; + } - if ( myResult ) + QSqlDatabase myDb = QSqlDatabase::addDatabase ( "QSQLITE" ); + myDb.setDatabaseName ( db ); + if (!myDb.open()) { - QgsDebugMsg( "Can't open database: " + QString( sqlite3_errmsg( *db ) ) ); - // XXX This will likely never happen since on open, sqlite creates the - // database if it does not exist. - // ... unfortunately it happens on Windows - QgsMessageOutput* output = QgsMessageOutput::createMessageOutput(); - output->setTitle( "Error" ); - output->setMessage( "Could not open CRS database " + path + - "
Error(" + QString::number( myResult ) + "): " + - QString( sqlite3_errmsg( *db ) ), QgsMessageOutput::MessageText ); - output->showMessage(); + QgsDebugMsg( "failed : " + db + " could not be opened!" ); + return false; } - return myResult; + return true; } void QgsCoordinateReferenceSystem::setCustomSrsValidation( CUSTOM_CRS_VALIDATION f ) @@ -1212,7 +1185,7 @@ .arg( toProj4() ); //if this is the first record we need to ensure that its srs_id is 10000. For - //any rec after that sqlite3 will take care of the autonumering + //any rec after that sqlite3 will take care of the autonumbering //this was done to support sqlite 3.0 as it does not yet support //the autoinc related system tables. if ( getRecordCount() == 0 ) @@ -1230,58 +1203,50 @@ + "','" + ellipsoidAcronym() + "','" + sqlSafeString( toProj4() ) + "',0)"; // <-- is_geo shamelessly hard coded for now } - sqlite3 *myDatabase; - const char *myTail; - sqlite3_stmt *myPreparedStatement; - int myResult; //check the db is available - myResult = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase ); - if ( myResult != SQLITE_OK ) + bool myResult = openDb( QgsApplication::qgisUserDbFilePath().toUtf8().data() ); + if ( !myResult ) { - QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " ).arg( sqlite3_errmsg( myDatabase ) ).arg( QgsApplication::qgisUserDbFilePath() ) ); - // XXX This will likely never happen since on open, sqlite creates the - // database if it does not exist. - assert( myResult == SQLITE_OK ); + QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n (file name) " ).arg( QgsApplication::qgisUserDbFilePath() ) ); + assert( myResult == true ); } QgsDebugMsg( QString( "Update or insert sql \n%1" ).arg( mySql ) ); - myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); - sqlite3_step( myPreparedStatement ); - // XXX Need to free memory from the error msg if one is set - return myResult == SQLITE_OK; + QSqlQuery myQuery; + if ( !myQuery.exec( mySql ) ) + { + QgsDebugMsg( "failed : " + mySql ); + return false; + } + return true; } long QgsCoordinateReferenceSystem::getRecordCount() { - sqlite3 *myDatabase; - const char *myTail; - sqlite3_stmt *myPreparedStatement; - int myResult; long myRecordCount = 0; //check the db is available - myResult = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase ); - if ( myResult != SQLITE_OK ) + QString myDatabaseFileName = QgsApplication::srsDbFilePath(); + bool myResult = openDb( myDatabaseFileName ); + if ( !myResult ) { - QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) ); + QgsDebugMsg( QString( "Can't open database: %1" ).arg( myDatabaseFileName ) ); // XXX This will likely never happen since on open, sqlite creates the // database if it does not exist. - assert( myResult == SQLITE_OK ); + assert( myResult ); } // Set up the query to retrieve the projection information needed to populate the ELLIPSOID list - QString mySql = "select count(*) from tbl_srs"; - myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail ); - // XXX Need to free memory from the error msg if one is set - if ( myResult == SQLITE_OK ) + QSqlQuery myQuery; + QString mySql = "select count(*) as reccount from tbl_srs"; + if ( !myQuery.exec( mySql ) ) { - if ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW ) - { - QString myRecordCountString = QString::fromUtf8(( char * )sqlite3_column_text( myPreparedStatement, 0 ) ); - myRecordCount = myRecordCountString.toLong(); - } + QgsDebugMsg( "failed : " + mySql ); + return false; } - // close the sqlite3 statement - sqlite3_finalize( myPreparedStatement ); - sqlite3_close( myDatabase ); + if ( myQuery.next() ) //there should be only one... + { + QString myRecordCountString = myQuery.value(myQuery.record().indexOf("reccount")).toString(); + myRecordCount = myRecordCountString.toLong(); + } return myRecordCount; } Index: src/core/CMakeLists.txt =================================================================== --- src/core/CMakeLists.txt (revision 11794) +++ src/core/CMakeLists.txt (working copy) @@ -1,5 +1,4 @@ - ############################################################# # sources @@ -269,6 +268,7 @@ ${QT_QTSVG_LIBRARY} ${QT_QTNETWORK_LIBRARY} ${QT_QTMAIN_LIBRARY} + ${QT_QTSQL_LIBRARY} ${PROJ_LIBRARY} ${GEOS_LIBRARY} ${GDAL_LIBRARY} Index: src/core/qgscoordinatereferencesystem.h =================================================================== --- src/core/qgscoordinatereferencesystem.h (revision 11794) +++ src/core/qgscoordinatereferencesystem.h (working copy) @@ -361,9 +361,11 @@ */ RecordMap getRecord( QString theSql ); - // Open SQLite db and show message if ccannot be opened - // returns the same code as sqlite3_open - static int openDb( QString path, sqlite3 **db ); + /** Open SQLite db and show message if cannot be opened + @note changed in QGIS 1.4 since we no longer use + native sqlite interface but rather the QtSql abstraction + so the second sqlite db instance parameter has been removed */ + static bool openDb( QString path ); //!The internal sqlite3 srs.db primary key for this srs long mSrsId; Index: mac/xcode/Qgis.xcodeproj/project.pbxproj =================================================================== --- mac/xcode/Qgis.xcodeproj/project.pbxproj (revision 11794) +++ mac/xcode/Qgis.xcodeproj/project.pbxproj (working copy) @@ -2714,6 +2714,13 @@ remoteGlobalIDString = 72FB0B4A0FFD680F0052360B; remoteInfo = evisplugin; }; + 80D57495107A94AC00EEB8C4 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 20286C28FDCF999611CA2CEA /* Project object */; + proxyType = 1; + remoteGlobalIDString = 72A07CBB0F3BBFEE004FC2A4; + remoteInfo = memoryprovider; + }; /* End PBXContainerItemProxy section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -12481,6 +12488,7 @@ 729288730F71F8A0006D9489 /* PBXBuildRule */, ); dependencies = ( + 80D57496107A94AC00EEB8C4 /* PBXTargetDependency */, ); name = qgis_help; productName = ggis_help; @@ -14804,6 +14812,11 @@ target = 72FB0B4A0FFD680F0052360B /* evisplugin */; targetProxy = 72FB0B6C0FFD6B8F0052360B /* PBXContainerItemProxy */; }; + 80D57496107A94AC00EEB8C4 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 72A07CBB0F3BBFEE004FC2A4 /* memoryprovider */; + targetProxy = 80D57495107A94AC00EEB8C4 /* PBXContainerItemProxy */; + }; /* End PBXTargetDependency section */ /* Begin XCBuildConfiguration section */