aboutsummaryrefslogtreecommitdiffstats
path: root/Plugins/SqlEnterpriseFormatter/formatforeignkey.cpp
blob: 6b28a86b58bbd66bcda2f374da75187e3f55f912 (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
#include "formatforeignkey.h"

FormatForeignKey::FormatForeignKey(SqliteForeignKey* fk) :
    fk(fk)
{
}

void FormatForeignKey::formatInternal()
{
    withKeyword("REFERENCES").withId(fk->foreignTable);

    if (fk->indexedColumns.size() > 0)
        withParExprLeft().withStatementList(fk->indexedColumns).withParExprRight();

    if (fk->conditions.size() > 0)
    {
        markAndKeepIndent("constr_conditions").withStatementList(fk->conditions, QString(), ListSeparator::NEW_LINE).withDecrIndent();
    }

    if (fk->deferrable != SqliteDeferrable::null)
    {
        if (fk->deferrable == SqliteDeferrable::NOT_DEFERRABLE)
            withKeyword("NOT").withKeyword("DEFERRABLE");
        else if (fk->deferrable == SqliteDeferrable::DEFERRABLE)
            withKeyword("DEFERRABLE");

        if (fk->initially != SqliteInitially::null)
            withKeyword("INITIALLY").withKeyword(sqliteInitially(fk->initially));
    }
}


FormatForeignKeyCondition::FormatForeignKeyCondition(SqliteForeignKey::Condition* cond) :
    cond(cond)
{
}

void FormatForeignKeyCondition::formatInternal()
{
    switch (cond->action)
    {
        case SqliteForeignKey::Condition::UPDATE:
            withKeyword("ON").withKeyword("UPDATE");
            break;
        case SqliteForeignKey::Condition::INSERT:
            withKeyword("ON").withKeyword("INSERT");
            break;
        case SqliteForeignKey::Condition::DELETE:
            withKeyword("ON").withKeyword("DELETE");
            break;
        case SqliteForeignKey::Condition::MATCH:
            withKeyword("MATCH").withId(cond->name);
            return;
    }
    formatReaction();
}

void FormatForeignKeyCondition::formatReaction()
{
    switch (cond->reaction)
    {
        case SqliteForeignKey::Condition::SET_NULL:
            withKeyword("SET").withKeyword("NULL");
            break;
        case SqliteForeignKey::Condition::SET_DEFAULT:
            withKeyword("SET").withKeyword("DEFAULT");
            break;
        case SqliteForeignKey::Condition::CASCADE:
            withKeyword("CASCADE");
            break;
        case SqliteForeignKey::Condition::RESTRICT:
            withKeyword("RESTRICT");
            break;
        case SqliteForeignKey::Condition::NO_ACTION:
            withKeyword("NO").withKeyword("ACTION");
            break;
    }
}