forked from andre/qt-sql-example
140 lines
3.4 KiB
C++
140 lines
3.4 KiB
C++
#include "db_controller.h"
|
|
|
|
DbController::DbController(QObject* parent) :
|
|
QObject(parent)
|
|
{
|
|
|
|
}
|
|
|
|
DbController::~DbController()
|
|
{
|
|
if (db.isOpen())
|
|
db.close();
|
|
}
|
|
|
|
void DbController::connectToServerRequested(QString engine, QString driver, QString server, int port, QString database,
|
|
QString login, QString password, bool is_sql_authentication)
|
|
{
|
|
db = QSqlDatabase();
|
|
db.removeDatabase("example-connection"); // remove old connection if exists
|
|
|
|
if (engine == "mysql")
|
|
{
|
|
db = QSqlDatabase::addDatabase("QMYSQL", "example-connection");
|
|
}
|
|
else if (engine == "mssql")
|
|
{
|
|
db = QSqlDatabase::addDatabase("QODBC", "example-connection");
|
|
}
|
|
else
|
|
{
|
|
emit serverErrorWithConnection("Unknown database engine");
|
|
return;
|
|
}
|
|
|
|
bool connection_succesfull;
|
|
|
|
if (engine == "mysql")
|
|
{
|
|
connection_succesfull = connectToServerMySQL(server, port, database, login, password);
|
|
}
|
|
else if (engine == "mssql")
|
|
{
|
|
connection_succesfull =
|
|
(is_sql_authentication ? connectToServerMSSQL(driver, server, port, database, login, password) :
|
|
connectToServerMSSQL(driver, server, port, database));
|
|
}
|
|
else
|
|
{
|
|
emit serverErrorWithConnection("Unknown database engine");
|
|
return;
|
|
}
|
|
|
|
if (connection_succesfull)
|
|
emit serverConnected();
|
|
else
|
|
emit serverErrorWithConnection(getLastError().driverText());
|
|
}
|
|
|
|
void DbController::disconnectFromServerRequested()
|
|
{
|
|
disconnectFromServer();
|
|
|
|
emit serverDisconnected();
|
|
}
|
|
|
|
bool DbController::checkIfTableExists(QString table)
|
|
{
|
|
return db.tables().contains(table);
|
|
}
|
|
|
|
bool DbController::checkIfConnected()
|
|
{
|
|
return db.isOpen();
|
|
}
|
|
|
|
void DbController::selectTableRequested(QString table)
|
|
{
|
|
QSqlQueryModel* model = selectTable(table);
|
|
|
|
emit tableSelected(model);
|
|
}
|
|
|
|
void DbController::getTablesNamesRequested()
|
|
{
|
|
emit gotTablesNames(db.tables());
|
|
}
|
|
|
|
bool DbController::connectToServerMSSQL(QString driver, QString server, int port, QString database,
|
|
QString login, QString password)
|
|
{
|
|
db.setDatabaseName(connection_string_sqlauth.arg(driver).arg(server).arg(port).arg(database)
|
|
.arg(login).arg(password));
|
|
|
|
return db.open();
|
|
}
|
|
|
|
bool DbController::connectToServerMSSQL(QString driver, QString server, int port, QString database)
|
|
{
|
|
db.setDatabaseName(connection_string_winauth.arg(driver).arg(server).arg(port).arg(database));
|
|
|
|
return db.open();
|
|
}
|
|
|
|
bool DbController::connectToServerMySQL(QString server, int port, QString database,
|
|
QString login, QString password)
|
|
{
|
|
db.setHostName(server);
|
|
db.setPort(port);
|
|
db.setDatabaseName(database);
|
|
db.setUserName(login);
|
|
db.setPassword(password);
|
|
|
|
return db.open();
|
|
}
|
|
|
|
void DbController::disconnectFromServer()
|
|
{
|
|
db.close();
|
|
}
|
|
|
|
QSqlQueryModel* DbController::selectTable(QString name)
|
|
{
|
|
QSqlQueryModel* model = new QSqlQueryModel;
|
|
|
|
model->setQuery("SELECT * FROM " + name, db);
|
|
|
|
return model;
|
|
}
|
|
|
|
QSqlError DbController::getLastError()
|
|
{
|
|
return db.lastError();
|
|
}
|
|
|
|
const QString DbController::connection_string_sqlauth =
|
|
QString("DRIVER={%1};SERVER=%2;PORT=%3;DATABASE=%4;UID=%5;PWD=%6");
|
|
|
|
const QString DbController::connection_string_winauth =
|
|
QString("DRIVER={%1};SERVER=%2;PORT=%3;DATABASE=%4");
|