~ubuntu-branches/ubuntu/quantal/shotwell/quantal

« back to all changes in this revision

Viewing changes to plugins/shotwell-data-imports/FSpotTagsTable.vala

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-02-21 13:52:58 UTC
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: package-import@ubuntu.com-20120221135258-ao9jiib5qicomq7q
Tags: upstream-0.11.92
ImportĀ upstreamĀ versionĀ 0.11.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2011 Yorba Foundation
 
2
 *
 
3
 * This software is licensed under the GNU Lesser General Public License
 
4
 * (version 2.1 or later).  See the COPYING file in this distribution. 
 
5
 */
 
6
 
 
7
namespace DataImports.FSpot.Db {
 
8
 
 
9
/**
 
10
 * The value object for the "tags" table, representing a single database row.
 
11
 */
 
12
public class FSpotTagRow : Object {
 
13
    public int64 tag_id;
 
14
    public string name;
 
15
    public int64 category_id;
 
16
    public bool is_category;
 
17
    public int sort_priority;
 
18
    public string stock_icon; // only store stock icons
 
19
}
 
20
 
 
21
/**
 
22
 * This class represents the F-Spot tags table.
 
23
 */
 
24
public class FSpotTagsTable : FSpotDatabaseTable<FSpotTagRow> {
 
25
    public static const string TABLE_NAME = "Tags";
 
26
    
 
27
    public static const string PREFIX_STOCK_ICON = "stock_icon:";
 
28
    public static const string STOCK_ICON_FAV    = "stock_icon:emblem-favorite";
 
29
    public static const string STOCK_ICON_PEOPLE = "stock_icon:emblem-people";
 
30
    public static const string STOCK_ICON_PLACES = "stock_icon:emblem-places";
 
31
    public static const string STOCK_ICON_EVENTS = "stock_icon:emblem-event";
 
32
    
 
33
    private FSpotTableBehavior<FSpotPhotoTagRow> photo_tags_behavior;
 
34
    
 
35
    public FSpotTagsTable(Sqlite.Database db, FSpotDatabaseBehavior db_behavior) {
 
36
        base(db);
 
37
        set_behavior(db_behavior.get_tags_behavior());
 
38
        photo_tags_behavior = db_behavior.get_photo_tags_behavior();
 
39
    }
 
40
    
 
41
    public FSpotTagRow? get_by_id(int64 tag_id) throws DatabaseError {
 
42
        Sqlite.Statement stmt;
 
43
        FSpotTagRow? row = null;
 
44
        string column_list = get_joined_column_list();
 
45
        string sql = "SELECT %s FROM %s WHERE id=?".printf(column_list, table_name);
 
46
 
 
47
        int res = fspot_db.prepare_v2(sql, -1, out stmt);
 
48
        if (res != Sqlite.OK)
 
49
            throw_error("Statement failed: %s".printf(sql), res);
 
50
        
 
51
        res = stmt.bind_int64(1, tag_id);
 
52
        assert(res == Sqlite.OK);
 
53
        
 
54
        res = stmt.step();
 
55
        if (res == Sqlite.ROW)
 
56
            behavior.build_row(stmt, out row);
 
57
        else if (res == Sqlite.DONE)
 
58
            message("Could not find tag row with ID %d", (int)tag_id);
 
59
        
 
60
        return row;
 
61
    }
 
62
    
 
63
    public Gee.ArrayList<FSpotTagRow> get_by_photo_id(int64 photo_id) throws DatabaseError {
 
64
        Gee.ArrayList<FSpotTagRow> rows = new Gee.ArrayList<FSpotTagRow?>();
 
65
        
 
66
        Sqlite.Statement stmt;
 
67
        
 
68
        string column_list = get_joined_column_list(true);
 
69
        string sql = "SELECT %1$s FROM %2$s, %3$s WHERE %3$s.photo_id=? AND %3$s.tag_id = %2$s.id".printf(
 
70
            column_list, table_name, photo_tags_behavior.get_table_name()
 
71
        );
 
72
 
 
73
        int res = fspot_db.prepare_v2(sql, -1, out stmt);
 
74
        if (res != Sqlite.OK)
 
75
            throw_error("Statement failed: %s".printf(sql), res);
 
76
        
 
77
        res = stmt.bind_int64(1, photo_id);
 
78
        if (res != Sqlite.OK)
 
79
            throw_error("Bind failed for photo_id", res);
 
80
        
 
81
        res = stmt.step();
 
82
        while (res == Sqlite.ROW) {
 
83
            FSpotTagRow row;
 
84
            behavior.build_row(stmt, out row);
 
85
            rows.add(row);
 
86
            res = stmt.step();
 
87
        }
 
88
        
 
89
        return rows;
 
90
    }
 
91
}
 
92
 
 
93
public class FSpotTagsV0Behavior : FSpotTableBehavior<FSpotTagRow>, Object {
 
94
    private static FSpotTagsV0Behavior instance;
 
95
    
 
96
    private FSpotTagsV0Behavior() {
 
97
    }
 
98
    
 
99
    public static FSpotTagsV0Behavior get_instance() {
 
100
        if (instance == null)
 
101
            instance = new FSpotTagsV0Behavior();
 
102
        return instance;
 
103
    }
 
104
    
 
105
    public string get_table_name() {
 
106
        return FSpotTagsTable.TABLE_NAME;
 
107
    }
 
108
 
 
109
    public string[] list_columns() {
 
110
        return { "id", "name", "category_id", "is_category", "sort_priority", "icon" };
 
111
    }
 
112
    
 
113
    public void build_row(Sqlite.Statement stmt, out FSpotTagRow row, int offset = 0) {
 
114
        row = new FSpotTagRow();
 
115
        row.tag_id = stmt.column_int64(offset + 0);
 
116
        row.name = stmt.column_text(offset + 1);
 
117
        row.category_id = stmt.column_int64(offset + 2);
 
118
        row.is_category = (stmt.column_int(offset + 3) > 0);
 
119
        row.sort_priority = stmt.column_int(offset + 4);
 
120
        string icon_str = stmt.column_text(offset + 5);
 
121
        if (icon_str != null && icon_str.has_prefix(FSpotTagsTable.PREFIX_STOCK_ICON))
 
122
            row.stock_icon = icon_str;
 
123
        else
 
124
            row.stock_icon = "";
 
125
    }
 
126
}
 
127
 
 
128
}
 
129