1 #include "formatupsert.h"
2 #include "parser/ast/sqliteupsert.h"
3 #include "parser/ast/sqliteexpr.h"
4 #include "parser/ast/sqliteorderby.h"
5 
FormatUpsert(SqliteUpsert * upsert)6 FormatUpsert::FormatUpsert(SqliteUpsert* upsert) :
7     upsert(upsert)
8 {
9 
10 }
11 
formatInternal()12 void FormatUpsert::formatInternal()
13 {
14     withKeyword("ON").withKeyword("CONFLICT");
15     if (!upsert->conflictColumns.isEmpty())
16     {
17         withParDefLeft().withStatementList(upsert->conflictColumns).withParDefRight();
18         if (upsert->conflictWhere)
19             withKeyword("WHERE").withStatement(upsert->conflictWhere);
20     }
21 
22     withKeyword("DO");
23 
24     if (upsert->doNothing)
25     {
26         withKeyword("NOTHING");
27     }
28     else
29     {
30         withKeyword("UPDATE").withKeyword("SET");
31         bool first = true;
32         for (const SqliteUpsert::ColumnAndValue& keyVal : upsert->keyValueMap)
33         {
34             if (!first)
35                 withListComma();
36 
37             if (keyVal.first.type() == QVariant::StringList)
38                 withParDefLeft().withIdList(keyVal.first.toStringList()).withParDefRight().withOperator("=").withStatement(keyVal.second);
39             else
40                 withId(keyVal.first.toString()).withOperator("=").withStatement(keyVal.second);
41 
42             first = false;
43         }
44 
45         if (upsert->setWhere)
46             withKeyword("WHERE").withStatement(upsert->setWhere);
47     }
48 
49 }
50