~ubuntu-branches/ubuntu/trusty/rygel/trusty

« back to all changes in this revision

Viewing changes to src/plugins/media-export/rygel-media-export-sqlite-wrapper.vala

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2011-12-16 15:21:25 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20111216152125-qgn31dkfmhouhrf0
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Jens Georg <mail@jensge.org>.
 
3
 *
 
4
 * Author: Jens Georg <mail@jensge.org>
 
5
 *
 
6
 * This file is part of Rygel.
 
7
 *
 
8
 * Rygel is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU Lesser General Public License as published by
 
10
 * the Free Software Foundation; either version 2 of the License, or
 
11
 * (at your option) any later version.
 
12
 *
 
13
 * Rygel is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public License
 
19
 * along with this program; if not, write to the Free Software Foundation,
 
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
21
 */
 
22
 
 
23
using Sqlite;
 
24
 
 
25
internal class Rygel.MediaExport.SqliteWrapper : Object {
 
26
    private Sqlite.Database database = null;
 
27
    private Sqlite.Database *reference = null;
 
28
 
 
29
    /**
 
30
     * Property to access the wrapped database
 
31
     */
 
32
    protected unowned Sqlite.Database db {
 
33
        get { return reference; }
 
34
    }
 
35
 
 
36
    /**
 
37
     * Wrap an existing SQLite Database object.
 
38
     *
 
39
     * The SqliteWrapper doesn't take ownership of the passed db
 
40
     */
 
41
    public SqliteWrapper.wrap (Sqlite.Database db) {
 
42
        this.reference = db;
 
43
    }
 
44
 
 
45
    /**
 
46
     * Create or open a new SQLite database in path.
 
47
     *
 
48
     * @note: Path may also be ":memory:" for temporary databases
 
49
     */
 
50
    public SqliteWrapper (string path) throws DatabaseError {
 
51
        Sqlite.Database.open (path, out this.database);
 
52
        this.reference = this.database;
 
53
        this.throw_if_db_has_error ();
 
54
    }
 
55
 
 
56
    /**
 
57
     * Convert a SQLite return code to a DatabaseError
 
58
     */
 
59
    protected void throw_if_code_is_error (int sqlite_error)
 
60
                                           throws DatabaseError {
 
61
        switch (sqlite_error) {
 
62
            case Sqlite.OK:
 
63
            case Sqlite.DONE:
 
64
            case Sqlite.ROW:
 
65
                return;
 
66
            default:
 
67
                throw new DatabaseError.SQLITE_ERROR
 
68
                                        ("SQLite error %d: %s",
 
69
                                         sqlite_error,
 
70
                                         this.reference->errmsg ());
 
71
        }
 
72
    }
 
73
 
 
74
    /**
 
75
     * Check if the last operation on the database was an error
 
76
     */
 
77
    protected void throw_if_db_has_error () throws DatabaseError {
 
78
        this.throw_if_code_is_error (this.reference->errcode ());
 
79
    }
 
80
}