From ba2c4879a66c9a920b1af7f918aeddc628486a57 Mon Sep 17 00:00:00 2001 From: Rahul Pandey Date: Thu, 10 Apr 2014 16:08:44 +0530 Subject: [PATCH] Initial Commit --- plugin.xml | 40 ++++++++++++++++++ src/android/DatabaseHelper.java | 74 +++++++++++++++++++++++++++++++++ src/android/sqlDB.java | 69 ++++++++++++++++++++++++++++++ src/ios/sqlDB.m | 73 ++++++++++++++++++++++++++++++++ www/sqlDB.js | 5 +++ 5 files changed, 261 insertions(+) create mode 100644 plugin.xml create mode 100644 src/android/DatabaseHelper.java create mode 100644 src/android/sqlDB.java create mode 100644 src/ios/sqlDB.m create mode 100644 www/sqlDB.js diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..f61ce49 --- /dev/null +++ b/plugin.xml @@ -0,0 +1,40 @@ + + + + sqlDB + + Copy SQLite Database to Document Directory + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/android/DatabaseHelper.java b/src/android/DatabaseHelper.java new file mode 100644 index 0000000..0a91fbd --- /dev/null +++ b/src/android/DatabaseHelper.java @@ -0,0 +1,74 @@ +package me.rahul.plugins.sqlDB; + +import java.io.File; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CallbackContext; + +import org.json.JSONArray; +import org.json.JSONException; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +public class DatabaseHelper extends SQLiteOpenHelper { + + private Context myContext; + + public DatabaseHelper(Context context) { + super(context, sqlDB.dbname, null, 1); + this.myContext = context; + // TODO Auto-generated constructor stub + } + + public void createdatabase(File dbPath) throws IOException { + + Log.d("CordovaLog","Inside CreateDatabase = "+dbPath); + this.getReadableDatabase(); + try { + copyDatabase(dbPath); + } catch (IOException e) { + throw new Error("Create Database Exception ============================ "+e); + } + + } + + private void copyDatabase(File database) throws IOException { + Log.d("CordovaLog","Inside copydatabase = "+database+"dbname = "+sqlDB.dbname); + InputStream myInput = myContext.getAssets().open(sqlDB.dbname); + OutputStream myOutput = new FileOutputStream(database); + byte[] buffer = new byte[1024]; + Log.d("CordovaLog","Inside copydatabase = "+database+"dbname = "+sqlDB.dbname); + int length; + 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 + + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + // TODO Auto-generated method stub + + } + + +} diff --git a/src/android/sqlDB.java b/src/android/sqlDB.java new file mode 100644 index 0000000..2c523eb --- /dev/null +++ b/src/android/sqlDB.java @@ -0,0 +1,69 @@ +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 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 { + 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; + final DatabaseHelper dbhelper = new DatabaseHelper(this.cordova.getActivity().getApplicationContext()); + dbpath = this.cordova.getActivity().getDatabasePath(dbname); + Boolean dbexists = dbpath.exists(); + Log.d("CordovaLog", "Inside Plugin"); + Log.d("CordovaLog", "DatabasePath = "+dbpath+"&&&& dbname = "+dbname+"&&&&DB Exists ="+dbexists); + + if(dbexists){ + plresult = new PluginResult(PluginResult.Status.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); + callbackContext.sendPluginResult(plResult); + } + } + + }); + } + } +} diff --git a/src/ios/sqlDB.m b/src/ios/sqlDB.m new file mode 100644 index 0000000..f10bdab --- /dev/null +++ b/src/ios/sqlDB.m @@ -0,0 +1,73 @@ +// +// sqlDB Implementation File +// sqlDB Phonegap/Cordova Plugin +// +// Created by Rahul Pandey on 09/04/14. +// +// + +#import +#import +#import + +@interface sqlDB : CDVPlugin { + // Member variables go here. + NSString* callbackId; + NSFileManager* fileManager; + NSArray* paths; + NSString* documentsDirectory; +} + +@property (nonatomic, copy) NSString* callbackId; + +- (void)copy:(CDVInvokedUrlCommand*)command; + +@end + +@implementation sqlDB + +- (void)copy:(CDVInvokedUrlCommand*)command +{ + self.callbackId = command.callbackId; + BOOL success; + NSError *error = nil; + CDVPluginResult *result = nil; + NSString *dbname = [command argumentAtIndex:0]; + + NSLog(@"[sqlDB] Dbname = %@",dbname); + + paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + documentsDirectory = [paths objectAtIndex:0]; + + NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:dbname]; + + + fileManager = [NSFileManager defaultManager]; + + success = [fileManager fileExistsAtPath:dbPath isDirectory:NO]; + + if (!success) { + NSLog(@"[sqlDB] copying db file"); + + NSString *dbPathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbname]; + NSLog(@"[sqlDB] Source: %@",dbPathFromApp); + NSLog(@"[sqlDB] Destinatiion: %@",dbPath); + if (!([fileManager copyItemAtPath:dbPathFromApp toPath:dbPath error:&error])) { + NSLog(@"[sqlDB] Could not copy file from %@ to %@. Error = %@",dbPathFromApp,dbPath,error); + result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description]; + } else { + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"File Copied"]; + } + + + } + else + { + NSLog(@"[sqlDB] File Aready Exists"); + result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"File Already Exists"]; + } + + [self.commandDelegate sendPluginResult:result callbackId:callbackId]; +} + +@end diff --git a/www/sqlDB.js b/www/sqlDB.js new file mode 100644 index 0000000..2d5d4fa --- /dev/null +++ b/www/sqlDB.js @@ -0,0 +1,5 @@ +var exec = require('cordova/exec'); + +exports.copy = function(dbname, success, error) { + exec(success, error, "sqlDB", "copy", [dbname]); +};