QgsSpatiaLiteConnection::Error QgsSpatiaLiteConnection::fetchTables( bool loadGeometrylessTables )
{
  mErrorMsg = QString();

  QFileInfo fi( mPath );
  if ( !fi.exists() )
  {
    return NotExists;
  }

  sqlite3* handle = openSpatiaLiteDb( fi.canonicalFilePath() );
  if ( handle == NULL )
  {
    return FailedToOpen;
  }

  checkHasMetadataTables( handle );
  if ( !mErrorMsg.isNull() )
  {
    // unexpected error; invalid SpatiaLite DB
    return FailedToCheckMetadata;
  }

  if ( !getTableInfo( handle, loadGeometrylessTables ) )
  {
    return FailedToGetTables;
  }
  closeSpatiaLiteDb( handle );

  return NoError;
}
示例#2
0
void QgsSpatiaLiteSourceSelect::on_btnNew_clicked()
{
// Retrieve last used project dir from persistent settings
  sqlite3 *handle;
  QSettings settings;
  QString lastUsedDir = settings.value( "/UI/lastSpatiaLiteDir", "." ).toString();

  QString myFile = QFileDialog::getOpenFileName( this,
                   tr( "Choose a SpatiaLite/SQLite DB to open" ),
                   lastUsedDir, QObject::tr( "SQLite DB (*.sqlite *.db);;All files (*)" ) );

  if ( myFile.isEmpty() )
    return;

  QFileInfo myFI( myFile );
  QString myPath = myFI.path();
  QString myName = myFI.fileName();

  handle = openSpatiaLiteDb( myFI.canonicalFilePath() );
  if ( !handle )
    return;

  // OK, this one is a valid SpatiaLite DB
  closeSpatiaLiteDb( handle );

  // Persist last used SpatiaLite dir
  settings.setValue( "/UI/lastSpatiaLiteDir", myPath );
  // inserting this SQLite DB path
  QString baseKey = "/SpatiaLite/connections/";
  settings.setValue( baseKey + "selected", myName );
  baseKey += myName;
  settings.setValue( baseKey + "/sqlitepath", myFI.canonicalFilePath() );

  populateConnectionList();
}
示例#3
0
void QgsSpatiaLiteSourceSelect::on_btnConnect_clicked()
{
  sqlite3 *handle;

  QSettings settings;
  QString subKey = cmbConnections->currentText();
  int idx = subKey.indexOf( "@" );
  if ( idx > 0 )
    subKey.truncate( idx );

  QFileInfo fi( settings.value( QString( "/SpatiaLite/connections/%1/sqlitepath" ).arg( subKey ) ).toString() );

  if ( !fi.exists() )
    // db doesn't exists
    return;

  // trying to connect to SpatiaLite DB
  handle = openSpatiaLiteDb( fi.canonicalFilePath() );
  if ( handle == NULL )
  {
    // unexpected error; invalid SpatiaLite DB
    return;
  }

  QModelIndex rootItemIndex = mTableModel.indexFromItem( mTableModel.invisibleRootItem() );
  mTableModel.removeRows( 0, mTableModel.rowCount( rootItemIndex ), rootItemIndex );

  // populate the table list
  // get the list of suitable tables and columns and populate the UI
  geomCol details;

  if ( !getTableInfo( handle ) )
  {
    QgsDebugMsg( QString( "Unable to get list of spatially enabled tables from the database\n%1" ).arg( sqlite3_errmsg( handle ) ) );
  }
  closeSpatiaLiteDb( handle );

  if ( cmbConnections->count() > 0 )
    mAddButton->setEnabled( true );

  mTablesTreeView->sortByColumn( 0, Qt::AscendingOrder );

  //expand all the toplevel items
  int numTopLevelItems = mTableModel.invisibleRootItem()->rowCount();
  for ( int i = 0; i < numTopLevelItems; ++i )
  {
    mTablesTreeView->expand( mProxyModel.mapFromSource( mTableModel.indexFromItem( mTableModel.invisibleRootItem()->child( i ) ) ) );
  }
  mTablesTreeView->resizeColumnToContents( 0 );
  mTablesTreeView->resizeColumnToContents( 1 );
}
示例#4
0
QgsSpatiaLiteConnection::Error QgsSpatiaLiteConnection::fetchTables( bool loadGeometrylessTables )
{
  mErrorMsg = QString();

  QFileInfo fi( mPath );
  if ( !fi.exists() )
    return NotExists;

  sqlite3 *handle = openSpatiaLiteDb( fi.canonicalFilePath() );
  if ( !handle )
    return FailedToOpen;

  int ret = checkHasMetadataTables( handle );
  if ( !mErrorMsg.isNull() || ret == LayoutUnknown )
  {
    // unexpected error; invalid SpatiaLite DB
    return FailedToCheckMetadata;
  }

  bool recentVersion = false;
#ifdef SPATIALITE_VERSION_GE_4_0_0
  // only if libspatialite version is >= 4.0.0
  recentVersion = true;
#endif

  if ( ret == LayoutCurrent && !recentVersion )
  {
    // obsolete library version
    mErrorMsg = tr( "obsolete libspatialite: connecting to this DB requires using v.4.0 (or any subsequent)" );
    return FailedToCheckMetadata;
  }

#ifdef SPATIALITE_VERSION_GE_4_0_0
  // only if libspatialite version is >= 4.0.0
  // using v.4.0 Abstract Interface
  if ( !getTableInfoAbstractInterface( handle, loadGeometrylessTables ) )
#else
  // obsolete library: still using the traditional approach
  if ( !getTableInfo( handle, loadGeometrylessTables ) )
#endif
  {
    return FailedToGetTables;
  }
  closeSpatiaLiteDb( handle );

  return NoError;
}
示例#5
0
bool QgsSpatiaLiteConnection::updateStatistics()
{
#ifdef SPATIALITE_VERSION_GE_4_0_0
  QFileInfo fi( mPath );
  if ( !fi.exists() )
    return false;

  sqlite3 *handle = openSpatiaLiteDb( fi.canonicalFilePath() );
  if ( !handle )
    return false;

  bool ret = update_layer_statistics( handle, nullptr, nullptr );

  closeSpatiaLiteDb( handle );

  return ret;
#else
  return false;
#endif
}