예제 #1
0
void trm::Database::openDb()
{
    fs::path newDb(dbFile);
    if (fs::exists(newDb))
        newDbFile = false;
    else
        newDbFile = true;
    //open a Database Connection and create a Database Object
    dbStatus = sqlite3_open(dbFile.c_str(), &db);
    if (dbStatus != SQLITE_OK)
        std::cout << "Cant open Database: " << sqlite3_errmsg(db) << std::endl;
    if (newDbFile)
        trm::Database::createDbTable();
}
예제 #2
0
SQLiteDBPtr SQLite::open(const String& name) {
    UpgradeLock upgrade_lock(mDBMutex);

    // First get the thread local storage for this database
    DBMap::iterator it = mDBs.find(name);
    if (it == mDBs.end()) {
        // the thread specific store hasn't even been allocated
        UpgradedLock upgraded_lock(upgrade_lock);
        // verify another thread hasn't added it, then add it
        it = mDBs.find(name);
        if (it == mDBs.end()) {
            std::tr1::shared_ptr<ThreadDBPtr> newDb(new ThreadDBPtr());
            mDBs[name]=newDb;
            it = mDBs.find(name);
        }
    }
    assert(it != mDBs.end());
    std::tr1::shared_ptr<ThreadDBPtr> thread_db_ptr_ptr = it->second;
    assert(thread_db_ptr_ptr);

    // Now get the thread specific weak database connection
    WeakSQLiteDBPtr* weak_db_ptr_ptr = thread_db_ptr_ptr->get();
    if (weak_db_ptr_ptr == NULL) {
        // we don't have a weak pointer for this thread
        UpgradedLock upgraded_lock(upgrade_lock);
        weak_db_ptr_ptr = thread_db_ptr_ptr->get();
        if (weak_db_ptr_ptr == NULL) { // verify and create
            weak_db_ptr_ptr = new WeakSQLiteDBPtr();
            thread_db_ptr_ptr->reset( weak_db_ptr_ptr );
        }
    }
    assert(weak_db_ptr_ptr != NULL);

    SQLiteDBPtr db;
    db = weak_db_ptr_ptr->lock();
    if (!db) {
        // the weak pointer for this thread is NULL
        UpgradedLock upgraded_lock(upgrade_lock);
        db = weak_db_ptr_ptr->lock();
        if (!db) {
            db = SQLiteDBPtr(new SQLiteDB(name));
            *weak_db_ptr_ptr = db;
        }
    }
    assert(db);

    return db;
}
예제 #3
0
bool QgsOfflineEditing::createSpatialiteDB( const QString& offlineDbPath )
{
  int ret;
  sqlite3 *sqlite_handle;
  char *errMsg = NULL;
  QFile newDb( offlineDbPath );
  if ( newDb.exists() )
  {
    QFile::remove( offlineDbPath );
  }

  // see also QgsNewSpatialiteLayerDialog::createDb()

  QFileInfo fullPath = QFileInfo( offlineDbPath );
  QDir path = fullPath.dir();

  // Must be sure there is destination directory ~/.qgis
  QDir().mkpath( path.absolutePath( ) );

  // creating/opening the new database
  QString dbPath = newDb.fileName();
  spatialite_init( 0 );
  ret = sqlite3_open_v2( dbPath.toUtf8().constData(), &sqlite_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
  if ( ret )
  {
    // an error occurred
    QString errCause = tr( "Could not create a new database\n" );
    errCause += QString::fromUtf8( sqlite3_errmsg( sqlite_handle ) );
    sqlite3_close( sqlite_handle );
    showWarning( errCause );
    return false;
  }
  // activating Foreign Key constraints
  ret = sqlite3_exec( sqlite_handle, "PRAGMA foreign_keys = 1", NULL, 0, &errMsg );
  if ( ret != SQLITE_OK )
  {
    showWarning( tr( "Unable to activate FOREIGN_KEY constraints" ) );
    sqlite3_free( errMsg );
    sqlite3_close( sqlite_handle );
    return false;
  }
  initializeSpatialMetadata( sqlite_handle );

  // all done: closing the DB connection
  sqlite3_close( sqlite_handle );

  return true;
}
bool QgsNewSpatialiteLayerDialog::createDb()
{
    QString dbPath = mDatabaseComboBox->currentText();
    if ( dbPath.isEmpty() )
        return false;

    QFile newDb( dbPath );
    if ( !newDb.exists() )
    {
        QString errCause;
        bool res = false;

        QString spatialite_lib = QgsProviderRegistry::instance()->library( "spatialite" );
        QLibrary* myLib = new QLibrary( spatialite_lib );
        bool loaded = myLib->load();
        if ( loaded )
        {
            QgsDebugMsg( "spatialite provider loaded" );

            typedef bool ( *createDbProc )( const QString&, QString& );
            createDbProc createDbPtr = ( createDbProc ) cast_to_fptr( myLib->resolve( "createDb" ) );
            if ( createDbPtr )
            {
                res = createDbPtr( dbPath, errCause );
            }
            else
            {
                errCause = "Resolving createDb(...) failed";
            }
        }
        delete myLib;

        if ( !res )
        {
            QMessageBox::warning( 0, tr( "SpatiaLite Database" ), errCause );
            pbnFindSRID->setEnabled( false );
        }
    }

    QFileInfo fi( newDb );
    if ( !fi.exists() )
    {
        pbnFindSRID->setEnabled( false );
        return false;
    }

    QString key = "/SpatiaLite/connections/" + fi.fileName() + "/sqlitepath";

    QSettings settings;
    if ( !settings.contains( key ) )
    {
        settings.setValue( "/SpatiaLite/connections/selected", fi.fileName() + tr( "@" ) + fi.canonicalFilePath() );
        settings.setValue( key, fi.canonicalFilePath() );

        QMessageBox::information( 0, tr( "SpatiaLite Database" ), tr( "Registered new database!" ) );
    }

    pbnFindSRID->setEnabled( true );

    return true;
}
예제 #5
0
bool QgsNewSpatialiteLayerDialog::createDb()
{
  QString dbPath = mDatabaseComboBox->currentText();
  if ( dbPath.isEmpty() )
    return false;

  QFile newDb( dbPath );
  if ( newDb.exists() )
  {
    QMessageBox msgBox;
    msgBox.setIcon( QMessageBox::Question );
    msgBox.setWindowTitle( tr( "The File Already Exists." ) );
    msgBox.setText( tr( "Do you want to overwrite the existing file with a new database or add a new layer to it?" ) );
    QPushButton *overwriteButton = msgBox.addButton( tr( "Overwrite" ), QMessageBox::ActionRole );
    QPushButton *addNewLayerButton = msgBox.addButton( tr( "Add new layer" ), QMessageBox::ActionRole );
    msgBox.setStandardButtons( QMessageBox::Cancel );
    msgBox.setDefaultButton( addNewLayerButton );
    int ret = msgBox.exec();
    if ( ret == QMessageBox::Cancel )
    {
      return false;
    }

    if ( msgBox.clickedButton() == overwriteButton )
    {
      newDb.remove();
    }
  }

  if ( !newDb.exists() )
  {
    QString errCause;
    bool res = false;

    QString spatialite_lib = QgsProviderRegistry::instance()->library( QStringLiteral( "spatialite" ) );
    QLibrary *myLib = new QLibrary( spatialite_lib );
    bool loaded = myLib->load();
    if ( loaded )
    {
      QgsDebugMsg( "SpatiaLite provider loaded" );

      typedef bool ( *createDbProc )( const QString &, QString & );
      createDbProc createDbPtr = ( createDbProc ) cast_to_fptr( myLib->resolve( "createDb" ) );
      if ( createDbPtr )
      {
        res = createDbPtr( dbPath, errCause );
      }
      else
      {
        errCause = QStringLiteral( "Resolving createDb(...) failed" );
      }
    }
    delete myLib;

    if ( !res )
    {
      QMessageBox::warning( nullptr, tr( "SpatiaLite Database" ), errCause );
      pbnFindSRID->setEnabled( false );
    }
  }

  QFileInfo fi( newDb );
  if ( !fi.exists() )
  {
    pbnFindSRID->setEnabled( false );
    return false;
  }

  QString key = "/SpatiaLite/connections/" + fi.fileName() + "/sqlitepath";

  QgsSettings settings;
  if ( !settings.contains( key ) )
  {
    settings.setValue( QStringLiteral( "SpatiaLite/connections/selected" ), fi.fileName() + tr( "@" ) + fi.canonicalFilePath() );
    settings.setValue( key, fi.canonicalFilePath() );

    QMessageBox::information( nullptr, tr( "SpatiaLite Database" ), tr( "Registered new database!" ) );
  }

  pbnFindSRID->setEnabled( true );

  return true;
}
bool QgsNewSpatialiteLayerDialog::createDb()
{
  QSettings settings;
  int ret;
  sqlite3 *sqlite_handle;
  char *errMsg = NULL;

  if ( mDatabaseComboBox->currentText().isEmpty() )
    return false;

  QFile newDb( mDatabaseComboBox->currentText() );
  if ( !newDb.exists() )
  {
    QgsDebugMsg( "creating a new db" );

    QFileInfo fullPath = QFileInfo( mDatabaseComboBox->currentText() );
    QDir path = fullPath.dir();
    QgsDebugMsg( QString( "making this dir: %1" ).arg( path.absolutePath() ) );

    // Must be sure there is destination directory ~/.qgis
    QDir().mkpath( path.absolutePath( ) );

    // creating/opening the new database
    QString dbPath = newDb.fileName();
    spatialite_init( 0 );
    ret = sqlite3_open_v2( dbPath.toUtf8().constData(), &sqlite_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
    if ( ret )
    {
      // an error occurred
      QString errCause = tr( "Could not create a new database\n" );
      errCause += QString::fromUtf8( sqlite3_errmsg( sqlite_handle ) );
      sqlite3_close( sqlite_handle );
      QMessageBox::warning( 0, tr( "SpatiaLite Database" ), errCause );
      pbnFindSRID->setEnabled( false );
      return false;
    }
    // activating Foreign Key constraints
    ret = sqlite3_exec( sqlite_handle, "PRAGMA foreign_keys = 1", NULL, 0, &errMsg );
    if ( ret != SQLITE_OK )
    {
      QMessageBox::warning( 0, tr( "SpatiaLite Database" ), tr( "Unable to activate FOREIGN_KEY constraints" ) );
      sqlite3_free( errMsg );
      sqlite3_close( sqlite_handle );
      pbnFindSRID->setEnabled( false );
      return false;
    }
    initializeSpatialMetadata( sqlite_handle );

    // all done: closing the DB connection
    sqlite3_close( sqlite_handle );
  }

  QFileInfo fi( newDb );
  if ( !fi.exists() )
  {
    pbnFindSRID->setEnabled( false );
    return false;
  }

  QString key = "/SpatiaLite/connections/" + fi.fileName() + "/sqlitepath";

  if ( !settings.contains( key ) )
  {
    settings.setValue( "/SpatiaLite/connections/selected", fi.fileName() + tr( "@" ) + fi.canonicalFilePath() );
    settings.setValue( key, fi.canonicalFilePath() );

    QMessageBox::information( 0, tr( "SpatiaLite Database" ), tr( "Registered new database!" ) );
  }

  pbnFindSRID->setEnabled( true );

  return true;
}