From 912fe87e74dd3111895f681e103051bb3086574d Mon Sep 17 00:00:00 2001 From: Fernando Date: Mon, 20 Jun 2016 15:59:59 +0200 Subject: [PATCH] Update localstoragedb.js Added two new methods: - Rename table [(String) old_table_name, (String) new_table_name] [Boolean] - Show tables [] => [Array] --- localstoragedb.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/localstoragedb.js b/localstoragedb.js index 9e4a93e..d44d2b1 100644 --- a/localstoragedb.js +++ b/localstoragedb.js @@ -401,6 +401,41 @@ return new_data; } + function renameTable(old_table, new_table){ + + if (old_table == new_table){ + throw "The new table name is the same as the old one"; + } + + if (!db.data.hasOwnProperty(old_table) && !db.tables.hasOwnProperty(old_table)){ + throw "The table doesn't exists on database"; + } + + if (db.data.hasOwnProperty(new_table) || db.tables.hasOwnProperty(new_table)){ + throw "New table name already exists on database"; + } + + db.data[new_table] = db.data[old_table]; + db.tables[new_table] = db.tables[old_table]; + + delete db.data[old_table]; + delete db.tables[old_table]; + + storage.setItem(db_id, JSON.stringify(db)); + + return true; + } + + function showTables() { + var list = []; + for(var table in db.tables) { + if( db.tables.hasOwnProperty(table) ) { + list.push(table); + } + } + return list; + } + // ______________________ public methods return { @@ -419,6 +454,14 @@ drop(); }, + // Rename table + renameTable: function(old_table, new_table){ + return renameTable(old_table, new_table); + }, + + // Added + showTables: showTables, + // serialize the database serialize: function() { return serialize();