Skip to content

Commit

Permalink
add DbConnect and DbAdapter#getByUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzu-ting committed Sep 26, 2016
1 parent 5be8714 commit 8ba94da
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
21 changes: 21 additions & 0 deletions appmate/src/main/java/nctu/fintech/appmate/DbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* SQL connect, client side
*
* <p>
* Implemented features:
* <ol>
* <li>Get: get all</li>
Expand All @@ -32,6 +33,7 @@
* <li>Update: update specific item by index</li>
* <li>Delete: delete specific item by index</li>
* </ol>
* </p>
*
* @version 0.9.2 (2016-09-14)
* @author Tzu-ting, NCTU Fintech Center
Expand Down Expand Up @@ -252,6 +254,25 @@ public boolean delete(int index) {
}
}

/**
* get object by URL
* <p>
* <strong>NOTICE</strong>
* This method currently run WITHOUT any anti-foolish procedure. Please DO NOT use this
*
* @param url request url
* @return retrieved object, or null when connection failed
*/
public JSONObject getByUrl(String url) {
try {
String response = sendRequest("GET", url, HttpURLConnection.HTTP_OK, true);
return new JSONObject(response);
} catch (Exception e) {
Log.e(this.getClass().getName(), e.getMessage());
return null;
}
}

//*******************************************************
// Internal members
//
Expand Down
64 changes: 64 additions & 0 deletions appmate/src/main/java/nctu/fintech/appmate/DbConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package nctu.fintech.appmate;

import android.support.annotation.NonNull;

import java.lang.*;

/**
* Handle the connect to the database.
* <p>This class helps to handle host domain and authentication to prevent rookie mistake.</p>
*<p>usage example:
* {@code
* DbConnection connection = new DbConnection("localhost:8000", "USER", "PASSW0RD");
* DbAdapter tableAdapter = connection.getTable("table_name");
* } </p>
* @version 1.0 (2016-9-22)
* @author Tzu-ting, NCTU Fintech Center
*/
public class DbConnection {

private final String _host;

private final boolean _useAuth;
private String _username = null;
private String _password = null;

/**
* Create a connection to the database which does NOT require authentication
*
* @param host host domain where database located
*/
public DbConnection(@NonNull String host) {
_host = host;
_useAuth = false;
}

/**
* Create a connection to the database which requires authentication.
*
* @param host host domain where database located
* @param username username/id to login
* @param password password to login
*/
public DbConnection(@NonNull String host, @NonNull String username, @NonNull String password) {
_host = host;
_useAuth = true;
_username = username;
_password = password;
}

/**
* Get {@code DbAdapter} for specific table
*
* @param tableName table name to retrieve
* @return a {@code DbAdapter} linked to the specific table
*/
public DbAdapter getTable(@NonNull String tableName) {
if (_useAuth) {
return new DbAdapter(_host, tableName, _username, _password);
} else {
return new DbAdapter(_host, tableName);
}
}

}

0 comments on commit 8ba94da

Please sign in to comment.