~dobey/shardbridge/trunk

« back to all changes in this revision

Viewing changes to src/shardbridge-database.vala

  • Committer: Rodney Dawes
  • Date: 2011-12-26 21:06:50 UTC
  • Revision ID: dobey@wayofthemonkey.com-20111226210650-7r87fr2456k1paes
Allow Document content and revision to be null
Allow Document.get_content_string to return null
Implement Database.delete_doc, and test it

Show diffs side-by-side

added added

removed removed

Lines of Context:
219
219
                                                                                         Document new_doc)
220
220
                        throws SQLHeavy.Error
221
221
                {
 
222
                        string content = new_doc.get_content_string ();
222
223
                        if (old_doc != null) {
223
224
                                _db.execute (
224
225
                                        "UPDATE document SET doc_rev=:rev, doc=:doc WHERE doc_id = :id;",
225
226
                                        ":rev", typeof (string), new_doc.revision,
226
 
                                        ":doc", typeof (string), new_doc.get_content_string (),
 
227
                                        ":doc", typeof (string), content != null ? content : "",
227
228
                                        ":id", typeof (string), new_doc.id);
228
229
                                _db.execute (
229
230
                                        "DELETE FROM document_fields WHERE doc_id = :id;",
233
234
                                        "INSERT INTO document VALUES (:id, :rev, :doc);",
234
235
                                        ":id", typeof (string), new_doc.id,
235
236
                                        ":rev", typeof (string), new_doc.revision,
236
 
                                        ":doc", typeof (string), new_doc.get_content_string ());
 
237
                                        ":doc", typeof (string), content != null ? content : "");
237
238
                        }
238
239
                        // TODO: Update the index too
239
240
                        _db.execute ("INSERT INTO transaction_log(doc_id) VALUES (:id);",
256
257
                        return new_rev;
257
258
                }
258
259
 
259
 
                public void delete (string id, string old_rev) {
 
260
                public string? delete_doc (owned Document doc) throws GLib.Error {
 
261
                        var old_doc = _get_doc_internal (doc.id);
 
262
                        if (old_doc == null)
 
263
                                return null;
 
264
 
 
265
                        if (old_doc.revision != doc.revision)
 
266
                                throw new DataError.CONFLICTED_REVISION (
 
267
                                        "Revisions do not match.");
 
268
 
 
269
                        if (old_doc.content == null)
 
270
                                return null;
 
271
 
 
272
                        if (doc.conflicted)
 
273
                                throw new DataError.CONFLICTED_DOCUMENT (
 
274
                                        "Document has conflicts.");
 
275
 
 
276
                        var new_rev = _allocate_doc_rev (doc.revision);
 
277
                        var new_doc = new Document (doc.id, new_rev, null, doc.conflicted);
 
278
                        _put_and_update_indices (old_doc, new_doc);
 
279
                        doc.revision = new_rev;
 
280
                        doc.content = null;
 
281
                        return new_rev;
260
282
                }
261
283
 
262
284
        } // class Database