diff --git a/package.json b/package.json index 7e0b38e..08e1ecd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-dbcopy", - "version": "1.0.3", + "version": "1.0.4-dev", "description": "Cordova/Phonegap plugin to copy SQLite Database from www directory to app database directory", "cordova": { "id": "me.rahul.plugins.sqlDB", diff --git a/plugin.xml b/plugin.xml index fed7c31..3378cde 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,6 +1,6 @@ diff --git a/src/android/DatabaseHelper.java b/src/android/DatabaseHelper.java index e30177e..8e6fa27 100644 --- a/src/android/DatabaseHelper.java +++ b/src/android/DatabaseHelper.java @@ -1,6 +1,7 @@ package me.rahul.plugins.sqlDB; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -22,10 +23,16 @@ public class DatabaseHelper extends SQLiteOpenHelper { public void createdatabase(File dbPath) throws IOException { + createdatabase(null, dbPath); + + } + + public void createdatabase(String path, File dbPath) throws IOException { + // Log.d("CordovaLog","Inside CreateDatabase = "+dbPath); this.getReadableDatabase(); try { - copyDatabase(dbPath); + copyDatabase(path, dbPath); } catch (IOException e) { throw new Error( "Create Database Exception ============================ " @@ -34,8 +41,34 @@ public class DatabaseHelper extends SQLiteOpenHelper { } + public void exportdatabase(String path, File dbPath) throws IOException { + + // Log.d("CordovaLog","Inside CreateDatabase = "+dbPath); + this.getReadableDatabase(); + try { + copyDatabaseFrom(path, dbPath); + } catch (IOException e) { + throw new Error( + "Export Database Exception ============================ " + + e); + } + + } + private void copyDatabase(File database) throws IOException { - InputStream myInput = myContext.getAssets().open("www/"+sqlDB.dbname); + copyDatabase(null, database); + } + + private void copyDatabase(String path, File database) throws IOException { + InputStream myInput; + + if(path!=null){ + File root = new File(Environment.getExternalStorageDirectory().getPath() + "/" + path + "/" + sqlDB.dbname); + myInput = new FileInputStream(root); + }else{ + myInput = myContext.getAssets().open("www/"+sqlDB.dbname); + } + OutputStream myOutput = new FileOutputStream(database); byte[] buffer = new byte[1024]; while ((myInput.read(buffer)) > -1) { @@ -48,6 +81,24 @@ public class DatabaseHelper extends SQLiteOpenHelper { } + private void copyDatabaseFrom(String path, File database) throws IOException { + InputStream myInput = new FileInputStream(database); + + OutputStream myOutput; + File root = new File(Environment.getExternalStorageDirectory().getPath() + "/" + path + "/" + sqlDB.dbname); + myOutput = new FileOutputStream(root); + + byte[] buffer = new byte[1024]; + while ((myInput.read(buffer)) > -1) { + myOutput.write(buffer); + } + + myOutput.flush(); + myOutput.close(); + myInput.close(); + + } + @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub diff --git a/src/android/sqlDB.java b/src/android/sqlDB.java index 9a6322f..dcac08b 100644 --- a/src/android/sqlDB.java +++ b/src/android/sqlDB.java @@ -1,102 +1,200 @@ -package me.rahul.plugins.sqlDB; - -import java.io.File; - -import org.apache.cordova.CordovaPlugin; -import org.apache.cordova.CallbackContext; -import org.apache.cordova.PluginResult; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import android.util.Log; - -/** - * This class echoes a string called from JavaScript. - */ -public class sqlDB extends CordovaPlugin { - - public static String dbname = "dbname"; - PluginResult plresult = new PluginResult(PluginResult.Status.NO_RESULT); - - @Override - public boolean execute(String action, JSONArray args, - CallbackContext callbackContext) throws JSONException { - - if (action.equalsIgnoreCase("copy")) { - this.copyDB(args.getString(0), callbackContext); - return true; - } else if (action.equalsIgnoreCase("remove")) { - String db = args.getString(0); - File path = cordova.getActivity().getDatabasePath(db); - Boolean fileExists = path.exists(); - if (fileExists) { - boolean deleted = path.delete(); - if (deleted) { - plresult = new PluginResult(PluginResult.Status.OK, deleted); - callbackContext.sendPluginResult(plresult); - } else { - plresult = new PluginResult(PluginResult.Status.ERROR, - deleted); - callbackContext.sendPluginResult(plresult); - } - } else { - plresult = new PluginResult(PluginResult.Status.ERROR, - "File Doesn't Exists"); - callbackContext.sendPluginResult(plresult); - } - return true; - } else { - plresult = new PluginResult(PluginResult.Status.INVALID_ACTION); - callbackContext.sendPluginResult(plresult); - return false; - } - } - - private void copyDB(String dbName, final CallbackContext callbackContext) { - - final File dbpath; - dbname = dbName; - JSONObject error = new JSONObject(); - final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova - .getActivity().getApplicationContext()); - dbpath = this.cordova.getActivity().getDatabasePath(dbname); - Boolean dbexists = dbpath.exists(); - //Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists); - - if (dbexists) { - try { - error.put("message", "File already exists"); - error.put("code", 516); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - plresult = new PluginResult(PluginResult.Status.ERROR, error); - callbackContext.sendPluginResult(plresult); - } else { - cordova.getThreadPool().execute(new Runnable() { - - @Override - public void run() { - PluginResult plResult = new PluginResult( - PluginResult.Status.NO_RESULT); - // TODO Auto-generated method stub - try { - dbhelper.createdatabase(dbpath); - plResult = new PluginResult(PluginResult.Status.OK); - callbackContext.sendPluginResult(plResult); - } catch (Exception e) { - - plResult = new PluginResult(PluginResult.Status.ERROR, - e.getMessage()); - callbackContext.sendPluginResult(plResult); - } - } - - }); - } - } -} +package me.rahul.plugins.sqlDB; + +import java.io.File; + +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CallbackContext; +import org.apache.cordova.PluginResult; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.util.Log; + +/** + * This class echoes a string called from JavaScript. + */ +public class sqlDB extends CordovaPlugin { + + public static String dbname = "dbname"; + PluginResult plresult = new PluginResult(PluginResult.Status.NO_RESULT); + + @Override + public boolean execute(String action, JSONArray args, + CallbackContext callbackContext) throws JSONException { + + if (action.equalsIgnoreCase("copyTo")) { + this.copyDBTo(args.getString(0), args.getString(1), callbackContext); + return true; + }else if (action.equalsIgnoreCase("copyFrom")) { + this.copyDBFrom(args.getString(0), args.getString(1), callbackContext); + return true; + } else if (action.equalsIgnoreCase("copy")) { + this.copyDB(args.getString(0), callbackContext); + return true; + } else if (action.equalsIgnoreCase("remove")) { + String db = args.getString(0); + File path = cordova.getActivity().getDatabasePath(db); + Boolean fileExists = path.exists(); + if (fileExists) { + boolean deleted = path.delete(); + if (deleted) { + plresult = new PluginResult(PluginResult.Status.OK, deleted); + callbackContext.sendPluginResult(plresult); + } else { + plresult = new PluginResult(PluginResult.Status.ERROR, + deleted); + callbackContext.sendPluginResult(plresult); + } + } else { + plresult = new PluginResult(PluginResult.Status.ERROR, + "File Doesn't Exists"); + callbackContext.sendPluginResult(plresult); + } + return true; + } else { + plresult = new PluginResult(PluginResult.Status.INVALID_ACTION); + callbackContext.sendPluginResult(plresult); + return false; + } + } + + private void copyDB(String dbName, final CallbackContext callbackContext) { + + final File dbpath; + dbname = dbName; + JSONObject error = new JSONObject(); + final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova + .getActivity().getApplicationContext()); + dbpath = this.cordova.getActivity().getDatabasePath(dbname); + Boolean dbexists = dbpath.exists(); + //Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists); + + if (dbexists) { + try { + error.put("message", "File already exists"); + error.put("code", 516); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + plresult = new PluginResult(PluginResult.Status.ERROR, error); + callbackContext.sendPluginResult(plresult); + } else { + cordova.getThreadPool().execute(new Runnable() { + + @Override + public void run() { + PluginResult plResult = new PluginResult( + PluginResult.Status.NO_RESULT); + // TODO Auto-generated method stub + try { + dbhelper.createdatabase(dbpath); + plResult = new PluginResult(PluginResult.Status.OK); + callbackContext.sendPluginResult(plResult); + } catch (Exception e) { + + plResult = new PluginResult(PluginResult.Status.ERROR, + e.getMessage()); + callbackContext.sendPluginResult(plResult); + } + } + + }); + } + } + + private void copyDBFrom(String path, String dbName, final CallbackContext callbackContext) { + + final File dbpath; + dbname = dbName; + JSONObject error = new JSONObject(); + final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova + .getActivity().getApplicationContext()); + dbpath = this.cordova.getActivity().getDatabasePath(dbname); + Boolean dbexists = dbpath.exists(); + //Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists); + + if (dbexists) { + try { + error.put("message", "File already exists"); + error.put("code", 516); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + plresult = new PluginResult(PluginResult.Status.ERROR, error); + callbackContext.sendPluginResult(plresult); + } else { + cordova.getThreadPool().execute(new Runnable() { + + @Override + public void run() { + PluginResult plResult = new PluginResult( + PluginResult.Status.NO_RESULT); + // TODO Auto-generated method stub + try { + dbhelper.createdatabase(dbpath); + plResult = new PluginResult(PluginResult.Status.OK); + callbackContext.sendPluginResult(plResult); + } catch (Exception e) { + + plResult = new PluginResult(PluginResult.Status.ERROR, + e.getMessage()); + callbackContext.sendPluginResult(plResult); + } + } + + }); + } + } + + private void copyDBTo(String path, String dbName, final CallbackContext callbackContext) { + + final File dbpath; + dbname = dbName; + JSONObject error = new JSONObject(); + final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova + .getActivity().getApplicationContext()); + dbpath = this.cordova.getActivity().getDatabasePath(dbname); + Boolean dbexists = dbpath.exists(); + //Log.d("CordovaLog", "DatabasePath = " + dbpath + "&&&& dbname = " + dbname + "&&&&DB Exists =" + dbexists); + + if (dbexists) { + try { + error.put("message", "File already exists"); + error.put("code", 516); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + plresult = new PluginResult(PluginResult.Status.ERROR, error); + callbackContext.sendPluginResult(plresult); + } else { + cordova.getThreadPool().execute(new Runnable() { + + @Override + public void run() { + PluginResult plResult = new PluginResult( + PluginResult.Status.NO_RESULT); + // TODO Auto-generated method stub + try { + dbhelper.exportdatabase(dbpath); + plResult = new PluginResult(PluginResult.Status.OK); + callbackContext.sendPluginResult(plResult); + } catch (Exception e) { + + plResult = new PluginResult(PluginResult.Status.ERROR, + e.getMessage()); + callbackContext.sendPluginResult(plResult); + } + } + + }); + } + } +} diff --git a/www/sqlDB.js b/www/sqlDB.js index 2f1cbc7..95c3907 100644 --- a/www/sqlDB.js +++ b/www/sqlDB.js @@ -1,9 +1,17 @@ -var exec = require('cordova/exec'); - -exports.copy = function(dbname, location, success, error) { - exec(success, error, "sqlDB", "copy", [dbname, location]); -}; - -exports.remove = function(dbname, location, success,error) { - exec(success, error, "sqlDB", "remove", [dbname, location]); -}; +var exec = require('cordova/exec'); + +exports.copy = function(dbname, location, success, error) { + exec(success, error, "sqlDB", "copy", [dbname, location]); +}; + +exports.remove = function(dbname, location, success,error) { + exec(success, error, "sqlDB", "remove", [dbname, location]); +}; + +exports.copyFrom = function(path, dbname, location, success, error) { + exec(success, error, "sqlDB", "copyFrom", [path, dbname, location]); +}; + +exports.copyTo = function(path, dbname, location, success, error) { + exec(success, error, "sqlDB", "copyTo", [path, dbname, location]); +}; \ No newline at end of file