From 7bf165371f8630513ea243bbd2baee93cf5f38b8 Mon Sep 17 00:00:00 2001 From: ltdong Date: Fri, 1 Jul 2022 17:58:28 +0800 Subject: [PATCH] Add database backup and recovery, data modification and deletion interfaces Signed-off-by: ltdong --- .../main/js/MainAbility/pages/index/index.css | 20 +++- .../main/js/MainAbility/pages/index/index.hml | 34 +++--- .../main/js/MainAbility/pages/index/index.js | 107 +++++++++++++++++- 3 files changed, 139 insertions(+), 22 deletions(-) diff --git a/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.css b/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.css index f0c8ba2..7ec7eab 100755 --- a/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.css +++ b/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.css @@ -12,7 +12,7 @@ .log_div { font-size: 20px; width: 100%; - height: 60%; + height: 100%; border-width: 0.5px; border-color: black; justify-content: flex-start; @@ -22,13 +22,27 @@ .log_text { font-size: 20px; + overflow: scroll; + width: 100%; + height: 100%; } .btn { width: 50%; height: 80px; - font-size: 30px; - margin-bottom: 3fp; + font-size: 20px; + margin-bottom: 8fp; + margin-top: 2fp; + margin-left: 2fp; + margin-right: 2fp; +} + + +.btn_bottom { + width: 50%; + height: 80px; + font-size: 20px; + margin-bottom: 1fp; margin-top: 2fp; margin-left: 2fp; margin-right: 2fp; diff --git a/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.hml b/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.hml index 5f8a7a4..34f4151 100755 --- a/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.hml +++ b/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.hml @@ -1,25 +1,29 @@
-
- - - +
+
- - - + + + +
- - - + + + +
- - - + + + +
-
- {{ logText }} +
+ + + +
diff --git a/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.js b/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.js index d8fe074..f23244f 100755 --- a/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.js +++ b/rdb/drdb_demo/entry/src/main/js/MainAbility/pages/index/index.js @@ -5,7 +5,21 @@ import ability_featureAbility from '@ohos.ability.featureAbility' let rdbStore let dmInstance = null; -let context =ability_featureAbility.getContext() +let context = ability_featureAbility.getContext() +const BACKUP_STORE_NAME = "distributed_rdb_bak.db" +var nameList = ["Leila", "Ursa", "Crystal", "Rebecca", "Taylor", "Elizabeth", "Ayn", "Rosa", "Beatrice", "Tracy", +"Emily", "Glenn", "Iris", "Sara", "Freddie", "Cindy", "Devin", "Karen", "Zillah", "Ivy", "Xena", "Aimee", "Olivia", +"Elisabeth", "Ulrica", "Chris", "Leslie", "Felicity", "Alessandra", "Cherie", "Sonia", "Elsa", "Natasha", "Candy", +"Kim", "Darleen", "Lillian", "Abigail", "Del", "Serena", "Candice", "Gemma", "Irene", "Rita", "Genevieve", "Nora", +"Ivory", "Niki", "Jacky", "Betty", "Isabelle", "Ray", "Patty", "Cathy", "Tanya", "Alison", "Rachel", "Julia", +"Julie", "Yasmine", "Susan", "Odin", "Emma", "Jessie", "Zoie", "Paula", "Carrie", "Harriet", "Christian", "Hana", +"Charlotte", "Terry", "Leah", "Ximena", "Frederica", "Wing", "Qara", "Elaine", "Silvia", "Ella", "Francesca", +"Janet", "Phoebe", "Holly", "Xochitl", "Urania", "Qatar", "Yolanda", "Athena", "Georgia", "Beth", "Barbie", +"Della", "Isabella", "Wendi", "Ophelia", "Hanna", "Sarah", "Hayden", "Teresa"]; + +function random(min, max) { + return Math.floor(Math.random() * (max - min)) + min; +} export default { data: { @@ -83,8 +97,8 @@ export default { return } const record = { - "name": "Jim", - "age": 20, + "name": nameList[Math.floor(Math.random() * nameList.length)], + "age": random(1, 100), } let promise = rdbStore.insert("employee", record) promise.then((rowId) => { @@ -94,6 +108,73 @@ export default { }) }, + batchInsert: function () { + let i = 0; + while (i < 10) { + this.insert() + i++ + } + }, + + delete: function () { + if (rdbStore == undefined) { + this.showLog("create or open rdb store first") + return + } + let result = rdbStore.querySql("select id from employee order by random() limit 1") + result.then((resultSet) => { + if (resultSet.rowCount == 0) { + this.showLog("No record") + return + } + resultSet.goToFirstRow(); + + let employeeId = resultSet.getLong(0); + let predicates = new rdb.RdbPredicates("employee") + predicates.equalTo("id", employeeId); + let promise = rdbStore.delete(predicates) + promise.then((rowId) => { + this.showLog("delete one record success id = " + employeeId) + }).catch(() => { + this.showLog("delete one record failed id = " + employeeId) + }) + resultSet.close(); + }).catch(() => { + this.showLog("query failed") + }) + }, + + update: function () { + if (rdbStore == undefined) { + this.showLog("create or open rdb store first") + return + } + let result = rdbStore.querySql("select id from employee order by random() limit 1") + result.then((resultSet) => { + if (resultSet.rowCount == 0) { + this.showLog("No record") + return + } + resultSet.goToFirstRow(); + let employeeId = resultSet.getLong(0); + const record = { + "name": nameList[Math.floor(Math.random() * nameList.length)], + "age": random(1, 100), + } + let predicates = new rdb.RdbPredicates("employee") + predicates.equalTo("id", employeeId); + let promise = rdbStore.update(record, predicates) + promise.then((rowId) => { + this.showLog("update one record success id = " + employeeId + ", name = " + record.name + ", age = " + record.age) + }).catch(() => { + this.showLog("update one record failed id = " + employeeId + ", name = " + record.name + ", age = " + record.age) + }) + resultSet.close(); + }).catch(() => { + this.showLog("query failed") + }) + }, + queryByTableName: function(tableName) { let promise = rdbStore.querySql("SELECT * FROM " + tableName) promise.then((resultSet) => { @@ -171,7 +252,25 @@ export default { }) }, - clearData: function() { + backup: function () { + let promiseBackup = rdbStore.backup(BACKUP_STORE_NAME) + promiseBackup.then(() => { + this.showLog("database backup success "); + }).catch((err) => { + this.showLog("backup database failed " + err); + }) + }, + + restore: function () { + let promiseRestore = rdbStore.restore(BACKUP_STORE_NAME) + promiseRestore.then(() => { + this.showLog("database restore success "); + }).catch((err) => { + this.showLog("database restore failed " + err); + }) + }, + + clearData: function () { let predicate = new rdb.RdbPredicates("employee"); let promise = rdbStore.delete(predicate); promise.then((number) => { -- Gitee