public class Database { public DesktopCouch.Session session; public CouchDB.Database db; private string db_name = "speedyrss"; public struct Field { string name; string val; } // constructor public Database() { // create new session this.session = new DesktopCouch.Session(); stderr.printf("Connecting to CouchDB at %s\n", this.session.get_uri ()); stderr.printf("Authenticated: %s\n", this.session.is_authentication_enabled () ? "true" : "false"); // create database, if not exists try { this.session.create_database(this.db_name); } catch (Error e) { stderr.printf("%s\n", e.message); } // connect to database this.db = new CouchDB.Database(this.session, this.db_name); } public string insert(Field[] fields) { var doc = new CouchDB.Document(); foreach (Field data in fields) { doc.set_string_field(data.name, data.val); } try { // store document this.db.put_document(doc); } catch (Error e) { stderr.printf("%s\n", e.message); } stderr.printf("Entry added\n"); return doc.get_id(); } public void remove(string id) { try { var doc = this.db.get_document(id); this.db.delete_document(doc); } catch (Error e) { stderr.printf("%s\n", e.message); } } public void search(Field field, string view_name) { } public void add_view(string name, string map, string reduce = "") { var view = new CouchDB.DesignDocument(); view.add_view(name, map, reduce); view.set_language(CouchDB.DesignDocumentLanguage.JAVASCRIPT); stderr.printf("%s %s %s\n", name, map, reduce); } }