Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Patches from Michael Bridgen <mikeb@lshift.net>
  • Loading branch information
mbreese committed Jun 25, 2009
1 parent 56f7d25 commit 99997ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/java/com/fourspaces/couchdb/Database.java
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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();
}
Expand Down
10 changes: 7 additions & 3 deletions src/java/com/fourspaces/couchdb/Session.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 28 additions & 6 deletions src/java/com/fourspaces/couchdb/View.java
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public View(String fullname) {
* This <i>does not actually add it to the document</i>. That is handled by
* Document.addView()
* <p>
* This contructor should only be called by Document.addView();
* This constructor should only be called by Document.addView();
*
* @param doc
* @param name
Expand Down Expand Up @@ -96,28 +97,44 @@ 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+="&"; }
queryString+="update=true";
}
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
Expand All @@ -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
Expand Down

0 comments on commit 99997ca

Please sign in to comment.