-
-
-
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
+
-
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 d8fe0743defccf93b88cccc8bbac3ef401573d85..f23244fe662a267b5fa96aa12804d0edb9b9bbd2 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) => {