Changeset View
Changeset View
Standalone View
Standalone View
src/KDbQuerySchema.h
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 701 Lines • ▼ Show 20 Line(s) | 206 | public: | |||
712 | static KDbEscapedString sqlColumnsList(const KDbQueryColumnInfo::List& infolist, KDbConnection *conn = 0, | 712 | static KDbEscapedString sqlColumnsList(const KDbQueryColumnInfo::List& infolist, KDbConnection *conn = 0, | ||
713 | KDb::IdentifierEscapingType escapingType = KDb::DriverEscaping); | 713 | KDb::IdentifierEscapingType escapingType = KDb::DriverEscaping); | ||
714 | 714 | | |||
715 | /*! @return cached list of autoincrement fields created using sqlColumnsList() | 715 | /*! @return cached list of autoincrement fields created using sqlColumnsList() | ||
716 | on a list returned by autoIncrementFields(). The field names are escaped using | 716 | on a list returned by autoIncrementFields(). The field names are escaped using | ||
717 | driver escaping. */ | 717 | driver escaping. */ | ||
718 | KDbEscapedString autoIncrementSQLFieldsList(KDbConnection *conn) const; | 718 | KDbEscapedString autoIncrementSQLFieldsList(KDbConnection *conn) const; | ||
719 | 719 | | |||
720 | /*! Sets a WHERE expression @a exp. | 720 | /** | ||
721 | Previously set WHERE expression will be removed. | 721 | * @brief Sets a WHERE expression @a exp. | ||
722 | You can pass null expression (KDbExpression()) to remove existing WHERE expresssion. */ | 722 | * | ||
723 | void setWhereExpression(const KDbExpression& expr); | 723 | * Previously set WHERE expression will be removed. A null expression | ||
724 | * (KDbExpression()) can be passed to remove existing WHERE expresssion. | ||||
725 | * @return @c false if @a expr is not a valid WHERE expression. validate() is called | ||||
726 | * to check this. On failure the WHERE expression for this query is cleared. In this | ||||
727 | * case a string pointed by @a errorMessage (if provided) is set to a general error | ||||
728 | * message and a string pointed by @a errorDescription (if provided) is set to a | ||||
729 | * detailed description of the error. | ||||
730 | */ | ||||
731 | bool setWhereExpression(const KDbExpression &expr, QString *errorMessage = nullptr, | ||||
732 | QString *errorDescription = nullptr); | ||||
724 | 733 | | |||
725 | /*! @return WHERE expression or 0 if this query has no WHERE expression */ | 734 | /*! @return WHERE expression or 0 if this query has no WHERE expression */ | ||
726 | KDbExpression whereExpression() const; | 735 | KDbExpression whereExpression() const; | ||
727 | 736 | | |||
728 | /*! Adds a part to WHERE expression. | 737 | /** | ||
729 | Simplifies creating of WHERE expression, if used instead | 738 | * @brief Appends a part to WHERE expression. | ||
730 | of setWhereExpression(KDbExpression *expr). */ | 739 | * | ||
731 | void addToWhereExpression(KDbField *field, const QVariant& value, KDbToken relation = '='); | 740 | * Simplifies creating of WHERE expression if used instead of setWhereExpression(). | ||
741 | * @return @c false if the newly constructed WHERE expression is not valid. | ||||
742 | * validate() is called to check this. On failure the WHERE expression for this query | ||||
743 | * is left unchanged. In this case a string pointed by @a errorMessage (if provided) | ||||
744 | * is set to a general error message and a string pointed by @a errorDescription | ||||
745 | * (if provided) is set to a detailed description of the error. | ||||
746 | */ | ||||
747 | bool addToWhereExpression(KDbField *field, const QVariant &value, | ||||
748 | KDbToken relation = '=', QString *errorMessage = nullptr, | ||||
749 | QString *errorDescription = nullptr); | ||||
732 | 750 | | |||
733 | /*! Sets a list of columns for ORDER BY section of the query. | 751 | /*! Sets a list of columns for ORDER BY section of the query. | ||
734 | Each name on the list must be a field or alias present within the query | 752 | Each name on the list must be a field or alias present within the query | ||
735 | and must not be covered by aliases. If one or more names cannot be found | 753 | and must not be covered by aliases. If one or more names cannot be found | ||
736 | within the query, the method will have no effect. | 754 | within the query, the method will have no effect. | ||
737 | Any previous ORDER BY settings will be removed. | 755 | Any previous ORDER BY settings will be removed. | ||
738 | 756 | | |||
739 | Note that this information is cleared whenever you call methods that | 757 | Note that this information is cleared whenever you call methods that | ||
Show All 15 Lines | |||||
755 | (a tree of expression items). */ | 773 | (a tree of expression items). */ | ||
756 | QList<KDbQuerySchemaParameter> parameters() const; | 774 | QList<KDbQuerySchemaParameter> parameters() const; | ||
757 | 775 | | |||
758 | //! @return true if this query is valid | 776 | //! @return true if this query is valid | ||
759 | /*! Detailed validation is performed in the same way as parsing of query statements | 777 | /*! Detailed validation is performed in the same way as parsing of query statements | ||
760 | * by the KDbParser. | 778 | * by the KDbParser. | ||
761 | * Example :Let the query be "SELECT <fields> FROM <tables> WHERE <whereExpression>". | 779 | * Example :Let the query be "SELECT <fields> FROM <tables> WHERE <whereExpression>". | ||
762 | * First each field from <fields> (@see fields()) is validated using | 780 | * First each field from <fields> (@see fields()) is validated using | ||
763 | * KDbField::expression().validate(). Then the <whereExpression> (@see whereExpression()) | 781 | * KDbField::expression().validate(). Then the <whereExpression> (@see | ||
782 | * whereExpression()) | ||||
764 | * is validated using KDbExpression::validate(). | 783 | * is validated using KDbExpression::validate(). | ||
765 | * | 784 | * | ||
766 | * On error, a string pointed by @a errorMessage is set to a general error message | 785 | * On error a string pointed by @a errorMessage (if provided) is set to a general | ||
767 | * and a string pointed by @a errorDescription is set to a detailed description | 786 | * error message and a string pointed by @a errorDescription (if provided) is set to a | ||
768 | * of the error. */ | 787 | * detailed description of the error. | ||
788 | */ | ||||
769 | //! @todo add tests | 789 | //! @todo add tests | ||
770 | bool validate(QString *errorMessage, QString *errorDescription); | 790 | bool validate(QString *errorMessage = nullptr, QString *errorDescription = nullptr); | ||
771 | 791 | | |||
772 | class Private; | 792 | class Private; | ||
773 | 793 | | |||
774 | protected: | 794 | protected: | ||
775 | //! @internal associates @a conn with this query so it's possible to find tables | 795 | //! @internal associates @a conn with this query so it's possible to find tables | ||
776 | explicit KDbQuerySchema(KDbConnection *conn); | 796 | explicit KDbQuerySchema(KDbConnection *conn); | ||
777 | 797 | | |||
778 | void init(); | 798 | void init(); | ||
779 | 799 | | |||
780 | void computeFieldsExpanded() const; | 800 | void computeFieldsExpanded() const; | ||
781 | 801 | | |||
782 | //! Used by fieldsExpanded(FieldsExpandedOptions) | 802 | //! Used by fieldsExpanded(FieldsExpandedOptions) | ||
783 | //! and visibleFieldsExpanded(FieldsExpandedOptions options). | 803 | //! and visibleFieldsExpanded(FieldsExpandedOptions options). | ||
784 | KDbQueryColumnInfo::Vector fieldsExpandedInternal(FieldsExpandedOptions options, | 804 | KDbQueryColumnInfo::Vector fieldsExpandedInternal(FieldsExpandedOptions options, | ||
785 | bool onlyVisible) const; | 805 | bool onlyVisible) const; | ||
786 | 806 | | |||
807 | /** Internal method used by a query parser. | ||||
808 | */ | ||||
809 | void setWhereExpressionInternal(const KDbExpression &expr); | ||||
810 | | ||||
787 | Private * const d; | 811 | Private * const d; | ||
788 | }; | 812 | }; | ||
789 | 813 | | |||
790 | //! @short KDbQueryAsterisk class encapsulates information about single asterisk in query definition | 814 | //! @short KDbQueryAsterisk class encapsulates information about single asterisk in query definition | ||
791 | /*! There are two types of query asterisks: | 815 | /*! There are two types of query asterisks: | ||
792 | 816 | | |||
793 | 1. "Single-table" asterisk, that references all fields of given table used | 817 | 1. "Single-table" asterisk, that references all fields of given table used | ||
794 | in the query. | 818 | in the query. | ||
▲ Show 20 Lines • Show All 80 Lines • Show Last 20 Lines |