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

« back to all changes in this revision

Viewing changes to src/alien_db/f_spot/FSpotMetaTable.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 AlienDb.FSpot {
8
 
 
9
 
/**
10
 
 * The value object for the "meta" table, representing a single database row.
11
 
 */
12
 
public class FSpotMetaRow : Object {
13
 
    // ignore the ID
14
 
    public string name;
15
 
    public string data;
16
 
}
17
 
 
18
 
/**
19
 
 * This class represents the F-Spot meta table, which stores some essential
20
 
 * meta-data for the whole database. It is implemented as a simple dictionary
21
 
 * where each row in the table is a key/value pair.
22
 
 *
23
 
 * The meta table implementation is the only one that throws a database error
24
 
 * if something goes wrong because:
25
 
 *  * it is essential to read the content of that table in order to identify
26
 
 *    the version of the database and select the correct behavior,
27
 
 *  * this table is read at the very beginning of the process so any failure
28
 
 *    will occur immediately,
29
 
 *  * failing to read this table means that there is no point in reading the
30
 
 *    attempting to read the rest of the database so we might as well abort.
31
 
 */
32
 
public class FSpotMetaTable : FSpotDatabaseTable<FSpotMetaRow> {
33
 
    
34
 
    public FSpotMetaTable(Sqlite.Database db) {
35
 
        base(db);
36
 
        set_behavior(FSpotMetaBehavior.get_instance());
37
 
    }
38
 
    
39
 
    public string? get_data(string name) throws DatabaseError {
40
 
        string[] columns = behavior.list_columns();
41
 
        string column_list = string.joinv(", ", columns);
42
 
        string sql = "SELECT %s FROM %s WHERE name=?".printf(column_list, table_name);
43
 
        Sqlite.Statement stmt;
44
 
        int res = fspot_db.prepare_v2(sql, -1, out stmt);
45
 
        if (res != Sqlite.OK)
46
 
            throw_error("Statement failed: %s".printf(sql), res);
47
 
        
48
 
        res = stmt.bind_text(1, name);
49
 
        if (res != Sqlite.OK)
50
 
            throw_error("Bind failed for name %s".printf(name), res);
51
 
        
52
 
        res = stmt.step();
53
 
        if (res != Sqlite.ROW) {
54
 
            if (res != Sqlite.DONE)
55
 
                throw_error("FSpotMetaTable.get_data", res);
56
 
            
57
 
            return null;
58
 
        }
59
 
        
60
 
        FSpotMetaRow row;
61
 
        behavior.build_row(stmt, out row);
62
 
        return row.data;
63
 
    }
64
 
    
65
 
    public string? get_app_version() throws DatabaseError {
66
 
        return get_data("F-Spot Version");
67
 
    }
68
 
    
69
 
    public string? get_db_version() throws DatabaseError {
70
 
        return get_data("F-Spot Database Version");
71
 
    }
72
 
    
73
 
    public int64 get_hidden_tag_id() throws DatabaseError {
74
 
        string id_str = get_data("Hidden Tag Id");
75
 
        if(id_str != null) {
76
 
            return int64.parse(id_str);
77
 
        } else {
78
 
            return -1;
79
 
        }
80
 
    }
81
 
}
82
 
 
83
 
public class FSpotMetaBehavior : FSpotTableBehavior<FSpotMetaRow>, Object {
84
 
    public static const string TABLE_NAME = "Meta";
85
 
    
86
 
    private static FSpotMetaBehavior instance;
87
 
    
88
 
    private FSpotMetaBehavior() {
89
 
    }
90
 
    
91
 
    public static FSpotMetaBehavior get_instance() {
92
 
        if (instance == null)
93
 
            instance = new FSpotMetaBehavior();
94
 
        return instance;
95
 
    }
96
 
    
97
 
    public string get_table_name() {
98
 
        return TABLE_NAME;
99
 
    }
100
 
    
101
 
    public string[] list_columns() {
102
 
        return { "name", "data" };
103
 
    }
104
 
    
105
 
    public void build_row(Sqlite.Statement stmt, out FSpotMetaRow row, int offset = 0) {
106
 
        row = new FSpotMetaRow();
107
 
        row.name = stmt.column_text(offset + 0);
108
 
        row.data = stmt.column_text(offset + 1);
109
 
    }
110
 
}
111
 
 
112
 
}
113