예제 #1
0
void toResultLong::query(const QString &sql, const toQueryParams &param)
{
    setSqlAndParams(sql, param);
    slotStop();
    Query = NULL;
    LastItem = NULL;
    RowNumber = 0;
    First = true;

    clear();

    setSorting( -1);

    if (NumberColumn)
    {
        addColumn(QString::fromLatin1("#"));
        setColumnAlignment(0, Qt::AlignRight);
    }

    if (Filter)
        Filter->startingQuery();

    try
    {
        Query = new toEventQuery(this
                                 , connection()
                                 , sql
                                 , param
                                 //, Statistics
                                 , toEventQuery::READ_ALL
                                );
        connect(Query, SIGNAL(dataAvailable(toEventQuery*)), this, SLOT(slotAddItem()));
        connect(Query, SIGNAL(done(toEventQuery*)), this, SLOT(slotQueryDone()));
        if (ReadAll)
        {
            MaxNumber = -1;
            //// Query->slotReadAll(); // indicate that all records should be fetched
        }
        else
            MaxNumber = toConfigurationNewSingle::Instance().option(ToConfiguration::Database::InitialFetchInt).toInt();
        Query->start();
    }
    catch (const toConnection::exception &str)
    {
        First = false;
        emit firstResult(toResult::sql(), str, true);
        emit done();
        //// if (Mode != toQuery::Long)
        Utils::toStatusMessage(str);
    }
    catch (const QString &str)
    {
        First = false;
        emit firstResult(toResult::sql(), str, true);
        emit done();
        //// if (Mode != toQuery::Long)
        Utils::toStatusMessage(str);
    }
    updateContents();
}
예제 #2
0
void toResultModel::slotQueryError(toEventQuery*, const toConnection::exception &err)
{
    if (First)
    {
        emit firstResult(err, true);
        First = !First;
        Utils::toStatusMessage(err);
        cleanup();
    }
}
예제 #3
0
void toResultLong::slotAddItem(void)
{
    if (!Utils::toCheckModal(this))
        return;

    try
    {
        while (Query->hasMore())
        {
            bool em = false; // should we emit signal about first row fetched?
            QString buffer;
            if (First)
            {
                QString tmp = sql().simplified().mid(0, 10).toLower();
                if (tmp.startsWith(QString::fromLatin1("update")) ||
                        tmp.startsWith(QString::fromLatin1("delete")) ||
                        tmp.startsWith(QString::fromLatin1("insert")))
                    buffer = tr("%1 rows processed").arg(Query->rowsProcessed());
                else if (tmp.startsWith(QString::fromLatin1("select")))
                    buffer = tr("Query executed");
                else
                    buffer = tr("Statement executed");
                em = true;
            }
            if (!HasHeaders)
            {
                Description = Query->describe();
                bool hidden = false;

                for (toQColumnDescriptionList::iterator i = Description.begin(); i != Description.end(); i++)
                {
                    QString name = (*i).Name;
                    if (ReadableColumns)
                        Utils::toReadableColumn(name);
                    if (name.length() > 0 && name[0].toLatin1() != ' ')
                    {
                        if (hidden)
                            throw tr("Can only hide last column in query");
                        if (name[0].toLatin1() == '-')
                        {
                            addColumn(Utils::toTranslateMayby(sqlName(), name.right(name.length() - 1)));
                            setColumnAlignment(columns() - 1, Qt::AlignRight);
                        }
                        else
                        {
                            addColumn(Utils::toTranslateMayby(sqlName(), name));
                            if ((*i).AlignRight)
                                setColumnAlignment(columns() - 1, Qt::AlignRight);
                        }
                    }
                    else
                        hidden = true;
                }
                HasHeaders = true;

                setResizeMode(toTreeWidget::AllColumns);

                if (sortColumn() < 0)
                {
                    if (NumberColumn)
                        setSorting(0);
                    else
                        setSorting(Description.size());
                }
            }

            if (Query->hasMore())
            {
                int disp = 0;
                unsigned int cols = Description.size();
                if (NumberColumn)
                {
                    disp = 1;
                }
                do
                {
                    toTreeWidgetItem *last = LastItem;
                    LastItem = createItem(LastItem, QString::null);
                    if (NumberColumn)
                        LastItem->setText(0, QString::number(RowNumber + 1));
                    else
                        LastItem->setText(cols, QString::number(RowNumber + 1));
                    toResultViewItem *ri = dynamic_cast<toResultViewItem *>(LastItem);
                    toResultViewCheck *ci = dynamic_cast<toResultViewCheck *>(LastItem);
                    for (unsigned int j = 0; (j < cols || j == 0) && !Query->eof(); j++)
                    {
                        if (ri)
                            ri->setText(j + disp, (QString)Query->readValue());
                        else if (ci)
                            ci->setText(j + disp, (QString)Query->readValue());
                        else
                            LastItem->setText(j + disp, (QString)Query->readValue());
                    }
                    if (Filter && !Filter->check(LastItem))
                    {
                        delete LastItem;
                        LastItem = last;
                    }
                    else
                        RowNumber++;
                }
                while (Query->hasMore() && (MaxNumber < 0 || MaxNumber > RowNumber));
            }
            if (em)
            {
                First = false;
                emit firstResult(sql(), toConnection::exception(buffer), false);
            }
            if (!(MaxNumber < 0 || MaxNumber > RowNumber))
                Query->stop();
        }
    }
    catch (const toConnection::exception &str)
    {
        if (First)
        {
            First = false;
            emit firstResult(sql(), str, true);
            //// if (Mode != toQuery::Long)
            Utils::toStatusMessage(str);
        }
        else
            Utils::toStatusMessage(str);
        cleanup();
    }
    catch (const QString &str)
    {
        if (First)
        {
            First = false;
            emit firstResult(sql(), str, true);
            //// if (Mode != toQuery::Long)
            Utils::toStatusMessage(str);
        }
        else
            Utils::toStatusMessage(str);
        cleanup();
    }
}
예제 #4
0
void toResultModel::slotReadData()
{
    if (!Query)
    {
        cleanup();
        return;
    }

    try
    {
        // must check for errors
        Query->eof();

        int cols = Headers.size();
        if (cols < 1)
            return;

        // don't actually modify any data until we can call
        // beginInsertRows(). but to do that, we have to know how many
        // records we're going to add.
        toQueryAbstr::RowList tmp;
        int     current = Rows.size();

        while (Query->hasMore() &&
                (MaxRows < 0 || MaxRows > current))
        {
            toQueryAbstr::Row row;

            // The number column (rowKey). should never change
            toRowDesc rowDesc;
            rowDesc.key = CurrRowKey++;
            rowDesc.status = EXISTED;
            row.append(toQValue(rowDesc));
            //row.append(toQValue(CurrRowKey++));

            for (int j = 1; (j < cols || j == 0) && Query->hasMore(); j++)
                row.append(Query->readValue());

            tmp.append(row);
            current++;
        }

        // if we read some data, then go ahead and insert them now.
        if (tmp.size() > 0)
        {
            beginInsertRows(QModelIndex(), Rows.size(), current - 1);
            Rows << tmp;
            endInsertRows();
        }

        // not really first, but just be sure to emit before done()
        // must be emitted even if there's no data....
        if (First)
        {
            if (tmp.size() > 0 || !Query || Query->eof())
            {
                First = !First;

                // need to reset view(s) since we have to poll for data
                beginResetModel();
                endResetModel();
                if (Query && Query->rowsProcessed() > 0)
                {
                    emit firstResult(QString::number(Query->rowsProcessed()) +
                                     (Query->rowsProcessed() == 1 ?
                                      tr(" row processed") :
                                      tr(" rows processed")),
                                     false);
                }
                else
                    emit firstResult(tr("Statement executed"), false);
            }
        }

        if (!Query)
            return;
        if (!Query->hasMore() && Query->eof())
        {
            cleanup();
            return;
        }
    }
    catch (const toConnection::exception &str)
    {
        if (First)
        {
            First = !First;
            emit firstResult(str, true);
        }
        Utils::toStatusMessage(str);
        cleanup();
        return;
    }
    catch (const QString &str)
    {
        if (First)
        {
            First = !First;
            emit firstResult(str, true);
        }
        Utils::toStatusMessage(str);
        cleanup();
        return;
    }
}
예제 #5
0
void toResultTableView::querySub(QSharedPointer<toConnectionSubLoan> &con, const QString &sql, toQueryParams const& param)
{
    setSqlAndParams(sql, param);

    TLOG(7, toDecorator, __HERE__) << "Query from toResultTableView::querySub :" << sql << std::endl;
    try
    {
        if (Model && running())
            Model->stop();
        freeModel();

        readAllAct->setEnabled(true);
        Ready = false;
        Finished = false;
        Working->setText(tr("Please wait..."));
        Working->hide();

        // sets visible true but won't show if parent is hidden
        QTimer t(this);
        t.singleShot(300, Working, SLOT(forceShow()));

        toEventQuery *query = new toEventQuery(this
                                               , con
                                               , sql
                                               , param
                                               , toEventQuery::READ_FIRST
                                               //, Statistics
                                              );

        toResultModel *model = allocModel(query);
        setModel(model);

        connect(Model, SIGNAL(done()), this, SLOT(slotHandleDone()));
        connect(Model, SIGNAL(modelReset()), this, SLOT(slotHandleReset()));
        connect(Model,
                SIGNAL(firstResult(const toConnection::exception &, bool)),
                this,
                SLOT(slotHandleFirst(const toConnection::exception &, bool)));
        setSortingEnabled(true);
        query->start();
    }
    catch (const toConnection::exception &str)
    {
        emit firstResult(toResult::sql(), str, true);
        emit done();
        slotHandleDone();
        Utils::toStatusMessage(str);
    }
    catch (const QString &str)
    {
        emit firstResult(toResult::sql(), str, true);
        emit done();
        slotHandleDone();
        Utils::toStatusMessage(str);
    }

    verticalHeader()->setVisible(false);
    verticalHeader()->setDefaultSectionSize(QFontMetrics(QFont()).height() + 4);

    horizontalHeader()->setHighlightSections(false);

    // when a new model is created the column sizes are lost
    ColumnsResized = false;
}