~ubuntu-branches/ubuntu/saucy/zeitgeist/saucy

« back to all changes in this revision

Viewing changes to src/sql.vala

  • Committer: Package Import Robot
  • Author(s): Siegfried-Angel Gevatter Pujals
  • Date: 2012-01-27 15:56:06 UTC
  • mfrom: (6.2.3 experimental)
  • Revision ID: package-import@ubuntu.com-20120127155606-x1bejcdh8jbv09ec
Tags: 0.8.99~alpha2-1
* New upstream release. Some of the changes are:
  - Enhanced error handling, reporting and recovery (LP: #848710,
    LP: #743857).
  - The database file is now only readable by the user (LP: #910273).
  - GVFS usage in StorageMonitor has been disabled for now (LP: #905898).
  - Fixed a problem where the FTS extension could block the DB (LP: #919111).
  - Debug messages are no longer displayed by default (LP: #906451).
* debian/rules:
  - Do not install the raw .trig files.
* debian/zeitgeist-core.bash_completion:
  - Change source directory from extra/ to data/.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
 
68
68
        public ZeitgeistDatabase () throws EngineError
69
69
        {
70
 
            int rc = Sqlite.Database.open_v2 (
71
 
                Utils.get_database_file_path (),
72
 
                out database);
73
 
            assert_query_success (rc, "Can't open database");
74
 
 
75
 
            DatabaseSchema.ensure_schema (database);
 
70
            open_database (true);
76
71
 
77
72
            prepare_queries ();
78
73
 
81
76
            database.update_hook (update_callback);
82
77
        }
83
78
 
 
79
        private void open_database (bool retry)
 
80
            throws EngineError
 
81
        {
 
82
            int rc = Sqlite.Database.open_v2 (
 
83
                Utils.get_database_file_path (),
 
84
                out database);
 
85
            
 
86
            if (rc == Sqlite.OK)
 
87
            {
 
88
                try
 
89
                {
 
90
                    // Error (like a malformed database) may not be exposed
 
91
                    // until we try to operate on the database.
 
92
                    DatabaseSchema.ensure_schema (database);
 
93
                }
 
94
                catch (EngineError err)
 
95
                {
 
96
                    if (err is EngineError.DATABASE_CORRUPT && retry)
 
97
                        rc = Sqlite.CORRUPT;
 
98
                    else if (err is EngineError.DATABASE_CANTOPEN)
 
99
                        rc = Sqlite.CANTOPEN;
 
100
                    else if (err is EngineError.DATABASE_BUSY)
 
101
                        rc = Sqlite.BUSY;
 
102
                    else
 
103
                        throw err;
 
104
                }
 
105
            }
 
106
            
 
107
            if (rc != Sqlite.OK)
 
108
            {
 
109
                if (rc == Sqlite.CORRUPT && retry)
 
110
                {
 
111
                    // The database disk image is malformed
 
112
                    warning ("It looks like your database is corrupt. " +
 
113
                        "It will be renamed and a new one will be created.");
 
114
                    try
 
115
                    {
 
116
                        Utils.retire_database ();
 
117
                    }
 
118
                    catch (Error err)
 
119
                    {
 
120
                        string message =
 
121
                            "Could not rename database: %s".printf (
 
122
                                err.message);
 
123
                        throw new EngineError.DATABASE_RETIRE_FAILED (message);
 
124
                    }
 
125
                    open_database (false);
 
126
                }
 
127
                else if (rc == Sqlite.PERM || rc == Sqlite.CANTOPEN)
 
128
                {
 
129
                    // Access permission denied / Unable to open database file
 
130
                    throw new EngineError.DATABASE_CANTOPEN (
 
131
                        database.errmsg ());
 
132
                }
 
133
                else if (rc == Sqlite.BUSY)
 
134
                {
 
135
                    // The database file is locked
 
136
                    throw new EngineError.DATABASE_BUSY (database.errmsg ());
 
137
                }
 
138
                else
 
139
                {
 
140
                    string message = "Can't open database: %d, %s".printf(rc,
 
141
                        database.errmsg ());
 
142
                    throw new EngineError.DATABASE_ERROR (message);
 
143
                }
 
144
            }
 
145
        }
 
146
 
84
147
        public uint32 get_last_id () throws EngineError
85
148
        {
86
149
            int last_id = -1;
98
161
            return last_id;
99
162
        }
100
163
 
101
 
        public void set_deletion_callback (DeletionCallback? callback)
 
164
        public void set_deletion_callback (owned DeletionCallback? callback)
102
165
        {
103
 
            deletion_callback = callback;
 
166
            deletion_callback = (owned) callback;
104
167
        }
105
168
 
106
169
        /**
189
252
            database = null;
190
253
        }
191
254
 
 
255
#if EXPLAIN_QUERIES
 
256
        public void explain_query (Sqlite.Statement prepared_stmt)
 
257
            throws EngineError
 
258
        {
 
259
            int rc;
 
260
            Sqlite.Statement stmt;
 
261
 
 
262
            var explain_sql = "EXPLAIN QUERY PLAN %s".printf (prepared_stmt.sql ());
 
263
 
 
264
            rc = prepared_stmt.db_handle ().prepare_v2 (explain_sql, -1, out stmt);
 
265
            assert_query_success(rc, "SQL error");
 
266
 
 
267
            while ((rc = stmt.step()) == Sqlite.ROW)
 
268
            {
 
269
                int select_id = stmt.column_int (0);
 
270
                int order = stmt.column_int (1);
 
271
                int from = stmt.column_int (2);
 
272
                unowned string detail = stmt.column_text (3);
 
273
 
 
274
                print ("%d %d %d %s\n", select_id, order, from, detail);
 
275
            }
 
276
        }
 
277
#endif
 
278
 
192
279
        /**
193
280
         * Ensure `rc' is SQLITE_OK. If it isn't, print an error message
194
281
         * and throw an error.
200
287
        public void assert_query_success (int rc, string msg,
201
288
            int success_code=Sqlite.OK) throws EngineError
202
289
        {
203
 
            if (rc != success_code)
 
290
            if (unlikely (rc != success_code))
204
291
            {
205
292
                string error_message = "%s: %d, %s".printf(
206
293
                    msg, rc, database.errmsg ());