uk.co.workingedge.cordova.plugin.sqliteporter
Advanced tools
Comparing version 0.1.1 to 0.1.3
{ | ||
"version": "0.1.1", | ||
"version": "0.1.3", | ||
"name": "uk.co.workingedge.cordova.plugin.sqliteporter", | ||
@@ -47,9 +47,3 @@ "cordova_name": "sqlite porter", | ||
"export" | ||
], | ||
"engines": [ | ||
{ | ||
"name": "cordova", | ||
"version": ">=3.0.0" | ||
} | ||
] | ||
} |
@@ -1,2 +0,2 @@ | ||
SQLite Porter Cordova/Phonegap Plugin | ||
SQLite Porter Cordova/Phonegap Plugin [![Latest Stable Version](https://img.shields.io/npm/v/uk.co.workingedge.cordova.plugin.sqliteporter.svg)](https://www.npmjs.com/package/uk.co.workingedge.cordova.plugin.sqliteporter) [![Total Downloads](https://img.shields.io/npm/dt/uk.co.workingedge.cordova.plugin.sqliteporter.svg)](https://npm-stat.com/charts.html?package=uk.co.workingedge.cordova.plugin.sqliteporter) | ||
================================= | ||
@@ -106,3 +106,3 @@ | ||
var progressFn = function(current, total){ | ||
console.log("Imported "+current+"/"+total+" statements"; | ||
console.log("Imported "+current+"/"+total+" statements"); | ||
}; | ||
@@ -151,2 +151,3 @@ cordova.plugins.sqlitePorter.importSqlToDb(db, sql, { | ||
- {boolean} dataOnly - if true, only row data will be exported. Otherwise, table structure will also be exported. Defaults to false. | ||
- {boolean} structureOnly - if true, only table structure will be exported. Otherwise, row will also be exported. Defaults to false. | ||
@@ -290,2 +291,3 @@ ### Example usage | ||
- {boolean} dataOnly - if true, only row data will be exported. Otherwise, table structure will also be exported. Defaults to false. | ||
- {boolean} structureOnly - if true, only table structure will be exported. Otherwise, row will also be exported. Defaults to false. | ||
@@ -307,3 +309,3 @@ ### Example usage | ||
cordova.plugins.sqlitePorter.wipeData(db, opts); | ||
cordova.plugins.sqlitePorter.wipeDb(db, opts); | ||
@@ -334,3 +336,3 @@ ### Parameters | ||
}; | ||
cordova.plugins.sqlitePorter.wipeData(db, { | ||
cordova.plugins.sqlitePorter.wipeDb(db, { | ||
successFn: successFn, | ||
@@ -337,0 +339,0 @@ errorFn: errorFn, |
@@ -132,2 +132,3 @@ /** | ||
* <li>{boolean} dataOnly - if true, only row data will be exported. Otherwise, table structure will also be exported. Defaults to false.</li> | ||
* <li>{boolean} structureOnly - if true, only table structure will be exported. Otherwise, row will also be exported. Defaults to false.</li> | ||
*/ | ||
@@ -139,7 +140,7 @@ sqlitePorter.exportDbToSql = function (db, opts){ | ||
var exportTables = function (tables) { | ||
if (tables.n < tables.sqlTables.length) { | ||
if (tables.n < tables.sqlTables.length && !opts.structureOnly) { | ||
db.transaction( | ||
function (tx) { | ||
var tableName = tables.sqlTables[tables.n], | ||
sqlStatement = "SELECT * FROM " + tableName; | ||
var tableName = sqlUnescape(tables.sqlTables[tables.n]), | ||
sqlStatement = "SELECT * FROM " + sqlEscape(tableName); | ||
tx.executeSql(sqlStatement, [], | ||
@@ -153,6 +154,6 @@ function (tx, rslt) { | ||
for (col in dataRow) { | ||
_fields.push(col); | ||
_fields.push(sqlEscape(col)); | ||
_values.push("'" + sanitiseForSql(dataRow[col]) + "'"); | ||
} | ||
exportSQL += "INSERT OR REPLACE INTO " + tableName + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ")" + separator; | ||
exportSQL += "INSERT OR REPLACE INTO " + sqlEscape(tableName) + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ")" + separator; | ||
statementCount++; | ||
@@ -181,4 +182,4 @@ } | ||
if (row.sql != null && row.sql.indexOf("CREATE TABLE") != -1 && row.sql.indexOf("__") == -1) { | ||
var tableName = trimWhitespace(trimWhitespace(row.sql.replace("CREATE TABLE", "")).split(/ |\(/)[0]); | ||
sqlStatements.push("DROP TABLE IF EXISTS " + tableName); | ||
var tableName = sqlUnescape(trimWhitespace(trimWhitespace(row.sql.replace("CREATE TABLE", "")).split(/ |\(/)[0])); | ||
sqlStatements.push("DROP TABLE IF EXISTS " + sqlEscape(tableName)); | ||
} | ||
@@ -202,4 +203,5 @@ if(row.sql != null && row.sql.indexOf("__") == -1){ | ||
for (var k = 0; k < res.rows.length; k++) { | ||
if (res.rows.item(k).tbl_name.indexOf("__") == -1) { | ||
sqlTables.push(res.rows.item(k).tbl_name); | ||
var tableName = res.rows.item(k).tbl_name; | ||
if (tableName.indexOf("__") == -1 && !isReservedTable(tableName)) { | ||
sqlTables.push(tableName); | ||
} | ||
@@ -230,2 +232,3 @@ } | ||
* <li>{boolean} dataOnly - if true, only row data will be exported. Otherwise, table structure will also be exported. Defaults to false.</li> | ||
* <li>{boolean} structureOnly - if true, only table structure will be exported. Otherwise, row will also be exported. Defaults to false.</li> | ||
*/ | ||
@@ -237,7 +240,7 @@ sqlitePorter.exportDbToJson = function (db, opts){ | ||
var exportTables = function (tables) { | ||
if (tables.n < tables.sqlTables.length) { | ||
if (tables.n < tables.sqlTables.length && !opts.structureOnly) { | ||
db.transaction( | ||
function (tx) { | ||
var tableName = tables.sqlTables[tables.n], | ||
sqlStatement = "SELECT * FROM " + tableName; | ||
var tableName = sqlUnescape(tables.sqlTables[tables.n]), | ||
sqlStatement = "SELECT * FROM " + sqlEscape(tableName); | ||
tx.executeSql(sqlStatement, [], | ||
@@ -284,6 +287,8 @@ function (tx, rslt) { | ||
if (row.sql.indexOf("CREATE TABLE") != -1){ | ||
var tableName = trimWhitespace(trimWhitespace(row.sql.replace("CREATE TABLE", "")).split(/ |\(/)[0]), | ||
tableStructure = trimWhitespace(row.sql.replace("CREATE TABLE " + tableName, "")); | ||
json.structure.tables[tableName] = tableStructure.replace(/\s+/g," "); | ||
statementCount+=2; // One for DROP, one for create | ||
var tableName = sqlUnescape(trimWhitespace(trimWhitespace(row.sql.replace("CREATE TABLE", "")).split(/ |\(/)[0])); | ||
if(!isReservedTable(tableName)){ | ||
var tableStructure = trimWhitespace(row.sql.replace("CREATE TABLE " + sqlEscape(tableName), "")); | ||
json.structure.tables[tableName] = tableStructure.replace(/\s+/g," "); | ||
statementCount+=2; // One for DROP, one for create | ||
} | ||
}else{ | ||
@@ -359,4 +364,4 @@ json.structure.otherSQL.push(row.sql.replace(/\s+/g," ")); | ||
for(var tableName in json.structure.tables){ | ||
mainSql += "DROP TABLE IF EXISTS " + tableName + separator | ||
+ "CREATE TABLE " + tableName + json.structure.tables[tableName] + separator; | ||
mainSql += "DROP TABLE IF EXISTS " + sqlEscape(tableName) + separator | ||
+ "CREATE TABLE " + sqlEscape(tableName) + json.structure.tables[tableName] + separator; | ||
} | ||
@@ -392,5 +397,9 @@ for(var i=0; i<json.structure.otherSQL.length; i++){ | ||
if(_count === 0){ | ||
mainSql += "INSERT OR REPLACE INTO " + tableName + " SELECT"; | ||
mainSql += "INSERT OR REPLACE INTO " + sqlEscape(tableName) + " SELECT"; | ||
for(var j=0; j<_fields.length; j++){ | ||
mainSql += " '"+_values[j]+"' AS '"+_fields[j]+"'"; | ||
if(_values[j]=='null'){ | ||
mainSql += " " + _values[j] + " AS '" + _fields[j] + "'"; | ||
}else { | ||
mainSql += " '" + _values[j] + "' AS '" + _fields[j] + "'"; | ||
} | ||
if(j < _fields.length-1){ | ||
@@ -403,3 +412,7 @@ mainSql += ","; | ||
for(var j=0; j<_values.length; j++){ | ||
mainSql += " '"+_values[j]+"'"; | ||
if(_values[j]=='null'){ | ||
mainSql += " "+_values[j]; | ||
}else { | ||
mainSql += " '"+_values[j]+"'"; | ||
} | ||
if(j < _values.length-1){ | ||
@@ -421,3 +434,3 @@ mainSql += ","; | ||
_row = json.data.deletes[tableName][i]; | ||
mainSql += "DELETE FROM " + tableName; | ||
mainSql += "DELETE FROM " + sqlEscape(tableName); | ||
for(var col in _row){ | ||
@@ -437,3 +450,3 @@ mainSql += (_count === 0 ? " WHERE " : " AND ") + col + "='"+sanitiseForSql(_row[col])+"'"; | ||
var _row = json.data.updates[tableName][i]; | ||
mainSql += "UPDATE " + tableName; | ||
mainSql += "UPDATE " + sqlEscape(tableName); | ||
@@ -522,4 +535,6 @@ _count = 0; | ||
if (row.sql != null && row.sql.indexOf("CREATE TABLE") != -1 && row.sql.indexOf("__") == -1) { | ||
var tableName = trimWhitespace(trimWhitespace(row.sql.replace("CREATE TABLE", "")).split(/ |\(/)[0]); | ||
dropStatements.push("DROP TABLE IF EXISTS " + tableName); | ||
var tableName = sqlUnescape(trimWhitespace(trimWhitespace(row.sql.replace("CREATE TABLE", "")).split(/ |\(/)[0])); | ||
if(!isReservedTable(tableName)){ | ||
dropStatements.push("DROP TABLE IF EXISTS " + sqlEscape(tableName)); | ||
} | ||
} | ||
@@ -560,2 +575,27 @@ } | ||
/** | ||
* Escapes the given value if it contains special characters by wrapping it with back-ticks: value => `value`. | ||
* @param {string} value - unescaped value | ||
* @return {string} escaped value | ||
*/ | ||
function sqlEscape(value){ | ||
if(value.match(/[_-]+/)){ | ||
value = '`'+value+'`'; | ||
} | ||
return value; | ||
} | ||
/** | ||
* Unescapes the given value if it's escaped with back-ticks: `value` => value. | ||
* @param {string} value - unescaped value | ||
* @return {string} escaped value | ||
*/ | ||
function sqlUnescape(value){ | ||
var matchesEscaped = value.match(/`([^`]+)`/); | ||
if(matchesEscaped){ | ||
value = matchesEscaped[1]; | ||
} | ||
return value; | ||
} | ||
/** | ||
* Applies properties to the 1st object specified from the 2nd, 3rd, 4th, etc. | ||
@@ -573,3 +613,12 @@ * Emulates jQuery's $.extend() | ||
/** | ||
* Indicates if given table name is a reserved SQLite meta-table. | ||
* @param {string} tableName - name of table to check | ||
* @returns {boolean} true if table is a reserved SQLite table | ||
*/ | ||
function isReservedTable(tableName){ | ||
return !!tableName.match(/^sqlite_/); | ||
} | ||
module.exports = sqlitePorter; | ||
}()); | ||
}()); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47649
562
401