@@ -130,21 +130,13 @@ bool QgsStyleV2::removeSymbol( QString name )
130
130
131
131
// TODO
132
132
// Simplify this work here, its STUPID to run two DB queries for the sake of remove()
133
- QByteArray array = name.toUtf8 ();
134
- char *query;
135
133
if ( mCurrentDB == NULL )
136
134
{
137
135
QgsDebugMsg ( " Sorry! Cannot open database to tag." );
138
136
return false ;
139
137
}
140
138
141
- int symbolid = 0 ;
142
- query = sqlite3_mprintf ( " SELECT id FROM symbol WHERE name='%q';" , array.constData () );
143
- sqlite3_stmt *ppStmt;
144
- int err = sqlite3_prepare_v2 ( mCurrentDB , query, -1 , &ppStmt, NULL );
145
- if ( err == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
146
- symbolid = sqlite3_column_int ( ppStmt, 0 );
147
- sqlite3_finalize ( ppStmt );
139
+ int symbolid = symbolId ( name );
148
140
if ( !symbolid )
149
141
{
150
142
QgsDebugMsg ( " No such symbol for deleting in database: " + name + " . Cheers." );
@@ -188,6 +180,28 @@ bool QgsStyleV2::addColorRamp( QString name, QgsVectorColorRampV2* colorRamp )
188
180
if ( mColorRamps .contains ( name ) )
189
181
delete mColorRamps .value ( name );
190
182
183
+ // insert it into the database
184
+ QDomDocument doc ( " dummy" );
185
+ QDomElement rampEl = QgsSymbolLayerV2Utils::saveColorRamp ( name, colorRamp, doc );
186
+ if ( rampEl.isNull () )
187
+ {
188
+ QgsDebugMsg ( " Couldnot convert color ramp to valid XML!" );
189
+ return false ;
190
+ }
191
+
192
+ QByteArray *xmlArray = new QByteArray ();
193
+ QTextStream stream ( xmlArray );
194
+ rampEl.save ( stream, 4 );
195
+ QByteArray nameArray = name.toUtf8 ();
196
+ char *query = sqlite3_mprintf ( " INSERT INTO colorramp VALUES (NULL, '%q', '%q');" ,
197
+ nameArray.constData (), xmlArray->constData () );
198
+
199
+ if ( !runEmptyQuery ( query ) )
200
+ {
201
+ QgsDebugMsg ( " Couldnot insert colorramp into the Database!" );
202
+ return false ;
203
+ }
204
+
191
205
mColorRamps .insert ( name, colorRamp );
192
206
return true ;
193
207
}
@@ -197,6 +211,13 @@ bool QgsStyleV2::removeColorRamp( QString name )
197
211
if ( !mColorRamps .contains ( name ) )
198
212
return false ;
199
213
214
+ QByteArray array = name.toUtf8 ();
215
+ char *query = sqlite3_mprintf ( " DELETE FROM colorramp WHERE name='%q';" , array.constData () );
216
+ if ( !runEmptyQuery ( query ) )
217
+ {
218
+ QgsDebugMsg ( " Couldn't remove color ramp from the database." );
219
+ return false ;
220
+ }
200
221
// remove from map and delete
201
222
delete mColorRamps .take ( name );
202
223
return true ;
@@ -285,7 +306,7 @@ bool QgsStyleV2::load( QString filename )
285
306
QDomElement rampElement = doc.documentElement ();
286
307
QgsVectorColorRampV2* ramp = QgsSymbolLayerV2Utils::loadColorRamp ( rampElement );
287
308
if ( ramp != NULL )
288
- addColorRamp ( ramp_name, ramp );
309
+ mColorRamps . insert ( ramp_name, ramp );
289
310
}
290
311
291
312
mFileName = filename;
@@ -628,32 +649,27 @@ QStringList QgsStyleV2::findSymbols( QString qword )
628
649
629
650
bool QgsStyleV2::tagSymbol ( QString symbol, QStringList tags )
630
651
{
631
- QByteArray array = symbol.toUtf8 ();
632
- char *query;
633
652
if ( mCurrentDB == NULL )
634
653
{
635
654
QgsDebugMsg ( " Sorry! Cannot open database to tag." );
636
655
return false ;
637
656
}
638
657
639
- int symbolid = 0 ;
640
- query = sqlite3_mprintf ( " SELECT id FROM symbol WHERE name='%q';" , array.constData () );
641
- sqlite3_stmt *ppStmt;
642
- int err = sqlite3_prepare_v2 ( mCurrentDB , query, -1 , &ppStmt, NULL );
643
- if ( err == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
644
- symbolid = sqlite3_column_int ( ppStmt, 0 );
645
- sqlite3_finalize ( ppStmt );
658
+ int symbolid = symbolId ( symbol );
646
659
if ( !symbolid )
647
660
{
648
661
QgsDebugMsg ( " No such symbol for tagging in database: " + symbol );
649
662
return false ;
650
663
}
651
664
665
+ char *query;
666
+ int nErr;
667
+ sqlite3_stmt *ppStmt;
668
+
652
669
foreach ( const QString &tag, tags )
653
670
{
654
671
int tagid;
655
672
char *zErr = 0 ;
656
- int nErr;
657
673
QByteArray tagArray = tag.toUtf8 ();
658
674
// sql: gets the id of the tag if present or insert the tag and get the id of the tag
659
675
query = sqlite3_mprintf ( " SELECT id FROM tag WHERE name='%q';" , tagArray.constData () );
@@ -664,14 +680,7 @@ bool QgsStyleV2::tagSymbol( QString symbol, QStringList tags )
664
680
}
665
681
else
666
682
{
667
- query = sqlite3_mprintf ( " INSERT INTO tag VALUES (NULL,'%q');" , tagArray.constData () );
668
- nErr = sqlite3_exec ( mCurrentDB , query, NULL , NULL , &zErr );
669
- if ( nErr )
670
- {
671
- QgsDebugMsg ( zErr );
672
- return false ;
673
- }
674
- tagid = (int )sqlite3_last_insert_rowid ( mCurrentDB );
683
+ tagid = addTag ( tag );
675
684
}
676
685
sqlite3_finalize ( ppStmt );
677
686
// Now map the tag to the symbol
@@ -731,27 +740,21 @@ bool QgsStyleV2::detagSymbol( QString symbol, QStringList tags )
731
740
732
741
QStringList QgsStyleV2::tagsOfSymbol ( QString symbol )
733
742
{
734
- QStringList tagList;
735
- QByteArray array = symbol.toUtf8 ();
736
- char *query;
737
- sqlite3_stmt *ppStmt;
738
- int symbolid;
739
743
if ( mCurrentDB == NULL )
740
744
{
741
745
QgsDebugMsg ( " Sorry! Cannot open database for getting the tags." );
742
746
return QStringList ();
743
747
}
744
- // get the symbol id
745
- query = sqlite3_mprintf ( " SELECT id FROM symbol WHERE name='%q';" , array.constData () );
746
- int nErr = sqlite3_prepare_v2 ( mCurrentDB , query, -1 , &ppStmt, NULL );
747
- if ( nErr == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
748
- {
749
- symbolid = sqlite3_column_int ( ppStmt, 0 );
750
- }
751
- sqlite3_finalize ( ppStmt );
748
+ QStringList tagList;
749
+ char *query;
750
+ sqlite3_stmt *ppStmt;
751
+ int symbolid = symbolId ( symbol );
752
+ if ( !symbolid )
753
+ return QStringList ();
754
+
752
755
// get the ids of tags for the symbol
753
756
query = sqlite3_mprintf ( " SELECT tag_id FROM tagmap WHERE symbol_id=%d;" , symbolid );
754
- nErr = sqlite3_prepare_v2 ( mCurrentDB , query, -1 , &ppStmt, NULL );
757
+ int nErr = sqlite3_prepare_v2 ( mCurrentDB , query, -1 , &ppStmt, NULL );
755
758
while ( nErr == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
756
759
{
757
760
int tagid = sqlite3_column_int ( ppStmt, 0 );
@@ -770,3 +773,19 @@ QStringList QgsStyleV2::tagsOfSymbol( QString symbol )
770
773
771
774
return tagList;
772
775
}
776
+
777
+ int QgsStyleV2::symbolId ( QString name )
778
+ {
779
+ int symbolid = 0 ;
780
+ char *query;
781
+ sqlite3_stmt *ppStmt;
782
+ QByteArray array = name.toUtf8 ();
783
+ query = sqlite3_mprintf ( " SELECT id FROM symbol WHERE name='%q';" , array.constData () );
784
+ int nErr = sqlite3_prepare_v2 ( mCurrentDB , query, -1 , &ppStmt, NULL );
785
+ if ( nErr == SQLITE_OK && sqlite3_step ( ppStmt ) == SQLITE_ROW )
786
+ {
787
+ symbolid = sqlite3_column_int ( ppStmt, 0 );
788
+ }
789
+ sqlite3_finalize ( ppStmt );
790
+ return symbolid;
791
+ }
0 commit comments