summaryrefslogtreecommitdiff
path: root/plugins/shotwell-data-imports/FSpotDatabaseTable.vala
blob: f2bcdc9f022be552fb24e700fd07d41823219a3c (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
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
 * See the COPYING file in this distribution.
 */

namespace DataImports.FSpot.Db {

/**
 * This class represents a generic F-Spot table.
 */
public abstract class FSpotDatabaseTable<T> : ImportableDatabaseTable {
    protected unowned Sqlite.Database fspot_db;
    protected FSpotTableBehavior<T> behavior;
    
    public FSpotDatabaseTable(Sqlite.Database db) {
        this.fspot_db = db;
    }
    
    public void set_behavior(FSpotTableBehavior<T> behavior) {
        this.behavior = behavior;
        set_table_name(behavior.get_table_name());
    }
    
    public FSpotTableBehavior<T> get_behavior() {
        return behavior;
    }
    
    protected string get_joined_column_list(bool with_table = false) {
        string[] columns = behavior.list_columns();
        if (with_table)
            for (int i = 0; i < columns.length; i++)
                columns[i] = "%s.%s".printf(table_name, columns[i]);
        return string.joinv(", ", columns);
    }
    
    protected int select_all(out Sqlite.Statement stmt) throws DatabaseError {
        string column_list = get_joined_column_list();
        string sql = "SELECT %s FROM %s".printf(column_list, table_name);

        int res = fspot_db.prepare_v2(sql, -1, out stmt);
        if (res != Sqlite.OK)
            throw_error("Statement failed: %s".printf(sql), res);
        
        res = stmt.step();
        if (res != Sqlite.ROW && res != Sqlite.DONE)
            throw_error("select_all %s %s".printf(table_name, column_list), res);
        
        return res;
    }
}

}