10fbfc30aSopenharmony_ci/**
20fbfc30aSopenharmony_ci * @file Describe the file
30fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
40fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
50fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License.
60fbfc30aSopenharmony_ci * You may obtain a copy of the License at
70fbfc30aSopenharmony_ci *
80fbfc30aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
90fbfc30aSopenharmony_ci *
100fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
110fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
120fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and
140fbfc30aSopenharmony_ci * limitations under the License.
150fbfc30aSopenharmony_ci */
160fbfc30aSopenharmony_ci
170fbfc30aSopenharmony_ciimport ohosDataRdb from '@ohos.data.relationalStore';
180fbfc30aSopenharmony_ciimport { EventColumns } from '@ohos/datastructure/src/main/ets/events/EventColumns';
190fbfc30aSopenharmony_ciimport { InstancesColumns } from '@ohos/datastructure/src/main/ets/instances/InstancesColumns';
200fbfc30aSopenharmony_ciimport { CalendarsColumns } from '@ohos/datastructure/src/main/ets/calendars/CalendarsColumns';
210fbfc30aSopenharmony_ciimport { ColorsColumns } from '@ohos/datastructure/src/main/ets/colors/ColorsColumns';
220fbfc30aSopenharmony_ciimport { RemindersColumns } from '@ohos/datastructure/src/main/ets/reminders/RemindersColumns';
230fbfc30aSopenharmony_ciimport { AttendeesColumns } from '@ohos/datastructure/src/main/ets/attendees/AttendeesColumns';
240fbfc30aSopenharmony_ciimport { CalendarAlertsColumns } from '@ohos/datastructure/src/main/ets/calendaralerts/CalendarAlertsColumns';
250fbfc30aSopenharmony_ciimport { CalendarCacheColumns } from '@ohos/datastructure/src/main/ets/calendarcache/CalendarCacheColumns';
260fbfc30aSopenharmony_ciimport { EventsRawTimesColumns } from '@ohos/datastructure/src/main/ets/eventsrawtimes/EventsRawTimesColumns';
270fbfc30aSopenharmony_ciimport {
280fbfc30aSopenharmony_ci  ExtendCalendarEventColumns
290fbfc30aSopenharmony_ci} from '@ohos/datastructure/src/main/ets/extendcalendarevent/ExtendCalendarEventColumns';
300fbfc30aSopenharmony_ciimport {
310fbfc30aSopenharmony_ci  ExtendedPropertiesColumns
320fbfc30aSopenharmony_ci} from '@ohos/datastructure/src/main/ets/extendedproperties/ExtendedPropertiesColumns';
330fbfc30aSopenharmony_ciimport { SyncStateColumns } from '@ohos/datastructure/src/main/ets/syncstate/SyncStateColumns';
340fbfc30aSopenharmony_ciimport { MapAddressHistoryColumns } from '@ohos/datastructure/src/main/ets/location/MapAddressHistoryColumns';
350fbfc30aSopenharmony_ciimport { SyncStateMetadataColumns } from '@ohos/datastructure/src/main/ets/syncstate/SyncStateMetadataColumns';
360fbfc30aSopenharmony_ciimport { Constants } from '@ohos/datastructure/src/main/ets/Constants';
370fbfc30aSopenharmony_ciimport { CalendarMetaDataColumns } from '@ohos/datastructure/src/main/ets/calendarmetadata/CalendarMetaDataColumns';
380fbfc30aSopenharmony_ciimport { Log } from '@ohos/common/src/main/ets/utils/Log';
390fbfc30aSopenharmony_ciimport { GlobalThis } from '@ohos/common/src/main/ets/utils/GlobalThis';
400fbfc30aSopenharmony_ci
410fbfc30aSopenharmony_ci/**
420fbfc30aSopenharmony_ci * the create Events table sql. OS has a limit of 1024 bytes for each sql string, and this sql string is beyond
430fbfc30aSopenharmony_ci * 1024 bytes, so we don't use this sql directly now.
440fbfc30aSopenharmony_ci */
450fbfc30aSopenharmony_cilet CREATE_EVENTS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${EventColumns.TABLE_NAME} (`
460fbfc30aSopenharmony_ci  + `${EventColumns.ID} INTEGER PRIMARY KEY AUTOINCREMENT, `
470fbfc30aSopenharmony_ci  + `${EventColumns.SYNC_ID} TEXT, `
480fbfc30aSopenharmony_ci  + `${EventColumns.DIRTY} INTEGER, `
490fbfc30aSopenharmony_ci  + `${EventColumns.MUTATORS} TEXT, `
500fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_CALENDAR_TYPE} INTEGER NOT NULL DEFAULT 0), `
510fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_IMAGE_TYPE} TEXT, `
520fbfc30aSopenharmony_ci+ `${EventColumns.LAST_SYNCED} INTEGER DEFAULT 0, `
530fbfc30aSopenharmony_ci+ `${EventColumns.CALENDAR_ID} INTEGER NOT NULL, `
540fbfc30aSopenharmony_ci+ `${EventColumns.TITLE} TEXT, `
550fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_LOCATION} TEXT, `
560fbfc30aSopenharmony_ci+ `${EventColumns.LOCATION_LONGITUDE} TEXT, `
570fbfc30aSopenharmony_ci+ `${EventColumns.LOCATION_LATITUDE} TEXT, `
580fbfc30aSopenharmony_ci+ `${EventColumns.DESCRIPTION} TEXT, `
590fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_COLOR} INTEGER, `
600fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_COLOR_INDEX} TEXT, `
610fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_STATUS} INTEGER, `
620fbfc30aSopenharmony_ci+ `${EventColumns.SELF_ATTENDEE_STATUS} INTEGER NOT NULL DEFAULT 0, `
630fbfc30aSopenharmony_ci+ `${EventColumns.DTSTART} INTEGER, `
640fbfc30aSopenharmony_ci+ `${EventColumns.DTEND} INTEGER, `
650fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_TIMEZONE} TEXT, `
660fbfc30aSopenharmony_ci+ `${EventColumns.DURATION} TEXT, `
670fbfc30aSopenharmony_ci+ `${EventColumns.ALLDAY} INTEGER NOT NULL DEFAULT 0, `
680fbfc30aSopenharmony_ci+ `${EventColumns.ACCESS_LEVEL} INTEGER NOT NULL DEFAULT 0, `
690fbfc30aSopenharmony_ci+ `${EventColumns.AVAILABILITY} INTEGER NOT NULL DEFAULT 0, `
700fbfc30aSopenharmony_ci+ `${EventColumns.HAS_ALARM} INTEGER NOT NULL DEFAULT 0, `
710fbfc30aSopenharmony_ci+ `${EventColumns.HAS_EXTENDED_PROPERTIES} INTEGER NOT NULL DEFAULT 0, `
720fbfc30aSopenharmony_ci+ `${EventColumns.RRULE} TEXT, `
730fbfc30aSopenharmony_ci+ `${EventColumns.RDATE} TEXT, `
740fbfc30aSopenharmony_ci+ `${EventColumns.EXRULE} TEXT, `
750fbfc30aSopenharmony_ci+ `${EventColumns.EXDATE} TEXT, `
760fbfc30aSopenharmony_ci+ `${EventColumns.ORIGINAL_ID} INTEGER, `
770fbfc30aSopenharmony_ci+ `${EventColumns.ORIGINAL_SYNC_ID} TEXT, `
780fbfc30aSopenharmony_ci+ `${EventColumns.ORIGINAL_INSTANCE_TIME} INTEGER, `
790fbfc30aSopenharmony_ci+ `${EventColumns.ORIGINAL_ALL_DAY} INTEGER, `
800fbfc30aSopenharmony_ci+ `${EventColumns.LAST_DATE} INTEGER, `
810fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_TIME_STAMP} INTEGER NOT NULL DEFAULT 0, `
820fbfc30aSopenharmony_ci+ `${EventColumns.HAS_ATTENDEE_DATE} INTEGER NOT NULL DEFAULT 0, `
830fbfc30aSopenharmony_ci+ `${EventColumns.GUESTS_CAN_MODIFY} INTEGER NOT NULL DEFAULT 0, `
840fbfc30aSopenharmony_ci+ `${EventColumns.GUESTS_CAN_INVITE_OTHERS} INTEGER NOT NULL DEFAULT 1, `
850fbfc30aSopenharmony_ci+ `${EventColumns.GUESTS_CAN_SEE_GUESTS} INTEGER NOT NULL DEFAULT 1, `
860fbfc30aSopenharmony_ci+ `${EventColumns.ORGANIZER} TEXT, ` //STRING
870fbfc30aSopenharmony_ci+ `${EventColumns.IS_ORGANIZER} INTEGER, `
880fbfc30aSopenharmony_ci+ `${EventColumns.DELETED} INTEGER NOT NULL DEFAULT 0, `
890fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_END_TIMEZONE} TEXT, `
900fbfc30aSopenharmony_ci+ `${EventColumns.CUSTOM_APP_PACKAGE} TEXT, `
910fbfc30aSopenharmony_ci+ `${EventColumns.CUSTOM_APP_URI} TEXT, `
920fbfc30aSopenharmony_ci+ `${EventColumns.UID2445} TEXT, `
930fbfc30aSopenharmony_ci+ `${EventColumns.VERSION} INTEGER NOT NULL DEFAULT 0, `
940fbfc30aSopenharmony_ci+ `${EventColumns.EVENT_UUID} TEXT, `
950fbfc30aSopenharmony_ci+ `${EventColumns.HWEXT_ALERT_TYPE} INTEGER NOT NULL DEFAULT 0, `
960fbfc30aSopenharmony_ci+ `${EventColumns.IMPORTANT_EVENT_TYPE} INTEGER NOT NULL DEFAULT 0, `
970fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA1} TEXT, `
980fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA2} TEXT, `
990fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA3} TEXT, `
1000fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA4} TEXT, `
1010fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA5} TEXT, `
1020fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA6} TEXT, `
1030fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA7} TEXT, `
1040fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA8} TEXT, `
1050fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA9} TEXT, `
1060fbfc30aSopenharmony_ci+ `${EventColumns.SYNC_DATA10} TEXT, `
1070fbfc30aSopenharmony_ci+ `${EventColumns.KV_SYNC} INTEGER NOT NULL DEFAULT 0, `
1080fbfc30aSopenharmony_ci+ `${EventColumns.KV_SYNC_TIME} INTEGER NOT NULL DEFAULT 0, `
1090fbfc30aSopenharmony_ci+ `${EventColumns.CREATOR} TEXT NOT NULL)`;
1100fbfc30aSopenharmony_ci
1110fbfc30aSopenharmony_ci/**
1120fbfc30aSopenharmony_ci * the create Calendars table sql
1130fbfc30aSopenharmony_ci */
1140fbfc30aSopenharmony_cilet CREATE_CALENDARS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${CalendarsColumns.TABLE_NAME} (`
1150fbfc30aSopenharmony_ci+ `${CalendarsColumns.ID} INTEGER PRIMARY KEY, `
1160fbfc30aSopenharmony_ci+ `${CalendarsColumns.ACCOUNT_NAME} TEXT, `
1170fbfc30aSopenharmony_ci+ `${CalendarsColumns.ACCOUNT_TYPE} TEXT, `
1180fbfc30aSopenharmony_ci+ `${CalendarsColumns.SYNC_ID} TEXT, `
1190fbfc30aSopenharmony_ci+ `${CalendarsColumns.DIRTY} INTEGER, `
1200fbfc30aSopenharmony_ci+ `${CalendarsColumns.MUTATORS} TEXT, `
1210fbfc30aSopenharmony_ci+ `${CalendarsColumns.NAME} TEXT, `
1220fbfc30aSopenharmony_ci+ `${CalendarsColumns.CALENDAR_DISPLAY_NAME} TEXT, `
1230fbfc30aSopenharmony_ci+ `${CalendarsColumns.CALENDAR_COLOR} INTEGER, `
1240fbfc30aSopenharmony_ci+ `${CalendarsColumns.CALENDAR_COLOR_INDEX} TEXT, `
1250fbfc30aSopenharmony_ci+ `${CalendarsColumns.CALENDAR_ACCESS_LEVEL} INTEGER, `
1260fbfc30aSopenharmony_ci+ `${CalendarsColumns.VISIBLE} INTEGER NOT NULL DEFAULT 1, `
1270fbfc30aSopenharmony_ci+ `${CalendarsColumns.SYNC_EVENTS} INTEGER NOT NULL DEFAULT 0, `
1280fbfc30aSopenharmony_ci+ `${CalendarsColumns.CALENDAR_LOCATION} TEXT, `
1290fbfc30aSopenharmony_ci+ `${CalendarsColumns.CALENDAR_TIMEZONE} TEXT, `
1300fbfc30aSopenharmony_ci+ `${CalendarsColumns.OWNER_ACCOUNT} TEXT, `
1310fbfc30aSopenharmony_ci+ `${CalendarsColumns.IS_PRIMARY} INTEGER, `
1320fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAN_ORGANIZER_RESPOND} INTEGER NOT NULL DEFAULT 1, `
1330fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAN_MODIFY_TIMEZONE} INTEGER DEFAULT 1, `
1340fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAN_PARTIALLY_UPDATE} INTEGER DEFAULT 0, `
1350fbfc30aSopenharmony_ci+ `${CalendarsColumns.MAX_REMINDERS} INTEGER DEFAULT 5, `
1360fbfc30aSopenharmony_ci+ `${CalendarsColumns.ALLOWED_REMINDERS} TEXT DEFAULT '0,1', `
1370fbfc30aSopenharmony_ci+ `${CalendarsColumns.ALLOWED_AVAILABILITY} TEXT DEFAULT '0,1', `
1380fbfc30aSopenharmony_ci+ `${CalendarsColumns.ALLOWED_ATTENDEE_TYPES} TEXT DEFAULT '0,1,2', `
1390fbfc30aSopenharmony_ci+ `${CalendarsColumns.DELETED} INTEGER NOT NULL DEFAULT 0, `
1400fbfc30aSopenharmony_ci+ `${CalendarsColumns.CALENDAR_TIME_STAMP} INTEGER NOT NULL DEFAULT 0, `
1410fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC1} TEXT, `
1420fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC2} TEXT, `
1430fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC3} TEXT, `
1440fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC4} TEXT, `
1450fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC5} TEXT, `
1460fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC6} TEXT, `
1470fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC7} TEXT, `
1480fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC8} TEXT, `
1490fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC9} TEXT, `
1500fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAL_SYNC10} TEXT, `
1510fbfc30aSopenharmony_ci+ `${CalendarsColumns.CAN_REMINDER} INTEGER NOT NULL DEFAULT 1, `
1520fbfc30aSopenharmony_ci+ `${CalendarsColumns.CREATOR} TEXT NOT NULL)`;
1530fbfc30aSopenharmony_ci
1540fbfc30aSopenharmony_ci/**
1550fbfc30aSopenharmony_ci * the create Reminders table sql
1560fbfc30aSopenharmony_ci */
1570fbfc30aSopenharmony_cilet CREATE_REMINDERS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${RemindersColumns.TABLE_NAME} (`
1580fbfc30aSopenharmony_ci+ `${RemindersColumns.ID} INTEGER PRIMARY KEY, `
1590fbfc30aSopenharmony_ci+ `${RemindersColumns.EVENT_ID} INTEGER, `
1600fbfc30aSopenharmony_ci+ `${RemindersColumns.MINUTES} INTEGER, `
1610fbfc30aSopenharmony_ci+ `${RemindersColumns.METHOD} INTEGER NOT NULL DEFAULT 0, `
1620fbfc30aSopenharmony_ci+ `${RemindersColumns.CREATOR} TEXT NOT NULL)`;
1630fbfc30aSopenharmony_ci
1640fbfc30aSopenharmony_ci/**
1650fbfc30aSopenharmony_ci * the create Instances table sql
1660fbfc30aSopenharmony_ci */
1670fbfc30aSopenharmony_cilet CREATE_INSTANCES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${InstancesColumns.TABLE_NAME} (`
1680fbfc30aSopenharmony_ci+ `${InstancesColumns.ID} INTEGER PRIMARY KEY, `
1690fbfc30aSopenharmony_ci+ `${InstancesColumns.EVENT_ID} INTEGER, `
1700fbfc30aSopenharmony_ci+ `${InstancesColumns.BEGIN} INTEGER, `
1710fbfc30aSopenharmony_ci+ `${InstancesColumns.END} INTEGER, `
1720fbfc30aSopenharmony_ci+ `${InstancesColumns.START_DAY} INTEGER, `
1730fbfc30aSopenharmony_ci+ `${InstancesColumns.END_DAY} INTEGER, `
1740fbfc30aSopenharmony_ci+ `${InstancesColumns.START_MINUTE} INTEGER, `
1750fbfc30aSopenharmony_ci+ `${InstancesColumns.END_MINUTE} INTEGER, `
1760fbfc30aSopenharmony_ci+ `${InstancesColumns.CREATOR} TEXT NOT NULL,`
1770fbfc30aSopenharmony_ci+ `UNIQUE (${InstancesColumns.EVENT_ID},${InstancesColumns.BEGIN},`
1780fbfc30aSopenharmony_ci+ `${InstancesColumns.END}))`;
1790fbfc30aSopenharmony_ci
1800fbfc30aSopenharmony_ci/**
1810fbfc30aSopenharmony_ci * the create Colors table sql
1820fbfc30aSopenharmony_ci */
1830fbfc30aSopenharmony_cilet CREATE_COLORS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${ColorsColumns.TABLE_NAME} (`
1840fbfc30aSopenharmony_ci+ `${ColorsColumns.ID} INTEGER PRIMARY KEY, `
1850fbfc30aSopenharmony_ci+ `${ColorsColumns.ACCOUNT_NAME} TEXT NOT NULL, `
1860fbfc30aSopenharmony_ci+ `${ColorsColumns.ACCOUNT_TYPE} TEXT NOT NULL, `
1870fbfc30aSopenharmony_ci+ `${ColorsColumns.DATA} TEXT, `
1880fbfc30aSopenharmony_ci+ `${ColorsColumns.COLOR_TYPE} INTEGER NOT NULL, `
1890fbfc30aSopenharmony_ci+ `${ColorsColumns.COLOR_INDEX} TEXT NOT NULL, `
1900fbfc30aSopenharmony_ci+ `${ColorsColumns.COLOR} INTEGER NOT NULL)`;
1910fbfc30aSopenharmony_ci
1920fbfc30aSopenharmony_ci/**
1930fbfc30aSopenharmony_ci * the create Attendees table sql
1940fbfc30aSopenharmony_ci */
1950fbfc30aSopenharmony_cilet CREATE_ATTENDEES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${AttendeesColumns.TABLE_NAME} (`
1960fbfc30aSopenharmony_ci+ `${AttendeesColumns.ID} INTEGER PRIMARY KEY, `
1970fbfc30aSopenharmony_ci+ `${AttendeesColumns.EVENT_ID} INTEGER, `
1980fbfc30aSopenharmony_ci+ `${AttendeesColumns.ATTENDEE_NAME} TEXT, `
1990fbfc30aSopenharmony_ci+ `${AttendeesColumns.ATTENDEE_EMAIL} TEXT, `
2000fbfc30aSopenharmony_ci+ `${AttendeesColumns.ATTENDEE_STATUS} INTEGER, `
2010fbfc30aSopenharmony_ci+ `${AttendeesColumns.ATTENDEE_RELATIONSHIP} INTEGER, `
2020fbfc30aSopenharmony_ci+ `${AttendeesColumns.ATTENDEE_TYPE} INTEGER, `
2030fbfc30aSopenharmony_ci+ `${AttendeesColumns.ATTENDEE_IDENTITY} TEXT, `
2040fbfc30aSopenharmony_ci+ `${AttendeesColumns.ATTENDEE_ID_NAMESPACE} TEXT)`;
2050fbfc30aSopenharmony_ci
2060fbfc30aSopenharmony_ci/**
2070fbfc30aSopenharmony_ci * the create CalendarAlerts table sql
2080fbfc30aSopenharmony_ci */
2090fbfc30aSopenharmony_cilet CREATE_CALENDAR_ALERTS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${CalendarAlertsColumns.TABLE_NAME} (`
2100fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.ID} INTEGER PRIMARY KEY, `
2110fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.EVENT_ID} INTEGER, `
2120fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.BEGIN} INTEGER NOT NULL, `
2130fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.END} INTEGER NOT NULL, `
2140fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.ALARM_TIME} INTEGER NOT NULL, `
2150fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.CREATION_TIME} INTEGER NOT NULL DEFAULT 0, `
2160fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.RECEIVED_TIME} INTEGER NOT NULL DEFAULT 0, `
2170fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.NOTIFY_TIME} INTEGER NOT NULL DEFAULT 0, `
2180fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.STATE} INTEGER NOT NULL, `
2190fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.MINUTES} INTEGER, `
2200fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.CREATOR} TEXT NOT NULL,`
2210fbfc30aSopenharmony_ci+ `UNIQUE (${CalendarAlertsColumns.ALARM_TIME},${CalendarAlertsColumns.BEGIN},`
2220fbfc30aSopenharmony_ci+ `${CalendarAlertsColumns.EVENT_ID}))`;
2230fbfc30aSopenharmony_ci
2240fbfc30aSopenharmony_ci/**
2250fbfc30aSopenharmony_ci * the create CalendarCache table sql
2260fbfc30aSopenharmony_ci */
2270fbfc30aSopenharmony_cilet CREATE_CALENDAR_CACHE_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${CalendarCacheColumns.TABLE_NAME} (`
2280fbfc30aSopenharmony_ci+ `${CalendarCacheColumns.ID} INTEGER PRIMARY KEY, `
2290fbfc30aSopenharmony_ci+ `${CalendarCacheColumns.KEY} TEXT NOT NULL, `
2300fbfc30aSopenharmony_ci+ `${CalendarCacheColumns.VALUE} TEXT)`;
2310fbfc30aSopenharmony_ci
2320fbfc30aSopenharmony_ci/**
2330fbfc30aSopenharmony_ci * the create CalendarMetaData table sql
2340fbfc30aSopenharmony_ci */
2350fbfc30aSopenharmony_cilet CREATE_CALENDAR_METADATA_TABLE_SQL = `CREATE TABLE IF NOT EXISTS `
2360fbfc30aSopenharmony_ci+ `${CalendarMetaDataColumns.TABLE_NAME} (`
2370fbfc30aSopenharmony_ci+ `${CalendarMetaDataColumns.ID} INTEGER PRIMARY KEY, `
2380fbfc30aSopenharmony_ci+ `${CalendarMetaDataColumns.LOCAL_TIME_ZONE} TEXT, `
2390fbfc30aSopenharmony_ci+ `${CalendarMetaDataColumns.MIN_INSTANCE} INTEGER, `
2400fbfc30aSopenharmony_ci+ `${CalendarMetaDataColumns.MAX_INSTANCE} INTEGER)`;
2410fbfc30aSopenharmony_ci
2420fbfc30aSopenharmony_ci/**
2430fbfc30aSopenharmony_ci * the create EventsRawTimes table sql
2440fbfc30aSopenharmony_ci */
2450fbfc30aSopenharmony_cilet CREATE_EVENTS_RAW_TIMES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS `
2460fbfc30aSopenharmony_ci  + `${EventsRawTimesColumns.TABLE_NAME} (`
2470fbfc30aSopenharmony_ci  + `${EventsRawTimesColumns.ID} INTEGER PRIMARY KEY, `
2480fbfc30aSopenharmony_ci  + `${EventsRawTimesColumns.EVENT_ID} INTEGER NOT NULL, `
2490fbfc30aSopenharmony_ci  + `${EventsRawTimesColumns.DT_START_2445} TEXT, `
2500fbfc30aSopenharmony_ci  + `${EventsRawTimesColumns.DT_END_2445} TEXT, `
2510fbfc30aSopenharmony_ci+ `${EventsRawTimesColumns.ORIGINAL_INSTANCE_TIME2445} TEXT, `
2520fbfc30aSopenharmony_ci+ `${EventsRawTimesColumns.LAST_DATE2445} TEXT, `
2530fbfc30aSopenharmony_ci+ `UNIQUE (${EventsRawTimesColumns.EVENT_ID}))`;
2540fbfc30aSopenharmony_ci
2550fbfc30aSopenharmony_ci/**
2560fbfc30aSopenharmony_ci * the create map_address_history table sql
2570fbfc30aSopenharmony_ci */
2580fbfc30aSopenharmony_cilet CREATE_MAP_ADDRESS_HISTORY_TABLE_SQL = `CREATE TABLE IF NOT EXISTS `
2590fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.TABLE_NAME} (`
2600fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.ID} INTEGER PRIMARY KEY AUTOINCREMENT, `
2610fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.ADDRESS} TEXT, `
2620fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.NAME} TEXT, `
2630fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.LAT} TEXT, `
2640fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.LON} TEXT, `
2650fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.TIME} TEXT, `
2660fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.DATA1} TEXT, `
2670fbfc30aSopenharmony_ci+ `${MapAddressHistoryColumns.DATA2} TEXT)`;
2680fbfc30aSopenharmony_ci
2690fbfc30aSopenharmony_ci/**
2700fbfc30aSopenharmony_ci * the create ExtendedProperties table sql
2710fbfc30aSopenharmony_ci */
2720fbfc30aSopenharmony_cilet CREATE_EXTENDED_PROPERTIES_TABLE_SQL = `CREATE TABLE IF NOT EXISTS `
2730fbfc30aSopenharmony_ci+ `${ExtendedPropertiesColumns.TABLE_NAME} (`
2740fbfc30aSopenharmony_ci+ `${ExtendedPropertiesColumns.ID} INTEGER PRIMARY KEY, `
2750fbfc30aSopenharmony_ci+ `${ExtendedPropertiesColumns.EVENT_ID} INTEGER, `
2760fbfc30aSopenharmony_ci+ `${ExtendedPropertiesColumns.NAME} TEXT, `
2770fbfc30aSopenharmony_ci+ `${ExtendedPropertiesColumns.VALUE} TEXT)`;
2780fbfc30aSopenharmony_ci
2790fbfc30aSopenharmony_ci/**
2800fbfc30aSopenharmony_ci * the create _sync_state table sql
2810fbfc30aSopenharmony_ci */
2820fbfc30aSopenharmony_cilet CREATE_SYNC_STATE_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${SyncStateColumns.TABLE_NAME} (`
2830fbfc30aSopenharmony_ci+ `${SyncStateColumns.ID} INTEGER PRIMARY KEY, `
2840fbfc30aSopenharmony_ci+ `${SyncStateColumns.ACCOUNT_NAME} TEXT NOT NULL, `
2850fbfc30aSopenharmony_ci+ `${SyncStateColumns.ACCOUNT_TYPE} TEXT NOT NULL, `
2860fbfc30aSopenharmony_ci+ `${SyncStateColumns.DATA} TEXT NOT NULL, `
2870fbfc30aSopenharmony_ci+ `UNIQUE (${SyncStateColumns.ACCOUNT_NAME},${SyncStateColumns.ACCOUNT_TYPE}))`;
2880fbfc30aSopenharmony_ci
2890fbfc30aSopenharmony_ci/**
2900fbfc30aSopenharmony_ci * the create _sync_state_metadata table sql
2910fbfc30aSopenharmony_ci */
2920fbfc30aSopenharmony_cilet CREATE_SYNC_STATE_METADATA_TABLE_SQL = `CREATE TABLE IF NOT EXISTS `
2930fbfc30aSopenharmony_ci+ `${SyncStateMetadataColumns.TABLE_NAME} (`
2940fbfc30aSopenharmony_ci+ `${SyncStateMetadataColumns.VERSION} INTEGER)`;
2950fbfc30aSopenharmony_ci
2960fbfc30aSopenharmony_ci/**
2970fbfc30aSopenharmony_ci * the create ExtendCalendarEvent table sql
2980fbfc30aSopenharmony_ci */
2990fbfc30aSopenharmony_cilet CREATE_EXTEND_CALENDAR_EVENT_TABLE_NAME_SQL = `CREATE TABLE IF NOT EXISTS `
3000fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.TABLE_NAME} (`
3010fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.ID} INTEGER PRIMARY KEY, `
3020fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_EVENT_ID} INTEGER, `
3030fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_TYPE} INTEGER, `
3040fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_APP_URI} TEXT, `
3050fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_APP_NAME} TEXT, `
3060fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_ACTION} TEXT, `
3070fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_METHOD} INTEGER, `
3080fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_SUPPORT_MIN_VERSION} INTEGER, `
3090fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_STATUS} INTEGER, `
3100fbfc30aSopenharmony_ci+ `${ExtendCalendarEventColumns.HWEXT_DESCRIPTION} TEXT)`;
3110fbfc30aSopenharmony_ci
3120fbfc30aSopenharmony_cilet TAG = "CalendarDataHelper"
3130fbfc30aSopenharmony_ci
3140fbfc30aSopenharmony_ci/**
3150fbfc30aSopenharmony_ci * the class to initialize the calendar database
3160fbfc30aSopenharmony_ci *
3170fbfc30aSopenharmony_ci * @since 2022-04-06
3180fbfc30aSopenharmony_ci */
3190fbfc30aSopenharmony_ciclass CalendarDataHelper {
3200fbfc30aSopenharmony_ci  private static instance: CalendarDataHelper;
3210fbfc30aSopenharmony_ci
3220fbfc30aSopenharmony_ci  private rdbStore: ohosDataRdb.RdbStore | null = null;
3230fbfc30aSopenharmony_ci
3240fbfc30aSopenharmony_ci  private constructor() {
3250fbfc30aSopenharmony_ci  }
3260fbfc30aSopenharmony_ci
3270fbfc30aSopenharmony_ci  /**
3280fbfc30aSopenharmony_ci   * get the instance of CalendarDataHelper class
3290fbfc30aSopenharmony_ci   */
3300fbfc30aSopenharmony_ci  public static getInstance() {
3310fbfc30aSopenharmony_ci    if (!CalendarDataHelper.instance) {
3320fbfc30aSopenharmony_ci      Log.log(TAG, 'call getInstance init');
3330fbfc30aSopenharmony_ci      CalendarDataHelper.instance = new CalendarDataHelper();
3340fbfc30aSopenharmony_ci    }
3350fbfc30aSopenharmony_ci    return CalendarDataHelper.instance;
3360fbfc30aSopenharmony_ci  }
3370fbfc30aSopenharmony_ci
3380fbfc30aSopenharmony_ci  /**
3390fbfc30aSopenharmony_ci   * get the instance of rdbStore
3400fbfc30aSopenharmony_ci   */
3410fbfc30aSopenharmony_ci  public async getRdbStore() {
3420fbfc30aSopenharmony_ci    if (this.rdbStore === null || this.rdbStore === undefined) {
3430fbfc30aSopenharmony_ci      await CalendarDataHelper.getInstance().initRdbStore();
3440fbfc30aSopenharmony_ci    }
3450fbfc30aSopenharmony_ci    return this.rdbStore;
3460fbfc30aSopenharmony_ci  }
3470fbfc30aSopenharmony_ci
3480fbfc30aSopenharmony_ci  private async initRdbStore() {
3490fbfc30aSopenharmony_ci    Log.log(TAG, 'call initRdbStore start');
3500fbfc30aSopenharmony_ci    this.rdbStore = await ohosDataRdb.getRdbStore(GlobalThis.getExtensionContext(), {
3510fbfc30aSopenharmony_ci      name: Constants.DB_NAME,
3520fbfc30aSopenharmony_ci      securityLevel: ohosDataRdb.SecurityLevel.S1,
3530fbfc30aSopenharmony_ci    });
3540fbfc30aSopenharmony_ci    if (this.rdbStore == null || this.rdbStore == undefined) {
3550fbfc30aSopenharmony_ci      Log.error(TAG, 'initRdbStore rdbStore is null');
3560fbfc30aSopenharmony_ci      return;
3570fbfc30aSopenharmony_ci    }
3580fbfc30aSopenharmony_ci    try {
3590fbfc30aSopenharmony_ci      await this.createEventsTable();
3600fbfc30aSopenharmony_ci      Log.log(TAG, "initRdbStore createEventsTable succeed");
3610fbfc30aSopenharmony_ci    } catch (err) {
3620fbfc30aSopenharmony_ci      Log.error(TAG, `initRdbStore createEventsTable get err:${err}`);
3630fbfc30aSopenharmony_ci    }
3640fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore executeSql start");
3650fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_CALENDARS_TABLE_SQL, []);
3660fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore createCalendarsTable succeed");
3670fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_REMINDERS_TABLE_SQL, []);
3680fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_REMINDERS_TABLE_SQL succeed");
3690fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_INSTANCES_TABLE_SQL, []);
3700fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_INSTANCES_TABLE_SQL succeed");
3710fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_COLORS_TABLE_SQL, []);
3720fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_COLORS_TABLE_SQL succeed");
3730fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_ATTENDEES_TABLE_SQL, []);
3740fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_ATTENDEES_TABLE_SQL succeed");
3750fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_CALENDAR_ALERTS_TABLE_SQL, []);
3760fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_CALENDAR_ALERTS_TABLE_SQL succeed");
3770fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_CALENDAR_CACHE_TABLE_SQL, []);
3780fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_CALENDAR_CACHE_TABLE_SQL succeed");
3790fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_CALENDAR_METADATA_TABLE_SQL, []);
3800fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_CALENDAR_METADATA_TABLE_SQL succeed");
3810fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_EVENTS_RAW_TIMES_TABLE_SQL, []);
3820fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_EVENTS_RAW_TIMES_TABLE_SQL succeed");
3830fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_MAP_ADDRESS_HISTORY_TABLE_SQL, []);
3840fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_MAP_ADDRESS_HISTORY_TABLE_SQL succeed");
3850fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_EXTENDED_PROPERTIES_TABLE_SQL, []);
3860fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_EXTENDED_PROPERTIES_TABLE_SQL succeed");
3870fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_SYNC_STATE_TABLE_SQL, []);
3880fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_SYNC_STATE_TABLE_SQL succeed");
3890fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_SYNC_STATE_METADATA_TABLE_SQL, []);
3900fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_SYNC_STATE_METADATA_TABLE_SQL succeed");
3910fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_EXTEND_CALENDAR_EVENT_TABLE_NAME_SQL, []);
3920fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore CREATE_EXTEND_CALENDAR_EVENT_TABLE_NAME_SQL succeed");
3930fbfc30aSopenharmony_ci
3940fbfc30aSopenharmony_ci    this.createSyncIdUpdateTrigger();
3950fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore createSyncIdUpdateTrigger succeed");
3960fbfc30aSopenharmony_ci    this.createDeleteEventTriggers();
3970fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore createDeleteEventTriggers succeed");
3980fbfc30aSopenharmony_ci    this.createColorsTriggers();
3990fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore createColorsTriggers succeed");
4000fbfc30aSopenharmony_ci    this.createCalendarCleanupTrigger();
4010fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore createCalendarCleanupTrigger succeed");
4020fbfc30aSopenharmony_ci    Log.log(TAG, "initRdbStore executeSql end");
4030fbfc30aSopenharmony_ci  }
4040fbfc30aSopenharmony_ci
4050fbfc30aSopenharmony_ci  private async createEventsTable() {
4060fbfc30aSopenharmony_ci    // the database has a limit of 1024 byte for sql strings, so we create some fields in the create sql,
4070fbfc30aSopenharmony_ci    // other fields are added later.
4080fbfc30aSopenharmony_ci    Log.log(TAG, 'call createEventsTable start');
4090fbfc30aSopenharmony_ci    let CREATE_EVENTS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS ${EventColumns.TABLE_NAME} (`
4100fbfc30aSopenharmony_ci      + `${EventColumns.ID} INTEGER PRIMARY KEY AUTOINCREMENT, `
4110fbfc30aSopenharmony_ci      + `${EventColumns.SYNC_ID} TEXT, `
4120fbfc30aSopenharmony_ci      + `${EventColumns.DIRTY} INTEGER, `
4130fbfc30aSopenharmony_ci      + `${EventColumns.MUTATORS} TEXT, `
4140fbfc30aSopenharmony_ci      + `${EventColumns.EVENT_CALENDAR_TYPE} INTEGER NOT NULL DEFAULT 0)`;
4150fbfc30aSopenharmony_ci
4160fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_EVENTS_TABLE_SQL, []);
4170fbfc30aSopenharmony_ci    Log.log(TAG, 'call createEventsTable add column start');
4180fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4190fbfc30aSopenharmony_ci      `${EventColumns.EVENT_IMAGE_TYPE} TEXT`, []);
4200fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4210fbfc30aSopenharmony_ci      `${EventColumns.LAST_SYNCED} INTEGER DEFAULT 0`, []);
4220fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4230fbfc30aSopenharmony_ci      `${EventColumns.CALENDAR_ID} INTEGER NOT NULL`, []);
4240fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4250fbfc30aSopenharmony_ci      `${EventColumns.TITLE} TEXT`, []);
4260fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4270fbfc30aSopenharmony_ci      `${EventColumns.EVENT_LOCATION} TEXT`, []);
4280fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4290fbfc30aSopenharmony_ci      `${EventColumns.LOCATION_LONGITUDE} TEXT`, []);
4300fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4310fbfc30aSopenharmony_ci      `${EventColumns.LOCATION_LATITUDE} TEXT`, []);
4320fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4330fbfc30aSopenharmony_ci      `${EventColumns.DESCRIPTION} TEXT`, []);
4340fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4350fbfc30aSopenharmony_ci      `${EventColumns.EVENT_COLOR} INTEGER`, []);
4360fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4370fbfc30aSopenharmony_ci      `${EventColumns.EVENT_COLOR_INDEX} TEXT`, []);
4380fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4390fbfc30aSopenharmony_ci      `${EventColumns.EVENT_STATUS} INTEGER`, []);
4400fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4410fbfc30aSopenharmony_ci      `${EventColumns.SELF_ATTENDEE_STATUS} INTEGER NOT NULL DEFAULT 0`, []);
4420fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4430fbfc30aSopenharmony_ci      `${EventColumns.DTSTART} INTEGER`, []);
4440fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4450fbfc30aSopenharmony_ci      `${EventColumns.DTEND} INTEGER`, []);
4460fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4470fbfc30aSopenharmony_ci      `${EventColumns.EVENT_TIMEZONE} TEXT`, []);
4480fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4490fbfc30aSopenharmony_ci      `${EventColumns.DURATION} TEXT`, []);
4500fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4510fbfc30aSopenharmony_ci      `${EventColumns.ALLDAY} INTEGER NOT NULL DEFAULT 0`, []);
4520fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4530fbfc30aSopenharmony_ci      `${EventColumns.ACCESS_LEVEL} INTEGER NOT NULL DEFAULT 0`, []);
4540fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4550fbfc30aSopenharmony_ci      `${EventColumns.AVAILABILITY} INTEGER NOT NULL DEFAULT 0`, []);
4560fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4570fbfc30aSopenharmony_ci      `${EventColumns.HAS_ALARM} INTEGER NOT NULL DEFAULT 0`, []);
4580fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4590fbfc30aSopenharmony_ci      `${EventColumns.HAS_EXTENDED_PROPERTIES} INTEGER NOT NULL DEFAULT 0`, []);
4600fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ${EventColumns.RRULE} TEXT`, []);
4610fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ${EventColumns.RDATE} TEXT`, []);
4620fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ${EventColumns.EXRULE} TEXT`, []);
4630fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ${EventColumns.EXDATE} TEXT`, []);
4640fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4650fbfc30aSopenharmony_ci      `${EventColumns.ORIGINAL_ID} INTEGER`, []);
4660fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4670fbfc30aSopenharmony_ci      `${EventColumns.ORIGINAL_SYNC_ID} TEXT`, []);
4680fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4690fbfc30aSopenharmony_ci      `${EventColumns.ORIGINAL_INSTANCE_TIME} INTEGER`, []);
4700fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4710fbfc30aSopenharmony_ci      `${EventColumns.ORIGINAL_ALL_DAY} INTEGER`, []);
4720fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4730fbfc30aSopenharmony_ci      `${EventColumns.LAST_DATE} INTEGER`, []);
4740fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4750fbfc30aSopenharmony_ci      `${EventColumns.EVENT_TIME_STAMP} INTEGER NOT NULL DEFAULT 0`, []);
4760fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4770fbfc30aSopenharmony_ci      `${EventColumns.HAS_ATTENDEE_DATE} INTEGER NOT NULL DEFAULT 0`, []);
4780fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4790fbfc30aSopenharmony_ci      `${EventColumns.GUESTS_CAN_MODIFY} INTEGER NOT NULL DEFAULT 0`, []);
4800fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4810fbfc30aSopenharmony_ci      `${EventColumns.GUESTS_CAN_INVITE_OTHERS} INTEGER NOT NULL DEFAULT 1`, []);
4820fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4830fbfc30aSopenharmony_ci      `${EventColumns.GUESTS_CAN_SEE_GUESTS} INTEGER NOT NULL DEFAULT 1`, []);
4840fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4850fbfc30aSopenharmony_ci      `${EventColumns.ORGANIZER} STRING`, []);
4860fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4870fbfc30aSopenharmony_ci      `${EventColumns.IS_ORGANIZER} INTEGER`, []);
4880fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4890fbfc30aSopenharmony_ci      `${EventColumns.DELETED} INTEGER NOT NULL DEFAULT 0`, []);
4900fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4910fbfc30aSopenharmony_ci      `${EventColumns.EVENT_END_TIMEZONE} TEXT`, []);
4920fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4930fbfc30aSopenharmony_ci      `${EventColumns.CUSTOM_APP_PACKAGE} TEXT`, []);
4940fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4950fbfc30aSopenharmony_ci      `${EventColumns.CUSTOM_APP_URI} TEXT`, []);
4960fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ${EventColumns.UID2445} TEXT`, []);
4970fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
4980fbfc30aSopenharmony_ci      `${EventColumns.VERSION} INTEGER NOT NULL DEFAULT 0`, []);
4990fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5000fbfc30aSopenharmony_ci      `${EventColumns.EVENT_UUID} TEXT`, []);
5010fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5020fbfc30aSopenharmony_ci      `${EventColumns.HWEXT_ALERT_TYPE} INTEGER NOT NULL DEFAULT 0`, []);
5030fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5040fbfc30aSopenharmony_ci      `${EventColumns.IMPORTANT_EVENT_TYPE} INTEGER NOT NULL DEFAULT 0`, []);
5050fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE EVENTS ADD COLUMN ` +
5060fbfc30aSopenharmony_ci      `${EventColumns.SERVICE.SERVICE_VERIFIED} INTEGER`, []);
5070fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE EVENTS ADD COLUMN ` +
5080fbfc30aSopenharmony_ci      `${EventColumns.SERVICE.SERVICE_TYPE} TEXT`, []);
5090fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE EVENTS ADD COLUMN ` +
5100fbfc30aSopenharmony_ci      `${EventColumns.SERVICE.SERVICE_CP_BZ_URI} TEXT`, []);
5110fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE EVENTS ADD COLUMN ` +
5120fbfc30aSopenharmony_ci      `${EventColumns.SERVICE.SERVICE_CP_MNG_URI} TEXT`, []);
5130fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE EVENTS ADD COLUMN ` +
5140fbfc30aSopenharmony_ci      `${EventColumns.SERVICE.SERVICE_DESCRIPTION} TEXT`, []);
5150fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE EVENTS ADD COLUMN ` +
5160fbfc30aSopenharmony_ci      `${EventColumns.SERVICE.SERVICE_MIN_VERSION} INTEGER`, []);
5170fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5180fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA1} TEXT`, []);
5190fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5200fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA2} TEXT`, []);
5210fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5220fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA3} TEXT`, []);
5230fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5240fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA4} TEXT`, []);
5250fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5260fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA5} TEXT`, []);
5270fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5280fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA6} TEXT`, []);
5290fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5300fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA7} TEXT`, []);
5310fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5320fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA8} TEXT`, []);
5330fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5340fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA9} TEXT`, []);
5350fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5360fbfc30aSopenharmony_ci      `${EventColumns.SYNC_DATA10} TEXT`, []);
5370fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5380fbfc30aSopenharmony_ci      `${EventColumns.KV_SYNC} INTEGER NOT NULL DEFAULT 0`, []);
5390fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5400fbfc30aSopenharmony_ci      `${EventColumns.KV_SYNC_TIME} INTEGER NOT NULL DEFAULT 0`, []);
5410fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(`ALTER TABLE Events ADD COLUMN ` +
5420fbfc30aSopenharmony_ci      `${EventColumns.CREATOR} TEXT NOT NULL`, []);
5430fbfc30aSopenharmony_ci    Log.log(TAG, 'call createEventsTable end');
5440fbfc30aSopenharmony_ci  }
5450fbfc30aSopenharmony_ci
5460fbfc30aSopenharmony_ci  private async createDeleteEventTriggers() {
5470fbfc30aSopenharmony_ci    Log.log(TAG, 'call createDeleteEventTriggers start');
5480fbfc30aSopenharmony_ci    let EVENTS_CLEANUP_TRIGGER_SQL =
5490fbfc30aSopenharmony_ci      `DELETE FROM ${InstancesColumns.TABLE_NAME}` +
5500fbfc30aSopenharmony_ci      ` WHERE ${InstancesColumns.EVENT_ID} = old.${EventColumns.ID};` +
5510fbfc30aSopenharmony_ci      `DELETE FROM ${EventsRawTimesColumns.TABLE_NAME}` +
5520fbfc30aSopenharmony_ci      ` WHERE ${EventsRawTimesColumns.EVENT_ID} = old.${EventColumns.ID};` +
5530fbfc30aSopenharmony_ci      `DELETE FROM ${AttendeesColumns.TABLE_NAME}` +
5540fbfc30aSopenharmony_ci      ` WHERE ${AttendeesColumns.EVENT_ID} = old.${EventColumns.ID};` +
5550fbfc30aSopenharmony_ci      `DELETE FROM ${ExtendCalendarEventColumns.TABLE_NAME}` +
5560fbfc30aSopenharmony_ci      ` WHERE ${ExtendCalendarEventColumns.HWEXT_EVENT_ID} = ` +
5570fbfc30aSopenharmony_ci      `old.${EventColumns.ID};` +
5580fbfc30aSopenharmony_ci      `DELETE FROM ${RemindersColumns.TABLE_NAME}` +
5590fbfc30aSopenharmony_ci      ` WHERE ${RemindersColumns.EVENT_ID} = old.${EventColumns.ID};` +
5600fbfc30aSopenharmony_ci      `DELETE FROM ${CalendarAlertsColumns.TABLE_NAME}` +
5610fbfc30aSopenharmony_ci      ` WHERE ${CalendarAlertsColumns.EVENT_ID} = old.${EventColumns.ID};` +
5620fbfc30aSopenharmony_ci      `DELETE FROM ${ExtendedPropertiesColumns.TABLE_NAME}` +
5630fbfc30aSopenharmony_ci      ` WHERE ${ExtendedPropertiesColumns.EVENT_ID} = old.${EventColumns.ID};`
5640fbfc30aSopenharmony_ci
5650fbfc30aSopenharmony_ci    let EVENTS_CLEANUP_TRIGGER = `CREATE TRIGGER events_cleanup_delete DELETE ON ` +
5660fbfc30aSopenharmony_ci    `${EventColumns.TABLE_NAME} BEGIN ${EVENTS_CLEANUP_TRIGGER_SQL} END`
5670fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(EVENTS_CLEANUP_TRIGGER, []);
5680fbfc30aSopenharmony_ci    Log.log(TAG, 'call createDeleteEventTriggers end');
5690fbfc30aSopenharmony_ci  }
5700fbfc30aSopenharmony_ci
5710fbfc30aSopenharmony_ci  private async createColorsTriggers() {
5720fbfc30aSopenharmony_ci    Log.log(TAG, 'call createColorsTriggers start');
5730fbfc30aSopenharmony_ci    let EVENT_UPDATE_COLOR_TRIGGER_SQL = `UPDATE ${EventColumns.TABLE_NAME}` +
5740fbfc30aSopenharmony_ci    ` SET eventColor=(SELECT ${ColorsColumns.COLOR} FROM ` +
5750fbfc30aSopenharmony_ci    `${ColorsColumns.TABLE_NAME}` +
5760fbfc30aSopenharmony_ci    ` WHERE ${ColorsColumns.ACCOUNT_NAME} = (SELECT ` +
5770fbfc30aSopenharmony_ci    `${CalendarsColumns.ACCOUNT_NAME}` +
5780fbfc30aSopenharmony_ci    ` FROM ${CalendarsColumns.TABLE_NAME} WHERE ${CalendarsColumns.ID} =` +
5790fbfc30aSopenharmony_ci    ` new.${EventColumns.CALENDAR_ID}) AND ${ColorsColumns.ACCOUNT_TYPE}` +
5800fbfc30aSopenharmony_ci    ` = (SELECT ${CalendarsColumns.ACCOUNT_TYPE} FROM ` +
5810fbfc30aSopenharmony_ci    `${CalendarsColumns.TABLE_NAME}` +
5820fbfc30aSopenharmony_ci    ` WHERE ${CalendarsColumns.ID} = new.${EventColumns.CALENDAR_ID})` +
5830fbfc30aSopenharmony_ci    ` AND ${ColorsColumns.COLOR_INDEX} = ` +
5840fbfc30aSopenharmony_ci    `new.${EventColumns.EVENT_COLOR_INDEX}` +
5850fbfc30aSopenharmony_ci    ` AND ${ColorsColumns.COLOR_TYPE} = 1)` +
5860fbfc30aSopenharmony_ci    ` WHERE ${EventColumns.ID} = old.${EventColumns.ID};`
5870fbfc30aSopenharmony_ci    let CREATE_EVENT_COLOR_UPDATE_TRIGGER = `CREATE TRIGGER ` +
5880fbfc30aSopenharmony_ci    `event_color_update UPDATE OF ${EventColumns.EVENT_COLOR_INDEX} ON ` +
5890fbfc30aSopenharmony_ci    `${EventColumns.TABLE_NAME} WHEN new.${EventColumns.EVENT_COLOR_INDEX} ` +
5900fbfc30aSopenharmony_ci    `NOT NULL BEGIN ${EVENT_UPDATE_COLOR_TRIGGER_SQL} END`
5910fbfc30aSopenharmony_ci
5920fbfc30aSopenharmony_ci    let CALENDAR_UPDATE_COLOR_TRIGGER_SQL = `UPDATE ${CalendarsColumns.TABLE_NAME} ` +
5930fbfc30aSopenharmony_ci    `SET ${CalendarsColumns.CALENDAR_COLOR} = (SELECT ` +
5940fbfc30aSopenharmony_ci    `${ColorsColumns.COLOR} FROM ${ColorsColumns.TABLE_NAME} WHERE ` +
5950fbfc30aSopenharmony_ci    `${ColorsColumns.ACCOUNT_NAME} = ` +
5960fbfc30aSopenharmony_ci    `new.${CalendarsColumns.ACCOUNT_NAME} AND ` +
5970fbfc30aSopenharmony_ci    `${ColorsColumns.ACCOUNT_TYPE} = ` +
5980fbfc30aSopenharmony_ci    `new.${CalendarsColumns.ACCOUNT_TYPE} AND ` +
5990fbfc30aSopenharmony_ci    `${ColorsColumns.COLOR_INDEX} = ` +
6000fbfc30aSopenharmony_ci    `new.${CalendarsColumns.CALENDAR_COLOR_INDEX} AND ` +
6010fbfc30aSopenharmony_ci    `${ColorsColumns.COLOR_TYPE} = 0) ` +
6020fbfc30aSopenharmony_ci    ` WHERE ${CalendarsColumns.ID} = old.${CalendarsColumns.ID};`
6030fbfc30aSopenharmony_ci    let CREATE_CALENDAR_COLOR_UPDATE_TRIGGER = `CREATE TRIGGER ` +
6040fbfc30aSopenharmony_ci    `calendar_color_update UPDATE OF ${CalendarsColumns.CALENDAR_COLOR_INDEX}` +
6050fbfc30aSopenharmony_ci    ` ON ${CalendarsColumns.TABLE_NAME} WHEN` +
6060fbfc30aSopenharmony_ci    ` new.${CalendarsColumns.CALENDAR_COLOR_INDEX} NOT NULL` +
6070fbfc30aSopenharmony_ci    ` BEGIN ${CALENDAR_UPDATE_COLOR_TRIGGER_SQL} END`
6080fbfc30aSopenharmony_ci
6090fbfc30aSopenharmony_ci    Log.log(TAG, 'call CREATE_EVENT_COLOR_UPDATE_TRIGGER');
6100fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_EVENT_COLOR_UPDATE_TRIGGER, [])
6110fbfc30aSopenharmony_ci    Log.log(TAG, 'call CREATE_CALENDAR_COLOR_UPDATE_TRIGGER');
6120fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_CALENDAR_COLOR_UPDATE_TRIGGER, [])
6130fbfc30aSopenharmony_ci    Log.log(TAG, 'call createColorsTriggers end');
6140fbfc30aSopenharmony_ci  }
6150fbfc30aSopenharmony_ci
6160fbfc30aSopenharmony_ci  private async createCalendarCleanupTrigger() {
6170fbfc30aSopenharmony_ci    Log.log(TAG, 'call createCalendarCleanupTrigger start')
6180fbfc30aSopenharmony_ci    let CALENDAR_CLEANUP_TRIGGER_SQL = `DELETE FROM ` +
6190fbfc30aSopenharmony_ci    `${EventColumns.TABLE_NAME} WHERE ` +
6200fbfc30aSopenharmony_ci    `${EventColumns.CALENDAR_ID} = old.${EventColumns.ID};`
6210fbfc30aSopenharmony_ci    let CALENDAR_CLEANUP_TRIGGER = `CREATE TRIGGER calendar_cleanup DELETE ON ` +
6220fbfc30aSopenharmony_ci    `${CalendarsColumns.TABLE_NAME} ` +
6230fbfc30aSopenharmony_ci    `BEGIN ${CALENDAR_CLEANUP_TRIGGER_SQL} END`
6240fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CALENDAR_CLEANUP_TRIGGER, []);
6250fbfc30aSopenharmony_ci    Log.log(TAG, 'call createCalendarCleanupTrigger end');
6260fbfc30aSopenharmony_ci  }
6270fbfc30aSopenharmony_ci
6280fbfc30aSopenharmony_ci  private async createSyncIdUpdateTrigger() {
6290fbfc30aSopenharmony_ci    Log.log(TAG, 'call createSyncIdUpdateTrigger start');
6300fbfc30aSopenharmony_ci    let EVENTS_ORIGINAL_SYNC_TRIGGER_SQL = `UPDATE ${EventColumns.TABLE_NAME}` +
6310fbfc30aSopenharmony_ci    ` SET ${EventColumns.ORIGINAL_SYNC_ID} = ` +
6320fbfc30aSopenharmony_ci    `new.${EventColumns.SYNC_ID}` +
6330fbfc30aSopenharmony_ci      ` WHERE ${EventColumns.ORIGINAL_ID} =old.${EventColumns.ID};`
6340fbfc30aSopenharmony_ci    let CREATE_SYNC_ID_UPDATE_TRIGGER = `CREATE TRIGGER original_sync_update` +
6350fbfc30aSopenharmony_ci      ` UPDATE OF ${EventColumns.SYNC_ID}` +
6360fbfc30aSopenharmony_ci      ` ON ${EventColumns.TABLE_NAME}` +
6370fbfc30aSopenharmony_ci      ` BEGIN ${EVENTS_ORIGINAL_SYNC_TRIGGER_SQL} END`
6380fbfc30aSopenharmony_ci
6390fbfc30aSopenharmony_ci    await this.rdbStore?.executeSql(CREATE_SYNC_ID_UPDATE_TRIGGER, []);
6400fbfc30aSopenharmony_ci    Log.log(TAG, 'call createSyncIdUpdateTrigger end');
6410fbfc30aSopenharmony_ci  }
6420fbfc30aSopenharmony_ci}
6430fbfc30aSopenharmony_ci
6440fbfc30aSopenharmony_ciexport default CalendarDataHelper;