Changeset View
Changeset View
Standalone View
Standalone View
src/KDbQuerySchema.cpp
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org> | 2 | Copyright (C) 2003-2017 Jarosław Staniek <staniek@kde.org> | ||
3 | 3 | | |||
4 | This library is free software; you can redistribute it and/or | 4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public | 5 | modify it under the terms of the GNU Library General Public | ||
6 | License as published by the Free Software Foundation; either | 6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | 7 | version 2 of the License, or (at your option) any later version. | ||
8 | 8 | | |||
9 | This library is distributed in the hope that it will be useful, | 9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
▲ Show 20 Lines • Show All 1513 Lines • ▼ Show 20 Line(s) | 1523 | if ( /*d->lastUsedDriverForAutoIncrementSQLFieldsList != driverWeakPointer | |||
1524 | ||*/ d->autoIncrementSQLFieldsList.isEmpty()) | 1524 | ||*/ d->autoIncrementSQLFieldsList.isEmpty()) | ||
1525 | { | 1525 | { | ||
1526 | d->autoIncrementSQLFieldsList = KDbQuerySchema::sqlColumnsList(*autoIncrementFields(), conn); | 1526 | d->autoIncrementSQLFieldsList = KDbQuerySchema::sqlColumnsList(*autoIncrementFields(), conn); | ||
1527 | //d->lastUsedDriverForAutoIncrementSQLFieldsList = driverWeakPointer; | 1527 | //d->lastUsedDriverForAutoIncrementSQLFieldsList = driverWeakPointer; | ||
1528 | } | 1528 | } | ||
1529 | return d->autoIncrementSQLFieldsList; | 1529 | return d->autoIncrementSQLFieldsList; | ||
1530 | } | 1530 | } | ||
1531 | 1531 | | |||
1532 | void KDbQuerySchema::setWhereExpression(const KDbExpression& expr) | 1532 | static void setResult(const KDbParseInfoInternal &parseInfo, | ||
1533 | QString *errorMessage, QString *errorDescription) | ||||
1533 | { | 1534 | { | ||
1534 | d->whereExpr = expr.clone(); | 1535 | if (errorMessage) { | ||
1536 | *errorMessage = parseInfo.errorMessage(); | ||||
1537 | } | ||||
1538 | if (errorDescription) { | ||||
1539 | *errorDescription = parseInfo.errorDescription(); | ||||
1540 | } | ||||
1541 | } | ||||
1542 | | ||||
1543 | bool KDbQuerySchema::setWhereExpression(const KDbExpression &expr, QString *errorMessage, | ||||
1544 | QString *errorDescription) | ||||
1545 | { | ||||
1546 | KDbExpression newWhereExpr = expr.clone(); | ||||
1547 | KDbParseInfoInternal parseInfo(this); | ||||
1548 | QString tempErrorMessage; | ||||
1549 | QString tempErrorDescription; | ||||
1550 | QString *errorMessagePointer = errorMessage ? errorMessage : &tempErrorMessage; | ||||
1551 | QString *errorDescriptionPointer | ||||
1552 | = errorDescription ? errorDescription : &tempErrorDescription; | ||||
1553 | if (!newWhereExpr.validate(&parseInfo)) { | ||||
1554 | setResult(parseInfo, errorMessagePointer, errorDescription); | ||||
1555 | kdbWarning() << "message=" << *errorMessagePointer | ||||
1556 | << "description=" << *errorDescriptionPointer; | ||||
1557 | kdbWarning() << newWhereExpr; | ||||
1558 | d->whereExpr = KDbExpression(); | ||||
1559 | return false; | ||||
1560 | } | ||||
1561 | errorMessagePointer->clear(); | ||||
1562 | errorDescriptionPointer->clear(); | ||||
1563 | Private::setWhereExpressionInternal(this, newWhereExpr); | ||||
1564 | return true; | ||||
1535 | } | 1565 | } | ||
1536 | 1566 | | |||
1537 | void KDbQuerySchema::addToWhereExpression(KDbField *field, | 1567 | bool KDbQuerySchema::addToWhereExpression(KDbField *field, const QVariant &value, | ||
1538 | const QVariant& value, KDbToken relation) | 1568 | KDbToken relation, QString *errorMessage, | ||
1569 | QString *errorDescription) | ||||
1539 | { | 1570 | { | ||
1540 | KDbToken token; | 1571 | KDbToken token; | ||
1541 | if (value.isNull()) { | 1572 | if (value.isNull()) { | ||
1542 | token = KDbToken::SQL_NULL; | 1573 | token = KDbToken::SQL_NULL; | ||
1543 | } else { | 1574 | } else { | ||
1544 | const KDbField::Type type = field->type(); // cache: evaluating type of expressions can be expensive | 1575 | const KDbField::Type type = field->type(); // cache: evaluating type of expressions can be expensive | ||
1545 | if (KDbField::isIntegerType(type)) { | 1576 | if (KDbField::isIntegerType(type)) { | ||
1546 | token = KDbToken::INTEGER_CONST; | 1577 | token = KDbToken::INTEGER_CONST; | ||
1547 | } else if (KDbField::isFPNumericType(type)) { | 1578 | } else if (KDbField::isFPNumericType(type)) { | ||
1548 | token = KDbToken::REAL_CONST; | 1579 | token = KDbToken::REAL_CONST; | ||
1549 | } else { | 1580 | } else { | ||
1550 | token = KDbToken::CHARACTER_STRING_LITERAL; | 1581 | token = KDbToken::CHARACTER_STRING_LITERAL; | ||
1551 | } | 1582 | } | ||
1552 | //! @todo date, time | 1583 | //! @todo date, time | ||
1553 | } | 1584 | } | ||
1554 | 1585 | | |||
1555 | KDbBinaryExpression newExpr( | 1586 | KDbBinaryExpression newExpr( | ||
1556 | KDbConstExpression(token, value), | 1587 | KDbConstExpression(token, value), | ||
1557 | relation, | 1588 | relation, | ||
1558 | KDbVariableExpression((field->table() ? (field->table()->name() + QLatin1Char('.')) : QString()) + field->name()) | 1589 | KDbVariableExpression((field->table() ? (field->table()->name() + QLatin1Char('.')) : QString()) + field->name()) | ||
1559 | ); | 1590 | ); | ||
1560 | if (d->whereExpr.isNull()) { | 1591 | const KDbExpression origWhereExpr = d->whereExpr; | ||
1561 | d->whereExpr = newExpr; | 1592 | if (!d->whereExpr.isNull()) { | ||
1562 | } | 1593 | newExpr = KDbBinaryExpression( | ||
1563 | else { | | |||
1564 | d->whereExpr = KDbBinaryExpression( | | |||
1565 | d->whereExpr, | 1594 | d->whereExpr, | ||
1566 | KDbToken::AND, | 1595 | KDbToken::AND, | ||
1567 | newExpr | 1596 | newExpr | ||
1568 | ); | 1597 | ); | ||
1569 | } | 1598 | } | ||
1599 | const bool result = setWhereExpression(newExpr, errorMessage, errorDescription); | ||||
1600 | if (!result) { // revert, setWhereExpression() cleared it | ||||
1601 | d->whereExpr = origWhereExpr; | ||||
1602 | } | ||||
1603 | return result; | ||||
1570 | } | 1604 | } | ||
1571 | 1605 | | |||
1572 | /* | 1606 | /* | ||
1573 | void KDbQuerySchema::addToWhereExpression(KDbField *field, const QVariant& value) | 1607 | void KDbQuerySchema::addToWhereExpression(KDbField *field, const QVariant& value) | ||
1574 | switch (value.type()) { | 1608 | switch (value.type()) { | ||
1575 | case Int: case UInt: case Bool: case LongLong: case ULongLong: | 1609 | case Int: case UInt: case Bool: case LongLong: case ULongLong: | ||
1576 | token = INTEGER_CONST; | 1610 | token = INTEGER_CONST; | ||
1577 | break; | 1611 | break; | ||
Show All 33 Lines | |||||
1611 | { | 1645 | { | ||
1612 | if (whereExpression().isNull()) | 1646 | if (whereExpression().isNull()) | ||
1613 | return QList<KDbQuerySchemaParameter>(); | 1647 | return QList<KDbQuerySchemaParameter>(); | ||
1614 | QList<KDbQuerySchemaParameter> params; | 1648 | QList<KDbQuerySchemaParameter> params; | ||
1615 | whereExpression().getQueryParameters(¶ms); | 1649 | whereExpression().getQueryParameters(¶ms); | ||
1616 | return params; | 1650 | return params; | ||
1617 | } | 1651 | } | ||
1618 | 1652 | | |||
1619 | static void setResult(const KDbParseInfoInternal &parseInfo, | | |||
1620 | QString *errorMessage, QString *errorDescription) | | |||
1621 | { | | |||
1622 | if (errorMessage) { | | |||
1623 | *errorMessage = parseInfo.errorMessage(); | | |||
1624 | } | | |||
1625 | if (errorDescription) { | | |||
1626 | *errorDescription = parseInfo.errorDescription(); | | |||
1627 | } | | |||
1628 | } | | |||
1629 | | ||||
1630 | bool KDbQuerySchema::validate(QString *errorMessage, QString *errorDescription) | 1653 | bool KDbQuerySchema::validate(QString *errorMessage, QString *errorDescription) | ||
1631 | { | 1654 | { | ||
1632 | KDbParseInfoInternal parseInfo(this); | 1655 | KDbParseInfoInternal parseInfo(this); | ||
1633 | foreach(KDbField* f, *fields()) { | 1656 | foreach(KDbField* f, *fields()) { | ||
1634 | if (f->isExpression()) { | 1657 | if (f->isExpression()) { | ||
1635 | if (!f->expression().validate(&parseInfo)) { | 1658 | if (!f->expression().validate(&parseInfo)) { | ||
1636 | setResult(parseInfo, errorMessage, errorDescription); | 1659 | setResult(parseInfo, errorMessage, errorDescription); | ||
1637 | return false; | 1660 | return false; | ||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |