mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Missed this error in the merge commit
This commit is contained in:
parent
fd12f57f10
commit
43df9f6b9c
@ -37,203 +37,203 @@ import android.database.sqlite.*;
|
|||||||
*/
|
*/
|
||||||
public class Storage extends Plugin {
|
public class Storage extends Plugin {
|
||||||
|
|
||||||
// Data Definition Language
|
// Data Definition Language
|
||||||
private static final String ALTER = "alter";
|
private static final String ALTER = "alter";
|
||||||
private static final String CREATE = "create";
|
private static final String CREATE = "create";
|
||||||
private static final String DROP = "drop";
|
private static final String DROP = "drop";
|
||||||
private static final String TRUNCATE = "truncate";
|
private static final String TRUNCATE = "truncate";
|
||||||
|
|
||||||
SQLiteDatabase myDb = null; // Database object
|
SQLiteDatabase myDb = null; // Database object
|
||||||
String path = null; // Database path
|
String path = null; // Database path
|
||||||
String dbName = null; // Database name
|
String dbName = null; // Database name
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public Storage() {
|
public Storage() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes the request and returns PluginResult.
|
* Executes the request and returns PluginResult.
|
||||||
*
|
*
|
||||||
* @param action
|
* @param action
|
||||||
* The action to execute.
|
* The action to execute.
|
||||||
* @param args
|
* @param args
|
||||||
* JSONArry of arguments for the plugin.
|
* JSONArry of arguments for the plugin.
|
||||||
* @param callbackId
|
* @param callbackId
|
||||||
* The callback id used when calling back into JavaScript.
|
* The callback id used when calling back into JavaScript.
|
||||||
* @return A PluginResult object with a status and message.
|
* @return A PluginResult object with a status and message.
|
||||||
*/
|
*/
|
||||||
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
||||||
PluginResult.Status status = PluginResult.Status.OK;
|
PluginResult.Status status = PluginResult.Status.OK;
|
||||||
String result = "";
|
String result = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (action.equals("openDatabase")) {
|
if (action.equals("openDatabase")) {
|
||||||
this.openDatabase(args.getString(0), args.getString(1),
|
this.openDatabase(args.getString(0), args.getString(1),
|
||||||
args.getString(2), args.getLong(3));
|
args.getString(2), args.getLong(3));
|
||||||
} else if (action.equals("executeSql")) {
|
} else if (action.equals("executeSql")) {
|
||||||
String[] s = null;
|
String[] s = null;
|
||||||
if (args.isNull(1)) {
|
if (args.isNull(1)) {
|
||||||
s = new String[0];
|
s = new String[0];
|
||||||
} else {
|
} else {
|
||||||
JSONArray a = args.getJSONArray(1);
|
JSONArray a = args.getJSONArray(1);
|
||||||
int len = a.length();
|
int len = a.length();
|
||||||
s = new String[len];
|
s = new String[len];
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
s[i] = a.getString(i);
|
s[i] = a.getString(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.executeSql(args.getString(0), s, args.getString(2));
|
this.executeSql(args.getString(0), s, args.getString(2));
|
||||||
}
|
}
|
||||||
return new PluginResult(status, result);
|
return new PluginResult(status, result);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identifies if action to be executed returns a value and should be run
|
* Identifies if action to be executed returns a value and should be run
|
||||||
* synchronously.
|
* synchronously.
|
||||||
*
|
*
|
||||||
* @param action
|
* @param action
|
||||||
* The action to execute
|
* The action to execute
|
||||||
* @return T=returns value
|
* @return T=returns value
|
||||||
*/
|
*/
|
||||||
public boolean isSynch(String action) {
|
public boolean isSynch(String action) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clean up and close database.
|
* Clean up and close database.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
if (this.myDb != null) {
|
if (this.myDb != null) {
|
||||||
this.myDb.close();
|
this.myDb.close();
|
||||||
this.myDb = null;
|
this.myDb = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// LOCAL METHODS
|
// LOCAL METHODS
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open database.
|
* Open database.
|
||||||
*
|
*
|
||||||
* @param db
|
* @param db
|
||||||
* The name of the database
|
* The name of the database
|
||||||
* @param version
|
* @param version
|
||||||
* The version
|
* The version
|
||||||
* @param display_name
|
* @param display_name
|
||||||
* The display name
|
* The display name
|
||||||
* @param size
|
* @param size
|
||||||
* The size in bytes
|
* The size in bytes
|
||||||
*/
|
*/
|
||||||
public void openDatabase(String db, String version, String display_name,
|
public void openDatabase(String db, String version, String display_name,
|
||||||
long size) {
|
long size) {
|
||||||
|
|
||||||
// If database is open, then close it
|
// If database is open, then close it
|
||||||
if (this.myDb != null) {
|
if (this.myDb != null) {
|
||||||
this.myDb.close();
|
this.myDb.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no database path, generate from application package
|
// If no database path, generate from application package
|
||||||
if (this.path == null) {
|
if (this.path == null) {
|
||||||
this.path = this.ctx.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
|
this.path = this.ctx.getActivity().getDir("database", Context.MODE_PRIVATE).getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.dbName = this.path + File.pathSeparator + db + ".db";
|
this.dbName = this.path + File.pathSeparator + db + ".db";
|
||||||
this.myDb = SQLiteDatabase.openOrCreateDatabase(this.dbName, null);
|
this.myDb = SQLiteDatabase.openOrCreateDatabase(this.dbName, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute SQL statement.
|
* Execute SQL statement.
|
||||||
*
|
*
|
||||||
* @param query
|
* @param query
|
||||||
* The SQL query
|
* The SQL query
|
||||||
* @param params
|
* @param params
|
||||||
* Parameters for the query
|
* Parameters for the query
|
||||||
* @param tx_id
|
* @param tx_id
|
||||||
* Transaction id
|
* Transaction id
|
||||||
*/
|
*/
|
||||||
public void executeSql(String query, String[] params, String tx_id) {
|
public void executeSql(String query, String[] params, String tx_id) {
|
||||||
try {
|
try {
|
||||||
if (isDDL(query)) {
|
if (isDDL(query)) {
|
||||||
this.myDb.execSQL(query);
|
this.myDb.execSQL(query);
|
||||||
this.sendJavascript("cordova.require('cordova/plugin/android/storage').completeQuery('" + tx_id + "', '');");
|
this.sendJavascript("cordova.require('cordova/plugin/android/storage').completeQuery('" + tx_id + "', '');");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Cursor myCursor = this.myDb.rawQuery(query, params);
|
Cursor myCursor = this.myDb.rawQuery(query, params);
|
||||||
this.processResults(myCursor, tx_id);
|
this.processResults(myCursor, tx_id);
|
||||||
myCursor.close();
|
myCursor.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (SQLiteException ex) {
|
catch (SQLiteException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
System.out.println("Storage.executeSql(): Error=" + ex.getMessage());
|
System.out.println("Storage.executeSql(): Error=" + ex.getMessage());
|
||||||
|
|
||||||
// Send error message back to JavaScript
|
// Send error message back to JavaScript
|
||||||
this.sendJavascript("cordova.require('cordova/plugin/android/storage').failQuery('" + ex.getMessage() + "','" + tx_id + "');");
|
this.sendJavascript("cordova.require('cordova/plugin/android/storage').failQuery('" + ex.getMessage() + "','" + tx_id + "');");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks to see the the query is a Data Definintion command
|
* Checks to see the the query is a Data Definintion command
|
||||||
*
|
*
|
||||||
* @param query to be executed
|
* @param query to be executed
|
||||||
* @return true if it is a DDL command, false otherwise
|
* @return true if it is a DDL command, false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean isDDL(String query) {
|
private boolean isDDL(String query) {
|
||||||
String cmd = query.toLowerCase();
|
String cmd = query.toLowerCase();
|
||||||
if (cmd.startsWith(DROP) || cmd.startsWith(CREATE) || cmd.startsWith(ALTER) || cmd.startsWith(TRUNCATE)) {
|
if (cmd.startsWith(DROP) || cmd.startsWith(CREATE) || cmd.startsWith(ALTER) || cmd.startsWith(TRUNCATE)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process query results.
|
* Process query results.
|
||||||
*
|
*
|
||||||
* @param cur
|
* @param cur
|
||||||
* Cursor into query results
|
* Cursor into query results
|
||||||
* @param tx_id
|
* @param tx_id
|
||||||
* Transaction id
|
* Transaction id
|
||||||
*/
|
*/
|
||||||
public void processResults(Cursor cur, String tx_id) {
|
public void processResults(Cursor cur, String tx_id) {
|
||||||
|
|
||||||
String result = "[]";
|
String result = "[]";
|
||||||
// If query result has rows
|
// If query result has rows
|
||||||
|
|
||||||
if (cur.moveToFirst()) {
|
if (cur.moveToFirst()) {
|
||||||
JSONArray fullresult = new JSONArray();
|
JSONArray fullresult = new JSONArray();
|
||||||
String key = "";
|
String key = "";
|
||||||
String value = "";
|
String value = "";
|
||||||
int colCount = cur.getColumnCount();
|
int colCount = cur.getColumnCount();
|
||||||
|
|
||||||
// Build up JSON result object for each row
|
// Build up JSON result object for each row
|
||||||
do {
|
do {
|
||||||
JSONObject row = new JSONObject();
|
JSONObject row = new JSONObject();
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < colCount; ++i) {
|
for (int i = 0; i < colCount; ++i) {
|
||||||
key = cur.getColumnName(i);
|
key = cur.getColumnName(i);
|
||||||
value = cur.getString(i);
|
value = cur.getString(i);
|
||||||
row.put(key, value);
|
row.put(key, value);
|
||||||
}
|
}
|
||||||
fullresult.put(row);
|
fullresult.put(row);
|
||||||
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (cur.moveToNext());
|
} while (cur.moveToNext());
|
||||||
|
|
||||||
result = fullresult.toString();
|
result = fullresult.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let JavaScript know that there are no more rows
|
// Let JavaScript know that there are no more rows
|
||||||
this.sendJavascript("cordova.require('cordova/plugin/android/storage').completeQuery('" + tx_id + "', " + result + ");");
|
this.sendJavascript("cordova.require('cordova/plugin/android/storage').completeQuery('" + tx_id + "', " + result + ");");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user