Permalink
Browse files

Issue #1: "Named views don't work" - added missing '_view' string, cr…

…eated private, internal method for all others to call.
  • Loading branch information...
1 parent 5dfe165 commit a946e7674e8e2308b46c1e5b1bb68229779eac59 william.kinney committed Aug 26, 2008
Showing with 38 additions and 17 deletions.
  1. +38 −17 src/java/com/fourspaces/couchdb/Database.java
@@ -39,6 +39,8 @@
private Session session;
+ private static final String VIEW = "_view";
+
/**
* C-tor only used by the Session object. You'd never call this directly.
* @param json
@@ -82,44 +84,63 @@ public int getUpdateSeq() {
* @return ViewResults - the results of the view... this can be iterated over to get each document.
*/
public ViewResults getAllDocuments() {
- return view("_all_docs");
+ return view(new View("_all_docs"), false);
}
/**
* Runs "_all_docs_by_update_seq?startkey=revision" view on this database
* @return ViewResults - the results of the view... this can be iterated over to get each document.
*/
public ViewResults getAllDocuments(int revision) {
- return view("_all_docs_by_seq?startkey=" + revision);
+ return view(new View("_all_docs_by_seq?startkey=" + revision), false);
}
/**
* Runs a named view on the database
* This will run a view and apply any filtering that is requested (reverse, startkey, etc).
- * <i>Not currently working in CouchDB code</i>
*
* @param view
* @return
*/
public ViewResults view(View view) {
- CouchResponse resp = session.get(name+"/"+view.getFullName(), view.getQueryString());
- if (resp.isOk()) {
- ViewResults results = new ViewResults(view,resp.getBodyAsJSON());
- results.setDatabase(this);
- return results;
- }
- return null;
- }
-
+ return view(view, true);
+ }
+
/**
- * Runs a named view
- * <i>Not currently working in CouchDB code</i>
- * @param name - the fullname (including the document name) ex: foodoc:viewname
+ * Runs a view, appending "_view" to the request if isPermanentView is true.
+ * *
+ * @param view
+ * @param isPermanentView
* @return
*/
+ private ViewResults view(final View view, final boolean isPermanentView) {
+ String url = null;
+ if (isPermanentView) {
+ url = this.name + "/" + VIEW + "/" + view.getFullName();
+ } else {
+ url = this.name + "/" + view.getFullName();
+ }
+
+ CouchResponse resp = session.get(url, view.getQueryString());
+ if (resp.isOk()) {
+ ViewResults results = new ViewResults(view, resp.getBodyAsJSON());
+ results.setDatabase(this);
+ return results;
+ }
+ return null;
+
+ }
+
+ /**
+ * Runs a named view <i>Not currently working in CouchDB code</i>
+ *
+ * @param name
+ * - the fullname (including the document name) ex: foodoc:viewname
+ * @return
+ */
public ViewResults view(String fullname) {
- return view(new View(fullname));
+ return view(new View(fullname), true);
}
/**
@@ -166,7 +187,7 @@ public ViewResults adhoc(AdHocView view) {
public void saveDocument(Document doc, String docId) {
CouchResponse resp;
if (docId==null || docId.equals("")) {
- resp= session.post(name+"/",doc.getJSONObject().toString());
+ resp= session.post(name,doc.getJSONObject().toString());
} else {
resp= session.put(name+"/"+docId,doc.getJSONObject().toString());
}

0 comments on commit a946e76

Please sign in to comment.