Skip to content

Commit 707069d

Browse files
author
jef
committedApr 3, 2010
cleanup and fix #2619
git-svn-id: http://svn.osgeo.org/qgis/trunk@13227 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 61234d5 commit 707069d

File tree

2 files changed

+82
-118
lines changed

2 files changed

+82
-118
lines changed
 

‎src/app/qgscustomprojectiondialog.cpp

Lines changed: 81 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <QLocale>
3131

3232
//stdc++ includes
33-
#include <cassert>
3433
#include <fstream>
3534
#include <sqlite3.h>
3635
#include "qgslogger.h"
@@ -116,10 +115,10 @@ void QgsCustomProjectionDialog::on_pbnDelete_clicked()
116115
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
117116
// XXX This will likely never happen since on open, sqlite creates the
118117
// database if it does not exist.
119-
assert( myResult == SQLITE_OK );
118+
Q_ASSERT( myResult == SQLITE_OK );
120119
}
121120
// Set up the query to retrieve the projection information needed to populate the ELLIPSOID list
122-
QString mySql = "delete from tbl_srs where srs_id='" + mCurrentRecordId + "'";
121+
QString mySql = "delete from tbl_srs where srs_id=" + quotedValue( mCurrentRecordId );
123122
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
124123
// XXX Need to free memory from the error msg if one is set
125124
QgsDebugMsg( QString( "Query to delete current:%1" ).arg( mySql ) );
@@ -152,14 +151,6 @@ void QgsCustomProjectionDialog::on_pbnDelete_clicked()
152151
return ;
153152
}
154153

155-
void QgsCustomProjectionDialog::on_pbnClose_clicked()
156-
{
157-
close();
158-
}
159-
160-
161-
162-
163154
long QgsCustomProjectionDialog::getRecordCount()
164155
{
165156
sqlite3 *myDatabase;
@@ -174,7 +165,7 @@ long QgsCustomProjectionDialog::getRecordCount()
174165
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
175166
// XXX This will likely never happen since on open, sqlite creates the
176167
// database if it does not exist.
177-
assert( myResult == SQLITE_OK );
168+
Q_ASSERT( myResult == SQLITE_OK );
178169
}
179170
// Set up the query to retrieve the projection information needed to populate the ELLIPSOID list
180171
QString mySql = "select count(*) from tbl_srs";
@@ -209,10 +200,10 @@ QString QgsCustomProjectionDialog::getProjectionFamilyName( QString theProjectio
209200
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
210201
// XXX This will likely never happen since on open, sqlite creates the
211202
// database if it does not exist.
212-
assert( myResult == SQLITE_OK );
203+
Q_ASSERT( myResult == SQLITE_OK );
213204
}
214205
// Set up the query to retrieve the projection information needed to populate the PROJECTION list
215-
QString mySql = "select name from tbl_projection where acronym='" + theProjectionFamilyAcronym + "'";
206+
QString mySql = "select name from tbl_projection where acronym=" + quotedValue( theProjectionFamilyAcronym );
216207
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
217208
// XXX Need to free memory from the error msg if one is set
218209
if ( myResult == SQLITE_OK )
@@ -240,10 +231,10 @@ QString QgsCustomProjectionDialog::getEllipsoidName( QString theEllipsoidAcronym
240231
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
241232
// XXX This will likely never happen since on open, sqlite creates the
242233
// database if it does not exist.
243-
assert( myResult == SQLITE_OK );
234+
Q_ASSERT( myResult == SQLITE_OK );
244235
}
245236
// Set up the query to retrieve the projection information needed to populate the ELLIPSOID list
246-
QString mySql = "select name from tbl_ellipsoid where acronym='" + theEllipsoidAcronym + "'";
237+
QString mySql = "select name from tbl_ellipsoid where acronym=" + quotedValue( theEllipsoidAcronym );
247238
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
248239
// XXX Need to free memory from the error msg if one is set
249240
if ( myResult == SQLITE_OK )
@@ -271,10 +262,10 @@ QString QgsCustomProjectionDialog::getProjectionFamilyAcronym( QString theProjec
271262
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
272263
// XXX This will likely never happen since on open, sqlite creates the
273264
// database if it does not exist.
274-
assert( myResult == SQLITE_OK );
265+
Q_ASSERT( myResult == SQLITE_OK );
275266
}
276267
// Set up the query to retrieve the projection information needed to populate the PROJECTION list
277-
QString mySql = "select acronym from tbl_projection where name='" + theProjectionFamilyName + "'";
268+
QString mySql = "select acronym from tbl_projection where name=" + quotedValue( theProjectionFamilyName );
278269
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
279270
// XXX Need to free memory from the error msg if one is set
280271
if ( myResult == SQLITE_OK )
@@ -302,10 +293,10 @@ QString QgsCustomProjectionDialog::getEllipsoidAcronym( QString theEllipsoidName
302293
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
303294
// XXX This will likely never happen since on open, sqlite creates the
304295
// database if it does not exist.
305-
assert( myResult == SQLITE_OK );
296+
Q_ASSERT( myResult == SQLITE_OK );
306297
}
307298
// Set up the query to retrieve the projection information needed to populate the ELLIPSOID list
308-
QString mySql = "select acronym from tbl_ellipsoid where name='" + theEllipsoidName + "'";
299+
QString mySql = "select acronym from tbl_ellipsoid where name=" + quotedValue( theEllipsoidName );
309300
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
310301
// XXX Need to free memory from the error msg if one is set
311302
if ( myResult == SQLITE_OK )
@@ -334,7 +325,7 @@ void QgsCustomProjectionDialog::on_pbnFirst_clicked()
334325
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
335326
// XXX This will likely never happen since on open, sqlite creates the
336327
// database if it does not exist.
337-
assert( myResult == SQLITE_OK );
328+
Q_ASSERT( myResult == SQLITE_OK );
338329
}
339330

340331
QString mySql = "select * from tbl_srs order by srs_id limit 1";
@@ -406,7 +397,7 @@ void QgsCustomProjectionDialog::on_pbnPrevious_clicked()
406397
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
407398
// XXX This will likely never happen since on open, sqlite creates the
408399
// database if it does not exist.
409-
assert( myResult == SQLITE_OK );
400+
Q_ASSERT( myResult == SQLITE_OK );
410401
}
411402

412403
QString mySql = "select * from tbl_srs where srs_id < " + mCurrentRecordId + " order by srs_id desc limit 1";
@@ -479,7 +470,7 @@ void QgsCustomProjectionDialog::on_pbnNext_clicked()
479470
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
480471
// XXX This will likely never happen since on open, sqlite creates the
481472
// database if it does not exist.
482-
assert( myResult == SQLITE_OK );
473+
Q_ASSERT( myResult == SQLITE_OK );
483474
}
484475

485476
QString mySql = "select * from tbl_srs where srs_id > " + mCurrentRecordId + " order by srs_id asc limit 1";
@@ -548,7 +539,7 @@ void QgsCustomProjectionDialog::on_pbnLast_clicked()
548539
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
549540
// XXX This will likely never happen since on open, sqlite creates the
550541
// database if it does not exist.
551-
assert( myResult == SQLITE_OK );
542+
Q_ASSERT( myResult == SQLITE_OK );
552543
}
553544

554545
QString mySql = "select * from tbl_srs order by srs_id desc limit 1";
@@ -733,50 +724,54 @@ void QgsCustomProjectionDialog::on_pbnSave_clicked()
733724
//the autoinc related system tables.
734725
if ( getRecordCount() == 0 )
735726
{
736-
mySql = QString( "insert into tbl_srs (srs_id,description,projection_acronym,ellipsoid_acronym,parameters,is_geo) " )
737-
+ " values (" + QString::number( USER_CRS_START_ID ) + ",'"
738-
+ sqlSafeString( myName ) + "','" + myProjectionAcronym
739-
+ "','" + myEllipsoidAcronym + "','" + sqlSafeString( myParameters )
740-
+ "',0)"; // <-- is_geo shamelessly hard coded for now
727+
mySql = "insert into tbl_srs (srs_id,description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ("
728+
+ QString::number( USER_CRS_START_ID )
729+
+ "," + quotedValue( myName )
730+
+ "," + quotedValue( myProjectionAcronym )
731+
+ "," + quotedValue( myEllipsoidAcronym )
732+
+ "," + quotedValue( myParameters )
733+
+ ",0)"; // <-- is_geo shamelessly hard coded for now
741734
}
742735
else
743736
{
744-
mySql = "insert into tbl_srs (description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ('"
745-
+ sqlSafeString( myName ) + "','" + myProjectionAcronym
746-
+ "','" + myEllipsoidAcronym + "','" + sqlSafeString( myParameters )
747-
+ "',0)"; // <-- is_geo shamelessly hard coded for now
737+
mySql = "insert into tbl_srs (description,projection_acronym,ellipsoid_acronym,parameters,is_geo) values ("
738+
+ quotedValue( myName )
739+
+ "," + quotedValue( myProjectionAcronym )
740+
+ "," + quotedValue( myEllipsoidAcronym )
741+
+ "," + quotedValue( myParameters )
742+
+ ",0)"; // <-- is_geo shamelessly hard coded for now
748743
}
749744
}
750745
else //user is updating an existing record
751746
{
752-
mySql = "update tbl_srs set description='" + sqlSafeString( myName )
753-
+ "',projection_acronym='" + myProjectionAcronym
754-
+ "',ellipsoid_acronym='" + myEllipsoidAcronym
755-
+ "',parameters='" + sqlSafeString( myParameters ) + "' "
747+
mySql = "update tbl_srs set description="
748+
+ quotedValue( myName )
749+
+ ",projection_acronym=" + quotedValue( myProjectionAcronym )
750+
+ ",ellipsoid_acronym=" + quotedValue( myEllipsoidAcronym )
751+
+ ",parameters=" + quotedValue( myParameters )
756752
+ ",is_geo=0" // <--shamelessly hard coded for now
757-
+ " where srs_id='" + mCurrentRecordId + "'"
753+
+ " where srs_id=" + quotedValue( mCurrentRecordId )
758754
;
759755
}
760756
sqlite3 *myDatabase;
761757
const char *myTail;
762758
sqlite3_stmt *myPreparedStatement;
763759
int myResult;
764760
//check the db is available
765-
myResult = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8().data(), &myDatabase );
761+
myResult = sqlite3_open( QgsApplication::qgisUserDbFilePath().toUtf8(), &myDatabase );
766762
if ( myResult != SQLITE_OK )
767763
{
768764
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() ) );
769765
// XXX This will likely never happen since on open, sqlite creates the
770766
// database if it does not exist.
771-
assert( myResult == SQLITE_OK );
767+
Q_ASSERT( myResult == SQLITE_OK );
772768
}
773-
QgsDebugMsg( QString( "Update or insert sql \n%1" ).arg( mySql ) );
774769
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
775770
sqlite3_step( myPreparedStatement );
776771
// XXX Need to free memory from the error msg if one is set
777772
if ( myResult != SQLITE_OK )
778773
{
779-
QgsDebugMsg( "Update or insert failed in custom projection dialog " );
774+
QgsDebugMsg( QString( "update or insert failed in custom projection dialog: %1 [%2]" ).arg( mySql ).arg( sqlite3_errmsg( myDatabase ) ) );
780775
}
781776
//reinstate button if we were doing an insert
782777
else if ( pbnNew->text() == tr( "Abort" ) )
@@ -794,49 +789,56 @@ void QgsCustomProjectionDialog::on_pbnSave_clicked()
794789
// This is a must, or else we can't select it from the vw_srs table.
795790
// Actually, add it always and let the SQL PRIMARY KEY remove duplicates.
796791

797-
sqlite3 *srsDatabase;
798-
const char *srsTail;
799-
sqlite3_stmt *srsPreparedStatement;
800-
int srsResult;
801-
802792
//check the db is available
803-
srsResult = sqlite3_open( QgsApplication::srsDbFilePath().toUtf8().data(), &srsDatabase );
804-
if ( myResult != SQLITE_OK )
805-
{
806-
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( srsDatabase ) ) );
807-
// XXX This will likely never happen since on open, sqlite creates the
808-
// database if it does not exist.
809-
assert( myResult == SQLITE_OK );
810-
}
811-
// Set up the query to retrieve the projection information needed to populate the PROJECTION list
812-
QString srsSql = "select * from tbl_projection where acronym='" + myProjectionAcronym + "'";
813-
srsResult = sqlite3_prepare( srsDatabase, srsSql.toUtf8(), srsSql.length(), &srsPreparedStatement, &srsTail );
814-
// XXX Need to free memory from the error msg if one is set
815-
if ( srsResult == SQLITE_OK )
793+
sqlite3 *srsDatabase;
794+
int srsResult = sqlite3_open( QgsApplication::srsDbFilePath().toUtf8(), &srsDatabase );
795+
if ( myResult == SQLITE_OK )
816796
{
817-
if ( sqlite3_step( srsPreparedStatement ) == SQLITE_ROW )
797+
// Set up the query to retrieve the projection information needed to populate the PROJECTION list
798+
QString srsSql = "select acronym,name,notes,parameters from tbl_projection where acronym=" + quotedValue( myProjectionAcronym );
799+
800+
const char *srsTail;
801+
sqlite3_stmt *srsPreparedStatement;
802+
srsResult = sqlite3_prepare( srsDatabase, srsSql.toUtf8(), srsSql.length(), &srsPreparedStatement, &srsTail );
803+
// XXX Need to free memory from the error msg if one is set
804+
if ( srsResult == SQLITE_OK )
818805
{
819-
QgsDebugMsg( "Trying to insert projection" );
820-
// We have the result from system srs.db. Now insert into user db.
821-
mySql = QString( "INSERT INTO tbl_projection VALUES('%1','%2','%3','%4')" )
822-
.arg( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 0 ) ) )
823-
.arg( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 1 ) ) )
824-
.arg( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 2 ) ) )
825-
.arg( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 3 ) ) );
826-
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.length(), &myPreparedStatement, &myTail );
827-
sqlite3_step( myPreparedStatement );
828-
if ( myResult != SQLITE_OK )
806+
if ( sqlite3_step( srsPreparedStatement ) == SQLITE_ROW )
829807
{
830-
QgsDebugMsg( "Update or insert failed in custom projection dialog: " + mySql );
808+
QgsDebugMsg( "Trying to insert projection" );
809+
// We have the result from system srs.db. Now insert into user db.
810+
mySql = "insert into tbl_projection(acronym,name,notes,parameters) values ("
811+
+ quotedValue( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 0 ) ) )
812+
+ "," + quotedValue( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 1 ) ) )
813+
+ "," + quotedValue( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 2 ) ) )
814+
+ "," + quotedValue( QString::fromUtf8(( char * )sqlite3_column_text( srsPreparedStatement, 3 ) ) )
815+
+ ")"
816+
;
817+
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.length(), &myPreparedStatement, &myTail );
818+
sqlite3_step( myPreparedStatement );
819+
if ( myResult != SQLITE_OK )
820+
{
821+
QgsDebugMsg( QString( "Update or insert failed in custom projection dialog: %1 [%2]" ).arg( mySql ).arg( sqlite3_errmsg( myDatabase ) ) );
822+
}
823+
sqlite3_finalize( myPreparedStatement );
831824
}
832-
sqlite3_finalize( myPreparedStatement );
825+
826+
sqlite3_finalize( srsPreparedStatement );
833827
}
828+
else
829+
{
830+
QgsDebugMsg( QString( "prepare failed: %1 [%2]" ).arg( srsSql ).arg( sqlite3_errmsg( srsDatabase ) ) );
831+
}
832+
833+
sqlite3_close( srsDatabase );
834+
}
835+
else
836+
{
837+
QgsDebugMsg( QString( "Can't open database %1 [%2]" ).arg( QgsApplication::srsDbFilePath() ).arg( sqlite3_errmsg( srsDatabase ) ) );
834838
}
835839

836-
// close the user and srs sqlite3 db
840+
// close sqlite3 db
837841
sqlite3_close( myDatabase );
838-
sqlite3_finalize( srsPreparedStatement );
839-
sqlite3_close( srsDatabase );
840842

841843
pbnDelete->setEnabled( true );
842844
}
@@ -957,44 +959,9 @@ QString QgsCustomProjectionDialog::getEllipseFromParameters()
957959
}
958960
}
959961

960-
/*!
961-
* \brief Make the string safe for use in SQL statements.
962-
* This involves escaping single quotes, double quotes, backslashes,
963-
* and optionally, percentage symbols. Percentage symbols are used
964-
* as wildcards sometimes and so when using the string as part of the
965-
* LIKE phrase of a select statement, should be escaped.
966-
* \arg const QString in The input string to make safe.
967-
* \return The string made safe for SQL statements.
968-
*/
969-
const QString QgsCustomProjectionDialog::sqlSafeString( const QString theSQL )
962+
QString QgsCustomProjectionDialog::quotedValue( QString value )
970963
{
971-
972-
QString myRetval;
973-
QChar *it = ( QChar * )theSQL.unicode();
974-
for ( int i = 0; i < theSQL.length(); i++ )
975-
{
976-
if ( *it == '\"' )
977-
{
978-
myRetval += "\\\"";
979-
}
980-
else if ( *it == '\'' )
981-
{
982-
myRetval += "\\'";
983-
}
984-
else if ( *it == '\\' )
985-
{
986-
myRetval += "\\\\";
987-
}
988-
else if ( *it == '%' )
989-
{
990-
myRetval += "\\%";
991-
}
992-
else
993-
{
994-
myRetval += *it;
995-
}
996-
it++;
997-
}
998-
return myRetval;
964+
value.replace( "'", "''" );
965+
return value.prepend( "'" ).append( "'" );
999966
}
1000967

‎src/app/qgscustomprojectiondialog.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class QgsCustomProjectionDialog : public QDialog, private Ui::QgsCustomProjectio
3737
//a recursive function to make a directory and its ancestors
3838
public slots:
3939
void on_pbnCalculate_clicked();
40-
void on_pbnClose_clicked();
4140
void on_pbnDelete_clicked();
4241
//
4342
// Database navigation controles
@@ -67,15 +66,13 @@ class QgsCustomProjectionDialog : public QDialog, private Ui::QgsCustomProjectio
6766
QString getProjFromParameters();
6867
QString getEllipseFromParameters();
6968

70-
7169
QString mCurrentRecordId;
7270
long mCurrentRecordLong;
7371
//the record previous to starting an insert operation
7472
//so that we can return to it if the record insert is aborted
7573
long mLastRecordLong;
7674
long mRecordCountLong;
77-
const QString sqlSafeString( const QString theSQL );
78-
75+
QString quotedValue( QString value );
7976
};
8077

8178
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.