1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef RELATIONAL_STORE_H 17 #define RELATIONAL_STORE_H 18 19 /** 20 * @addtogroup RDB 21 * @{ 22 * 23 * @brief The relational database (RDB) store manages data based on relational models. 24 * With the underlying SQLite database, the RDB store provides a complete mechanism for managing local databases. 25 * To satisfy different needs in complicated scenarios, the RDB store offers a series of APIs for performing operations 26 * such as adding, deleting, modifying, and querying data, and supports direct execution of SQL statements. 27 * 28 * @syscap SystemCapability.DistributedDataManager.RelationalStore.Core 29 * @since 10 30 */ 31 32 /** 33 * @file relational_store.h 34 * 35 * @brief Provides database related functions and enumerations. 36 * 37 * @since 10 38 */ 39 40 #include "oh_cursor.h" 41 #include "oh_predicates.h" 42 #include "oh_value_object.h" 43 #include "oh_values_bucket.h" 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 /** 50 * @brief Describe the security level of the database. 51 * 52 * @since 10 53 */ 54 typedef enum OH_Rdb_SecurityLevel { 55 /** 56 * @brief Low-level security. Data leaks have a minor impact. 57 */ 58 S1 = 1, 59 /** 60 * @brief Medium-level security. Data leaks have a major impact. 61 */ 62 S2, 63 /** 64 * @brief High-level security. Data leaks have a severe impact. 65 */ 66 S3, 67 /** 68 * @brief Critical-level security. Data leaks have a critical impact. 69 */ 70 S4 71 } OH_Rdb_SecurityLevel; 72 73 /** 74 * @brief Describe the security area of the database. 75 * 76 * @since 11 77 */ 78 typedef enum Rdb_SecurityArea { 79 /** 80 * @brief Security Area 1. 81 */ 82 RDB_SECURITY_AREA_EL1 = 1, 83 /** 84 * @brief Security Area 2. 85 */ 86 RDB_SECURITY_AREA_EL2, 87 /** 88 * @brief Security Area 3. 89 */ 90 RDB_SECURITY_AREA_EL3, 91 /** 92 * @brief Security Area 4. 93 */ 94 RDB_SECURITY_AREA_EL4, 95 /** 96 * @brief Security Area 5. 97 * 98 * @since 12 99 */ 100 RDB_SECURITY_AREA_EL5, 101 } Rdb_SecurityArea; 102 103 /** 104 * @brief Manages relational database configurations. 105 * 106 * @since 10 107 */ 108 #pragma pack(1) 109 typedef struct { 110 /** 111 * Indicates the size of the {@link OH_Rdb_Config}. It is mandatory. 112 */ 113 int selfSize; 114 /** 115 * Indicates the directory of the database. 116 */ 117 const char *dataBaseDir; 118 /** 119 * Indicates the name of the database. 120 */ 121 const char *storeName; 122 /** 123 * Indicates the bundle name of the application. 124 */ 125 const char *bundleName; 126 /** 127 * Indicates the module name of the application. 128 */ 129 const char *moduleName; 130 /** 131 * Indicates whether the database is encrypted. 132 */ 133 bool isEncrypt; 134 /** 135 * Indicates the security level {@link OH_Rdb_SecurityLevel} of the database. 136 */ 137 int securityLevel; 138 /** 139 * Indicates the security area {@link Rdb_SecurityArea} of the database. 140 * 141 * @since 11 142 */ 143 int area; 144 } OH_Rdb_Config; 145 #pragma pack() 146 147 /** 148 * @brief Define OH_Rdb_Store type. 149 * 150 * @since 10 151 */ 152 typedef struct { 153 /** 154 * The id used to uniquely identify the OH_Rdb_Store struct. 155 */ 156 int64_t id; 157 } OH_Rdb_Store; 158 159 /** 160 * @brief Creates an {@link OH_VObject} instance. 161 * 162 * @return If the creation is successful, a pointer to the instance of the @link OH_VObject} structure is returned, 163 * otherwise NULL is returned. 164 * @see OH_VObject. 165 * @since 10 166 */ 167 OH_VObject *OH_Rdb_CreateValueObject(); 168 169 /** 170 * @brief Creates an {@link OH_VBucket} object. 171 * 172 * @return If the creation is successful, a pointer to the instance of the @link OH_VBucket} structure is returned, 173 * otherwise NULL is returned. 174 * @see OH_VBucket. 175 * @since 10 176 */ 177 OH_VBucket *OH_Rdb_CreateValuesBucket(); 178 179 /** 180 * @brief Creates an {@link OH_Predicates} instance. 181 * 182 * @param table Indicates the table name. 183 * @return If the creation is successful, a pointer to the instance of the @link OH_Predicates} structure is returned. 184 * If the table name is nullptr, nullptr is returned. 185 * @see OH_Predicates. 186 * @since 10 187 */ 188 OH_Predicates *OH_Rdb_CreatePredicates(const char *table); 189 190 /** 191 * @brief Obtains an RDB store. 192 * 193 * You can set parameters of the RDB store as required. In general, 194 * this method is recommended to obtain a rdb store. 195 * 196 * @param config Represents a pointer to an {@link OH_Rdb_Config} instance. 197 * Indicates the configuration of the database related to this RDB store. 198 * @param errCode This parameter is the output parameter, 199 * and the execution status of a function is written to this variable. 200 * @return If the creation is successful, a pointer to the instance of the @link OH_Rdb_Store} structure is returned. 201 * If the Config is empty, config.size does not match, or errCode is empty. 202 * Get database path failed.Get RDB Store fail. Nullptr is returned. 203 * @see OH_Rdb_Config, OH_Rdb_Store. 204 * @since 10 205 */ 206 OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode); 207 208 /** 209 * @brief Close the {@link OH_Rdb_Store} object and reclaim the memory occupied by the object. 210 * 211 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 212 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 213 * {@link RDB_OK} - success. 214 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 215 * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 216 * @see OH_Rdb_Store, OH_Rdb_ErrCode. 217 * @since 10 218 */ 219 int OH_Rdb_CloseStore(OH_Rdb_Store *store); 220 221 /** 222 * @brief Deletes the database with a specified path. 223 * 224 * @param config Represents a pointer to an {@link OH_Rdb_Config} instance. 225 * Indicates the configuration of the database related to this RDB store. 226 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 227 * {@link RDB_OK} - success. 228 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 229 * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 230 * @see OH_Rdb_ErrCode. 231 * @since 10 232 */ 233 int OH_Rdb_DeleteStore(const OH_Rdb_Config *config); 234 235 /** 236 * @brief Inserts a row of data into the target table. 237 * 238 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 239 * @param table Indicates the target table. 240 * @param valuesBucket Indicates the row of data {@link OH_VBucket} to be inserted into the table. 241 * @return Returns the rowId if success, returns a specific error code. 242 * {@link RDB_ERR} - Indicates that the function execution exception. 243 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 244 * Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 245 * @see OH_Rdb_Store, OH_VBucket, OH_Rdb_ErrCode. 246 * @since 10 247 */ 248 int OH_Rdb_Insert(OH_Rdb_Store *store, const char *table, OH_VBucket *valuesBucket); 249 250 /** 251 * @brief Updates data in the database based on specified conditions. 252 * 253 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 254 * @param valuesBucket Indicates the row of data {@link OH__VBucket} to be updated in the database 255 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 256 * Indicates the specified update condition. 257 * @return Returns the number of rows changed if success, otherwise, returns a specific error code. 258 * {@link RDB_ERR} - Indicates that the function execution exception. 259 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 260 * Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 261 * @see OH_Rdb_Store, OH_Bucket, OH_Predicates, OH_Rdb_ErrCode. 262 * @since 10 263 */ 264 int OH_Rdb_Update(OH_Rdb_Store *store, OH_VBucket *valuesBucket, OH_Predicates *predicates); 265 266 /** 267 * @brief Deletes data from the database based on specified conditions. 268 * 269 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 270 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 271 * Indicates the specified delete condition. 272 * @return Returns the number of rows changed if success, otherwise, returns a specific error code. 273 * {@link RDB_ERR} - Indicates that the function execution exception. 274 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 275 * Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 276 * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode. 277 * @since 10 278 */ 279 int OH_Rdb_Delete(OH_Rdb_Store *store, OH_Predicates *predicates); 280 281 /** 282 * @brief Queries data in the database based on specified conditions. 283 * 284 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 285 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 286 * Indicates the specified query condition. 287 * @param columnNames Indicates the columns to query. If the value is empty array, the query applies to all columns. 288 * @param length Indicates the length of columnNames. 289 * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 290 * If Get store failed or resultSet is nullptr, nullptr is returned. 291 * @see OH_Rdb_Store, OH_Predicates, OH_Cursor. 292 * @since 10 293 */ 294 OH_Cursor *OH_Rdb_Query(OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length); 295 296 /** 297 * @brief Executes an SQL statement. 298 * 299 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 300 * @param sql Indicates the SQL statement to execute. 301 * @return Returns the status code of the execution. 302 * {@link RDB_OK} - success. 303 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 304 * @see OH_Rdb_Store. 305 * @since 10 306 */ 307 int OH_Rdb_Execute(OH_Rdb_Store *store, const char *sql); 308 309 /** 310 * @brief Queries data in the database based on an SQL statement. 311 * 312 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 313 * @param sql Indicates the SQL statement to execute. 314 * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 315 * If Get store failed,sql is nullptr or resultSet is nullptr, nullptr is returned. 316 * @see OH_Rdb_Store. 317 * @since 10 318 */ 319 OH_Cursor *OH_Rdb_ExecuteQuery(OH_Rdb_Store *store, const char *sql); 320 321 /** 322 * @brief Begins a transaction in EXCLUSIVE mode. 323 * 324 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 325 * @return Returns the status code of the execution. 326 * {@link RDB_OK} - success. 327 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 328 * @see OH_Rdb_Store. 329 * @since 10 330 */ 331 int OH_Rdb_BeginTransaction(OH_Rdb_Store *store); 332 333 /** 334 * @brief Rolls back a transaction in EXCLUSIVE mode. 335 * 336 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 337 * @return Returns the status code of the execution. 338 * {@link RDB_OK} - success. 339 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 340 * @see OH_Rdb_Store. 341 * @since 10 342 */ 343 int OH_Rdb_RollBack(OH_Rdb_Store *store); 344 345 /** 346 * @brief Commits a transaction in EXCLUSIVE mode. 347 * 348 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 349 * @return Returns the status code of the execution. 350 * {@link RDB_OK} - success. 351 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 352 * @see OH_Rdb_Store. 353 * @since 10 354 */ 355 int OH_Rdb_Commit(OH_Rdb_Store *store); 356 357 /** 358 * @brief Backs up a database on specified path. 359 * 360 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 361 * @param databasePath Indicates the database file path. 362 * @return Returns the status code of the execution. 363 * {@link RDB_OK} - success. 364 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 365 * @see OH_Rdb_Store. 366 * @since 10 367 */ 368 int OH_Rdb_Backup(OH_Rdb_Store *store, const char *databasePath); 369 370 /** 371 * @brief Restores a database from a specified database file. 372 * 373 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 374 * @param databasePath Indicates the database file path. 375 * @return Returns the status code of the execution. 376 * {@link RDB_OK} - success. 377 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 378 * @see OH_Rdb_Store. 379 * @since 10 380 */ 381 int OH_Rdb_Restore(OH_Rdb_Store *store, const char *databasePath); 382 383 /** 384 * @brief Gets the version of a database. 385 * 386 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 387 * @param version Indicates the version number. 388 * @return Returns the status code of the execution. 389 * {@link RDB_OK} - success. 390 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 391 * @see OH_Rdb_Store. 392 * @since 10 393 */ 394 int OH_Rdb_GetVersion(OH_Rdb_Store *store, int *version); 395 396 /** 397 * @brief Sets the version of a database. 398 * 399 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 400 * @param version Indicates the version number. 401 * @return Returns the status code of the execution. 402 * {@link RDB_OK} - success. 403 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 404 * @see OH_Rdb_Store. 405 * @since 10 406 */ 407 int OH_Rdb_SetVersion(OH_Rdb_Store *store, int version); 408 409 /** 410 * @brief Describes the distribution type of the tables. 411 * 412 * @since 11 413 */ 414 typedef enum Rdb_DistributedType { 415 /** 416 * @brief Indicates the table is distributed among the devices. 417 */ 418 RDB_DISTRIBUTED_CLOUD 419 } Rdb_DistributedType; 420 421 /** 422 * @brief Indicates version of {@link Rdb_DistributedConfig} 423 * 424 * @since 11 425 */ 426 #define DISTRIBUTED_CONFIG_VERSION 1 427 /** 428 * @brief Manages the distributed configuration of the table. 429 * 430 * @since 11 431 */ 432 typedef struct Rdb_DistributedConfig { 433 /** 434 * The version used to uniquely identify the Rdb_DistributedConfig struct. 435 */ 436 int version; 437 /** 438 * Specifies whether the table auto syncs. 439 */ 440 bool isAutoSync; 441 } Rdb_DistributedConfig; 442 443 /** 444 * @brief Set table to be distributed table. 445 * 446 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 447 * @param tables Indicates the table names you want to set. 448 * @param count Indicates the count of tables you want to set. 449 * @param type Indicates the distributed type {@link Rdb_DistributedType}. 450 * @param config Indicates the distributed config of the tables. For details, see {@link Rdb_DistributedConfig}. 451 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 452 * {@link RDB_OK} - success. 453 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 454 * @see OH_Rdb_Store. 455 * @see Rdb_DistributedConfig. 456 * @since 11 457 */ 458 int OH_Rdb_SetDistributedTables(OH_Rdb_Store *store, const char *tables[], uint32_t count, Rdb_DistributedType type, 459 const Rdb_DistributedConfig *config); 460 461 /** 462 * @brief Set table to be distributed table. 463 * 464 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 465 * @param tableName Indicates the name of the table to check. 466 * @param columnName Indicates the name of the column corresponding to the primary key. 467 * If the table has no primary key , please pass in "rowid". 468 * @param values Indicates the primary keys of the rows to check. 469 * If the table has no primary key , please pass in the row-ids of the rows to check. 470 * @return If the operation is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 471 * If Get store failed, nullptr is returned. 472 * There are two columns, "data_key" and "timestamp". Otherwise NULL is returned. 473 * @see OH_Rdb_Store. 474 * @see OH_VObject. 475 * @see OH_Cursor. 476 * @since 11 477 */ 478 OH_Cursor *OH_Rdb_FindModifyTime(OH_Rdb_Store *store, const char *tableName, const char *columnName, 479 OH_VObject *values); 480 481 /** 482 * @brief Describes the change type. 483 * 484 * @since 11 485 */ 486 typedef enum Rdb_ChangeType { 487 /** 488 * @brief Means the change type is data change. 489 */ 490 RDB_DATA_CHANGE, 491 /** 492 * @brief Means the change type is asset change. 493 */ 494 RDB_ASSET_CHANGE 495 } Rdb_ChangeType; 496 497 /** 498 * @brief Describes the primary keys or row-ids of changed rows. 499 * 500 * @since 11 501 */ 502 typedef struct Rdb_KeyInfo { 503 /** 504 * Indicates the count of the primary keys or row-ids. 505 */ 506 int count; 507 508 /** 509 * Indicates data type {@link OH_ColumnType} of the key. 510 */ 511 int type; 512 513 /** 514 * Indicates the data of the key info. 515 */ 516 union Rdb_KeyData { 517 /** 518 * Indicates uint64_t type of the data. 519 */ 520 uint64_t integer; 521 522 /** 523 * Indicates double type of the data. 524 */ 525 double real; 526 527 /** 528 * Indicates const char * type of the data. 529 */ 530 const char *text; 531 } *data; 532 } Rdb_KeyInfo; 533 534 /** 535 * @brief Indicates version of {@link Rdb_ChangeInfo} 536 * 537 * @since 11 538 */ 539 #define DISTRIBUTED_CHANGE_INFO_VERSION 1 540 541 /** 542 * @brief Describes the notify info of data change. 543 * 544 * @since 11 545 */ 546 typedef struct Rdb_ChangeInfo { 547 /** 548 * The version used to uniquely identify the Rdb_ChangeInfo struct. 549 */ 550 int version; 551 552 /** 553 * The name of changed table. 554 */ 555 const char *tableName; 556 557 /** 558 * The {@link Rdb_ChangeType} of changed table. 559 */ 560 int ChangeType; 561 562 /** 563 * The {@link Rdb_KeyInfo} of inserted rows. 564 */ 565 Rdb_KeyInfo inserted; 566 567 /** 568 * The {@link Rdb_KeyInfo} of updated rows. 569 */ 570 Rdb_KeyInfo updated; 571 572 /** 573 * The {@link Rdb_KeyInfo} of deleted rows. 574 */ 575 Rdb_KeyInfo deleted; 576 } Rdb_ChangeInfo; 577 578 /** 579 * @brief Indicates the subscribe type. 580 * 581 * @since 11 582 */ 583 typedef enum Rdb_SubscribeType { 584 /** 585 * @brief Subscription to cloud data changes. 586 */ 587 RDB_SUBSCRIBE_TYPE_CLOUD, 588 589 /** 590 * @brief Subscription to cloud data change details. 591 */ 592 RDB_SUBSCRIBE_TYPE_CLOUD_DETAILS, 593 594 /** 595 * @brief Subscription to local data change details. 596 * @since 12 597 */ 598 RDB_SUBSCRIBE_TYPE_LOCAL_DETAILS, 599 } Rdb_SubscribeType; 600 601 /** 602 * @brief The callback function of cloud data change event. 603 * 604 * @param context Represents the context of data observer. 605 * @param values Indicates the cloud accounts that changed. 606 * @param count The count of changed cloud accounts. 607 * @see OH_VObject. 608 * @since 11 609 */ 610 typedef void (*Rdb_BriefObserver)(void *context, const char *values[], uint32_t count); 611 612 /** 613 * @brief The callback function of cloud data change details event. 614 * 615 * @param context Represents the context of data observer. 616 * @param changeInfo Indicates the {@link Rdb_ChangeInfo} of changed tables. 617 * @param count The count of changed tables. 618 * @see Rdb_ChangeInfo. 619 * @since 11 620 */ 621 typedef void (*Rdb_DetailsObserver)(void *context, const Rdb_ChangeInfo **changeInfo, uint32_t count); 622 623 /** 624 * @brief Indicates the callback functions. 625 * 626 * @since 11 627 */ 628 typedef union Rdb_SubscribeCallback { 629 /** 630 * The callback function of cloud data change details event. 631 */ 632 Rdb_DetailsObserver detailsObserver; 633 634 /** 635 * The callback function of cloud data change event. 636 */ 637 Rdb_BriefObserver briefObserver; 638 } Rdb_SubscribeCallback; 639 640 /** 641 * @brief Indicates the observer of data. 642 * 643 * @since 11 644 */ 645 typedef struct Rdb_DataObserver { 646 /** 647 * The context of data observer. 648 */ 649 void *context; 650 651 /** 652 * The callback of data observer. 653 */ 654 Rdb_SubscribeCallback callback; 655 } Rdb_DataObserver; 656 657 /** 658 * @brief Registers an observer for the database. 659 * When data in the distributed database changes, the callback will be invoked. 660 * 661 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 662 * @param type Indicates the subscription type, which is defined in {@link Rdb_SubscribeType}. 663 * @param observer The {@link Rdb_DataObserver} of change events in the database. 664 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 665 * {@link RDB_OK} - success. 666 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 667 * @see OH_Rdb_Store. 668 * @see Rdb_DataObserver. 669 * @since 11 670 */ 671 int OH_Rdb_Subscribe(OH_Rdb_Store *store, Rdb_SubscribeType type, const Rdb_DataObserver *observer); 672 673 /** 674 * @brief Remove specified observer of specified type from the database. 675 * 676 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 677 * @param type Indicates the subscription type, which is defined in {@link Rdb_SubscribeType}. 678 * @param observer The {@link Rdb_DataObserver} of change events in the database. 679 * If this is nullptr, remove all observers of the type. 680 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 681 * {@link RDB_OK} - success. 682 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 683 * @see OH_Rdb_Store. 684 * @see Rdb_DataObserver. 685 * @since 11 686 */ 687 int OH_Rdb_Unsubscribe(OH_Rdb_Store *store, Rdb_SubscribeType type, const Rdb_DataObserver *observer); 688 689 /** 690 * @brief Indicates the database synchronization mode. 691 * 692 * @since 11 693 */ 694 typedef enum Rdb_SyncMode { 695 /** 696 * @brief Indicates that data is synchronized from the end with the closest modification time 697 * to the end with a more distant modification time. 698 */ 699 RDB_SYNC_MODE_TIME_FIRST, 700 /** 701 * @brief Indicates that data is synchronized from local to cloud. 702 */ 703 RDB_SYNC_MODE_NATIVE_FIRST, 704 /** 705 * @brief Indicates that data is synchronized from cloud to local. 706 */ 707 RDB_SYNC_MODE_CLOUD_FIRST 708 } Rdb_SyncMode; 709 710 /** 711 * @brief Describes the statistic of the cloud sync process. 712 * 713 * @since 11 714 */ 715 typedef struct Rdb_Statistic { 716 /** 717 * Describes the total number of data to sync. 718 */ 719 int total; 720 721 /** 722 * Describes the number of successfully synced data. 723 */ 724 int successful; 725 726 /** 727 * Describes the number of data failed to sync. 728 */ 729 int failed; 730 731 /** 732 * Describes the number of data remained to sync. 733 */ 734 int remained; 735 } Rdb_Statistic; 736 737 /** 738 * @brief Describes the {@link Rdb_Statistic} details of the table. 739 * 740 * @since 11 741 */ 742 typedef struct Rdb_TableDetails { 743 /** 744 * Indicates the name of changed table. 745 */ 746 const char *table; 747 748 /** 749 * Describes the {@link Rdb_Statistic} details of the upload process. 750 */ 751 Rdb_Statistic upload; 752 753 /** 754 * Describes the {@link Rdb_Statistic} details of the download process. 755 */ 756 Rdb_Statistic download; 757 } Rdb_TableDetails; 758 759 /** 760 * The cloud sync progress 761 * 762 * @since 11 763 */ 764 typedef enum Rdb_Progress { 765 /** 766 * @brief Means the sync process begin. 767 */ 768 RDB_SYNC_BEGIN, 769 770 /** 771 * @brief Means the sync process is in progress 772 */ 773 RDB_SYNC_IN_PROGRESS, 774 775 /** 776 * @brief Means the sync process is finished 777 */ 778 RDB_SYNC_FINISH 779 } Rdb_Progress; 780 781 /** 782 * Describes the status of cloud sync progress. 783 * 784 * @since 11 785 */ 786 typedef enum Rdb_ProgressCode { 787 /** 788 * @brief Means the status of progress is success. 789 */ 790 RDB_SUCCESS, 791 792 /** 793 * @brief Means the progress meets unknown error. 794 */ 795 RDB_UNKNOWN_ERROR, 796 797 /** 798 * @brief Means the progress meets network error. 799 */ 800 RDB_NETWORK_ERROR, 801 802 /** 803 * @brief Means cloud is disabled. 804 */ 805 RDB_CLOUD_DISABLED, 806 807 /** 808 * @brief Means the progress is locked by others. 809 */ 810 RDB_LOCKED_BY_OTHERS, 811 812 /** 813 * @brief Means the record exceeds the limit. 814 */ 815 RDB_RECORD_LIMIT_EXCEEDED, 816 817 /** 818 * Means the cloud has no space for the asset. 819 */ 820 RDB_NO_SPACE_FOR_ASSET 821 } Rdb_ProgressCode; 822 823 /** 824 * @brief Indicates version of {@link Rdb_ProgressDetails} 825 * 826 * @since 11 827 */ 828 #define DISTRIBUTED_PROGRESS_DETAIL_VERSION 1 829 830 /** 831 * @brief Describes detail of the cloud sync progress. 832 * 833 * @since 11 834 */ 835 typedef struct Rdb_ProgressDetails { 836 /** 837 * The version used to uniquely identify the Rdb_ProgressDetails struct. 838 */ 839 int version; 840 841 /** 842 * Describes the status of data sync progress. Defined in {@link Rdb_Progress}. 843 */ 844 int schedule; 845 846 /** 847 * Describes the code of data sync progress. Defined in {@link Rdb_ProgressCode}. 848 */ 849 int code; 850 851 /** 852 * Describes the length of changed tables in data sync progress. 853 */ 854 int32_t tableLength; 855 } Rdb_ProgressDetails; 856 857 /** 858 * @brief Get table details from progress details. 859 * 860 * @param progress Represents a pointer to an {@link Rdb_ProgressDetails} instance. 861 * @param version Indicates the version of current {@link Rdb_ProgressDetails}. 862 * @return If the operation is successful, a pointer to the instance of the {@link Rdb_TableDetails} 863 * structure is returned.If get details is failed, nullptr is returned. 864 * @see Rdb_ProgressDetails 865 * @see Rdb_TableDetails 866 * @since 11 867 */ 868 Rdb_TableDetails *OH_Rdb_GetTableDetails(Rdb_ProgressDetails *progress, int32_t version); 869 870 /** 871 * @brief The callback function of progress. 872 * 873 * @param progressDetails The details of the sync progress. 874 * @see Rdb_ProgressDetails. 875 * @since 11 876 */ 877 typedef void (*Rdb_ProgressCallback)(void *context, Rdb_ProgressDetails *progressDetails); 878 879 /** 880 * @brief The callback function of sync. 881 * 882 * @param progressDetails The details of the sync progress. 883 * @see Rdb_ProgressDetails. 884 * @since 11 885 */ 886 typedef void (*Rdb_SyncCallback)(Rdb_ProgressDetails *progressDetails); 887 888 /** 889 * @brief The observer of progress. 890 * 891 * @since 11 892 */ 893 typedef struct Rdb_ProgressObserver { 894 /** 895 * The context of progress observer. 896 */ 897 void *context; 898 899 /** 900 * The callback function of progress observer. 901 */ 902 Rdb_ProgressCallback callback; 903 } Rdb_ProgressObserver; 904 905 /** 906 * @brief Sync data to cloud. 907 * 908 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 909 * @param mode Represents the {@link Rdb_SyncMode} of sync progress. 910 * @param tables Indicates the names of tables to sync. 911 * @param count The count of tables to sync. If value equals 0, sync all tables of the store. 912 * @param observer The {@link Rdb_ProgressObserver} of cloud sync progress. 913 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 914 * {@link RDB_OK} - success. 915 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 916 * @see OH_Rdb_Store. 917 * @see Rdb_ProgressObserver. 918 * @since 11 919 */ 920 int OH_Rdb_CloudSync(OH_Rdb_Store *store, Rdb_SyncMode mode, const char *tables[], uint32_t count, 921 const Rdb_ProgressObserver *observer); 922 923 /** 924 * @brief Subscribes to the automatic synchronization progress of an RDB store. 925 * A callback will be invoked when there is a notification of the automatic synchronization progress. 926 * 927 * @param store Indicates the pointer to the target {@Link OH_Rdb_Store} instance. 928 * @param observer The {@link Rdb_SyncObserver} for the automatic synchornizaiton progress 929 * Indicates the callback invoked to return the automatic synchronization progress. 930 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 931 * {@link RDB_OK} - success. 932 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 933 * @see OH_Rdb_Store. 934 * @see Rdb_ProgressObserver. 935 * @since 11 936 */ 937 int OH_Rdb_SubscribeAutoSyncProgress(OH_Rdb_Store *store, const Rdb_ProgressObserver *observer); 938 939 /** 940 * @brief Unsubscribes from the automatic synchronization progress of an RDB store. 941 * 942 * @param store Indicates the pointer to the target {@Link OH_Rdb_Store} instance. 943 * @param observer Indicates the {@link Rdb_SyncObserver} callback for the automatic synchronization progress. 944 * If it is a null pointer, all callbacks for the automatic synchronization progress will be unregistered. 945 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 946 * {@link RDB_OK} - success. 947 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 948 * @see OH_Rdb_Store. 949 * @see Rdb_ProgressObserver. 950 * @since 11 951 */ 952 int OH_Rdb_UnsubscribeAutoSyncProgress(OH_Rdb_Store *store, const Rdb_ProgressObserver *observer); 953 954 /** 955 * @brief Lock data from the database based on specified conditions. 956 * 957 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 958 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 959 * Indicates the specified lock condition. 960 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 961 * {@link RDB_OK} - success. 962 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 963 * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode. 964 * @since 12 965 */ 966 int OH_Rdb_LockRow(OH_Rdb_Store *store, OH_Predicates *predicates); 967 968 /** 969 * @brief Unlock data from the database based on specified conditions. 970 * 971 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 972 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 973 * Indicates the specified unlock condition. 974 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 975 * {@link RDB_OK} - success. 976 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 977 * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode. 978 * @since 12 979 */ 980 int OH_Rdb_UnlockRow(OH_Rdb_Store *store, OH_Predicates *predicates); 981 982 /** 983 * @brief Queries locked data in the database based on specified conditions. 984 * 985 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 986 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 987 * Indicates the specified query condition. 988 * @param columnNames Indicates the columns to query. If the value is empty array, the query applies to all columns. 989 * @param length Indicates the length of columnNames. 990 * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 991 * If Get store failed or resultSet is nullptr, nullptr is returned. 992 * @see OH_Rdb_Store, OH_Predicates, OH_Cursor. 993 * @since 12 994 */ 995 OH_Cursor *OH_Rdb_QueryLockedRow( 996 OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length); 997 #ifdef __cplusplus 998 }; 999 #endif 1000 1001 #endif // RELATIONAL_STORE_H 1002