Exemple #1
0
int DatabaseManager::refreshServersModel()
{
    QString serversQuery = "SELECT * FROM Servers;";
    SqlQueryModel* serverModel = new SqlQueryModel();
    serverModel->setQuery(serversQuery);

    qDebug() << "Server Model Refeshed. No of Rows: "
             << serverModel->rowCount();

    context->setContextProperty("serversModel", serverModel);

    return serverModel->rowCount();
}
Exemple #2
0
void SqlEditor::actionRun_as_Script_triggered()
{
	if ((!creator) || !(creator->checkForPending())) { return; }
	SqlQueryModel * model = 0;
	bool rebuildTree = false;
	bool updateTable = false;
	m_scriptCancelled = false;
	toSQLParse::editorTokenizer tokens(ui.sqlTextEdit);
	int cpos, cline;
	ui.sqlTextEdit->getCursorPosition(&cline, &cpos);

	QProgressDialog * dialog =
		new QProgressDialog(tr("Executing all statements"),
			tr("Cancel"), 0, ui.sqlTextEdit->lines(), this);
	connect(dialog, SIGNAL(canceled()), this, SLOT(scriptCancelled()));

	int line;
	int pos;
	bool ignore = true;

	QSqlQuery query(QSqlDatabase::database(SESSION_NAME));
	QString sql;
	bool isError = false;

	emit sqlScriptStart();
	emit showSqlScriptResult("-- " + tr("Script started"));
	do {
		line = tokens.line();
		pos = tokens.offset();
		dialog->setValue(line);
		qApp->processEvents();
		if (m_scriptCancelled)
			break;
		toSQLParse::parseStatement(tokens);

		if (ignore && (tokens.line() > cline ||
				  (tokens.line() == cline &&
				  tokens.offset() >= cpos)))
		{
			ignore = false;
			cline = line;
			cpos = pos;
		}

		if (tokens.line() < ui.sqlTextEdit->lines() && !ignore)
		{
			sql = prepareExec(tokens, line, pos);
			emit showSqlScriptResult(sql);
			SqlQueryModel * mdl = new SqlQueryModel(creator);
			mdl->setQuery(sql, QSqlDatabase::database(SESSION_NAME));
            appendHistory(sql);
			if (mdl->lastError().isValid())
			{
				emit showSqlScriptResult(
					"-- " + tr("Error: %1.").arg(mdl->lastError().text()));
				int com = QMessageBox::question(this, tr("Run as Script"),
						tr("This script contains the following error:\n")
						+ mdl->lastError().text()
						+ tr("\nAt line: %1").arg(line),
						QMessageBox::Ignore, QMessageBox::Abort);
				if (com == QMessageBox::Abort)
				{
					scriptCancelled();
					isError = true;
					break;
				}
			}
			else
			{
				if (Utils::updateObjectTree(sql)) { rebuildTree = true; }
				if (Utils::updateTables(sql)) { updateTable = true; }
				emit showSqlScriptResult("-- " + tr("No error"));
				if (mdl->rowCount() > 0) { model = mdl; }
				else delete mdl;
			}
			emit showSqlScriptResult("--");
		}
	}
	while (tokens.line() < ui.sqlTextEdit->lines());

	delete dialog;
	ui.sqlTextEdit->setSelection(cline, cpos, tokens.line(), tokens.offset());
	if (!isError)
		emit showSqlScriptResult("-- " + tr("Script finished"));
	if (rebuildTree) { emit buildTree(); }
	if (updateTable) { emit refreshTable(); }
	if (model)
	{
		creator->setTableModel(model);
	}
	creator->buildPragmasTree();
}