1 #ifndef CONSTRAINTPANEL_H
2 #define CONSTRAINTPANEL_H
3 
4 #include "db/db.h"
5 #include "parser/ast/sqlitecreatetable.h"
6 #include "guiSQLiteStudio_global.h"
7 #include <QWidget>
8 #include <QPointer>
9 
10 class GUI_API_EXPORT ConstraintPanel : public QWidget
11 {
12         Q_OBJECT
13 
14     public:
15         explicit ConstraintPanel(QWidget *parent = 0);
16         virtual ~ConstraintPanel();
17 
18         void setConstraint(SqliteStatement* stmt);
19         void storeDefinition();
20         virtual void setDb(Db* value);
21 
22         /**
23          * @brief validate Validates panel for correct data filled in.
24          * @return true if the data is valid, or false otherwise.
25          * Apart from returning boolean result it also marks
26          * invalid fields with red color. See validateOnly() description
27          * for details on differences between those two methods.
28          */
29         virtual bool validate() = 0;
30 
31         /**
32          * @brief validateOnly Validates panel for correct data filled in.
33          * @return true if the data is valid, or false otherwise.
34          * The difference between validateOnly() and validate() is that validateOnly()
35          * will run all validations immediately (ie. SQL syntax checking
36          * in DEFAULT constraint, etc), while the validate() will wait for
37          * SqlEditor to do the validation in its scheduled time and return
38          * false until the validation isn't done yet.
39          * The validate() should be used when user actually edits the panel,
40          * while validateOnly() is to be used when using ConstraintPanel for validation
41          * of a Constraint object, but not displayed - in that case the validation
42          * result is needed immediately and that's where validateOnly() does its job.
43          * Not every constraint panel has to reimplement this. Most of the constraints
44          * don't work asynchronously and return proper result just from a validate() call.
45          * In that case the default implementation of validateOnly() will do the job.
46          */
47         virtual bool validateOnly();
48 
49         static ConstraintPanel* produce(SqliteCreateTable::Constraint* constr);
50         static ConstraintPanel* produce(SqliteCreateTable::Column::Constraint* constr);
51 
52     protected:
53         /**
54          * @brief constraintAvailable
55          * This method is called once the constraint object (the member variable)
56          * is available to the panel as well, as the database (the db member).
57          * The implementation should read values from constraint object and put them
58          * to panel's fields, but also initialise any database related data,
59          * like existing collations, etc.
60          */
61         virtual void constraintAvailable() = 0;
62 
63         /**
64          * @brief storeConfiguration
65          * The implementation should store all field valies into the constraint object.
66          */
67         virtual void storeConfiguration() = 0;
68 
69         Db* db = nullptr;
70         QPointer<SqliteStatement> constraint;
71 
72     public slots:
73 
74     signals:
75         void updateValidation();
76 
77 };
78 
79 #endif // CONSTRAINTPANEL_H
80