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 /** 17 * @addtogroup RDB 18 * @{ 19 * 20 * @brief The relational database (RDB) store manages data based on relational models. 21 * With the underlying SQLite database, the RDB store provides a complete mechanism for managing local databases. 22 * To satisfy different needs in complicated scenarios, the RDB store offers a series of APIs for performing operations 23 * such as adding, deleting, modifying, and querying data, and supports direct execution of SQL statements. 24 * 25 * @since 10 26 */ 27 28 /** 29 * @file relational_store.h 30 * 31 * @brief Provides database related functions and enumerations. 32 * 33 * @kit ArkData 34 * @library libnative_rdb_ndk.so 35 * @syscap SystemCapability.DistributedDataManager.RelationalStore.Core 36 * @since 10 37 */ 38 39 #ifndef RELATIONAL_STORE_H 40 #define RELATIONAL_STORE_H 41 42 #include "database/rdb/oh_cursor.h" 43 #include "database/rdb/oh_predicates.h" 44 #include "database/rdb/oh_value_object.h" 45 #include "database/rdb/oh_values_bucket.h" 46 #include "database/rdb/oh_rdb_transaction.h" 47 #include "database/rdb/oh_rdb_types.h" 48 #include "database/rdb/oh_rdb_crypto_param.h" 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif 53 54 /** 55 * @brief Describe the security level of the database. 56 * 57 * @since 10 58 */ 59 typedef enum OH_Rdb_SecurityLevel { 60 /** 61 * @brief Low-level security. Data leaks have a minor impact. 62 */ 63 S1 = 1, 64 /** 65 * @brief Medium-level security. Data leaks have a major impact. 66 */ 67 S2, 68 /** 69 * @brief High-level security. Data leaks have a severe impact. 70 */ 71 S3, 72 /** 73 * @brief Critical-level security. Data leaks have a critical impact. 74 */ 75 S4 76 } OH_Rdb_SecurityLevel; 77 78 /** 79 * @brief Describe the security area of the database. 80 * 81 * @since 11 82 */ 83 typedef enum Rdb_SecurityArea { 84 /** 85 * @brief Security Area 1. 86 */ 87 RDB_SECURITY_AREA_EL1 = 1, 88 /** 89 * @brief Security Area 2. 90 */ 91 RDB_SECURITY_AREA_EL2, 92 /** 93 * @brief Security Area 3. 94 */ 95 RDB_SECURITY_AREA_EL3, 96 /** 97 * @brief Security Area 4. 98 */ 99 RDB_SECURITY_AREA_EL4, 100 101 /** 102 * @brief Security Area 5. 103 * 104 * @since 12 105 */ 106 RDB_SECURITY_AREA_EL5, 107 } Rdb_SecurityArea; 108 109 /** 110 * @brief Manages relational database configurations. 111 * @since 10 112 */ 113 #pragma pack(1) 114 typedef struct { 115 /** 116 * Indicates the size of the {@link OH_Rdb_Config}. It is mandatory. 117 */ 118 int selfSize; 119 /** 120 * Indicates the directory of the database. 121 */ 122 const char *dataBaseDir; 123 /** 124 * Indicates the name of the database. 125 */ 126 const char *storeName; 127 /** 128 * Indicates the bundle name of the application. 129 */ 130 const char *bundleName; 131 /** 132 * Indicates the module name of the application. 133 */ 134 const char *moduleName; 135 /** 136 * Indicates whether the database is encrypted. 137 */ 138 bool isEncrypt; 139 /** 140 * Indicates the security level {@link OH_Rdb_SecurityLevel} of the database. 141 */ 142 int securityLevel; 143 /** 144 * Indicates the security area {@link Rdb_SecurityArea} of the database. 145 * 146 * @since 11 147 */ 148 int area; 149 } OH_Rdb_Config; 150 #pragma pack() 151 152 /** 153 * @brief Define OH_Rdb_Store type. 154 * 155 * @since 10 156 */ 157 typedef struct { 158 /** 159 * The id used to uniquely identify the OH_Rdb_Store struct. 160 */ 161 int64_t id; 162 } OH_Rdb_Store; 163 164 /** 165 * @brief Define OH_Rdb_ConfigV2 type. 166 * 167 * @since 14 168 */ 169 typedef struct OH_Rdb_ConfigV2 OH_Rdb_ConfigV2; 170 171 /** 172 * @brief Define Rdb_DBType type. 173 * 174 * @since 14 175 */ 176 typedef enum Rdb_DBType { 177 /** 178 * @brief Means using SQLITE as the db kernal 179 */ 180 RDB_SQLITE = 1, 181 /** 182 * @brief Means using CARLEY_DB as the db kernal 183 */ 184 RDB_CAYLEY = 2, 185 /** 186 * @brief Means largest value for Rdb_DBType 187 */ 188 DBTYPE_BUTT = 64, 189 } Rdb_DBType; 190 191 /** 192 * @brief Define Rdb_Tokenizer type. 193 * 194 * @since 17 195 */ 196 typedef enum Rdb_Tokenizer { 197 /** 198 * @brief Means not using tokenizer. 199 * @since 17 200 */ 201 RDB_NONE_TOKENIZER = 1, 202 /** 203 * @brief Means using native icu tokenizer. 204 * @since 17 205 */ 206 RDB_ICU_TOKENIZER = 2, 207 /** 208 * @brief Means using self-developed enhance tokenizer. 209 * @since 18 210 */ 211 RDB_CUSTOM_TOKENIZER = 3, 212 } Rdb_Tokenizer; 213 214 /** 215 * @brief Create OH_Rdb_ConfigV2 which is used to open store 216 * 217 * @return Returns the newly created OH_Rdb_ConfigV2 object. If NULL is returned, the creation fails. 218 * The possible cause is that the address space of the application is full, As a result, the space 219 * cannot be allocated. 220 * @see OH_Rdb_ConfigV2 221 * @since 14 222 */ 223 OH_Rdb_ConfigV2 *OH_Rdb_CreateConfig(); 224 225 /** 226 * @brief Destroy OH_Rdb_ConfigV2 which is created by OH_Rdb_CreateConfig 227 * 228 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 229 * Indicates the configuration of the database related to this RDB store. 230 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 231 * {@link RDB_OK} - success. 232 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 233 * @since 14 234 */ 235 int OH_Rdb_DestroyConfig(OH_Rdb_ConfigV2 *config); 236 237 /** 238 * @brief Set property databaseDir into config 239 * 240 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 241 * Indicates the configuration of the database related to this RDB store. 242 * @param databaseDir Indicates the directory of the database. 243 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 244 * {@link RDB_OK} - success. 245 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 246 * @since 14 247 */ 248 int OH_Rdb_SetDatabaseDir(OH_Rdb_ConfigV2 *config, const char *databaseDir); 249 250 /** 251 * @brief Set property storeName into config 252 * 253 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 254 * Indicates the configuration of the database related to this RDB store. 255 * @param storeName Indicates the name of the database. 256 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 257 * {@link RDB_OK} - success. 258 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 259 * @since 14 260 */ 261 int OH_Rdb_SetStoreName(OH_Rdb_ConfigV2 *config, const char *storeName); 262 263 /** 264 * @brief Set property bundleName into config 265 * 266 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 267 * Indicates the configuration of the database related to this RDB store. 268 * @param bundleName Indicates the bundle name of the application 269 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 270 * {@link RDB_OK} - success. 271 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 272 * @since 14 273 */ 274 int OH_Rdb_SetBundleName(OH_Rdb_ConfigV2 *config, const char *bundleName); 275 276 /** 277 * @brief Set property moduleName into config 278 * 279 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 280 * Indicates the configuration of the database related to this RDB store. 281 * @param moduleName Indicates the module name of the application. 282 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 283 * {@link RDB_OK} - success. 284 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 285 * @since 14 286 */ 287 int OH_Rdb_SetModuleName(OH_Rdb_ConfigV2 *config, const char *moduleName); 288 289 /** 290 * @brief Set property isEncrypted into config 291 * 292 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 293 * Indicates the configuration of the database related to this RDB store. 294 * @param isEncrypted Indicates whether the database is encrypted. 295 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 296 * {@link RDB_OK} - success. 297 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 298 * @since 14 299 */ 300 int OH_Rdb_SetEncrypted(OH_Rdb_ConfigV2 *config, bool isEncrypted); 301 302 /** 303 * @brief Set property securityLevel into config 304 * 305 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 306 * Indicates the configuration of the database related to this RDB store. 307 * @param securityLevel Indicates the security level {@link OH_Rdb_SecurityLevel} of the database. 308 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 309 * {@link RDB_OK} - success. 310 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 311 * @since 14 312 */ 313 int OH_Rdb_SetSecurityLevel(OH_Rdb_ConfigV2 *config, int securityLevel); 314 315 /** 316 * @brief Set property area into config 317 * 318 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 319 * Indicates the configuration of the database related to this RDB store 320 * @param area Represents the security area of the database. 321 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 322 * {@link RDB_OK} - success. 323 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 324 * @since 14 325 */ 326 int OH_Rdb_SetArea(OH_Rdb_ConfigV2 *config, int area); 327 328 /** 329 * @brief Set property dbType into config 330 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 331 * @param dbType Indicates the dbType {@link Rdb_DBType} of the database 332 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 333 * {@link RDB_OK} - success. 334 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 335 * {@link RDB_E_NOT_SUPPORTED} - The error code for not support db types. 336 * @since 14 337 */ 338 int OH_Rdb_SetDbType(OH_Rdb_ConfigV2 *config, int dbType); 339 340 /** 341 * @brief Sets the customized directory relative to the database. 342 * 343 * @param config Represents a pointer to a configuration of the database related to this relation database store. 344 * @param customDir Represents the customized relative to the database directory, the value cannot exceed 128 bytes. 345 * @return Returns the error code. 346 * Returns {@link RDB_OK} if the execution is successful. 347 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 348 * @since 20 349 */ 350 int OH_Rdb_SetCustomDir(OH_Rdb_ConfigV2 *config, const char *customDir); 351 352 /** 353 * @brief Sets the relation database store is read-only mode. 354 * 355 * @param config Represents a pointer to a configuration of the database related to this relation database store. 356 * @param readOnly Represents whether the relation database store is read-only. 357 * @return Returns the error code. 358 * Returns {@link RDB_OK} if the execution is successful. 359 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 360 * @since 20 361 */ 362 int OH_Rdb_SetReadOnly(OH_Rdb_ConfigV2 *config, bool readOnly); 363 364 /** 365 * @brief Sets the dynamic libraries with capabilities such as Full-Text Search (FTS). 366 * 367 * @param config Represents a pointer to a configuration of the database related to this relation database store. 368 * @param plugins Represents the dynamic libraries. 369 * @param length the size of plugins that the maximum value is 16. 370 * @return Returns the error code. 371 * Returns {@link RDB_OK} if the execution is successful. 372 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 373 * @since 20 374 */ 375 int OH_Rdb_SetPlugins(OH_Rdb_ConfigV2 *config, const char **plugins, int32_t length); 376 377 /** 378 * @brief Sets the custom encryption parameters. 379 * 380 * @param config Represents a pointer to a configuration of the database related to this relation database store. 381 * @param cryptoParam Represents the custom encryption parameters. 382 * @return Returns the error code. 383 * Returns {@link RDB_OK} if the execution is successful. 384 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 385 * @since 20 386 */ 387 int OH_Rdb_SetCryptoParam(OH_Rdb_ConfigV2 *config, const OH_Rdb_CryptoParam *cryptoParam); 388 389 /** 390 * @brief Set property tokenizer into config 391 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 392 * @param tokenizer Indicates the tokenizer {@link Rdb_Tokenizer} of the database 393 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 394 * {@link RDB_OK} - success. 395 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 396 * {@link RDB_E_NOT_SUPPORTED} - The error code for not support tokenizer. 397 * @since 17 398 */ 399 int OH_Rdb_SetTokenizer(OH_Rdb_ConfigV2 *config, Rdb_Tokenizer tokenizer); 400 401 /** 402 * @brief Set property persist into config 403 * 404 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 405 * Indicates the configuration of the database related to this RDB store. 406 * @param isPersistent Indicates whether the database need persistence. 407 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 408 * {@link RDB_OK} - success. 409 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 410 * @since 18 411 */ 412 int OH_Rdb_SetPersistent(OH_Rdb_ConfigV2 *config, bool isPersistent); 413 414 /** 415 * @brief Set whether the database enable the capabilities for semantic indexing processing. 416 * 417 * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance. 418 * Indicates the configuration of the database related to this RDB store. 419 * @param enableSemanticIndex Indicates whether the database enable the capabilities for semantic indexing processing. 420 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 421 * {@link RDB_OK} - success. 422 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 423 * @since 20 424 */ 425 int OH_Rdb_SetSemanticIndex(OH_Rdb_ConfigV2 *config, bool enableSemanticIndex); 426 427 /** 428 * @brief Check if a tokenizer is supported or not. 429 * 430 * @param tokenizer the tokenizer type of {@Link Rdb_Tokenizer}. 431 * @param isSupported Pointer to the Boolean value obtained. 432 * @return Returns the status code of the execution. 433 * {@link RDB_OK} indicates the operation is successful. 434 * {@link RDB_E_INVALID_ARGS} indicates invalid args are passed in. 435 * @since 18 436 */ 437 int OH_Rdb_IsTokenizerSupported(Rdb_Tokenizer tokenizer, bool *isSupported); 438 439 /** 440 * @brief Get support db type list 441 * @param typeCount The output parameter, which is used to receive the length of the support db type array. 442 * @return Return Rdb_DBType array contains supported db type, array length is number of support type 443 * @since 14 444 */ 445 const int *OH_Rdb_GetSupportedDbType(int *typeCount); 446 447 /** 448 * @brief Creates an {@link OH_VObject} instance. 449 * 450 * @return If the creation is successful, a pointer to the instance of the @link OH_VObject} structure is returned, 451 * otherwise NULL is returned. 452 * @see OH_VObject. 453 * @since 10 454 */ 455 OH_VObject *OH_Rdb_CreateValueObject(); 456 457 /** 458 * @brief Creates an {@link OH_VBucket} object. 459 * 460 * @return If the creation is successful, a pointer to the instance of the @link OH_VBucket} structure is returned, 461 * otherwise NULL is returned. 462 * @see OH_VBucket. 463 * @since 10 464 */ 465 OH_VBucket *OH_Rdb_CreateValuesBucket(); 466 467 /** 468 * @brief Creates an {@link OH_Predicates} instance. 469 * 470 * @param table Indicates the table name. 471 * @return If the creation is successful, a pointer to the instance of the @link OH_Predicates} structure is returned. 472 * If the table name is nullptr, Nullptr is returned. 473 * @see OH_Predicates. 474 * @since 10 475 */ 476 OH_Predicates *OH_Rdb_CreatePredicates(const char *table); 477 478 /** 479 * @brief Obtains an RDB store. 480 * 481 * You can set parameters of the RDB store as required. In general, 482 * this method is recommended to obtain a rdb store. 483 * 484 * @param config Represents a pointer to an {@link OH_Rdb_Config} instance. 485 * Indicates the configuration of the database related to this RDB store. 486 * @param errCode This parameter is the output parameter, 487 * and the execution status of a function is written to this variable. 488 * @return If the creation is successful, a pointer to the instance of the @link OH_Rdb_Store} structure is returned. 489 * If the Config is empty, config.size does not match, or errCode is empty. 490 * Get database path failed.Get RDB Store fail. Nullptr is returned. 491 * @see OH_Rdb_Config, OH_Rdb_Store. 492 * @since 10 493 */ 494 OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode); 495 496 /** 497 * @brief Obtains an RDB store with OH_Rdb_ConfigV2. 498 * 499 * You can set parameters of the RDB store as required. In general, 500 * this method is recommended to obtain a rdb store. 501 * 502 * @param config Represents a pointer to an {@link OH_Rdb_ConfigV2} instance. 503 * Indicates the configuration of the database related to this RDB store. 504 * @param errCode This parameter is the output parameter, 505 * and the execution status of a function is written to this variable. 506 * @return If the creation is successful, a pointer to the instance of the @link OH_Rdb_Store} structure is returned. 507 * If the Config is empty, config.size does not match, or errCode is empty. 508 * Get database path failed.Get RDB Store fail. Nullptr is returned. 509 * @see OH_Rdb_ConfigV2, OH_Rdb_Store. 510 * @since 14 511 */ 512 OH_Rdb_Store *OH_Rdb_CreateOrOpen(const OH_Rdb_ConfigV2 *config, int *errCode); 513 514 /** 515 * @brief Close the {@link OH_Rdb_Store} object and reclaim the memory occupied by the object. 516 * 517 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 518 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 519 * {@link RDB_OK} - success. 520 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 521 * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 522 * @see OH_Rdb_Store, OH_Rdb_ErrCode. 523 * @since 10 524 */ 525 int OH_Rdb_CloseStore(OH_Rdb_Store *store); 526 527 /** 528 * @brief Deletes the database with a specified path. 529 * 530 * @param config Represents a pointer to an {@link OH_Rdb_Config} instance. 531 * Indicates the configuration of the database related to this RDB store. 532 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 533 * {@link RDB_OK} - success. 534 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 535 * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 536 * @see OH_Rdb_ErrCode. 537 * @since 10 538 */ 539 int OH_Rdb_DeleteStore(const OH_Rdb_Config *config); 540 541 /** 542 * @brief Deletes the database with a specified path. 543 * 544 * @param config Represents a pointer to an {@link OH_Rdb_ConfigV2} instance. 545 * Indicates the configuration of the database related to this RDB store. 546 * @return Returns the status code of the execution. Successful execution returns RDB_OK, 547 * {@link RDB_OK} - success. 548 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 549 * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 550 * @see OH_Rdb_ErrCode. 551 * @since 14 552 */ 553 int OH_Rdb_DeleteStoreV2(const OH_Rdb_ConfigV2 *config); 554 555 /** 556 * @brief Inserts a row of data into the target table. 557 * 558 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 559 * @param table Indicates the target table. 560 * @param valuesBucket Indicates the row of data {@link OH_VBucket} to be inserted into the table. 561 * @return Returns the rowId if success, returns a specific error code. 562 * {@link RDB_ERR} - Indicates that the function execution exception. 563 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 564 * Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 565 * @see OH_Rdb_Store, OH_VBucket, OH_Rdb_ErrCode. 566 * @since 10 567 */ 568 int OH_Rdb_Insert(OH_Rdb_Store *store, const char *table, OH_VBucket *valuesBucket); 569 570 /** 571 * @brief Inserts a row of data into the target table and support conflict resolution. 572 * 573 * @param store Represents a pointer to an OH_Rdb_Store instance. 574 * @param table Represents the target table. 575 * @param row Represents the row data to be inserted into the table. 576 * @param resolution Represents the resolution when conflict occurs. 577 * @param rowId Represents the number of successful insertion. 578 * @return Returns the status code of the execution. 579 * Returns {@link RDB_OK} if the execution is successful. 580 * Returns {@link RDB_E_ERROR} database common error. 581 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 582 * Returns {@link RDB_E_ALREADY_CLOSED} database already closed. 583 * Returns {@link RDB_E_WAL_SIZE_OVER_LIMIT} the WAL file size over default limit. 584 * Returns {@link RDB_E_SQLITE_FULL} SQLite: The database is full. 585 * Returns {@link RDB_E_SQLITE_CORRUPT} database corrupted. 586 * Returns {@link RDB_E_SQLITE_PERM} SQLite: Access permission denied. 587 * Returns {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 588 * Returns {@link RDB_E_SQLITE_LOCKED} SQLite: A table in the database is locked. 589 * Returns {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 590 * Returns {@link RDB_E_SQLITE_READONLY} SQLite: Attempt to write a readonly database. 591 * Returns {@link RDB_E_SQLITE_IOERR} SQLite: Some kind of disk I/O error occurred. 592 * Returns {@link RDB_E_SQLITE_TOO_BIG} SQLite: TEXT or BLOB exceeds size limit. 593 * Returns {@link RDB_E_SQLITE_MISMATCH} SQLite: Data type mismatch. 594 * Returns {@link RDB_E_SQLITE_CONSTRAINT} SQLite: Abort due to constraint violation. 595 * @since 20 596 */ 597 int OH_Rdb_InsertWithConflictResolution(OH_Rdb_Store *store, const char *table, OH_VBucket *row, 598 Rdb_ConflictResolution resolution, int64_t *rowId); 599 600 /** 601 * @brief Inserts a batch of data into the target table. 602 * 603 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 604 * @param table Represents the target table. 605 * @param rows Represents the rows data to be inserted into the table. 606 * @param resolution Represents the resolution when conflict occurs. 607 * @param changes Represents the number of successful insertions. 608 * @return Returns the status code of the execution. 609 * Returns {@link RDB_OK} if the execution is successful. 610 * Returns {@link RDB_E_ERROR} database common error. 611 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 612 * Returns {@link RDB_E_ALREADY_CLOSED} database already closed. 613 * Returns {@link RDB_E_WAL_SIZE_OVER_LIMIT} the WAL file size over default limit. 614 * Returns {@link RDB_E_SQLITE_FULL} SQLite: The database is full. 615 * Returns {@link RDB_E_SQLITE_CORRUPT} database corrupted. 616 * Returns {@link RDB_E_SQLITE_PERM} SQLite: Access permission denied. 617 * Returns {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 618 * Returns {@link RDB_E_SQLITE_LOCKED} SQLite: A table in the database is locked. 619 * Returns {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 620 * Returns {@link RDB_E_SQLITE_READONLY} SQLite: Attempt to write a readonly database. 621 * Returns {@link RDB_E_SQLITE_IOERR} SQLite: Some kind of disk I/O error occurred. 622 * Returns {@link RDB_E_SQLITE_TOO_BIG} SQLite: TEXT or BLOB exceeds size limit. 623 * Returns {@link RDB_E_SQLITE_MISMATCH} SQLite: Data type mismatch. 624 * Returns {@link RDB_E_SQLITE_CONSTRAINT} SQLite: Abort due to constraint violation. 625 * @since 18 626 */ 627 int OH_Rdb_BatchInsert(OH_Rdb_Store *store, const char *table, 628 const OH_Data_VBuckets *rows, Rdb_ConflictResolution resolution, int64_t *changes); 629 630 /** 631 * @brief Updates data in the database based on specified conditions. 632 * 633 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 634 * @param valuesBucket Indicates the row of data {@link OH__VBucket} to be updated in the database 635 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 636 * Indicates the specified update condition. 637 * @return Returns the number of rows changed if success, otherwise, returns a specific error code. 638 * {@link RDB_ERR} - Indicates that the function execution exception. 639 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 640 * Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 641 * @see OH_Rdb_Store, OH_Bucket, OH_Predicates, OH_Rdb_ErrCode. 642 * @since 10 643 */ 644 int OH_Rdb_Update(OH_Rdb_Store *store, OH_VBucket *valuesBucket, OH_Predicates *predicates); 645 646 /** 647 * @brief Updates data in the database based on specified conditions and support conflict resolution. 648 * 649 * @param store Represents a pointer to an OH_Rdb_Store instance. 650 * @param row Represents the row data to be inserted into the table. 651 * @param predicates Represents a pointer to an link OH_Predicates instance. 652 * @param resolution Represents the resolution when conflict occurs. 653 * @param changes Represents the number of successful update. 654 * @return Returns the status code of the execution. 655 * Returns {@link RDB_OK} if the execution is successful. 656 * Returns {@link RDB_E_ERROR} database common error. 657 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 658 * Returns {@link RDB_E_ALREADY_CLOSED} database already closed. 659 * Returns {@link RDB_E_WAL_SIZE_OVER_LIMIT} the WAL file size over default limit. 660 * Returns {@link RDB_E_SQLITE_FULL} SQLite: The database is full. 661 * Returns {@link RDB_E_SQLITE_CORRUPT} database corrupted. 662 * Returns {@link RDB_E_SQLITE_PERM} SQLite: Access permission denied. 663 * Returns {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 664 * Returns {@link RDB_E_SQLITE_LOCKED} SQLite: A table in the database is locked. 665 * Returns {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 666 * Returns {@link RDB_E_SQLITE_READONLY} SQLite: Attempt to write a readonly database. 667 * Returns {@link RDB_E_SQLITE_IOERR} SQLite: Some kind of disk I/O error occurred. 668 * Returns {@link RDB_E_SQLITE_TOO_BIG} SQLite: TEXT or BLOB exceeds size limit. 669 * Returns {@link RDB_E_SQLITE_MISMATCH} SQLite: Data type mismatch. 670 * Returns {@link RDB_E_SQLITE_CONSTRAINT} SQLite: Abort due to constraint violation. 671 * @since 20 672 */ 673 int OH_Rdb_UpdateWithConflictResolution(OH_Rdb_Store *store, OH_VBucket *row, OH_Predicates *predicates, 674 Rdb_ConflictResolution resolution, int64_t *changes); 675 676 /** 677 * @brief Deletes data from the database based on specified conditions. 678 * 679 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 680 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 681 * Indicates the specified delete condition. 682 * @return Returns the number of rows changed if success, otherwise, returns a specific error code. 683 * {@link RDB_ERR} - Indicates that the function execution exception. 684 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 685 * Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 686 * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode. 687 * @since 10 688 */ 689 int OH_Rdb_Delete(OH_Rdb_Store *store, OH_Predicates *predicates); 690 691 /** 692 * @brief Queries data in the database based on specified conditions. 693 * 694 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 695 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 696 * Indicates the specified query condition. 697 * @param columnNames Indicates the columns to query. If the value is empty array, the query applies to all columns. 698 * @param length Indicates the length of columnNames. 699 * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 700 * If Get store failed or resultSet is nullptr, nullptr is returned. 701 * @see OH_Rdb_Store, OH_Predicates, OH_Cursor. 702 * @since 10 703 */ 704 OH_Cursor *OH_Rdb_Query(OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length); 705 706 /** 707 * @brief Executes an SQL statement. 708 * 709 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 710 * @param sql Indicates the SQL statement to execute. 711 * @return Returns the status code of the execution. 712 * {@link RDB_OK} - success. 713 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 714 * @see OH_Rdb_Store. 715 * @since 10 716 */ 717 int OH_Rdb_Execute(OH_Rdb_Store *store, const char *sql); 718 719 /** 720 * @brief Executes an SQL statement. 721 * 722 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 723 * @param sql Indicates the SQL statement to execute. 724 * @param args Represents the values of the parameters in the SQL statement. 725 * @param result Represents a pointer to OH_Data_Value instance when the execution is successful. 726 * The memory must be released through the OH_Value_Destroy interface after the use is complete. 727 * @return Returns the status code of the execution. 728 * Returns {@link RDB_OK} if the execution is successful. 729 * Returns {@link RDB_E_ERROR} database common error. 730 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 731 * Returns {@link RDB_E_ALREADY_CLOSED} database already closed. 732 * Returns {@link RDB_E_WAL_SIZE_OVER_LIMIT} the WAL file size over default limit. 733 * Returns {@link RDB_E_SQLITE_FULL} SQLite: The database is full. 734 * Returns {@link RDB_E_SQLITE_CORRUPT} database corrupted. 735 * Returns {@link RDB_E_SQLITE_PERM} SQLite: Access permission denied. 736 * Returns {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 737 * Returns {@link RDB_E_SQLITE_LOCKED} SQLite: A table in the database is locked. 738 * Returns {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 739 * Returns {@link RDB_E_SQLITE_READONLY} SQLite: Attempt to write a readonly database. 740 * Returns {@link RDB_E_SQLITE_IOERR} SQLite: Some kind of disk I/O error occurred. 741 * Returns {@link RDB_E_SQLITE_TOO_BIG} SQLite: TEXT or BLOB exceeds size limit. 742 * Returns {@link RDB_E_SQLITE_MISMATCH} SQLite: Data type mismatch. 743 * @see OH_Value_Destroy. 744 * @since 18 745 */ 746 int OH_Rdb_ExecuteV2(OH_Rdb_Store *store, const char *sql, const OH_Data_Values *args, OH_Data_Value **result); 747 748 /** 749 * @brief Write operations are performed using the specified transaction represented by the transaction ID 750 * 751 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 752 * @param trxId The transaction ID of the specified transaction, must be greater than 0 753 * @param sql Indicates the SQL statement to execute. 754 * @return Returns the status code of the execution. 755 * {@link RDB_OK} - success. 756 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 757 * {@link RDB_E_NOT_SUPPORTED} - The error code for not support. 758 * @see OH_Rdb_Store. 759 * @since 14 760 */ 761 int OH_Rdb_ExecuteByTrxId(OH_Rdb_Store *store, int64_t trxId, const char *sql); 762 763 /** 764 * @brief Queries data in the database based on an SQL statement. 765 * 766 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 767 * @param sql Indicates the SQL statement to execute. 768 * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 769 * If Get store failed,sql is nullptr or resultSet is nullptr, nullptr is returned. 770 * @see OH_Rdb_Store. 771 * @since 10 772 */ 773 OH_Cursor *OH_Rdb_ExecuteQuery(OH_Rdb_Store *store, const char *sql); 774 775 /** 776 * @brief Queries data in the database based on an SQL statement. 777 * 778 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 779 * @param sql Indicates the SQL statement to execute. 780 * @param args Represents a pointer to an instance of OH_Data_Values and it is the selection arguments. 781 * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 782 * If sql statement is invalid or the memory allocate failed, nullptr is returned. 783 * @see OH_Rdb_Store. 784 * @since 18 785 */ 786 OH_Cursor *OH_Rdb_ExecuteQueryV2(OH_Rdb_Store *store, const char *sql, const OH_Data_Values *args); 787 788 /** 789 * @brief Begins a transaction in EXCLUSIVE mode. 790 * 791 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 792 * @return Returns the status code of the execution. 793 * {@link RDB_OK} - success. 794 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 795 * @see OH_Rdb_Store. 796 * @since 10 797 */ 798 int OH_Rdb_BeginTransaction(OH_Rdb_Store *store); 799 800 /** 801 * @brief Rolls back a transaction in EXCLUSIVE mode. 802 * 803 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 804 * @return Returns the status code of the execution. 805 * {@link RDB_OK} - success. 806 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 807 * @see OH_Rdb_Store. 808 * @since 10 809 */ 810 int OH_Rdb_RollBack(OH_Rdb_Store *store); 811 812 /** 813 * @brief Commits a transaction in EXCLUSIVE mode. 814 * 815 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 816 * @return Returns the status code of the execution. 817 * {@link RDB_OK} - success. 818 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 819 * @see OH_Rdb_Store. 820 * @since 10 821 */ 822 int OH_Rdb_Commit(OH_Rdb_Store *store); 823 824 /** 825 * @brief Begin a transaction and the transaction ID corresponding to the transaction. 826 * 827 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 828 * @param trxId The output parameter, which is used to receive the transaction ID corresponding to the transaction 829 * @return Returns the status code of the execution. 830 * {@link RDB_OK} - success. 831 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 832 * {@link RDB_E_NOT_SUPPORTED} - The error code for not support. 833 * @see OH_Rdb_Store. 834 * @since 14 835 */ 836 int OH_Rdb_BeginTransWithTrxId(OH_Rdb_Store *store, int64_t *trxId); 837 838 /** 839 * @brief Roll back a transaction that is represented by a specified transaction ID 840 * 841 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 842 * @param trxId The transaction ID of the specified transaction, must be greater than 0 843 * @return Returns the status code of the execution. 844 * {@link RDB_OK} - success. 845 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 846 * {@link RDB_E_NOT_SUPPORTED} - The error code for not support. 847 * @see OH_Rdb_Store. 848 * @since 14 849 */ 850 int OH_Rdb_RollBackByTrxId(OH_Rdb_Store *store, int64_t trxId); 851 852 /** 853 * @brief Commit a transaction that is represented by a specified transaction ID 854 * 855 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 856 * @param trxId The transaction ID of the specified transaction, must be greater than 0 857 * @return Returns the status code of the execution. 858 * {@link RDB_OK} - success. 859 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 860 * {@link RDB_E_NOT_SUPPORTED} - The error code for not support. 861 * @see OH_Rdb_Store. 862 * @since 14 863 */ 864 int OH_Rdb_CommitByTrxId(OH_Rdb_Store *store, int64_t trxId); 865 866 /** 867 * @brief Backs up a database on specified path. 868 * 869 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 870 * @param databasePath Indicates the database file path. 871 * @return Returns the status code of the execution. 872 * {@link RDB_OK} - success. 873 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 874 * @see OH_Rdb_Store. 875 * @since 10 876 */ 877 int OH_Rdb_Backup(OH_Rdb_Store *store, const char *databasePath); 878 879 /** 880 * @brief Restores a database from a specified database file. 881 * 882 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 883 * @param databasePath Indicates the database file path. 884 * @return Returns the status code of the execution. 885 * {@link RDB_OK} - success. 886 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 887 * @see OH_Rdb_Store. 888 * @since 10 889 */ 890 int OH_Rdb_Restore(OH_Rdb_Store *store, const char *databasePath); 891 892 /** 893 * @brief Gets the version of a database. 894 * 895 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 896 * @param version Indicates the version number. 897 * @return Returns the status code of the execution. 898 * {@link RDB_OK} - success. 899 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 900 * @see OH_Rdb_Store. 901 * @since 10 902 */ 903 int OH_Rdb_GetVersion(OH_Rdb_Store *store, int *version); 904 905 /** 906 * @brief Sets the version of a database. 907 * 908 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 909 * @param version Indicates the version number. 910 * @return Returns the status code of the execution. 911 * {@link RDB_OK} - success. 912 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 913 * @see OH_Rdb_Store. 914 * @since 10 915 */ 916 int OH_Rdb_SetVersion(OH_Rdb_Store *store, int version); 917 918 /** 919 * @brief Describes the distribution type of the tables. 920 * 921 * @since 11 922 */ 923 typedef enum Rdb_DistributedType { 924 /** 925 * @brief Indicates the table is distributed among the devices. 926 */ 927 RDB_DISTRIBUTED_CLOUD 928 } Rdb_DistributedType; 929 930 /** 931 * @brief Indicates version of {@link Rdb_DistributedConfig} 932 * 933 * @since 11 934 */ 935 #define DISTRIBUTED_CONFIG_VERSION 1 936 /** 937 * @brief Manages the distributed configuration of the table. 938 * 939 * @since 11 940 */ 941 typedef struct Rdb_DistributedConfig { 942 /** 943 * The version used to uniquely identify the Rdb_DistributedConfig struct. 944 */ 945 int version; 946 /** 947 * Specifies whether the table auto syncs. 948 */ 949 bool isAutoSync; 950 } Rdb_DistributedConfig; 951 952 /** 953 * @brief Set table to be distributed table. 954 * 955 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 956 * @param tables Indicates the table names you want to set. 957 * @param count Indicates the count of tables you want to set. 958 * @param type Indicates the distributed type {@link Rdb_DistributedType}. 959 * @param config Indicates the distributed config of the tables. For details, see {@link Rdb_DistributedConfig}. 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. 964 * @see Rdb_DistributedConfig. 965 * @since 11 966 */ 967 int OH_Rdb_SetDistributedTables(OH_Rdb_Store *store, const char *tables[], uint32_t count, Rdb_DistributedType type, 968 const Rdb_DistributedConfig *config); 969 970 /** 971 * @brief Set table to be distributed table. 972 * 973 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 974 * @param tableName Indicates the name of the table to check. 975 * @param columnName Indicates the name of the column corresponding to the primary key. 976 * If the table has no primary key , please pass in "rowid". 977 * @param values Indicates the primary keys of the rows to check. 978 * If the table has no primary key , please pass in the row-ids of the rows to check. 979 * @return If the operation is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 980 * If Get store failed, NULL is returned. 981 * There are two columns, "data_key" and "timestamp". Otherwise NULL is returned. 982 * @see OH_Rdb_Store. 983 * @see OH_VObject. 984 * @see OH_Cursor. 985 * @since 11 986 */ 987 OH_Cursor *OH_Rdb_FindModifyTime(OH_Rdb_Store *store, const char *tableName, const char *columnName, 988 OH_VObject *values); 989 990 /** 991 * @brief Describes the change type. 992 * 993 * @since 11 994 */ 995 typedef enum Rdb_ChangeType { 996 /** 997 * @brief Means the change type is data change. 998 */ 999 RDB_DATA_CHANGE, 1000 /** 1001 * @brief Means the change type is asset change. 1002 */ 1003 RDB_ASSET_CHANGE 1004 } Rdb_ChangeType; 1005 1006 /** 1007 * @brief Describes the primary keys or row-ids of changed rows. 1008 * 1009 * @since 11 1010 */ 1011 typedef struct Rdb_KeyInfo { 1012 /** 1013 * Indicates the count of the primary keys or row-ids. 1014 */ 1015 int count; 1016 1017 /** 1018 * Indicates data type {@link OH_ColumnType} of the key. 1019 */ 1020 int type; 1021 1022 /** 1023 * Indicates the data of the key info. 1024 */ 1025 union Rdb_KeyData { 1026 /** 1027 * Indicates uint64_t type of the data. 1028 */ 1029 uint64_t integer; 1030 1031 /** 1032 * Indicates double type of the data. 1033 */ 1034 double real; 1035 1036 /** 1037 * Indicates const char * type of the data. 1038 */ 1039 const char *text; 1040 } *data; 1041 } Rdb_KeyInfo; 1042 1043 /** 1044 * @brief Indicates version of {@link Rdb_ChangeInfo} 1045 * 1046 * @since 11 1047 */ 1048 #define DISTRIBUTED_CHANGE_INFO_VERSION 1 1049 1050 /** 1051 * @brief Describes the notify info of data change. 1052 * 1053 * @since 11 1054 */ 1055 typedef struct Rdb_ChangeInfo { 1056 /** 1057 * The version used to uniquely identify the Rdb_ChangeInfo struct. 1058 */ 1059 int version; 1060 1061 /** 1062 * The name of changed table. 1063 */ 1064 const char *tableName; 1065 1066 /** 1067 * The {@link Rdb_ChangeType} of changed table. 1068 */ 1069 int ChangeType; 1070 1071 /** 1072 * The {@link Rdb_KeyInfo} of inserted rows. 1073 */ 1074 Rdb_KeyInfo inserted; 1075 1076 /** 1077 * The {@link Rdb_KeyInfo} of updated rows. 1078 */ 1079 Rdb_KeyInfo updated; 1080 1081 /** 1082 * The {@link Rdb_KeyInfo} of deleted rows. 1083 */ 1084 Rdb_KeyInfo deleted; 1085 } Rdb_ChangeInfo; 1086 1087 /** 1088 * @brief Indicates the subscribe type. 1089 * 1090 * @since 11 1091 */ 1092 typedef enum Rdb_SubscribeType { 1093 /** 1094 * @brief Subscription to cloud data changes. 1095 */ 1096 RDB_SUBSCRIBE_TYPE_CLOUD, 1097 1098 /** 1099 * @brief Subscription to cloud data change details. 1100 */ 1101 RDB_SUBSCRIBE_TYPE_CLOUD_DETAILS, 1102 1103 /** 1104 * @brief Subscription to local data change details. 1105 * @since 12 1106 */ 1107 RDB_SUBSCRIBE_TYPE_LOCAL_DETAILS, 1108 } Rdb_SubscribeType; 1109 1110 /** 1111 * @brief The callback function of cloud data change event. 1112 * 1113 * @param context Represents the context of data observer. 1114 * @param values Indicates the cloud accounts that changed. 1115 * @param count The count of changed cloud accounts. 1116 * @since 11 1117 */ 1118 typedef void (*Rdb_BriefObserver)(void *context, const char *values[], uint32_t count); 1119 1120 /** 1121 * @brief The callback function of cloud data change details event. 1122 * 1123 * @param context Represents the context of data observer. 1124 * @param changeInfo Indicates the {@link Rdb_ChangeInfo} of changed tables. 1125 * @param count The count of changed tables. 1126 * @see Rdb_ChangeInfo. 1127 * @since 11 1128 */ 1129 typedef void (*Rdb_DetailsObserver)(void *context, const Rdb_ChangeInfo **changeInfo, uint32_t count); 1130 1131 /** 1132 * @brief Indicates the callback functions. 1133 * 1134 * @since 11 1135 */ 1136 typedef union Rdb_SubscribeCallback { 1137 /** 1138 * The callback function of cloud data change details event. 1139 */ 1140 Rdb_DetailsObserver detailsObserver; 1141 1142 /** 1143 * The callback function of cloud data change event. 1144 */ 1145 Rdb_BriefObserver briefObserver; 1146 } Rdb_SubscribeCallback; 1147 1148 /** 1149 * @brief Indicates the observer of data. 1150 * 1151 * @since 11 1152 */ 1153 typedef struct Rdb_DataObserver { 1154 /** 1155 * The context of data observer. 1156 */ 1157 void *context; 1158 1159 /** 1160 * The callback of data observer. 1161 */ 1162 Rdb_SubscribeCallback callback; 1163 } Rdb_DataObserver; 1164 1165 /** 1166 * @brief Registers an observer for the database. 1167 * When data in the distributed database or the local database changes, the callback will be invoked. 1168 * 1169 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 1170 * @param type Indicates the subscription type, which is defined in {@link Rdb_SubscribeType}. 1171 * If its value is RDB_SUBSCRIBE_TYPE_LOCAL_DETAILS, the callback will be invoked for data changes 1172 * in the local database. 1173 * @param observer The {@link Rdb_DataObserver} of change events in the database. 1174 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 1175 * {@link RDB_OK} - success. 1176 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1177 * @see OH_Rdb_Store. 1178 * @see Rdb_DataObserver. 1179 * @since 11 1180 */ 1181 int OH_Rdb_Subscribe(OH_Rdb_Store *store, Rdb_SubscribeType type, const Rdb_DataObserver *observer); 1182 1183 /** 1184 * @brief Remove specified observer of specified type from the database. 1185 * 1186 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 1187 * @param type Indicates the subscription type, which is defined in {@link Rdb_SubscribeType}. 1188 * @param observer The {@link Rdb_DataObserver} of change events in the database. 1189 * If this is nullptr, remove all observers of the type. 1190 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 1191 * {@link RDB_OK} - success. 1192 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1193 * @see OH_Rdb_Store. 1194 * @see Rdb_DataObserver. 1195 * @since 11 1196 */ 1197 int OH_Rdb_Unsubscribe(OH_Rdb_Store *store, Rdb_SubscribeType type, const Rdb_DataObserver *observer); 1198 1199 /** 1200 * @brief Indicates the database synchronization mode. 1201 * 1202 * @since 11 1203 */ 1204 typedef enum Rdb_SyncMode { 1205 /** 1206 * @brief Indicates that data is synchronized from the end with the closest modification time 1207 * to the end with a more distant modification time. 1208 */ 1209 RDB_SYNC_MODE_TIME_FIRST, 1210 /** 1211 * @brief Indicates that data is synchronized from local to cloud. 1212 */ 1213 RDB_SYNC_MODE_NATIVE_FIRST, 1214 /** 1215 * @brief Indicates that data is synchronized from cloud to local. 1216 */ 1217 RDB_SYNC_MODE_CLOUD_FIRST 1218 } Rdb_SyncMode; 1219 1220 /** 1221 * @brief Describes the statistic of the cloud sync process. 1222 * 1223 * @since 11 1224 */ 1225 typedef struct Rdb_Statistic { 1226 /** 1227 * Describes the total number of data to sync. 1228 */ 1229 int total; 1230 1231 /** 1232 * Describes the number of successfully synced data. 1233 */ 1234 int successful; 1235 1236 /** 1237 * Describes the number of data failed to sync. 1238 */ 1239 int failed; 1240 1241 /** 1242 * Describes the number of data remained to sync. 1243 */ 1244 int remained; 1245 } Rdb_Statistic; 1246 1247 /** 1248 * @brief Describes the {@link Rdb_Statistic} details of the table. 1249 * 1250 * @since 11 1251 */ 1252 typedef struct Rdb_TableDetails { 1253 /** 1254 * Indicates the name of changed table. 1255 */ 1256 const char *table; 1257 1258 /** 1259 * Describes the {@link Rdb_Statistic} details of the upload process. 1260 */ 1261 Rdb_Statistic upload; 1262 1263 /** 1264 * Describes the {@link Rdb_Statistic} details of the download process. 1265 */ 1266 Rdb_Statistic download; 1267 } Rdb_TableDetails; 1268 1269 /** 1270 * The cloud sync progress 1271 * 1272 * @since 11 1273 */ 1274 typedef enum Rdb_Progress { 1275 /** 1276 * @brief Means the sync process begin. 1277 */ 1278 RDB_SYNC_BEGIN, 1279 1280 /** 1281 * @brief Means the sync process is in progress 1282 */ 1283 RDB_SYNC_IN_PROGRESS, 1284 1285 /** 1286 * @brief Means the sync process is finished 1287 */ 1288 RDB_SYNC_FINISH 1289 } Rdb_Progress; 1290 1291 /** 1292 * Describes the status of cloud sync progress. 1293 * 1294 * @since 11 1295 */ 1296 typedef enum Rdb_ProgressCode { 1297 /** 1298 * @brief Means the status of progress is success. 1299 */ 1300 RDB_SUCCESS, 1301 1302 /** 1303 * @brief Means the progress meets unknown error. 1304 */ 1305 RDB_UNKNOWN_ERROR, 1306 1307 /** 1308 * @brief Means the progress meets network error. 1309 */ 1310 RDB_NETWORK_ERROR, 1311 1312 /** 1313 * @brief Means cloud is disabled. 1314 */ 1315 RDB_CLOUD_DISABLED, 1316 1317 /** 1318 * @brief Means the progress is locked by others. 1319 */ 1320 RDB_LOCKED_BY_OTHERS, 1321 1322 /** 1323 * @brief Means the record exceeds the limit. 1324 */ 1325 RDB_RECORD_LIMIT_EXCEEDED, 1326 1327 /** 1328 * Means the cloud has no space for the asset. 1329 */ 1330 RDB_NO_SPACE_FOR_ASSET 1331 } Rdb_ProgressCode; 1332 1333 /** 1334 * @brief Indicates version of {@link Rdb_ProgressDetails} 1335 * 1336 * @since 11 1337 */ 1338 #define DISTRIBUTED_PROGRESS_DETAIL_VERSION 1 1339 1340 /** 1341 * @brief Describes detail of the cloud sync progress. 1342 * 1343 * @since 11 1344 */ 1345 typedef struct Rdb_ProgressDetails { 1346 /** 1347 * The version used to uniquely identify the Rdb_ProgressDetails struct. 1348 */ 1349 int version; 1350 1351 /** 1352 * Describes the status of data sync progress. Defined in {@link Rdb_Progress}. 1353 */ 1354 int schedule; 1355 1356 /** 1357 * Describes the code of data sync progress. Defined in {@link Rdb_ProgressCode}. 1358 */ 1359 int code; 1360 1361 /** 1362 * Describes the length of changed tables in data sync progress. 1363 */ 1364 int32_t tableLength; 1365 } Rdb_ProgressDetails; 1366 1367 /** 1368 * @brief Get table details from progress details. 1369 * 1370 * @param progress Represents a pointer to an {@link Rdb_ProgressDetails} instance. 1371 * @param version Indicates the version of current {@link Rdb_ProgressDetails}. 1372 * @return If the operation is successful, a pointer to the instance of the {@link Rdb_TableDetails} 1373 * structure is returned.If get details is failed, nullptr is returned. 1374 * @see Rdb_ProgressDetails 1375 * @see Rdb_TableDetails 1376 * @since 11 1377 */ 1378 Rdb_TableDetails *OH_Rdb_GetTableDetails(Rdb_ProgressDetails *progress, int32_t version); 1379 1380 /** 1381 * @brief The callback function of progress. 1382 * 1383 * @param progressDetails The details of the sync progress. 1384 * @see Rdb_ProgressDetails. 1385 * @since 11 1386 */ 1387 typedef void (*Rdb_ProgressCallback)(void *context, Rdb_ProgressDetails *progressDetails); 1388 1389 /** 1390 * @brief The callback function of sync. 1391 * 1392 * @param progressDetails The details of the sync progress. 1393 * @see Rdb_ProgressDetails. 1394 * @since 11 1395 */ 1396 typedef void (*Rdb_SyncCallback)(Rdb_ProgressDetails *progressDetails); 1397 1398 /** 1399 * @brief The observer of progress. 1400 * 1401 * @since 11 1402 */ 1403 typedef struct Rdb_ProgressObserver { 1404 /** 1405 * The context of progress observer. 1406 */ 1407 void *context; 1408 1409 /** 1410 * The callback function of progress observer. 1411 */ 1412 Rdb_ProgressCallback callback; 1413 } Rdb_ProgressObserver; 1414 1415 /** 1416 * @brief Sync data to cloud. 1417 * 1418 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 1419 * @param mode Represents the {@link Rdb_SyncMode} of sync progress. 1420 * @param tables Indicates the names of tables to sync. 1421 * @param count The count of tables to sync. If value equals 0, sync all tables of the store. 1422 * @param observer The {@link Rdb_ProgressObserver} of cloud sync progress. 1423 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 1424 * {@link RDB_OK} - success. 1425 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1426 * @see OH_Rdb_Store. 1427 * @see Rdb_ProgressObserver. 1428 * @since 11 1429 */ 1430 int OH_Rdb_CloudSync(OH_Rdb_Store *store, Rdb_SyncMode mode, const char *tables[], uint32_t count, 1431 const Rdb_ProgressObserver *observer); 1432 1433 /** 1434 * @brief Subscribes to the automatic synchronization progress of an RDB store. 1435 * A callback will be invoked when there is a notification of the automatic synchronization progress. 1436 * 1437 * @param store Indicates the pointer to the target {@Link OH_Rdb_Store} instance. 1438 * @param observer The {@link Rdb_ProgressObserver} for the automatic synchronization progress. 1439 * Indicates the callback invoked to return the automatic synchronization progress. 1440 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 1441 * {@link RDB_OK} - success. 1442 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1443 * @see OH_Rdb_Store. 1444 * @see Rdb_ProgressObserver. 1445 * @since 11 1446 **/ 1447 int OH_Rdb_SubscribeAutoSyncProgress(OH_Rdb_Store *store, const Rdb_ProgressObserver *observer); 1448 1449 /** 1450 * @brief Unsubscribes from the automatic synchronization progress of an RDB store. 1451 * 1452 * @param store Indicates the pointer to the target {@Link OH_Rdb_Store} instance. 1453 * @param observer Indicates the {@link Rdb_ProgressObserver} callback for the automatic synchronization progress. 1454 * If it is a null pointer, all callbacks for the automatic synchronization progress will be unregistered. 1455 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 1456 * {@link RDB_OK} - success. 1457 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1458 * @see OH_Rdb_Store. 1459 * @see Rdb_ProgressObserver. 1460 * @since 11 1461 */ 1462 int OH_Rdb_UnsubscribeAutoSyncProgress(OH_Rdb_Store *store, const Rdb_ProgressObserver *observer); 1463 1464 /** 1465 * @brief Lock data from the database based on specified conditions. 1466 * 1467 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 1468 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 1469 * Indicates the specified lock condition. 1470 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 1471 * {@link RDB_OK} - success. 1472 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1473 * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode. 1474 * @since 12 1475 */ 1476 int OH_Rdb_LockRow(OH_Rdb_Store *store, OH_Predicates *predicates); 1477 1478 /** 1479 * @brief Unlock data from the database based on specified conditions. 1480 * 1481 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 1482 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 1483 * Indicates the specified unlock condition. 1484 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}. 1485 * {@link RDB_OK} - success. 1486 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1487 * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode. 1488 * @since 12 1489 */ 1490 int OH_Rdb_UnlockRow(OH_Rdb_Store *store, OH_Predicates *predicates); 1491 1492 /** 1493 * @brief Queries locked data in the database based on specified conditions. 1494 * 1495 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 1496 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 1497 * Indicates the specified query condition. 1498 * @param columnNames Indicates the columns to query. If the value is empty array, the query applies to all columns. 1499 * @param length Indicates the length of columnNames. 1500 * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned. 1501 * If Get store failed or resultSet is nullptr, nullptr is returned. 1502 * @see OH_Rdb_Store, OH_Predicates, OH_Cursor. 1503 * @since 12 1504 */ 1505 OH_Cursor *OH_Rdb_QueryLockedRow( 1506 OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length); 1507 1508 /** 1509 * @brief Creates an OH_Rdb_Transaction instance object. 1510 * 1511 * @param store Represents a pointer to an instance of OH_Rdb_Store. 1512 * @param options Represents a pointer to an instance of OH_RDB_TransOptions. 1513 * @param trans Represents a pointer to OH_Rdb_Transaction instance when the execution is successful. 1514 * Otherwise, nullptr is returned. The memory must be released through the OH_RdbTrans_Destroy 1515 * interface after the use is complete. 1516 * @return Returns the error code. 1517 * Returns {@link RDB_OK} if the execution is successful. 1518 * Returns {@link RDB_E_ERROR} database common error. 1519 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 1520 * Returns {@link RDB_E_ALREADY_CLOSED} database already closed. 1521 * Returns {@link RDB_E_DATABASE_BUSY} database does not respond. 1522 * Returns {@link RDB_E_SQLITE_FULL} SQLite: The database is full. 1523 * Returns {@link RDB_E_SQLITE_CORRUPT} database corrupted. 1524 * Returns {@link RDB_E_SQLITE_PERM} SQLite: Access permission denied. 1525 * Returns {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 1526 * Returns {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 1527 * Returns {@link RDB_E_SQLITE_IOERR} SQLite: Some kind of disk I/O error occurred. 1528 * Returns {@link RDB_E_SQLITE_CANT_OPEN} SQLite: Unable to open the database file. 1529 * @see OH_RdbTrans_Destroy. 1530 * @since 18 1531 */ 1532 int OH_Rdb_CreateTransaction(OH_Rdb_Store *store, const OH_RDB_TransOptions *options, OH_Rdb_Transaction **trans); 1533 1534 /** 1535 * @brief Attaches a database file to the currently linked database. 1536 * 1537 * @param store Represents a pointer to an OH_Rdb_Store instance. 1538 * @param config Represents a pointer to an OH_Rdb_ConfigV2 configuration of the database related to this RDB store. 1539 * @param attachName Represents the alias of the database. 1540 * @param waitTime Represents the maximum time allowed for attaching the database, valid range is 1 to 300. 1541 * @param attachedNumber Represents the number of attached databases, It is an output parameter. 1542 * @return Returns the status code of the execution. 1543 * Returns {@link RDB_OK} if the execution is successful. 1544 * Returns {@link RDB_E_ERROR} database common error. 1545 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 1546 * Returns {@link RDB_E_ALREADY_CLOSED} database already closed. 1547 * Returns {@link RDB_E_NOT_SUPPORTED} - The error code for not support. 1548 * Returns {@link RDB_E_DATABASE_BUSY} database does not respond. 1549 * Returns {@link RDB_E_SQLITE_FULL} SQLite: The database is full. 1550 * Returns {@link RDB_E_SQLITE_CORRUPT} database corrupted. 1551 * Returns {@link RDB_E_SQLITE_PERM} SQLite: Access permission denied. 1552 * Returns {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 1553 * Returns {@link RDB_E_SQLITE_LOCKED} SQLite: A table in the database is locked. 1554 * Returns {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 1555 * Returns {@link RDB_E_SQLITE_READONLY} SQLite: Attempt to write a readonly database. 1556 * Returns {@link RDB_E_SQLITE_IOERR} SQLite: Some kind of disk I/O error occurred. 1557 * Returns {@link RDB_E_SQLITE_TOO_BIG} SQLite: TEXT or BLOB exceeds size limit. 1558 * Returns {@link RDB_E_SQLITE_MISMATCH} SQLite: Data type mismatch. 1559 * Returns {@link RDB_E_SQLITE_CONSTRAINT} SQLite: Abort due to constraint violation. 1560 * @since 20 1561 */ 1562 int OH_Rdb_Attach(OH_Rdb_Store *store, const OH_Rdb_ConfigV2 *config, const char *attachName, int64_t waitTime, 1563 size_t *attachedNumber); 1564 1565 /** 1566 * @brief Detaches a database from this database. 1567 * 1568 * @param store Represents a pointer to an OH_Rdb_Store instance. 1569 * @param attachName Represents the alias of the database. 1570 * @param waitTime Represents the maximum time allowed for detaching the database, valid range is 1 to 300. 1571 * @param attachedNumber Represents the number of attached databases, It is an output parameter. 1572 * @return Returns the status code of the execution. 1573 * Returns {@link RDB_OK} if the execution is successful. 1574 * Returns {@link RDB_E_ERROR} database common error. 1575 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 1576 * Returns {@link RDB_E_ALREADY_CLOSED} database already closed. 1577 * Returns {@link RDB_E_NOT_SUPPORTED} - The error code for not support. 1578 * Returns {@link RDB_E_DATABASE_BUSY} database does not respond. 1579 * Returns {@link RDB_E_SQLITE_FULL} SQLite: The database is full. 1580 * Returns {@link RDB_E_SQLITE_CORRUPT} database corrupted. 1581 * Returns {@link RDB_E_SQLITE_PERM} SQLite: Access permission denied. 1582 * Returns {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 1583 * Returns {@link RDB_E_SQLITE_LOCKED} SQLite: A table in the database is locked. 1584 * Returns {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 1585 * Returns {@link RDB_E_SQLITE_READONLY} SQLite: Attempt to write a readonly database. 1586 * Returns {@link RDB_E_SQLITE_IOERR} SQLite: Some kind of disk I/O error occurred. 1587 * Returns {@link RDB_E_SQLITE_TOO_BIG} SQLite: TEXT or BLOB exceeds size limit. 1588 * Returns {@link RDB_E_SQLITE_MISMATCH} SQLite: Data type mismatch. 1589 * Returns {@link RDB_E_SQLITE_CONSTRAINT} SQLite: Abort due to constraint violation. 1590 * @see OH_Rdb_Store, OH_Rdb_ErrCode. 1591 * @since 20 1592 */ 1593 int OH_Rdb_Detach(OH_Rdb_Store *store, const char *attachName, int64_t waitTime, size_t *attachedNumber); 1594 1595 /** 1596 * @brief Support for collations in different languages. 1597 * 1598 * @param store Represents a pointer to an {@link OH_Rdb_Store} instance. 1599 * @param locale Language related to the locale, for example, zh. The value complies with the ISO 639 standard. 1600 * @return Returns a specific error code. 1601 * {@link RDB_OK} if the execution is successful. 1602 * {@link RDB_ERR} - Indicates that the function execution exception. 1603 * {@link RDB_E_INVALID_ARGS} - The error code for common invalid args. 1604 * {@link RDB_E_ALREADY_CLOSED} database already closed. 1605 * {@link RDB_E_SQLITE_BUSY} SQLite: The database file is locked. 1606 * {@link RDB_E_SQLITE_NOMEM} SQLite: The database is out of memory. 1607 * Specific error codes can be referenced {@link OH_Rdb_ErrCode}. 1608 * @see OH_Rdb_Store. 1609 * @since 20 1610 */ 1611 int OH_Rdb_SetLocale(OH_Rdb_Store *store, const char *locale); 1612 1613 #ifdef __cplusplus 1614 }; 1615 #endif 1616 1617 /** @} */ 1618 1619 #endif // RELATIONAL_STORE_H 1620