From 99997caec6580daacc33f148d8addfbc8528c4d0 Mon Sep 17 00:00:00 2001 From: Marcus Breese Date: Thu, 25 Jun 2009 17:07:36 -0400 Subject: [PATCH] Patches from Michael Bridgen --- src/java/com/fourspaces/couchdb/Database.java | 11 +++--- src/java/com/fourspaces/couchdb/Session.java | 10 ++++-- src/java/com/fourspaces/couchdb/View.java | 34 +++++++++++++++---- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/java/com/fourspaces/couchdb/Database.java b/src/java/com/fourspaces/couchdb/Database.java index 1fbcbe5..652cba9 100644 --- a/src/java/com/fourspaces/couchdb/Database.java +++ b/src/java/com/fourspaces/couchdb/Database.java @@ -44,7 +44,9 @@ public class Database { private Session session; - private static final String VIEW = "_view"; + private static final String VIEW = "/_view/"; + private static final String DESIGN = "_design/"; + /** * C-tor only used by the Session object. You'd never call this directly. @@ -118,8 +120,8 @@ public ViewResults getAllDocuments(int revision) { * @return */ public ViewResults view(View view) { - return view(view, true); - } + return view(view, true); + } /** * Runs a view, appending "_view" to the request if isPermanentView is true. @@ -131,7 +133,8 @@ public ViewResults view(View view) { private ViewResults view(final View view, final boolean isPermanentView) { String url = null; if (isPermanentView) { - url = this.name + "/" + VIEW + "/" + view.getFullName(); + String[] elements = view.getFullName().split("/"); + url = this.name + "/" + ((elements.length < 2) ? elements[0] : DESIGN + elements[0] + VIEW + elements[1]); } else { url = this.name + "/" + view.getFullName(); } diff --git a/src/java/com/fourspaces/couchdb/Session.java b/src/java/com/fourspaces/couchdb/Session.java index 7a46fa3..963f7a9 100644 --- a/src/java/com/fourspaces/couchdb/Session.java +++ b/src/java/com/fourspaces/couchdb/Session.java @@ -94,7 +94,9 @@ public Session(String host, int port, String user, String pass, boolean usesAuth this.secure = secure; DefaultHttpClient defaultClient = new DefaultHttpClient(); - defaultClient.getCredentialsProvider().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(user, pass) ); + if (user != null) { + defaultClient.getCredentialsProvider().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(user, pass) ); + } this.httpClient = defaultClient; @@ -236,7 +238,7 @@ protected String buildUrl(String url) { } protected String buildUrl(String url, String queryString) { - return url + "?" + queryString; + return (queryString != null) ? buildUrl(url) + "?" + queryString : buildUrl(url); } protected String buildUrl(String url, NameValuePair[] params) { @@ -382,7 +384,9 @@ protected CouchResponse http(HttpRequestBase req) { HttpEntity entity = null; try { - req.getParams().setBooleanParameter(ClientPNames.HANDLE_AUTHENTICATION, true); + if (usesAuth) { + req.getParams().setBooleanParameter(ClientPNames.HANDLE_AUTHENTICATION, true); + } httpResponse = httpClient.execute(req); entity = httpResponse.getEntity(); lastResponse = new CouchResponse(req, httpResponse); diff --git a/src/java/com/fourspaces/couchdb/View.java b/src/java/com/fourspaces/couchdb/View.java index 30f9959..c20b85a 100644 --- a/src/java/com/fourspaces/couchdb/View.java +++ b/src/java/com/fourspaces/couchdb/View.java @@ -30,10 +30,11 @@ public class View { protected String startKey; protected String endKey; - protected Integer count; + protected Integer limit; protected Boolean update; protected Boolean reverse; protected String skip; + protected Boolean group; protected String name; protected Document document; @@ -64,7 +65,7 @@ public View(String fullname) { * This does not actually add it to the document. That is handled by * Document.addView() *

- * This contructor should only be called by Document.addView(); + * This constructor should only be called by Document.addView(); * * @param doc * @param name @@ -96,9 +97,9 @@ public String getQueryString() { if (!queryString.equals("")) { queryString+="&"; } queryString+="skip="+skip; } - if (count!=null) { + if (limit!=null) { if (!queryString.equals("")) { queryString+="&"; } - queryString+="count="+count; + queryString+="limit="+limit; } if (update!=null && update.booleanValue()) { if (!queryString.equals("")) { queryString+="&"; } @@ -106,18 +107,34 @@ public String getQueryString() { } if (reverse!=null && reverse.booleanValue()) { if (!queryString.equals("")) { queryString+="&"; } - queryString+="reverse=true"; + queryString+="descending=true"; } + if (group!=null && group.booleanValue()) { + if (!queryString.equals("")) { queryString+="&"; } + queryString+="group=true"; + } return queryString.equals("") ? null : queryString; + } /** * The number of entries to return * @param count + * @deprecated CouchDB 0.9 uses limit instead */ public void setCount(Integer count) { - this.count = count; + //this.count = count; + setLimit(count); } + + public void setLimit(Integer limit) { + this.limit = limit; + } + + public void setGroup(Boolean group) { + this.group = group; + } + /** * Stop listing at this key * @param endKey @@ -128,10 +145,15 @@ public void setEndKey(String endKey) { /** * Reverse the listing * @param reverse + * @deprecated CouchDB 0.9 uses "descending" instead */ public void setReverse(Boolean reverse) { this.reverse = reverse; } + + public void setDescending(Boolean descending) { + this.reverse = descending; + } /** * Skip listing these keys (not sure if this works, or the format) * @param skip