aboutsummaryrefslogtreecommitdiffstats
path: root/Plugins/SqlEnterpriseFormatter/formatinsert.cpp
blob: 2a49cfdb0030e5dddb2faf2a4d727ff2175f390a (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
#include "formatinsert.h"
#include "parser/ast/sqliteselect.h"
#include "parser/ast/sqliteinsert.h"
#include "parser/ast/sqliteupsert.h"
#include "formatwith.h"

FormatInsert::FormatInsert(SqliteInsert* insert) :
    insert(insert)
{
}

void FormatInsert::formatInternal()
{
    handleExplainQuery(insert);
    if (insert->replaceKw)
    {
        withStatement(insert->with);
        withKeyword("REPLACE");
    }
    else
    {
        withStatement(insert->with);
        withKeyword("INSERT");
        if (insert->onConflict != SqliteConflictAlgo::null)
            withKeyword("OR").withKeyword(sqliteConflictAlgo(insert->onConflict));
    }

    withKeyword("INTO");

    if (!insert->database.isNull())
        withId(insert->database);

    withId(insert->table);

    if (insert->defaultValuesKw)
    {
        withKeyword("DEFAULT").withKeyword("VALUES");
    }
    else
    {
        markAndKeepIndent("insertCols");
        if (insert->columnNames.size() > 0)
            withParDefLeft().withIdList(insert->columnNames).withParDefRight();

        if (insert->select)
        {
            withStatement(insert->select);
        }
        else if (dialect == Dialect::Sqlite2) // Sqlite2 uses classic single row values
        {
            withKeyword("VALUES").withParDefLeft().withStatementList(insert->values).withParDefRight();
        }
        if (insert->upsert)
            withStatement(insert->upsert);

        withDecrIndent();
    }

    withSemicolon();
}