diff --git a/limereport/items/items.qrc b/limereport/items/items.qrc index aec5e59..07cc670 100644 --- a/limereport/items/items.qrc +++ b/limereport/items/items.qrc @@ -11,7 +11,7 @@ images/insert-text_5.png images/shape5.png images/imageItem1.png - images/imageItem2.png + images/imageItem2.png images/settings.png images/settings2.png images/hlayuot_3_24.png diff --git a/limereport/lrdatadesignintf.cpp b/limereport/lrdatadesignintf.cpp index b9a15dd..3b24e66 100644 --- a/limereport/lrdatadesignintf.cpp +++ b/limereport/lrdatadesignintf.cpp @@ -369,6 +369,16 @@ void ConnectionDesc::setName(const QString &value) m_connectionName=value; } +bool ConnectionDesc::isEqual(const QSqlDatabase &db) +{ + return (db.databaseName() == m_databaseName) && + (db.driverName() == m_connectionDriver) && + (db.hostName() == m_connectionHost) && + (db.connectionName() == m_connectionName) && + (db.userName() == m_user) && + (db.password() == m_password); +} + QueryDesc::QueryDesc(QString queryName, QString queryText, QString connection) :m_queryName(queryName), m_queryText(queryText), m_connectionName(connection) {} diff --git a/limereport/lrdatadesignintf.h b/limereport/lrdatadesignintf.h index e53056f..abe26bb 100644 --- a/limereport/lrdatadesignintf.h +++ b/limereport/lrdatadesignintf.h @@ -129,6 +129,7 @@ public: QString password(){return m_password;} void setAutoconnect(bool value){m_autoconnect=value;} bool autoconnect(){return m_autoconnect;} + bool isEqual(const QSqlDatabase& db); signals: void nameChanged(const QString& oldName,const QString& newName); private: diff --git a/limereport/lrdatasourcemanager.cpp b/limereport/lrdatasourcemanager.cpp index b6c25d3..42ec024 100644 --- a/limereport/lrdatasourcemanager.cpp +++ b/limereport/lrdatasourcemanager.cpp @@ -634,6 +634,7 @@ bool DataSourceManager::checkConnectionDesc(ConnectionDesc *connection) QSqlDatabase::removeDatabase(connection->name()); return true; } + QSqlDatabase::removeDatabase(connection->name()); return false; } @@ -672,8 +673,38 @@ void DataSourceManager::putProxyDesc(ProxyDesc *proxyDesc) } else throw ReportError(tr("datasource with name \"%1\" already exists !").arg(proxyDesc->name())); } +bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connectionDesc){ + + bool connected = false; + db.setHostName(replaceVariables(connectionDesc.host())); + db.setUserName(replaceVariables(connectionDesc.userName())); + db.setPassword(replaceVariables(connectionDesc.password())); + + QString dbName = replaceVariables(connectionDesc.databaseName()); + if (connectionDesc.driver().compare("QSQLITE")==0){ + if (!defaultDatabasePath().isEmpty()){ + dbName = !QFileInfo(dbName).exists() ? + defaultDatabasePath()+QFileInfo(dbName).fileName() : + dbName; + } + if (QFileInfo(dbName).exists()){ + db.setDatabaseName(dbName); + } else { + setLastError(tr("Database \"%1\" not found").arg(dbName)); + return false; + } + } else { + db.setDatabaseName(dbName); + } + + connected=db.open(); + if (!connected) setLastError(db.lastError().text()); + return connected; +} + bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) { + bool connected = false; clearErrors(); QString lastError =""; @@ -682,11 +713,31 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) dataSourceHolder(datasourceName)->clearErrors(); } - if (QSqlDatabase::contains(connectionDesc->name())){ + if (!QSqlDatabase::contains(connectionDesc->name())){ + QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); + connected=initAndOpenDB(db, *connectionDesc); + if (!connected){ + setLastError(db.lastError().text()); + return false; + } + } else { + QSqlDatabase db = QSqlDatabase::database(connectionDesc->name()); + if (!connectionDesc->isEqual(db)){ + db.close(); + connected = initAndOpenDB(db, *connectionDesc); + } else { + connected = db.isOpen(); + } + } + + if (!connected) { + QSqlDatabase::removeDatabase(connectionDesc->name()); + return false; + } else { foreach(QString datasourceName, dataSourceNames()){ if (isQuery(datasourceName)){ QueryHolder* qh = dynamic_cast(dataSourceHolder(datasourceName)); - if (qh && qh->connectionName().compare(connectionDesc->name())==0){ + if (qh){ qh->invalidate(designTime()?IDataSource::DESIGN_MODE:IDataSource::RENDER_MODE); invalidateChildren(datasourceName); } @@ -700,41 +751,8 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) } } } - - QSqlDatabase::removeDatabase(connectionDesc->name()); + if (designTime()) emit datasourcesChanged(); } - - QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); - db.setHostName(replaceVariables(connectionDesc->host())); - db.setUserName(replaceVariables(connectionDesc->userName())); - db.setPassword(replaceVariables(connectionDesc->password())); - QString dbName = replaceVariables(connectionDesc->databaseName()); - if (connectionDesc->driver().compare("QSQLITE")==0){ - if (!defaultDatabasePath().isEmpty()){ - dbName = !QFileInfo(dbName).exists() ? - defaultDatabasePath()+QFileInfo(dbName).fileName() : - dbName; - } - if (QFileInfo(dbName).exists()){ - db.setDatabaseName(dbName); - connected=db.open(); - } else { - lastError = tr("Database \"%1\" not found").arg(dbName); - } - } else { - db.setDatabaseName(dbName); - connected=db.open(); - if (!connected) lastError=db.lastError().text(); - } - - if (designTime()) emit datasourcesChanged(); - - if (!connected) { - QSqlDatabase::removeDatabase(connectionDesc->name()); - setLastError(lastError); - return false; - } - return true; } diff --git a/limereport/lrdatasourcemanager.h b/limereport/lrdatasourcemanager.h index 1567ae5..fcf0f99 100644 --- a/limereport/lrdatasourcemanager.h +++ b/limereport/lrdatasourcemanager.h @@ -220,6 +220,7 @@ private slots: void slotQueryTextChanged(const QString& queryName, const QString& queryText); private: explicit DataSourceManager(QObject *parent = 0); + bool initAndOpenDB(QSqlDatabase &db, ConnectionDesc &connectionDesc); Q_DISABLE_COPY(DataSourceManager) private: QList m_connections;