0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 00:33:02 +03:00

Connect connection has been refactored

This commit is contained in:
Arin Alexander 2016-11-17 00:37:37 +03:00
parent 0055790bcd
commit 2fb47a5830
5 changed files with 67 additions and 37 deletions

View File

@ -11,7 +11,7 @@
<file>images/insert-text_5.png</file>
<file>images/shape5.png</file>
<file>images/imageItem1.png</file>
<file alias="ImageItem">images/imageItem2.png</file>
<file>images/imageItem2.png</file>
<file>images/settings.png</file>
<file>images/settings2.png</file>
<file alias="HorizontalLayout">images/hlayuot_3_24.png</file>

View File

@ -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)
{}

View File

@ -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:

View File

@ -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<QueryHolder*>(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;
}

View File

@ -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<ConnectionDesc*> m_connections;