30
30
#include < QLocale>
31
31
32
32
// stdc++ includes
33
- #include < cassert>
34
33
#include < fstream>
35
34
#include < sqlite3.h>
36
35
#include " qgslogger.h"
@@ -116,10 +115,10 @@ void QgsCustomProjectionDialog::on_pbnDelete_clicked()
116
115
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
117
116
// XXX This will likely never happen since on open, sqlite creates the
118
117
// database if it does not exist.
119
- assert ( myResult == SQLITE_OK );
118
+ Q_ASSERT ( myResult == SQLITE_OK );
120
119
}
121
120
// 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 ) ;
123
122
myResult = sqlite3_prepare ( myDatabase, mySql.toUtf8 (), mySql.toUtf8 ().length (), &myPreparedStatement, &myTail );
124
123
// XXX Need to free memory from the error msg if one is set
125
124
QgsDebugMsg ( QString ( " Query to delete current:%1" ).arg ( mySql ) );
@@ -152,14 +151,6 @@ void QgsCustomProjectionDialog::on_pbnDelete_clicked()
152
151
return ;
153
152
}
154
153
155
- void QgsCustomProjectionDialog::on_pbnClose_clicked ()
156
- {
157
- close ();
158
- }
159
-
160
-
161
-
162
-
163
154
long QgsCustomProjectionDialog::getRecordCount ()
164
155
{
165
156
sqlite3 *myDatabase;
@@ -174,7 +165,7 @@ long QgsCustomProjectionDialog::getRecordCount()
174
165
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
175
166
// XXX This will likely never happen since on open, sqlite creates the
176
167
// database if it does not exist.
177
- assert ( myResult == SQLITE_OK );
168
+ Q_ASSERT ( myResult == SQLITE_OK );
178
169
}
179
170
// Set up the query to retrieve the projection information needed to populate the ELLIPSOID list
180
171
QString mySql = " select count(*) from tbl_srs" ;
@@ -209,10 +200,10 @@ QString QgsCustomProjectionDialog::getProjectionFamilyName( QString theProjectio
209
200
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
210
201
// XXX This will likely never happen since on open, sqlite creates the
211
202
// database if it does not exist.
212
- assert ( myResult == SQLITE_OK );
203
+ Q_ASSERT ( myResult == SQLITE_OK );
213
204
}
214
205
// 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 ) ;
216
207
myResult = sqlite3_prepare ( myDatabase, mySql.toUtf8 (), mySql.toUtf8 ().length (), &myPreparedStatement, &myTail );
217
208
// XXX Need to free memory from the error msg if one is set
218
209
if ( myResult == SQLITE_OK )
@@ -240,10 +231,10 @@ QString QgsCustomProjectionDialog::getEllipsoidName( QString theEllipsoidAcronym
240
231
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
241
232
// XXX This will likely never happen since on open, sqlite creates the
242
233
// database if it does not exist.
243
- assert ( myResult == SQLITE_OK );
234
+ Q_ASSERT ( myResult == SQLITE_OK );
244
235
}
245
236
// 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 ) ;
247
238
myResult = sqlite3_prepare ( myDatabase, mySql.toUtf8 (), mySql.toUtf8 ().length (), &myPreparedStatement, &myTail );
248
239
// XXX Need to free memory from the error msg if one is set
249
240
if ( myResult == SQLITE_OK )
@@ -271,10 +262,10 @@ QString QgsCustomProjectionDialog::getProjectionFamilyAcronym( QString theProjec
271
262
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
272
263
// XXX This will likely never happen since on open, sqlite creates the
273
264
// database if it does not exist.
274
- assert ( myResult == SQLITE_OK );
265
+ Q_ASSERT ( myResult == SQLITE_OK );
275
266
}
276
267
// 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 ) ;
278
269
myResult = sqlite3_prepare ( myDatabase, mySql.toUtf8 (), mySql.toUtf8 ().length (), &myPreparedStatement, &myTail );
279
270
// XXX Need to free memory from the error msg if one is set
280
271
if ( myResult == SQLITE_OK )
@@ -302,10 +293,10 @@ QString QgsCustomProjectionDialog::getEllipsoidAcronym( QString theEllipsoidName
302
293
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
303
294
// XXX This will likely never happen since on open, sqlite creates the
304
295
// database if it does not exist.
305
- assert ( myResult == SQLITE_OK );
296
+ Q_ASSERT ( myResult == SQLITE_OK );
306
297
}
307
298
// 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 ) ;
309
300
myResult = sqlite3_prepare ( myDatabase, mySql.toUtf8 (), mySql.toUtf8 ().length (), &myPreparedStatement, &myTail );
310
301
// XXX Need to free memory from the error msg if one is set
311
302
if ( myResult == SQLITE_OK )
@@ -334,7 +325,7 @@ void QgsCustomProjectionDialog::on_pbnFirst_clicked()
334
325
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
335
326
// XXX This will likely never happen since on open, sqlite creates the
336
327
// database if it does not exist.
337
- assert ( myResult == SQLITE_OK );
328
+ Q_ASSERT ( myResult == SQLITE_OK );
338
329
}
339
330
340
331
QString mySql = " select * from tbl_srs order by srs_id limit 1" ;
@@ -406,7 +397,7 @@ void QgsCustomProjectionDialog::on_pbnPrevious_clicked()
406
397
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
407
398
// XXX This will likely never happen since on open, sqlite creates the
408
399
// database if it does not exist.
409
- assert ( myResult == SQLITE_OK );
400
+ Q_ASSERT ( myResult == SQLITE_OK );
410
401
}
411
402
412
403
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()
479
470
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
480
471
// XXX This will likely never happen since on open, sqlite creates the
481
472
// database if it does not exist.
482
- assert ( myResult == SQLITE_OK );
473
+ Q_ASSERT ( myResult == SQLITE_OK );
483
474
}
484
475
485
476
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()
548
539
QgsDebugMsg ( QString ( " Can't open database: %1" ).arg ( sqlite3_errmsg ( myDatabase ) ) );
549
540
// XXX This will likely never happen since on open, sqlite creates the
550
541
// database if it does not exist.
551
- assert ( myResult == SQLITE_OK );
542
+ Q_ASSERT ( myResult == SQLITE_OK );
552
543
}
553
544
554
545
QString mySql = " select * from tbl_srs order by srs_id desc limit 1" ;
@@ -733,50 +724,54 @@ void QgsCustomProjectionDialog::on_pbnSave_clicked()
733
724
// the autoinc related system tables.
734
725
if ( getRecordCount () == 0 )
735
726
{
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
741
734
}
742
735
else
743
736
{
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
748
743
}
749
744
}
750
745
else // user is updating an existing record
751
746
{
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 )
756
752
+ " ,is_geo=0" // <--shamelessly hard coded for now
757
- + " where srs_id=' " + mCurrentRecordId + " ' "
753
+ + " where srs_id=" + quotedValue ( mCurrentRecordId )
758
754
;
759
755
}
760
756
sqlite3 *myDatabase;
761
757
const char *myTail;
762
758
sqlite3_stmt *myPreparedStatement;
763
759
int myResult;
764
760
// check the db is available
765
- myResult = sqlite3_open ( QgsApplication::qgisUserDbFilePath ().toUtf8 (). data () , &myDatabase );
761
+ myResult = sqlite3_open ( QgsApplication::qgisUserDbFilePath ().toUtf8 (), &myDatabase );
766
762
if ( myResult != SQLITE_OK )
767
763
{
768
764
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 () ) );
769
765
// XXX This will likely never happen since on open, sqlite creates the
770
766
// database if it does not exist.
771
- assert ( myResult == SQLITE_OK );
767
+ Q_ASSERT ( myResult == SQLITE_OK );
772
768
}
773
- QgsDebugMsg ( QString ( " Update or insert sql \n %1" ).arg ( mySql ) );
774
769
myResult = sqlite3_prepare ( myDatabase, mySql.toUtf8 (), mySql.toUtf8 ().length (), &myPreparedStatement, &myTail );
775
770
sqlite3_step ( myPreparedStatement );
776
771
// XXX Need to free memory from the error msg if one is set
777
772
if ( myResult != SQLITE_OK )
778
773
{
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 ) ) );
780
775
}
781
776
// reinstate button if we were doing an insert
782
777
else if ( pbnNew->text () == tr ( " Abort" ) )
@@ -794,49 +789,56 @@ void QgsCustomProjectionDialog::on_pbnSave_clicked()
794
789
// This is a must, or else we can't select it from the vw_srs table.
795
790
// Actually, add it always and let the SQL PRIMARY KEY remove duplicates.
796
791
797
- sqlite3 *srsDatabase;
798
- const char *srsTail;
799
- sqlite3_stmt *srsPreparedStatement;
800
- int srsResult;
801
-
802
792
// 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 )
816
796
{
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 )
818
805
{
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 )
829
807
{
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 );
831
824
}
832
- sqlite3_finalize ( myPreparedStatement );
825
+
826
+ sqlite3_finalize ( srsPreparedStatement );
833
827
}
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 ) ) );
834
838
}
835
839
836
- // close the user and srs sqlite3 db
840
+ // close sqlite3 db
837
841
sqlite3_close ( myDatabase );
838
- sqlite3_finalize ( srsPreparedStatement );
839
- sqlite3_close ( srsDatabase );
840
842
841
843
pbnDelete->setEnabled ( true );
842
844
}
@@ -957,44 +959,9 @@ QString QgsCustomProjectionDialog::getEllipseFromParameters()
957
959
}
958
960
}
959
961
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 )
970
963
{
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 ( " '" );
999
966
}
1000
967
0 commit comments