aboutsummaryrefslogtreecommitdiff
path: root/qlite/src/upsert_builder.vala
blob: 6d29b2888c1ef1f589b933c9372f8a53b7a4892d (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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) {
        if (column.not_null) error("Can't set non-null column %s to null", column.name);
        fields += new NullField<T>(column);
        return this;
    }

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

    internal Statement prepare_update() {
        string update_set_list = "";
        string update_where_list = "";
        for (int i = 0; i < fields.length; i++) {
            if (i != 0) {
                update_set_list += ", ";
            }
            update_set_list += @"$(((!)fields[i].column).name) = ?";
        }
        for (int i = 0; i < keys.length; i++) {
            if (i != 0) {
                update_where_list += " AND ";
            }
            update_where_list += @"$(((!)keys[i].column).name) = ?";
        }

        string sql = @"UPDATE $table_name SET $update_set_list WHERE $update_where_list";

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

        return stmt;
    }

    internal Statement prepare_insert() {
        string insert_field_list = "";
        string insert_value_qs = "";
        for (int i = 0; i < fields.length; i++) {
            if (i != 0) {
                insert_value_qs += ", ";
                insert_field_list += ", ";
            }
            insert_field_list += ((!)fields[i].column).name;
            insert_value_qs += "?";
        }
        for (int i = 0; i < keys.length; i++) {
            if (i != 0 || fields.length > 0) {
                insert_value_qs += ", ";
                insert_field_list += ", ";
            }
            insert_field_list += ((!)keys[i].column).name;
            insert_value_qs += "?";
        }

        string sql = @"INSERT OR IGNORE INTO $table_name ($insert_field_list) VALUES ($insert_value_qs)";

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

        return stmt;
    }

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

}

}