@@ -187,9 +187,57 @@ void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
187
187
}
188
188
}
189
189
190
+ void QgsNewSpatialiteLayerDialog::initializeSpatialMetadata (sqlite3 *sqlite_handle)
191
+ {
192
+ // attempting to perform self-initialization for a newly created DB
193
+ int ret;
194
+ char sql[1024 ];
195
+ char *errMsg = NULL ;
196
+ int count;
197
+ int i;
198
+ char **results;
199
+ int rows;
200
+ int columns;
201
+
202
+ if (sqlite_handle == NULL )
203
+ return ;
204
+ // checking if this DB is really empty
205
+ strcpy (sql, " SELECT Count(*) from sqlite_master" );
206
+ ret = sqlite3_get_table (sqlite_handle, sql, &results, &rows, &columns, NULL );
207
+ if (ret != SQLITE_OK)
208
+ return ;
209
+ if (rows < 1 )
210
+ ;
211
+ else
212
+ {
213
+ for (i = 1 ; i <= rows; i++)
214
+ count = atoi (results[(i * columns) + 0 ]);
215
+ }
216
+ sqlite3_free_table (results);
217
+
218
+ if (count > 0 )
219
+ return ;
220
+
221
+ // all right, it's empty: proceding to initialize
222
+ strcpy (sql, " SELECT InitSpatialMetadata()" );
223
+ ret = sqlite3_exec (sqlite_handle, sql, NULL , NULL , &errMsg);
224
+ if (ret != SQLITE_OK)
225
+ {
226
+ QString errCause = tr ( " Unable to initialize SpatialMetedata:\n " );
227
+ errCause += QString::fromUtf8 (errMsg);
228
+ QMessageBox::warning ( 0 , tr ( " SpatiaLite Database" ), errCause );
229
+ sqlite3_free (errMsg);
230
+ return ;
231
+ }
232
+ spatial_ref_sys_init (sqlite_handle, 0 );
233
+ }
234
+
190
235
bool QgsNewSpatialiteLayerDialog::createDb ()
191
236
{
192
237
QSettings settings;
238
+ int ret;
239
+ sqlite3 *sqlite_handle;
240
+ char *errMsg = NULL ;
193
241
194
242
if ( mDatabaseComboBox ->currentText ().isEmpty () )
195
243
return false ;
@@ -199,26 +247,41 @@ bool QgsNewSpatialiteLayerDialog::createDb()
199
247
{
200
248
QgsDebugMsg ( " creating a new db" );
201
249
202
- // copy the spatilite template to the user specified path
203
- QString spatialiteTemplate = QgsApplication::qgisSpatialiteDbTemplatePath ();
204
- QFile spatialiteTemplateDb ( spatialiteTemplate );
205
-
206
250
QFileInfo fullPath = QFileInfo ( mDatabaseComboBox ->currentText () );
207
251
QDir path = fullPath.dir ();
208
252
QgsDebugMsg ( QString ( " making this dir: %1" ).arg ( path.absolutePath () ) );
209
253
210
254
// Must be sure there is destination directory ~/.qgis
211
255
QDir ().mkpath ( path.absolutePath ( ) );
212
256
213
- QgsDebugMsg ( QString ( " Copying %1 to %2" ).arg ( spatialiteTemplate ).arg ( newDb.fileName () ) );
214
-
215
- // now copy the template db file into the chosen location
216
- if ( !spatialiteTemplateDb.copy ( newDb.fileName () ) )
257
+ // creating/opening the new database
258
+ QString dbPath = newDb.fileName ();
259
+ spatialite_init (0 );
260
+ ret = sqlite3_open_v2 (dbPath.toUtf8 ().constData (), &sqlite_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
261
+ if (ret)
262
+ {
263
+ // an error occurred
264
+ QString errCause = tr ( " Could not create a new database\n " );
265
+ errCause += QString::fromUtf8 (sqlite3_errmsg (sqlite_handle));
266
+ sqlite3_close (sqlite_handle);
267
+ QMessageBox::warning ( 0 , tr ( " SpatiaLite Database" ), errCause );
268
+ pbnFindSRID->setEnabled ( false );
269
+ return false ;
270
+ }
271
+ // activating Foreign Key constraints
272
+ ret = sqlite3_exec (sqlite_handle, " PRAGMA foreign_keys = 1" , NULL , 0 , &errMsg);
273
+ if (ret != SQLITE_OK)
217
274
{
218
- QMessageBox::warning ( 0 , tr ( " SpatiaLite Database" ), tr ( " Could not copy the template database to new location" ) );
275
+ QMessageBox::warning ( 0 , tr ( " SpatiaLite Database" ), tr ( " Unable to activate FOREIGN_KEY constraints" ) );
276
+ sqlite3_free (errMsg);
277
+ sqlite3_close (sqlite_handle);
219
278
pbnFindSRID->setEnabled ( false );
220
279
return false ;
221
280
}
281
+ initializeSpatialMetadata (sqlite_handle);
282
+
283
+ // all done: closing the DB connection
284
+ sqlite3_close (sqlite_handle);
222
285
}
223
286
224
287
QFileInfo fi ( newDb );
0 commit comments