summaryrefslogtreecommitdiff
path: root/src/db/FaceTable.vala
blob: 4836910dcf172ded8eefafb56efd1843be4835bc (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright 2018 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

public struct FaceID {
    public const int64 INVALID = -1;

    public int64 id;
    
    public FaceID(int64 id = INVALID) {
        this.id = id;
    }
    
    public bool is_invalid() {
        return (id == INVALID);
    }
    
    public bool is_valid() {
        return (id != INVALID);
    }
}

public class FaceRow {
    public FaceID face_id;
    public string name;
    public time_t time_created;
}

public class FaceTable : DatabaseTable {
    private static FaceTable instance = null;
    
    private FaceTable() {
        set_table_name("FaceTable");
        
        Sqlite.Statement stmt;
        int res = db.prepare_v2("CREATE TABLE IF NOT EXISTS "
            + "FaceTable "
            + "("
            + "id INTEGER NOT NULL PRIMARY KEY, "
            + "name TEXT NOT NULL, "
            + "time_created TIMESTAMP"
            + ")", -1, out stmt);
        assert(res == Sqlite.OK);
        
        res = stmt.step();
        if (res != Sqlite.DONE)
            fatal("create FaceTable", res);
    }
    
    public static FaceTable get_instance() {
        if (instance == null)
            instance = new FaceTable();
        
        return instance;
    }
    
    public FaceRow add(string name) throws DatabaseError {
        Sqlite.Statement stmt;
        int res = db.prepare_v2("INSERT INTO FaceTable (name, time_created) VALUES (?, ?)", -1,
            out stmt);
        assert(res == Sqlite.OK);
        
        time_t time_created = (time_t) now_sec();
        
        res = stmt.bind_text(1, name);
        assert(res == Sqlite.OK);
        res = stmt.bind_int64(2, time_created);
        assert(res == Sqlite.OK);
        
        res = stmt.step();
        if (res != Sqlite.DONE)
            throw_error("FaceTable.add", res);
        
        FaceRow row = new FaceRow();
        row.face_id = FaceID(db.last_insert_rowid());
        row.name = name;
        row.time_created = time_created;
        
        return row;
    }
    
    public FaceID create_from_row(FaceRow row) throws DatabaseError {
        Sqlite.Statement stmt;
        int res = db.prepare_v2("INSERT INTO FaceTable (name, time_created) VALUES (?, ?)",
            -1, out stmt);
        assert(res == Sqlite.OK);
        
        res = stmt.bind_text(1, row.name);
        assert(res == Sqlite.OK);
        res = stmt.bind_int64(2, row.time_created);
        assert(res == Sqlite.OK);
        
        res = stmt.step();
        if (res != Sqlite.DONE)
            throw_error("FaceTable.create_from_row", res);
        
        return FaceID(db.last_insert_rowid());
    }
    
    public void remove(FaceID face_id) throws DatabaseError {
        delete_by_id(face_id.id);
    }
    
    public string? get_name(FaceID face_id) throws DatabaseError {
        Sqlite.Statement stmt;
        if (!select_by_id(face_id.id, "name", out stmt))
            return null;
        
        return stmt.column_text(0);
    }
    
    public FaceRow? get_row(FaceID face_id) throws DatabaseError {
        Sqlite.Statement stmt;
        int res = db.prepare_v2("SELECT name, time_created FROM FaceTable WHERE id=?",
            -1, out stmt);
        assert(res == Sqlite.OK);
        
        res = stmt.bind_int64(1, face_id.id);
        assert(res == Sqlite.OK);
        
        res = stmt.step();
        if (res == Sqlite.DONE)
            return null;
        else if (res != Sqlite.ROW)
            throw_error("FaceTable.get_row", res);
        
        FaceRow row = new FaceRow();
        row.face_id = face_id;
        row.name = stmt.column_text(0);
        row.time_created = (time_t) stmt.column_int64(1);
        
        return row;
    }
    
    public Gee.List<FaceRow?> get_all_rows() throws DatabaseError {
        Sqlite.Statement stmt;
        int res = db.prepare_v2("SELECT id, name, time_created FROM FaceTable", -1,
            out stmt);
        assert(res == Sqlite.OK);
        
        Gee.List<FaceRow?> rows = new Gee.ArrayList<FaceRow?>();
        
        for (;;) {
            res = stmt.step();
            if (res == Sqlite.DONE)
                break;
            else if (res != Sqlite.ROW)
                throw_error("FaceTable.get_all_rows", res);
            
            // res == Sqlite.ROW
            FaceRow row = new FaceRow();
            row.face_id = FaceID(stmt.column_int64(0));
            row.name = stmt.column_text(1);
            row.time_created = (time_t) stmt.column_int64(2);
            
            rows.add(row);
        }
        
        return rows;
    }
    
    public void rename(FaceID face_id, string new_name) throws DatabaseError {
        update_text_by_id_2(face_id.id, "name", new_name);
    }
}