diff --git a/ability/ORM/README_zh.md b/ability/ORM/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..17fb278df3f76586bf159666c40ac326e5bf2337 --- /dev/null +++ b/ability/ORM/README_zh.md @@ -0,0 +1,20 @@ +# 对象关系映射数据库 + +### 简介 + +本示例通过注解和继承来实现数据库和表的创建,实现了数据库的升级、备份、删除、恢复,实现了表的增删改查,并监听数据变化。 + +1. @Database注解,且继承了OrmDatabase的类,对应关系型数据库; +2. @Entity注解,且继承了OrmObject的类,对应关系型数据库中的表; +3. OrmContext实现数据库的升级、备份、删除、恢复,表的增删改查并监听数据变化。 + +### 使用说明 + +1. 点击插入按钮插入数据,点击更新按钮更新数据,点击删除按钮删除数据,点击查询按钮查询数据并显示查询结果; +2. 点击升级按钮数据库升级,从版本1升级到3,会从1升级到2再从2升级到3; +3. 点击备份按钮数据库备份,点击删除数据库按钮删除数据库,点击恢复按钮恢复数据库。 + +### 约束与限制 + +本示例支持在标准系统上运行。 + diff --git a/ability/ORM/build.gradle b/ability/ORM/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..91af1ca64995ab365fbf9f4d6b6c9a5f816d163f --- /dev/null +++ b/ability/ORM/build.gradle @@ -0,0 +1,36 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.4.2' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } +} \ No newline at end of file diff --git a/ability/ORM/entry/build.gradle b/ability/ORM/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..0f2a1265debf874411c66f6b18c8747a16b77cf8 --- /dev/null +++ b/ability/ORM/entry/build.gradle @@ -0,0 +1,22 @@ +apply plugin: 'com.huawei.ohos.hap' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } + compileOptions { + annotationEnabled true + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) +} diff --git a/ability/ORM/entry/src/main/config.json b/ability/ORM/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..14a7d62fd6f1c978b0066d2381e1cfea46bfb366 --- /dev/null +++ b/ability/ORM/entry/src/main/config.json @@ -0,0 +1,45 @@ +{ + "app": { + "bundleName": "ohos.samples.orm", + "version": { + "code": 1000000, + "name": "1.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.orm", + "name": ".MyApplication", + "mainAbility": "ohos.samples.orm.MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "name": "ohos.samples.orm.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ] + } +} \ No newline at end of file diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/MainAbility.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..9bac361c8b8cf2fb4e9f466610ec1912d311ae19 --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/MainAbility.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; +import ohos.samples.orm.slice.OrmContextSlice; + +/** + * MainAbility + * + * @since 2021-06-15 + */ +public class MainAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setMainRoute(OrmContextSlice.class.getName()); + } +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/AllDataType.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/AllDataType.java new file mode 100644 index 0000000000000000000000000000000000000000..a80f7557dea66d94a4bb40ca000ddcb0a4e6f395 --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/AllDataType.java @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.Blob; +import ohos.data.orm.Clob; +import ohos.data.orm.OrmObject; +import ohos.data.orm.annotation.Entity; +import ohos.data.orm.annotation.PrimaryKey; + +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; + +/** + * AllDataType + * + * @since 2021-06-15 + */ +@Entity(tableName = "AllDataType") +public class AllDataType extends OrmObject { + @PrimaryKey(autoGenerate = true) + private int id; + + private Integer integerValue; + + private Long longValue; + + private Short shortValue; + + private Boolean booleanValue; + + private Double doubleValue; + + private Float floatValue; + + private String stringValue; + + private Blob blobValue; + + private Clob clobValue; + + private Byte byteValue; + + private Date dateValue; + + private Time timeValue; + + private Timestamp timestampValue; + + private Calendar calendarValue; + + private Character characterValue; + + private int primIntValue; + + private long primLongValue; + + private short primShortValue; + + private float primFloatValue; + + private double primDoubleValue; + + private boolean primBooleanValue; + + private byte primByteValue; + + private char primCharValue; + + private Integer order; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Integer getIntegerValue() { + return integerValue; + } + + public void setIntegerValue(Integer integerValue) { + this.integerValue = integerValue; + } + + public Long getLongValue() { + return longValue; + } + + public void setLongValue(Long longValue) { + this.longValue = longValue; + } + + public Short getShortValue() { + return shortValue; + } + + public void setShortValue(Short shortValue) { + this.shortValue = shortValue; + } + + public Boolean getBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(Boolean booleanValue) { + this.booleanValue = booleanValue; + } + + public Double getDoubleValue() { + return doubleValue; + } + + public void setDoubleValue(Double doubleValue) { + this.doubleValue = doubleValue; + } + + public Float getFloatValue() { + return floatValue; + } + + public void setFloatValue(Float floatValue) { + this.floatValue = floatValue; + } + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(String stringValue) { + this.stringValue = stringValue; + } + + public Blob getBlobValue() { + return blobValue; + } + + public void setBlobValue(Blob blobValue) { + this.blobValue = blobValue; + } + + public Clob getClobValue() { + return clobValue; + } + + public void setClobValue(Clob clobValue) { + this.clobValue = clobValue; + } + + public Byte getByteValue() { + return byteValue; + } + + public void setByteValue(Byte byteValue) { + this.byteValue = byteValue; + } + + public Date getDateValue() { + return dateValue; + } + + public void setDateValue(Date dateValue) { + this.dateValue = dateValue; + } + + public Time getTimeValue() { + return timeValue; + } + + public void setTimeValue(Time timeValue) { + this.timeValue = timeValue; + } + + public Timestamp getTimestampValue() { + return timestampValue; + } + + public void setTimestampValue(Timestamp timestampValue) { + this.timestampValue = timestampValue; + } + + public Calendar getCalendarValue() { + return calendarValue; + } + + public void setCalendarValue(Calendar calendarValue) { + this.calendarValue = calendarValue; + } + + public Character getCharacterValue() { + return characterValue; + } + + public void setCharacterValue(Character characterValue) { + this.characterValue = characterValue; + } + + public int getPrimIntValue() { + return primIntValue; + } + + public void setPrimIntValue(int primIntValue) { + this.primIntValue = primIntValue; + } + + public long getPrimLongValue() { + return primLongValue; + } + + public void setPrimLongValue(long primLongValue) { + this.primLongValue = primLongValue; + } + + public short getPrimShortValue() { + return primShortValue; + } + + public void setPrimShortValue(short primShortValue) { + this.primShortValue = primShortValue; + } + + public float getPrimFloatValue() { + return primFloatValue; + } + + public void setPrimFloatValue(float primFloatValue) { + this.primFloatValue = primFloatValue; + } + + public double getPrimDoubleValue() { + return primDoubleValue; + } + + public void setPrimDoubleValue(double primDoubleValue) { + this.primDoubleValue = primDoubleValue; + } + + public boolean isPrimBooleanValue() { + return primBooleanValue; + } + + public void setPrimBooleanValue(boolean primBooleanValue) { + this.primBooleanValue = primBooleanValue; + } + + public byte getPrimByteValue() { + return primByteValue; + } + + public void setPrimByteValue(byte primByteValue) { + this.primByteValue = primByteValue; + } + + public char getPrimCharValue() { + return primCharValue; + } + + public void setPrimCharValue(char primCharValue) { + this.primCharValue = primCharValue; + } + + public Integer getOrder() { + return order; + } + + public void setOrder(Integer order) { + this.order = order; + } +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/AllDataTypeUpgrade.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/AllDataTypeUpgrade.java new file mode 100644 index 0000000000000000000000000000000000000000..e716bcc58bb78122c8775302d399fa6b852b81dd --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/AllDataTypeUpgrade.java @@ -0,0 +1,276 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.Blob; +import ohos.data.orm.Clob; +import ohos.data.orm.OrmObject; +import ohos.data.orm.annotation.Entity; +import ohos.data.orm.annotation.PrimaryKey; + +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Date; + +/** + * AllDataTypeUpgrade + * + * @since 2021-06-15 + */ +@Entity(tableName = "AllDataType") +public class AllDataTypeUpgrade extends OrmObject { + @PrimaryKey(autoGenerate = true) + private int id; + + private Integer integerValue; + + private Long longValue; + + private Short shortValue; + + private Boolean booleanValue; + + private Double doubleValue; + + private Float floatValue; + + private String stringValue; + + private Blob blobValue; + + private Clob clobValue; + + private Byte byteValue; + + private Date dateValue; + + private Time timeValue; + + private Timestamp timestampValue; + + private Calendar calendarValue; + + private Character characterValue; + + private int primIntValue; + + private long primLongValue; + + private short primShortValue; + + private float primFloatValue; + + private double primDoubleValue; + + private boolean primBooleanValue; + + private byte primByteValue; + + private char primCharValue; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Integer getIntegerValue() { + return integerValue; + } + + public void setIntegerValue(Integer integerValue) { + this.integerValue = integerValue; + } + + public Long getLongValue() { + return longValue; + } + + public void setLongValue(Long longValue) { + this.longValue = longValue; + } + + public Short getShortValue() { + return shortValue; + } + + public void setShortValue(Short shortValue) { + this.shortValue = shortValue; + } + + public Boolean getBooleanValue() { + return booleanValue; + } + + public void setBooleanValue(Boolean booleanValue) { + this.booleanValue = booleanValue; + } + + public Double getDoubleValue() { + return doubleValue; + } + + public void setDoubleValue(Double doubleValue) { + this.doubleValue = doubleValue; + } + + public Float getFloatValue() { + return floatValue; + } + + public void setFloatValue(Float floatValue) { + this.floatValue = floatValue; + } + + public String getStringValue() { + return stringValue; + } + + public void setStringValue(String stringValue) { + this.stringValue = stringValue; + } + + public Blob getBlobValue() { + return blobValue; + } + + public void setBlobValue(Blob blobValue) { + this.blobValue = blobValue; + } + + public Clob getClobValue() { + return clobValue; + } + + public void setClobValue(Clob clobValue) { + this.clobValue = clobValue; + } + + public Byte getByteValue() { + return byteValue; + } + + public void setByteValue(Byte byteValue) { + this.byteValue = byteValue; + } + + public Date getDateValue() { + return dateValue; + } + + public void setDateValue(Date dateValue) { + this.dateValue = dateValue; + } + + public Time getTimeValue() { + return timeValue; + } + + public void setTimeValue(Time timeValue) { + this.timeValue = timeValue; + } + + public Timestamp getTimestampValue() { + return timestampValue; + } + + public void setTimestampValue(Timestamp timestampValue) { + this.timestampValue = timestampValue; + } + + public Calendar getCalendarValue() { + return calendarValue; + } + + public void setCalendarValue(Calendar calendarValue) { + this.calendarValue = calendarValue; + } + + public Character getCharacterValue() { + return characterValue; + } + + public void setCharacterValue(Character characterValue) { + this.characterValue = characterValue; + } + + public int getPrimIntValue() { + return primIntValue; + } + + public void setPrimIntValue(int primIntValue) { + this.primIntValue = primIntValue; + } + + public long getPrimLongValue() { + return primLongValue; + } + + public void setPrimLongValue(long primLongValue) { + this.primLongValue = primLongValue; + } + + public short getPrimShortValue() { + return primShortValue; + } + + public void setPrimShortValue(short primShortValue) { + this.primShortValue = primShortValue; + } + + public float getPrimFloatValue() { + return primFloatValue; + } + + public void setPrimFloatValue(float primFloatValue) { + this.primFloatValue = primFloatValue; + } + + public double getPrimDoubleValue() { + return primDoubleValue; + } + + public void setPrimDoubleValue(double primDoubleValue) { + this.primDoubleValue = primDoubleValue; + } + + public boolean isPrimBooleanValue() { + return primBooleanValue; + } + + public void setPrimBooleanValue(boolean primBooleanValue) { + this.primBooleanValue = primBooleanValue; + } + + public byte getPrimByteValue() { + return primByteValue; + } + + public void setPrimByteValue(byte primByteValue) { + this.primByteValue = primByteValue; + } + + public char getPrimCharValue() { + return primCharValue; + } + + public void setPrimCharValue(char primCharValue) { + this.primCharValue = primCharValue; + } +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/Book.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/Book.java new file mode 100644 index 0000000000000000000000000000000000000000..1b08d58da8a1cdf64b9a3af2d0b1478ca1cc5ded --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/Book.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.OrmObject; +import ohos.data.orm.annotation.Column; +import ohos.data.orm.annotation.Entity; +import ohos.data.orm.annotation.ForeignKey; +import ohos.data.orm.annotation.PrimaryKey; + +import static ohos.data.orm.annotation.ForeignKey.CASCADE; + +/** + * Book + * + * @since 2021-06-15 + */ +@Entity(tableName = "Book", foreignKeys = { + @ForeignKey(name = "BookUser", parentEntity = User.class, parentColumns = "userId", childColumns = "user_id", + onDelete = CASCADE)}) +public class Book extends OrmObject { + @PrimaryKey(autoGenerate = true) + private int id; + + @Column(name = "Name", index = true) + private String name; + + @Column(name = "user_id") + private int userId; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookStore.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookStore.java new file mode 100644 index 0000000000000000000000000000000000000000..2a677bf0f60822c9177104dc92bff82f135ef2b1 --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookStore.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.OrmDatabase; +import ohos.data.orm.annotation.Database; + +/** + * BookStore + * + * @since 2021-06-15 + */ +@Database(entities = {User.class, Book.class, AllDataType.class}, version = 1) +public abstract class BookStore extends OrmDatabase { +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookStoreUpgrade.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookStoreUpgrade.java new file mode 100644 index 0000000000000000000000000000000000000000..bb81baa2e2889d57a36dc94df782943bf0ba3928 --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookStoreUpgrade.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.OrmDatabase; +import ohos.data.orm.annotation.Database; + +/** + * BookStoreUpgrade + * + * @since 2021-06-15 + */ +@Database(entities = {UserUpgrade.class, BookUpgrade.class, AllDataTypeUpgrade.class}, version = 3) +public abstract class BookStoreUpgrade extends OrmDatabase { +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookUpgrade.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookUpgrade.java new file mode 100644 index 0000000000000000000000000000000000000000..0cdd27b90c5cb4c63ffdec50947ea4b165ffb37f --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/BookUpgrade.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.OrmObject; +import ohos.data.orm.annotation.Column; +import ohos.data.orm.annotation.Entity; +import ohos.data.orm.annotation.ForeignKey; +import ohos.data.orm.annotation.PrimaryKey; + +import static ohos.data.orm.annotation.ForeignKey.CASCADE; + +/** + * BookUpgrade + * + * @since 2021-06-15 + */ +@Entity(tableName = "BookUpgrade", foreignKeys = { + @ForeignKey(name = "BookUser", parentEntity = UserUpgrade.class, + parentColumns = "userId", childColumns = "user_id", + onDelete = CASCADE)}) +public class BookUpgrade extends OrmObject { + @PrimaryKey(autoGenerate = true) + private Integer id; + + @Column(name = "Name", index = true) + private String name; + + @Column(name = "user_id") + private int userId; + + @Column(name = "addColumn12") + private int addColumn12; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public void setAddColumn12(int addColumn12) { + this.addColumn12 = addColumn12; + } + + public int getAddColumn12() { + return this.addColumn12; + } +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/User.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/User.java new file mode 100644 index 0000000000000000000000000000000000000000..03fa2ef5e78cec1a42284b2d9ee9b45264ad56f8 --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/User.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.OrmObject; +import ohos.data.orm.annotation.Entity; +import ohos.data.orm.annotation.Index; +import ohos.data.orm.annotation.PrimaryKey; + +/** + * User + * + * @since 2021-06-15 + */ +@Entity(tableName = "user", ignoredColumns = "ignoreColumn", + indices = {@Index(value = {"firstName", "lastName"}, name = "name_index")}) +public class User extends OrmObject { + @PrimaryKey(autoGenerate = true) + private Integer userId; + + private String firstName; + + private String lastName; + + private int age; + + private double balance; + + private int ignoreColumn; + + private long useTimestamp; + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer id) { + this.userId = id; + } + + public String getFirstName() { + return this.firstName; + } + + public void setFirstName(String name) { + this.firstName = name; + } + + public String getLastName() { + return this.lastName; + } + + public void setLastName(String name) { + this.lastName = name; + } + + public int getAge() { + return age; + } + + public void setAge(int mAge) { + this.age = mAge; + } + + public double getBalance() { + return balance; + } + + public void setBalance(double balance) { + this.balance = balance; + } + + public int getIgnoreColumn() { + return ignoreColumn; + } + + public void setIgnoreColumn(int ignoreColumn) { + this.ignoreColumn = ignoreColumn; + } + + public long getUseTimestamp() { + return useTimestamp; + } + + public void setUseTimestamp(long useTimestamp) { + this.useTimestamp = useTimestamp; + } +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/model/UserUpgrade.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/UserUpgrade.java new file mode 100644 index 0000000000000000000000000000000000000000..743b6af03c460f4b7a253f5119d47f37185f8236 --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/model/UserUpgrade.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.model; + +import ohos.data.orm.OrmObject; +import ohos.data.orm.annotation.Entity; +import ohos.data.orm.annotation.Index; +import ohos.data.orm.annotation.PrimaryKey; + +/** + * UserUpgrade + * + * @since 2021-06-15 + */ +@Entity(tableName = "user", ignoredColumns = "ignoreColumn", + indices = {@Index(value = {"firstName", "lastName"}, name = "name_index")}) +public class UserUpgrade extends OrmObject { + @PrimaryKey(autoGenerate = true) + private int userId; + + private String firstName; + + private String lastName; + + private int age; + + private double balance; + + private int ignoreColumn; + + private long useTimestamp; + + public int getUserId() { + return userId; + } + + public void setUserId(int id) { + this.userId = id; + } + + public String getFirstName() { + return this.firstName; + } + + public void setFirstName(String name) { + this.firstName = name; + } + + public String getLastName() { + return this.lastName; + } + + public void setLastName(String name) { + this.lastName = name; + } + + public int getAge() { + return age; + } + + public void setAge(int mAge) { + this.age = mAge; + } + + public double getBalance() { + return balance; + } + + public void setBalance(double balance) { + this.balance = balance; + } + + public int getIgnoreColumn() { + return ignoreColumn; + } + + public void setIgnoreColumn(int ignoreColumn) { + this.ignoreColumn = ignoreColumn; + } + + public long getUseTimestamp() { + return useTimestamp; + } + + public void setUseTimestamp(long useTimestamp) { + this.useTimestamp = useTimestamp; + } +} diff --git a/ability/ORM/entry/src/main/java/ohos/samples/orm/slice/OrmContextSlice.java b/ability/ORM/entry/src/main/java/ohos/samples/orm/slice/OrmContextSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..f65dacdd7d5d0bc0763c7d0460b2e64cf643e435 --- /dev/null +++ b/ability/ORM/entry/src/main/java/ohos/samples/orm/slice/OrmContextSlice.java @@ -0,0 +1,315 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.orm.slice; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Component; +import ohos.agp.components.Text; +import ohos.agp.window.dialog.ToastDialog; +import ohos.data.DatabaseHelper; +import ohos.data.orm.OrmContext; +import ohos.data.orm.OrmMigration; +import ohos.data.orm.OrmObjectObserver; +import ohos.data.orm.OrmPredicates; +import ohos.data.rdb.RdbStore; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.samples.orm.ResourceTable; +import ohos.samples.orm.model.BookStore; +import ohos.samples.orm.model.BookStoreUpgrade; +import ohos.samples.orm.model.User; +import ohos.samples.orm.model.UserUpgrade; +import ohos.samples.orm.model.BookUpgrade; + +import java.security.SecureRandom; +import java.util.List; + +/** + * OrmContextSlice + * + * @since 2021-06-15 + */ +public class OrmContextSlice extends AbilitySlice { + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, "OrmContextSlice"); + + private Text logText; + + private DatabaseHelper helper; + + private Component upgradeButton; + + @Override + protected void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_ability_main); + + initComponents(); + initRegisters(); + } + + private void initComponents() { + Component componentText = findComponentById(ResourceTable.Id_log_text); + if (componentText instanceof Text) { + logText = (Text) componentText; + } + upgradeButton = findComponentById(ResourceTable.Id_upgrade_button); + upgradeButton.setClickedListener(this::upgrade); + findComponentById(ResourceTable.Id_insert_button).setClickedListener(this::insert); + findComponentById(ResourceTable.Id_update_button).setClickedListener(this::update); + findComponentById(ResourceTable.Id_delete_button).setClickedListener(this::delete); + findComponentById(ResourceTable.Id_query_button).setClickedListener(this::query); + findComponentById(ResourceTable.Id_backupDB_button).setClickedListener(this::backup); + findComponentById(ResourceTable.Id_deleteDB_button).setClickedListener(this::deleteRdbStore); + findComponentById(ResourceTable.Id_restoreDB_button).setClickedListener(this::restore); + helper = new DatabaseHelper(this); + } + + @Override + protected void onStop() { + super.onStop(); + unRegisters(); + } + + private void initRegisters() { + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + ormContext.registerEntityObserver("user", entityOrmObjectObserver); + ormContext.registerStoreObserver("OrmTestDB", storeOrmObjectObserver); + ormContext.close(); + } + + private void unRegisters() { + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + ormContext.unregisterEntityObserver("user", entityOrmObjectObserver); + ormContext.unregisterStoreObserver("OrmTestDB", storeOrmObjectObserver); + ormContext.unregisterContextObserver(ormContext, contextOrmObjectObserver); + ormContext.close(); + } + + private void insert(Component component) { + User user = new User(); + user.setFirstName(getRandomFirstName()); + user.setLastName("San"); + user.setAge(29); + user.setBalance(100.51); + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + if (ormContext.insert(user)) { + logText.setText("insert success"); + } else { + logText.setText("insert fail"); + } + ormContext.registerContextObserver(ormContext, contextOrmObjectObserver); + ormContext.flush(); + ormContext.close(); + } + + private void delete(Component component) { + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + OrmPredicates predicates = ormContext.where(User.class); + predicates.equalTo("age", 29); + List users = ormContext.query(predicates); + if (users.size() == 0) { + logText.setText("no data not delete"); + return; + } + User user = users.get(0); + if (ormContext.delete(user)) { + logText.setText("delete success"); + } else { + logText.setText("delete fail"); + } + ormContext.flush(); + ormContext.close(); + } + + private void update(Component component) { + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + OrmPredicates predicates = ormContext.where(User.class); + predicates.equalTo("age", 29); + List users = ormContext.query(predicates); + if (users.size() == 0) { + new ToastDialog(this).setText("no data not update").show(); + return; + } + User user = users.get(0); + ormContext.registerObjectObserver(user, objectOrmObjectObserver); + user.setFirstName("Li"); + if (ormContext.update(user)) { + logText.setText("update success"); + } else { + logText.setText("update fail"); + } + ormContext.flush(); + ormContext.close(); + ormContext.unregisterObjectObserver(user, objectOrmObjectObserver); + } + + private void query(Component component) { + logText.setText(""); + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + OrmPredicates query = ormContext.where(User.class).equalTo("lastName", "San"); + List users = ormContext.query(query); + ormContext.flush(); + ormContext.close(); + if (users.size() == 0) { + logText.append("lastName为San:无"); + return; + } + for (User user : users) { + logText.append("lastName为San:" + user.getFirstName() + user.getLastName() + " "); + } + } + + private void restore(Component component) { + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + if (ormContext.restore("OrmTestDBBackup.db")) { + logText.setText("restoreDB success"); + } else { + logText.setText("restoreDB fail"); + } + ormContext.flush(); + ormContext.close(); + } + + private void deleteRdbStore(Component component) { + if (helper.deleteRdbStore("OrmTestDB.db")) { + logText.setText("deleteDB success"); + } else { + logText.setText("deleteDB fail"); + } + } + + private void backup(Component component) { + OrmContext ormContext = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStore.class); + if (ormContext.backup("OrmTestDBBackup.db")) { + HiLog.info(LABEL_LOG, "Path: " + getDatabaseDir()); + logText.setText("backup success"); + } else { + logText.setText("backup fail"); + } + ormContext.flush(); + ormContext.close(); + } + + private void upgrade(Component component) { + logText.setText(""); + testOrmMigration(); + } + + /** + * test Orm upgrade + */ + public void testOrmMigration() { + OrmContext context = helper.getOrmContext("OrmTestDB", "OrmTestDB.db", BookStoreUpgrade.class, + new TestOrmMigration32(), + new TestOrmMigration23(), + new TestOrmMigration12(), + new TestOrmMigration21()); + + UserUpgrade userUpgrade = new UserUpgrade(); + userUpgrade.setAge(41); + userUpgrade.setBalance(3.44); + boolean isSuccess = context.insert(userUpgrade); + HiLog.info(LABEL_LOG,"UserUpgrade insert " + isSuccess); + BookUpgrade bookUpgrade = new BookUpgrade(); + bookUpgrade.setId(101); + bookUpgrade.setAddColumn12(8); + bookUpgrade.setName("OrmTestDBBook"); + isSuccess = context.insert(bookUpgrade); + HiLog.info(LABEL_LOG,"BookUpgrade insert " + isSuccess); + context.flush(); + OrmPredicates predicates = context.where(BookUpgrade.class).equalTo("name", "OrmTestDBBook"); + List bookUpgradeList = context.query(predicates); + int id = bookUpgradeList.get(0).getId(); + HiLog.info(LABEL_LOG,"bookUpgradeList.get(0).getId() =" + id); + context.close(); + upgradeButton.setClickable(false); + logText.setText("upgrade success"); + } + + private static class TestOrmMigration12 extends OrmMigration { + public TestOrmMigration12() { + super(1, 2); + } + + @Override + public void onMigrate(RdbStore store) { + HiLog.info(LABEL_LOG, "DataBase Version 1->2 onMigrate called"); + store.executeSql("ALTER TABLE `Book` ADD COLUMN `addColumn12` INTEGER"); + } + } + + private static class TestOrmMigration21 extends OrmMigration { + public TestOrmMigration21() { + super(2, 1); + } + + @Override + public void onMigrate(RdbStore store) { + HiLog.info(LABEL_LOG, "DataBase Version 2->1 onMigrate called"); + store.executeSql("DROP TABLE IF EXISTS `Book`"); + store.executeSql("CREATE TABLE IF NOT EXISTS `Book` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT , " + + "`user_id` INTEGER NOT NULL, `useTimestamp` INTEGER, FOREIGN KEY (`user_id`) " + + "REFERENCES `user` (`userId`) ON UPDATE NO ACTION ON DELETE CASCADE)"); + store.executeSql("CREATE INDEX `index_Book_name` ON `Book` (`name`)"); + } + } + + private static class TestOrmMigration23 extends OrmMigration { + public TestOrmMigration23() { + super(2, 3); + } + + @Override + public void onMigrate(RdbStore store) { + HiLog.info(LABEL_LOG, "DataBase Version 2->3 onMigrate called"); + store.executeSql("ALTER TABLE `Book` RENAME TO `BookUpgrade`"); + } + } + + private static class TestOrmMigration32 extends OrmMigration { + public TestOrmMigration32() { + super(3, 2); + } + + @Override + public void onMigrate(RdbStore store) { + HiLog.info(LABEL_LOG, "DataBase Version 3->2 onMigrate called"); + store.executeSql("ALTER TABLE `BookUpgrade` RENAME TO `Book`"); + } + } + + private final OrmObjectObserver entityOrmObjectObserver = (changeContext, subAllChange) -> + HiLog.info(LABEL_LOG, "onChange Entity " + + " ,insert row=" + subAllChange.getInsertedList().size() + + " ,delete row=" + subAllChange.getDeletedList().size() + + " ,update row=" + subAllChange.getUpdatedList().size()); + + private final OrmObjectObserver storeOrmObjectObserver = (changeContext, subAllChange) -> + HiLog.info(LABEL_LOG, "onChange Store"); + + private final OrmObjectObserver contextOrmObjectObserver = (changeContext, subAllChange) -> + HiLog.info(LABEL_LOG, "onChange Context"); + + private final OrmObjectObserver objectOrmObjectObserver = (changeContext, subAllChange) -> + HiLog.info(LABEL_LOG, "onChange Object"); + + private String getRandomFirstName() { + String[] names = {"Zhang", "Ma", "Li", "Zhao", "Sun", "Guo"}; + int index = new SecureRandom().nextInt(names.length); + return names[index]; + } +} diff --git a/ability/ORM/entry/src/main/resources/base/element/color.json b/ability/ORM/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..146ce436389362fa21935299dea039c33ea1998c --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/element/color.json @@ -0,0 +1,16 @@ +{ + "color": [ + { + "name": "color_button_blue_empty", + "value": "#0000FF" + }, + { + "name": "color_button_blue_pressed", + "value": "#0000CC" + }, + { + "name": "color_button_blue_text", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/element/float.json b/ability/ORM/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..39d19dd54faa22f3ef074e8f1dcc9837390636ff --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/element/float.json @@ -0,0 +1,44 @@ +{ + "float": [ + { + "name": "button_height", + "value": "40vp" + }, + { + "name": "button_width", + "value": "280vp" + }, + { + "name": "button_margin", + "value": "10vp" + }, + { + "name": "button_text_size", + "value": "18fp" + }, + { + "name": "dialog_padding", + "value": "20vp" + }, + { + "name": "dialog_content_padding", + "value": "10vp" + }, + { + "name": "title_text_size", + "value": "18fp" + }, + { + "name": "content_text_size", + "value": "16fp" + }, + { + "name": "dialog_button_height", + "value": "40vp" + }, + { + "name": "dialog_check_box_size", + "value": "20vp" + } + ] +} \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/element/pattern.json b/ability/ORM/entry/src/main/resources/base/element/pattern.json new file mode 100644 index 0000000000000000000000000000000000000000..8917cc7110d853dfa9141805d68cd7f7664596fd --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/element/pattern.json @@ -0,0 +1,39 @@ +{ + "pattern": [ + { + "name": "button_base", + "value": [ + { + "name": "width", + "value": "$float:button_width" + }, + { + "name": "height", + "value": "$float:button_height" + }, + { + "name": "text_size", + "value": "$float:button_text_size" + }, + { + "name": "margin", + "value": "$float:button_margin" + } + ] + }, + { + "name": "button_blue", + "parent": "button_base", + "value": [ + { + "name": "background_element", + "value": "$graphic:button_blue" + }, + { + "name": "text_color", + "value": "$color:color_button_blue_text" + } + ] + } + ] +} \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/element/string.json b/ability/ORM/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..e81e57beb09d2ccb58570ea4172827455a4f0cf4 --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/element/string.json @@ -0,0 +1,44 @@ +{ + "string": [ + { + "name": "app_name", + "value": "ORM" + }, + { + "name": "mainability_description", + "value": "ORM Ability" + }, + { + "name": "insert", + "value": "insert" + }, + { + "name": "update", + "value": "update" + }, + { + "name": "delete", + "value": "delete" + }, + { + "name": "query", + "value": "query" + }, + { + "name": "upgrade", + "value": "upgrade" + }, + { + "name": "backupDB", + "value": "backup" + }, + { + "name": "deleteDB", + "value": "deleteDB" + }, + { + "name": "restoreDB", + "value": "restore" + } + ] +} \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/graphic/button_blue.xml b/ability/ORM/entry/src/main/resources/base/graphic/button_blue.xml new file mode 100644 index 0000000000000000000000000000000000000000..c7ac8a58fa538cb49c2002099764577c5465bfe3 --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/graphic/button_blue.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/graphic/button_blue_empty.xml b/ability/ORM/entry/src/main/resources/base/graphic/button_blue_empty.xml new file mode 100644 index 0000000000000000000000000000000000000000..7d0ecf30a505dffd62934daa194486c7de10e3f1 --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/graphic/button_blue_empty.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/graphic/button_blue_pressed.xml b/ability/ORM/entry/src/main/resources/base/graphic/button_blue_pressed.xml new file mode 100644 index 0000000000000000000000000000000000000000..1d11532e05a71249462cc31a5731bfd43f7ebe91 --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/graphic/button_blue_pressed.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/graphic/text_background.xml b/ability/ORM/entry/src/main/resources/base/graphic/text_background.xml new file mode 100644 index 0000000000000000000000000000000000000000..7efd825c8d028583dba965ef43b0974f3f9714c0 --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/graphic/text_background.xml @@ -0,0 +1,29 @@ + + + + + + + + + + \ No newline at end of file diff --git a/ability/ORM/entry/src/main/resources/base/layout/ability_main.xml b/ability/ORM/entry/src/main/resources/base/layout/ability_main.xml new file mode 100644 index 0000000000000000000000000000000000000000..61f2af87bf385c69a7378b2f50d021c0cc891ead --- /dev/null +++ b/ability/ORM/entry/src/main/resources/base/layout/ability_main.xml @@ -0,0 +1,152 @@ + + + + + + + + +