2222
2323#include " qgsspatialitesridsdialog.h"
2424#include " qgsapplication.h"
25+ #include " qgsproviderregistry.h"
2526#include " qgisapp.h" // <- for theme icons
2627#include < qgsvectorlayer.h>
2728#include < qgsmaplayerregistry.h>
3334#include < QLineEdit>
3435#include < QMessageBox>
3536#include < QFileDialog>
37+ #include < QLibrary>
3638
3739#include < spatialite.h>
3840
@@ -187,101 +189,43 @@ void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
187189 }
188190}
189191
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 = 0 ;
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 SpatialMetadata:\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-
235192bool QgsNewSpatialiteLayerDialog::createDb ()
236193{
237- QSettings settings;
238- int ret;
239- sqlite3 *sqlite_handle;
240- char *errMsg = NULL ;
241-
242- if ( mDatabaseComboBox ->currentText ().isEmpty () )
194+ QString dbPath = mDatabaseComboBox ->currentText ();
195+ if ( dbPath.isEmpty () )
243196 return false ;
244197
245- QFile newDb ( mDatabaseComboBox -> currentText () );
198+ QFile newDb ( dbPath );
246199 if ( !newDb.exists () )
247200 {
248- QgsDebugMsg ( " creating a new db" );
201+ QString errCause;
202+ bool res = false ;
249203
250- QFileInfo fullPath = QFileInfo ( mDatabaseComboBox ->currentText () );
251- QDir path = fullPath.dir ();
252- QgsDebugMsg ( QString ( " making this dir: %1" ).arg ( path.absolutePath () ) );
204+ QString spatialite_lib = QgsProviderRegistry::instance ()->library ( " spatialite" );
205+ QLibrary* myLib = new QLibrary ( spatialite_lib );
206+ bool loaded = myLib->load ();
207+ if ( loaded )
208+ {
209+ QgsDebugMsg ( " spatialite provider loaded" );
253210
254- // Must be sure there is destination directory ~/.qgis
255- QDir ().mkpath ( path.absolutePath ( ) );
211+ typedef bool ( *createDbProc )( const QString&, QString& );
212+ createDbProc createDbPtr = ( createDbProc ) cast_to_fptr ( myLib->resolve ( " createDb" ) );
213+ if ( createDbPtr )
214+ {
215+ res = createDbPtr ( dbPath, errCause );
216+ }
217+ else
218+ {
219+ errCause = " Resolving createDb(...) failed" ;
220+ }
221+ }
222+ delete myLib;
256223
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 )
224+ if ( !res )
262225 {
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 );
267226 QMessageBox::warning ( 0 , tr ( " SpatiaLite Database" ), errCause );
268227 pbnFindSRID->setEnabled ( false );
269- return false ;
270228 }
271- // activating Foreign Key constraints
272- ret = sqlite3_exec ( sqlite_handle, " PRAGMA foreign_keys = 1" , NULL , 0 , &errMsg );
273- if ( ret != SQLITE_OK )
274- {
275- QMessageBox::warning ( 0 , tr ( " SpatiaLite Database" ), tr ( " Unable to activate FOREIGN_KEY constraints" ) );
276- sqlite3_free ( errMsg );
277- sqlite3_close ( sqlite_handle );
278- pbnFindSRID->setEnabled ( false );
279- return false ;
280- }
281- initializeSpatialMetadata ( sqlite_handle );
282-
283- // all done: closing the DB connection
284- sqlite3_close ( sqlite_handle );
285229 }
286230
287231 QFileInfo fi ( newDb );
@@ -293,6 +237,7 @@ bool QgsNewSpatialiteLayerDialog::createDb()
293237
294238 QString key = " /SpatiaLite/connections/" + fi.fileName () + " /sqlitepath" ;
295239
240+ QSettings settings;
296241 if ( !settings.contains ( key ) )
297242 {
298243 settings.setValue ( " /SpatiaLite/connections/selected" , fi.fileName () + tr ( " @" ) + fi.canonicalFilePath () );
0 commit comments