From f6bdd2edbd47d84bb37334bad6ca5f55cae43d5b Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Wed, 2 Jun 2010 23:27:54 -0400 Subject: [PATCH] Some small refactoring in DBHelper --- src/org/openintents/safe/DBHelper.java | 78 +++++++++++--------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/src/org/openintents/safe/DBHelper.java b/src/org/openintents/safe/DBHelper.java index e98c8cc..66570a9 100644 --- a/src/org/openintents/safe/DBHelper.java +++ b/src/org/openintents/safe/DBHelper.java @@ -230,48 +230,58 @@ public class DBHelper { } return version; } - -////////// Salt Functions //////////////// - /** - * Store the salt - * - * @return String version of salt - */ - public String fetchSalt() { - String salt=""; +////////// Single value accessor Functions //////////////// + + private String fetchSV(String table, String key) { + String res=""; try { - Cursor c = db.query(true, TABLE_SALT, new String[] {"salt"}, + Cursor c = db.query(true, table, new String[] {key}, null, null, null, null, null,null); if(c.getCount() > 0) { c.moveToFirst(); - salt=c.getString(0); + res=c.getString(0); } c.close(); } catch (SQLException e) { Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage()); } - return salt; + return res; } - /** - * Store the salt into the database. - * - * @param salt String version of the salt - */ - public void storeSalt(String salt) { + public void storeSV(String table, String key, String value) { ContentValues args = new ContentValues(); try { - db.delete(TABLE_SALT, "1=1", null); - args.put("salt", salt); - db.insert(TABLE_SALT, null, args); + db.delete(table, "1=1", null); + args.put(key, value); + db.insert(table, null, args); } catch (SQLException e) { Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage()); } } +////////// Salt Functions //////////////// + + /** + * Store the salt + * + * @return String version of salt + */ + public String fetchSalt() { + return fetchSV(TABLE_SALT, "salt"); + } + + /** + * Store the salt into the database. + * + * @param salt String version of the salt + */ + public void storeSalt(String salt) { + storeSV(TABLE_SALT, "salt", salt); + } + ////////// Master Key Functions //////////////// /** @@ -279,20 +289,7 @@ public class DBHelper { * @return */ public String fetchMasterKey() { - String key=""; - try { - Cursor c = db.query(true, TABLE_MASTER_KEY, new String[] {"encryptedkey"}, - null, null, null, null, null,null); - if(c.getCount() > 0) { - c.moveToFirst(); - key=c.getString(0); - } - c.close(); - } catch (SQLException e) - { - Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage()); - } - return key; + return fetchSV(TABLE_MASTER_KEY, "encryptedkey"); } /** @@ -300,17 +297,10 @@ public class DBHelper { * @param PBEKey */ public void storeMasterKey(String MasterKey) { - ContentValues args = new ContentValues(); - try { - db.delete(TABLE_MASTER_KEY, "1=1", null); - args.put("encryptedkey", MasterKey); - db.insert(TABLE_MASTER_KEY, null, args); - } catch (SQLException e) - { - Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage()); - } + storeSV(TABLE_MASTER_KEY, "encryptedkey", MasterKey); } + //////////Category Functions //////////////// /** -- 2.50.1