1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include "constraintcheckpanel.h"
#include "ui_constraintcheckpanel.h"
#include "parser/ast/sqlitecreatetable.h"
#include "parser/parser.h"
#include "parser/keywords.h"
#include "uiutils.h"
#include <QDebug>
ConstraintCheckPanel::ConstraintCheckPanel(QWidget *parent) :
ConstraintPanel(parent),
ui(new Ui::ConstraintCheckPanel)
{
ui->setupUi(this);
init();
}
ConstraintCheckPanel::~ConstraintCheckPanel()
{
delete ui;
}
void ConstraintCheckPanel::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
bool ConstraintCheckPanel::validate()
{
bool nameOk = true;
if (ui->namedCheck->isChecked() && ui->namedEdit->text().isEmpty())
nameOk = false;
bool exprOk = !ui->exprEdit->toPlainText().trimmed().isEmpty() &&
!ui->exprEdit->haveErrors();
bool exprCheckedOk = exprOk && ui->exprEdit->isSyntaxChecked();
setValidState(ui->exprEdit, exprOk, tr("Enter a valid condition."));
setValidState(ui->namedEdit, nameOk, tr("Enter a name of the constraint."));
return exprCheckedOk && nameOk;
}
void ConstraintCheckPanel::constraintAvailable()
{
if (constraint.isNull())
return;
ui->onConflictCheck->setVisible(false);
ui->onConflictCombo->setVisible(false);
readConstraint();
updateVirtualSql();
}
void ConstraintCheckPanel::storeConfiguration()
{
if (constraint.isNull())
return;
storeType();
SqliteExprPtr expr = parseExpression(ui->exprEdit->toPlainText());
SqliteExpr* newExpr = new SqliteExpr(*expr.data());
newExpr->setParent(constraint.data());
storeExpr(newExpr);
QString name = QString();
if (ui->namedCheck->isChecked())
name = ui->namedEdit->text();
storeName(name);
}
void ConstraintCheckPanel::init()
{
setFocusProxy(ui->exprEdit);
ui->exprEdit->setShowLineNumbers(false);
connect(ui->namedCheck, SIGNAL(toggled(bool)), this, SIGNAL(updateValidation()));
connect(ui->namedEdit, SIGNAL(textChanged(QString)), this, SIGNAL(updateValidation()));
connect(ui->exprEdit, SIGNAL(textChanged()), this, SIGNAL(updateValidation()));
connect(ui->exprEdit, SIGNAL(errorsChecked(bool)), this, SIGNAL(updateValidation()));
connect(ui->namedCheck, SIGNAL(toggled(bool)), this, SLOT(updateState()));
connect(ui->onConflictCheck, SIGNAL(toggled(bool)), this, SLOT(updateState()));
ui->onConflictCombo->addItems(getConflictAlgorithms());
updateState();
}
void ConstraintCheckPanel::readConstraint()
{
SqliteExpr* expr = readExpr();
if (expr)
ui->exprEdit->setPlainText(expr->detokenize());
QString name = readName();
if (!name.isNull())
{
ui->namedCheck->setChecked(true);
ui->namedEdit->setText(name);
}
}
void ConstraintCheckPanel::updateVirtualSql()
{
ui->exprEdit->setDb(db);
SqliteCreateTable* createTable = getCreateTable();
createTable->rebuildTokens();
TokenList tokens = createTable->tokens;
int idx = tokens.lastIndexOf(Token::PAR_RIGHT);
if (idx == -1)
{
qWarning() << "CREATE TABLE tokens are invalid while call to ConstraintCheckPanel::updateVirtualSql().";
return;
}
TokenList newTokens;
newTokens << TokenPtr::create(Token::OPERATOR, ",")
<< TokenPtr::create(Token::SPACE, " ")
<< TokenPtr::create(Token::KEYWORD, "CHECK")
<< TokenPtr::create(Token::SPACE, " ")
<< TokenPtr::create(Token::PAR_LEFT, "(")
<< TokenPtr::create(Token::OTHER, "%1")
<< TokenPtr::create(Token::PAR_RIGHT, ")");
tokens.insert(idx, newTokens);
QString sql = tokens.detokenize();
ui->exprEdit->setVirtualSqlExpression(sql);
}
SqliteExprPtr ConstraintCheckPanel::parseExpression(const QString& sql)
{
Parser parser;
SqliteExpr *expr = parser.parseExpr(sql);
return SqliteExprPtr(expr);
}
void ConstraintCheckPanel::updateState()
{
ui->namedEdit->setEnabled(ui->namedCheck->isChecked());
ui->onConflictCombo->setEnabled(ui->onConflictCheck->isChecked());
}
bool ConstraintCheckPanel::validateOnly()
{
ui->exprEdit->checkSyntaxNow();
return validate();
}
|