summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/constraints/columnprimarykeypanel.cpp
blob: f8975d837771a04a99966fed1cbdbf7370a5d267 (plain) (blame)
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
#include "columnprimarykeypanel.h"
#include "ui_columnprimarykeypanel.h"
#include "parser/ast/sqlitecreatetable.h"
#include "parser/keywords.h"
#include "datatype.h"
#include "uiutils.h"

ColumnPrimaryKeyPanel::ColumnPrimaryKeyPanel(QWidget *parent) :
    ConstraintPanel(parent),
    ui(new Ui::ColumnPrimaryKeyPanel)
{
    ui->setupUi(this);
    init();
}

ColumnPrimaryKeyPanel::~ColumnPrimaryKeyPanel()
{
    delete ui;
}

void ColumnPrimaryKeyPanel::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
        case QEvent::LanguageChange:
            ui->retranslateUi(this);
            break;
        default:
            break;
    }
}

void ColumnPrimaryKeyPanel::init()
{
    QStringList sortOrders = {sqliteSortOrder(SqliteSortOrder::ASC), sqliteSortOrder(SqliteSortOrder::DESC)};
    ui->sortOrderCombo->addItems(sortOrders);

    ui->conflictCombo->addItems(getConflictAlgorithms());

    connect(ui->namedCheck, SIGNAL(toggled(bool)), this, SIGNAL(updateValidation()));
    connect(ui->namedEdit, SIGNAL(textChanged(QString)), this, SIGNAL(updateValidation()));
    connect(ui->sortOrderCheck, SIGNAL(toggled(bool)), this, SLOT(updateState()));
    connect(ui->namedCheck, SIGNAL(toggled(bool)), this, SLOT(updateState()));
    connect(ui->conflictCheck, SIGNAL(toggled(bool)), this, SLOT(updateState()));
    updateState();
}

void ColumnPrimaryKeyPanel::readConstraint()
{
    SqliteCreateTable::Column::Constraint* constr = dynamic_cast<SqliteCreateTable::Column::Constraint*>(constraint.data());
    if (constraint->dialect == Dialect::Sqlite3)
        ui->autoIncrCheck->setChecked(constr->autoincrKw);

    if (constr->sortOrder != SqliteSortOrder::null)
    {
        ui->sortOrderCheck->setChecked(true);
        ui->sortOrderCombo->setCurrentText(sqliteSortOrder(constr->sortOrder));
    }

    if (!constr->name.isNull())
    {
        ui->namedCheck->setEnabled(true);
        ui->namedEdit->setText(constr->name);
    }

    if (constr->onConflict != SqliteConflictAlgo::null)
    {
        ui->conflictCheck->setChecked(true);
        ui->conflictCombo->setCurrentText(sqliteConflictAlgo(constr->onConflict));
    }
}

void ColumnPrimaryKeyPanel::updateState()
{
    ui->sortOrderCombo->setEnabled(ui->sortOrderCheck->isChecked());
    ui->namedEdit->setEnabled(ui->namedCheck->isChecked());
    ui->conflictCombo->setEnabled(ui->conflictCheck->isChecked());
}


bool ColumnPrimaryKeyPanel::validate()
{
    bool nameOk = true;
    if (ui->namedCheck->isChecked() && ui->namedEdit->text().isEmpty())
        nameOk = false;

    setValidState(ui->namedEdit, nameOk, tr("Enter a name of the constraint."));

    return nameOk;
}

void ColumnPrimaryKeyPanel::constraintAvailable()
{
    if (constraint.isNull())
        return;

    ui->autoIncrCheck->setVisible(constraint->dialect == Dialect::Sqlite3);

    readConstraint();
}

void ColumnPrimaryKeyPanel::storeConfiguration()
{
    if (constraint.isNull())
        return;

    SqliteCreateTable::Column::Constraint* constr = dynamic_cast<SqliteCreateTable::Column::Constraint*>(constraint.data());
    constr->type = SqliteCreateTable::Column::Constraint::PRIMARY_KEY;

    if (constraint->dialect == Dialect::Sqlite3)
        constr->autoincrKw = ui->autoIncrCheck->isChecked();

    if (ui->sortOrderCheck->isChecked() && ui->sortOrderCombo->currentIndex() > -1)
        constr->sortOrder = sqliteSortOrder(ui->sortOrderCombo->currentText());

    if (ui->namedCheck->isChecked())
        constr->name = ui->namedEdit->text();

    if (ui->conflictCheck->isChecked() && ui->conflictCombo->currentIndex() > -1)
        constr->onConflict = sqliteConflictAlgo(ui->conflictCombo->currentText());
}