aboutsummaryrefslogtreecommitdiffstats
path: root/Plugins/SqlEnterpriseFormatter/formatupsert.cpp
blob: 70992f942c9b17927320d6673d041284e3e9d58c (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
#include "formatupsert.h"
#include "parser/ast/sqliteupsert.h"
#include "parser/ast/sqliteexpr.h"
#include "parser/ast/sqliteorderby.h"

FormatUpsert::FormatUpsert(SqliteUpsert* upsert) :
    upsert(upsert)
{

}

void FormatUpsert::formatInternal()
{
    withKeyword("ON").withKeyword("CONFLICT");
    if (!upsert->conflictColumns.isEmpty())
    {
        withParDefLeft().withStatementList(upsert->conflictColumns).withParDefRight();
        if (upsert->conflictWhere)
            withKeyword("WHERE").withStatement(upsert->conflictWhere);
    }

    withKeyword("DO");

    if (upsert->doNothing)
    {
        withKeyword("NOTHING");
    }
    else
    {
        withKeyword("UPDATE").withKeyword("SET");
        bool first = true;
        for (const SqliteUpsert::ColumnAndValue& keyVal : upsert->keyValueMap)
        {
            if (!first)
                withListComma();

            if (keyVal.first.type() == QVariant::StringList)
                withParDefLeft().withIdList(keyVal.first.toStringList()).withParDefRight().withOperator("=").withStatement(keyVal.second);
            else
                withId(keyVal.first.toString()).withOperator("=").withStatement(keyVal.second);

            first = false;
        }

        if (upsert->setWhere)
            withKeyword("WHERE").withStatement(upsert->setWhere);
    }

}