aboutsummaryrefslogtreecommitdiff
path: root/qlite/src/upsert_builder.vala
blob: 791049726fc32f7e5ce194727a1a3ae2b01d20c4 (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
using Sqlite;

namespace Qlite {

public class UpsertBuilder : StatementBuilder {
    // INTO [...]
    private Table table;
    private string table_name;

    // VALUES [...]
    private StatementBuilder.AbstractField[] keys = {};
    private StatementBuilder.AbstractField[] fields = {};

    internal UpsertBuilder(Database db, Table table) {
        base(db);
        this.table = table;
        this.table_name = table.name;
    }

    public UpsertBuilder value<T>(Column<T> column, T value, bool key = false) {
        if (key) {
            keys += new Field<T>(column, value);
        } else {
            fields += new Field<T>(column, value);
        }
        return this;
    }

    public UpsertBuilder value_null<T>(Column<T> column, bool key = false) {
        if (column.not_null) error("Can't set non-null column %s to null", column.name);
        if (key) {
            keys += new NullField<T>(column);
        } else {
            fields += new NullField<T>(column);
        }
        return this;
    }

    internal override Statement prepare() {
        error("prepare() not available for upsert.");
    }

    internal Statement prepare_upsert() {
        var unique_fields = new StringBuilder();
        var unique_values = new StringBuilder();
        var update_fields = new StringBuilder();
        var update_values = new StringBuilder();
        var update_fields_vals = new StringBuilder();

        for (int i = 0; i < keys.length; i++) {
            if (i != 0) {
                unique_fields.append(", ");
                unique_values.append(", ");
            }
            unique_fields.append(keys[i].column.name);
            unique_values.append("?");
        }

        for (int i = 0; i < fields.length; i++) {
            if (i != 0) {
                update_fields.append(", ");
                update_values.append(", ");
                update_fields_vals.append(", ");
            }
            update_fields.append(fields[i].column.name);
            update_values.append("?");
            update_fields_vals.append(fields[i].column.name).append("=excluded.").append(fields[i].column.name);
        }

        string sql = @"INSERT INTO $table_name ($(unique_fields.str), $(update_fields.str)) VALUES ($(unique_values.str), $(update_values.str)) " +
                @"ON CONFLICT ($(unique_fields.str)) DO UPDATE SET $(update_fields_vals.str)";

        Statement stmt = db.prepare(sql);
        for (int i = 0; i < keys.length; i++) {
            keys[i].bind(stmt, i + 1);
        }
        for (int i = 0; i < fields.length; i++) {
            fields[i].bind(stmt, i + keys.length + 1);
        }

        return stmt;
    }

    public int64 perform() {
        if (prepare_upsert().step() != DONE) {
            critical(@"SQLite error: %d - %s", db.errcode(), db.errmsg());
        }
        return db.last_insert_rowid();
    }

}

}