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 Define OH_Rdb_ConfigV2 type.
161  *
162  * @since 14
163  */
164 typedef struct OH_Rdb_ConfigV2 OH_Rdb_ConfigV2;
165 
166 /**
167  * @brief Define Rdb_DBType type.
168  *
169  * @since 14
170  */
171 typedef enum Rdb_DBType {
172     /**
173      * @brief Means using SQLITE as the db kernal
174      */
175     RDB_SQLITE = 1,
176     /**
177      * @brief Means using CARLEY_DB as the db kernal
178      */
179     RDB_CAYLEY = 2,
180     /**
181      * @brief Means largest value for Rdb_DBType
182      */
183     DBTYPE_BUTT = 64,
184 } Rdb_DBType;
185 
186 /**
187  * @brief Create OH_Rdb_ConfigV2 which is used to open store
188  *
189  * @return Returns the newly created OH_Rdb_ConfigV2 object. If NULL is returned, the creation fails.
190  * The possible cause is that the address space of the application is full, As a result, the space
191  * cannot be allocated.
192  * @see OH_Rdb_ConfigV2
193  * @since 14
194  */
195 OH_Rdb_ConfigV2 *OH_Rdb_CreateConfig();
196 
197 /**
198  * @brief Destroy OH_Rdb_ConfigV2 which is created by OH_Rdb_CreateConfig
199  *
200  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
201  * Indicates the configuration of the database related to this RDB store.
202  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
203  *     {@link RDB_OK} - success.
204  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
205  * @since 14
206  */
207 int OH_Rdb_DestroyConfig(OH_Rdb_ConfigV2 *config);
208 
209 /**
210  * @brief Set property databaseDir into config
211  *
212  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
213  * Indicates the configuration of the database related to this RDB store.
214  * @param dataBaseDir Indicates the directory of the database.
215  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
216  *     {@link RDB_OK} - success.
217  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
218  * @since 14
219  */
220 int OH_Rdb_SetDatabaseDir(OH_Rdb_ConfigV2 *config, const char *databaseDir);
221 
222 /**
223  * @brief Set property storeName into config
224  *
225  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
226  * Indicates the configuration of the database related to this RDB store.
227  * @param storeName Indicates the name of the database.
228  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
229  *     {@link RDB_OK} - success.
230  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
231  * @since 14
232  */
233 int OH_Rdb_SetStoreName(OH_Rdb_ConfigV2 *config, const char *storeName);
234 
235 /**
236  * @brief Set property bundleName into config
237  *
238  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
239  * Indicates the configuration of the database related to this RDB store.
240  * @param bundleName Indicates the bundle name of the application
241  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
242  *     {@link RDB_OK} - success.
243  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
244  * @since 14
245  */
246 int OH_Rdb_SetBundleName(OH_Rdb_ConfigV2 *config, const char *bundleName);
247 
248 /**
249  * @brief Set property moduleName into config
250  *
251  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
252  * Indicates the configuration of the database related to this RDB store.
253  * @param moduleName Indicates the module name of the application.
254  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
255  *     {@link RDB_OK} - success.
256  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
257  * @since 14
258  */
259 int OH_Rdb_SetModuleName(OH_Rdb_ConfigV2 *config, const char *moduleName);
260 
261 /**
262  * @brief Set property isEncrypted into config
263  *
264  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
265  * Indicates the configuration of the database related to this RDB store.
266  * @param isEncrypted Indicates whether the database is encrypted.
267  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
268  *     {@link RDB_OK} - success.
269  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
270  * @since 14
271  */
272 int OH_Rdb_SetEncrypted(OH_Rdb_ConfigV2 *config, bool isEncrypted);
273 
274 /**
275  * @brief Set property securityLevel into config
276  *
277  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
278  * Indicates the configuration of the database related to this RDB store.
279  * @param securityLevel Indicates the security level {@link OH_Rdb_SecurityLevel} of the database.
280  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
281  *     {@link RDB_OK} - success.
282  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
283  * @since 14
284  */
285 int OH_Rdb_SetSecurityLevel(OH_Rdb_ConfigV2 *config, int securityLevel);
286 
287 /**
288  * @brief Set property area into config
289  *
290  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
291  * Indicates the configuration of the database related to this RDB store
292  * @param area Represents the security area of the database.
293  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
294  *     {@link RDB_OK} - success.
295  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
296  * @since 14
297  */
298 int OH_Rdb_SetArea(OH_Rdb_ConfigV2 *config, int area);
299 
300 /**
301  * @brief Set property dbType into config
302  * @param config Represents a pointer to {@link OH_Rdb_ConfigV2} instance.
303  * @param dbType Indicates the dbType {@link Rdb_DBType} of the database
304  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
305  *     {@link RDB_OK} - success.
306  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
307  *     {@link RDB_E_NOT_SUPPORTED} - The error code for not support db types.
308  * @since 14
309  */
310 int OH_Rdb_SetDbType(OH_Rdb_ConfigV2 *config, int dbType);
311 
312 /**
313  * @brief Get support db type list
314  * @param typeCount The output parameter, which is used to recieve the length of the support db type array.
315  * @return Return Rdb_DBType array contains supported db type, array length is number of support type
316  * @since 14
317  */
318 const int *OH_Rdb_GetSupportedDbType(int *typeCount);
319 
320 /**
321  * @brief Creates an {@link OH_VObject} instance.
322  *
323  * @return If the creation is successful, a pointer to the instance of the @link OH_VObject} structure is returned,
324  * otherwise NULL is returned.
325  * @see OH_VObject.
326  * @since 10
327  */
328 OH_VObject *OH_Rdb_CreateValueObject();
329 
330 /**
331  * @brief Creates an {@link OH_VBucket} object.
332  *
333  * @return If the creation is successful, a pointer to the instance of the @link OH_VBucket} structure is returned,
334  * otherwise NULL is returned.
335  * @see OH_VBucket.
336  * @since 10
337  */
338 OH_VBucket *OH_Rdb_CreateValuesBucket();
339 
340 /**
341  * @brief Creates an {@link OH_Predicates} instance.
342  *
343  * @param table Indicates the table name.
344  * @return If the creation is successful, a pointer to the instance of the @link OH_Predicates} structure is returned.
345  *         If the table name is nullptr, nullptr is returned.
346  * @see OH_Predicates.
347  * @since 10
348  */
349 OH_Predicates *OH_Rdb_CreatePredicates(const char *table);
350 
351 /**
352  * @brief Obtains an RDB store.
353  *
354  * You can set parameters of the RDB store as required. In general,
355  * this method is recommended to obtain a rdb store.
356  *
357  * @param config Represents a pointer to an {@link OH_Rdb_Config} instance.
358  * Indicates the configuration of the database related to this RDB store.
359  * @param errCode This parameter is the output parameter,
360  * and the execution status of a function is written to this variable.
361  * @return If the creation is successful, a pointer to the instance of the @link OH_Rdb_Store} structure is returned.
362  *         If the Config is empty, config.size does not match, or errCode is empty.
363  * Get database path failed.Get RDB Store fail. Nullptr is returned.
364  * @see OH_Rdb_Config, OH_Rdb_Store.
365  * @since 10
366  */
367 OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode);
368 
369 /**
370  * @brief Obtains an RDB store with OH_Rdb_ConfigV2.
371  *
372  * You can set parameters of the RDB store as required. In general,
373  * this method is recommended to obtain a rdb store.
374  *
375  * @param config Represents a pointer to an {@link OH_Rdb_ConfigV2} instance.
376  * Indicates the configuration of the database related to this RDB store.
377  * @param errCode This parameter is the output parameter,
378  * and the execution status of a function is written to this variable.
379  * @return If the creation is successful, a pointer to the instance of the @link OH_Rdb_Store} structure is returned.
380  *         If the Config is empty, config.size does not match, or errCode is empty.
381  * Get database path failed.Get RDB Store fail. Nullptr is returned.
382  * @see OH_Rdb_ConfigV2, OH_Rdb_Store.
383  * @since 14
384  */
385 OH_Rdb_Store *OH_Rdb_CreateOrOpen(const OH_Rdb_ConfigV2 *config, int *errCode);
386 
387 /**
388  * @brief Close the {@link OH_Rdb_Store} object and reclaim the memory occupied by the object.
389  *
390  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
391  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
392  *     {@link RDB_OK} - success.
393  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
394  * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}.
395  * @see OH_Rdb_Store, OH_Rdb_ErrCode.
396  * @since 10
397  */
398 int OH_Rdb_CloseStore(OH_Rdb_Store *store);
399 
400 /**
401  * @brief Deletes the database with a specified path.
402  *
403  * @param config Represents a pointer to an {@link OH_Rdb_Config} instance.
404  * Indicates the configuration of the database related to this RDB store.
405  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
406  *     {@link RDB_OK} - success.
407  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
408  * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}.
409  * @see OH_Rdb_ErrCode.
410  * @since 10
411  */
412 int OH_Rdb_DeleteStore(const OH_Rdb_Config *config);
413 
414 /**
415  * @brief Deletes the database with a specified path.
416  *
417  * @param config Represents a pointer to an {@link OH_Rdb_ConfigV2} instance.
418  * Indicates the configuration of the database related to this RDB store.
419  * @return Returns the status code of the execution. Successful execution returns RDB_OK,
420  *     {@link RDB_OK} - success.
421  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
422  * while failure returns a specific error code. Specific error codes can be referenced {@link OH_Rdb_ErrCode}.
423  * @see OH_Rdb_ErrCode.
424  * @since 14
425  */
426 int OH_Rdb_DeleteStoreV2(const OH_Rdb_ConfigV2 *config);
427 
428 /**
429  * @brief Inserts a row of data into the target table.
430  *
431  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
432  * @param table Indicates the target table.
433  * @param valuesBucket Indicates the row of data {@link OH_VBucket} to be inserted into the table.
434  * @return Returns the rowId if success, returns a specific error code.
435  *     {@link RDB_ERR} - Indicates that the function execution exception.
436  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
437  * Specific error codes can be referenced {@link OH_Rdb_ErrCode}.
438  * @see OH_Rdb_Store, OH_VBucket, OH_Rdb_ErrCode.
439  * @since 10
440  */
441 int OH_Rdb_Insert(OH_Rdb_Store *store, const char *table, OH_VBucket *valuesBucket);
442 
443 /**
444  * @brief Updates data in the database based on specified conditions.
445  *
446  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
447  * @param valuesBucket Indicates the row of data {@link OH__VBucket} to be updated in the database
448  * @param predicates Represents a pointer to an {@link OH_Predicates} instance.
449  * Indicates the specified update condition.
450  * @return Returns the number of rows changed if success, otherwise, returns a specific error code.
451  *     {@link RDB_ERR} - Indicates that the function execution exception.
452  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
453  * Specific error codes can be referenced {@link OH_Rdb_ErrCode}.
454  * @see OH_Rdb_Store, OH_Bucket, OH_Predicates, OH_Rdb_ErrCode.
455  * @since 10
456  */
457 int OH_Rdb_Update(OH_Rdb_Store *store, OH_VBucket *valuesBucket, OH_Predicates *predicates);
458 
459 /**
460  * @brief Deletes data from the database based on specified conditions.
461  *
462  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
463  * @param predicates Represents a pointer to an {@link OH_Predicates} instance.
464  * Indicates the specified delete condition.
465  * @return Returns the number of rows changed if success, otherwise, returns a specific error code.
466  *     {@link RDB_ERR} - Indicates that the function execution exception.
467  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
468  * Specific error codes can be referenced {@link OH_Rdb_ErrCode}.
469  * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode.
470  * @since 10
471  */
472 int OH_Rdb_Delete(OH_Rdb_Store *store, OH_Predicates *predicates);
473 
474 /**
475  * @brief Queries data in the database based on specified conditions.
476  *
477  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
478  * @param predicates Represents a pointer to an {@link OH_Predicates} instance.
479  * Indicates the specified query condition.
480  * @param columnNames Indicates the columns to query. If the value is empty array, the query applies to all columns.
481  * @param length Indicates the length of columnNames.
482  * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned.
483  *         If Get store failed or resultSet is nullptr, nullptr is returned.
484  * @see OH_Rdb_Store, OH_Predicates, OH_Cursor.
485  * @since 10
486  */
487 OH_Cursor *OH_Rdb_Query(OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length);
488 
489 /**
490  * @brief Executes an SQL statement.
491  *
492  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
493  * @param sql Indicates the SQL statement to execute.
494  * @return Returns the status code of the execution.
495  *     {@link RDB_OK} - success.
496  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
497  * @see OH_Rdb_Store.
498  * @since 10
499  */
500 int OH_Rdb_Execute(OH_Rdb_Store *store, const char *sql);
501 
502 /**
503  * @brief Write operations are performed using the specified transaction represented by the transaction ID
504  *
505  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
506  * @param trxId The transaction ID of the specified transaction, must be greater than 0
507  * @param sql Indicates the SQL statement to execute.
508  * @return Returns the status code of the execution.
509  *     {@link RDB_OK} - success.
510  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
511  *     {@link RDB_E_NOT_SUPPORTED} - The error code for not supprt.
512  * @see OH_Rdb_Store.
513  * @since 14
514  */
515 int OH_Rdb_ExecuteByTrxId(OH_Rdb_Store *store, int64_t trxId, const char *sql);
516 
517 /**
518  * @brief Queries data in the database based on an SQL statement.
519  *
520  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
521  * @param sql Indicates the SQL statement to execute.
522  * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned.
523  *         If Get store failed,sql is nullptr or resultSet is nullptr, nullptr is returned.
524  * @see OH_Rdb_Store.
525  * @since 10
526  */
527 OH_Cursor *OH_Rdb_ExecuteQuery(OH_Rdb_Store *store, const char *sql);
528 
529 /**
530  * @brief Begins a transaction in EXCLUSIVE mode.
531  *
532  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
533  * @return Returns the status code of the execution.
534  *     {@link RDB_OK} - success.
535  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
536  * @see OH_Rdb_Store.
537  * @since 10
538  */
539 int OH_Rdb_BeginTransaction(OH_Rdb_Store *store);
540 
541 /**
542  * @brief Rolls back a transaction in EXCLUSIVE mode.
543  *
544  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
545  * @return Returns the status code of the execution.
546  *     {@link RDB_OK} - success.
547  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
548  * @see OH_Rdb_Store.
549  * @since 10
550  */
551 int OH_Rdb_RollBack(OH_Rdb_Store *store);
552 
553 /**
554  * @brief Commits a transaction in EXCLUSIVE mode.
555  *
556  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
557  * @return Returns the status code of the execution.
558  *     {@link RDB_OK} - success.
559  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
560  * @see OH_Rdb_Store.
561  * @since 10
562  */
563 int OH_Rdb_Commit(OH_Rdb_Store *store);
564 
565 /**
566  * @brief Begin a transaction and the transaction ID corresponding to the transaction.
567  *
568  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
569  * @param trxId The output parameter, which is used to receive the transaction ID corresponding to the transaction
570  * @return Returns the status code of the execution.
571  *     {@link RDB_OK} - success.
572  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
573  *     {@link RDB_E_NOT_SUPPORTED} - The error code for not supprt.
574  * @see OH_Rdb_Store.
575  * @since 14
576  */
577 int OH_Rdb_BeginTransWithTrxId(OH_Rdb_Store *store, int64_t *trxId);
578 
579 /**
580  * @brief Roll back a transaction that is represented by a specified transaction ID
581  *
582  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
583  * @param trxId The transaction ID of the specified transaction, must be greater than 0
584  * @return Returns the status code of the execution.
585  *     {@link RDB_OK} - success.
586  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
587  *     {@link RDB_E_NOT_SUPPORTED} - The error code for not supprt.
588  * @see OH_Rdb_Store.
589  * @since 14
590  */
591 int OH_Rdb_RollBackByTrxId(OH_Rdb_Store *store, int64_t trxId);
592 
593 /**
594  * @brief Commit a transaction that is represented by a specified transaction ID
595  *
596  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
597  * @param trxId The transaction ID of the specified transaction, must be greater than 0
598  * @return Returns the status code of the execution.
599  *     {@link RDB_OK} - success.
600  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
601  *     {@link RDB_E_NOT_SUPPORTED} - The error code for not supprt.
602  * @see OH_Rdb_Store.
603  * @since 14
604  */
605 int OH_Rdb_CommitByTrxId(OH_Rdb_Store *store, int64_t trxId);
606 
607 /**
608  * @brief Backs up a database on specified path.
609  *
610  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
611  * @param databasePath Indicates the database file path.
612  * @return Returns the status code of the execution.
613  *     {@link RDB_OK} - success.
614  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
615  * @see OH_Rdb_Store.
616  * @since 10
617  */
618 int OH_Rdb_Backup(OH_Rdb_Store *store, const char *databasePath);
619 
620 /**
621  * @brief Restores a database from a specified database file.
622  *
623  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
624  * @param databasePath Indicates the database file path.
625  * @return Returns the status code of the execution.
626  *     {@link RDB_OK} - success.
627  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
628  * @see OH_Rdb_Store.
629  * @since 10
630  */
631 int OH_Rdb_Restore(OH_Rdb_Store *store, const char *databasePath);
632 
633 /**
634  * @brief Gets the version of a database.
635  *
636  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
637  * @param version Indicates the version number.
638  * @return Returns the status code of the execution.
639  *     {@link RDB_OK} - success.
640  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
641  * @see OH_Rdb_Store.
642  * @since 10
643  */
644 int OH_Rdb_GetVersion(OH_Rdb_Store *store, int *version);
645 
646 /**
647  * @brief Sets the version of a database.
648  *
649  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
650  * @param version Indicates the version number.
651  * @return Returns the status code of the execution.
652  *     {@link RDB_OK} - success.
653  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
654  * @see OH_Rdb_Store.
655  * @since 10
656  */
657 int OH_Rdb_SetVersion(OH_Rdb_Store *store, int version);
658 
659 /**
660  * @brief Describes the distribution type of the tables.
661  *
662  * @since 11
663  */
664 typedef enum Rdb_DistributedType {
665     /**
666      * @brief Indicates the table is distributed among the devices.
667      */
668     RDB_DISTRIBUTED_CLOUD
669 } Rdb_DistributedType;
670 
671 /**
672  * @brief Indicates version of {@link Rdb_DistributedConfig}
673  *
674  * @since 11
675  */
676 #define DISTRIBUTED_CONFIG_VERSION 1
677 /**
678  * @brief Manages the distributed configuration of the table.
679  *
680  * @since 11
681  */
682 typedef struct Rdb_DistributedConfig {
683     /**
684      * The version used to uniquely identify the Rdb_DistributedConfig struct.
685      */
686     int version;
687     /**
688      * Specifies whether the table auto syncs.
689      */
690     bool isAutoSync;
691 } Rdb_DistributedConfig;
692 
693 /**
694  * @brief Set table to be distributed table.
695  *
696  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
697  * @param tables Indicates the table names you want to set.
698  * @param count Indicates the count of tables you want to set.
699  * @param type Indicates the distributed type {@link Rdb_DistributedType}.
700  * @param config Indicates the distributed config of the tables. For details, see {@link Rdb_DistributedConfig}.
701  * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}.
702  *     {@link RDB_OK} - success.
703  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
704  * @see OH_Rdb_Store.
705  * @see Rdb_DistributedConfig.
706  * @since 11
707  */
708 int OH_Rdb_SetDistributedTables(OH_Rdb_Store *store, const char *tables[], uint32_t count, Rdb_DistributedType type,
709     const Rdb_DistributedConfig *config);
710 
711 /**
712  * @brief Set table to be distributed table.
713  *
714  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
715  * @param tableName Indicates the name of the table to check.
716  * @param columnName Indicates the name of the column corresponding to the primary key.
717  * If the table has no primary key , please pass in "rowid".
718  * @param values Indicates the primary keys of the rows to check.
719  * If the table has no primary key , please pass in the row-ids of the rows to check.
720  * @return If the operation is successful, a pointer to the instance of the @link OH_Cursor} structure is returned.
721  *         If Get store failed, nullptr is returned.
722  * There are two columns, "data_key" and "timestamp". Otherwise NULL is returned.
723  * @see OH_Rdb_Store.
724  * @see OH_VObject.
725  * @see OH_Cursor.
726  * @since 11
727  */
728 OH_Cursor *OH_Rdb_FindModifyTime(OH_Rdb_Store *store, const char *tableName, const char *columnName,
729     OH_VObject *values);
730 
731 /**
732  * @brief Describes the change type.
733  *
734  * @since 11
735  */
736 typedef enum Rdb_ChangeType {
737     /**
738      * @brief Means the change type is data change.
739      */
740     RDB_DATA_CHANGE,
741     /**
742      * @brief Means the change type is asset change.
743      */
744     RDB_ASSET_CHANGE
745 } Rdb_ChangeType;
746 
747 /**
748  * @brief Describes the primary keys or row-ids of changed rows.
749  *
750  * @since 11
751  */
752 typedef struct Rdb_KeyInfo {
753     /**
754      * Indicates the count of the primary keys or row-ids.
755      */
756     int count;
757 
758     /**
759      * Indicates data type {@link OH_ColumnType} of the key.
760      */
761     int type;
762 
763     /**
764      * Indicates the data of the key info.
765      */
766     union Rdb_KeyData {
767         /**
768          * Indicates uint64_t type of the data.
769          */
770         uint64_t integer;
771 
772         /**
773          * Indicates double type of the data.
774          */
775         double real;
776 
777         /**
778          * Indicates const char * type of the data.
779          */
780         const char *text;
781     } *data;
782 } Rdb_KeyInfo;
783 
784 /**
785  * @brief Indicates version of {@link Rdb_ChangeInfo}
786  *
787  * @since 11
788  */
789 #define DISTRIBUTED_CHANGE_INFO_VERSION 1
790 
791 /**
792  * @brief Describes the notify info of data change.
793  *
794  * @since 11
795  */
796 typedef struct Rdb_ChangeInfo {
797     /**
798      * The version used to uniquely identify the Rdb_ChangeInfo struct.
799      */
800     int version;
801 
802     /**
803      * The name of changed table.
804      */
805     const char *tableName;
806 
807     /**
808      * The {@link Rdb_ChangeType} of changed table.
809      */
810     int ChangeType;
811 
812     /**
813      * The {@link Rdb_KeyInfo} of inserted rows.
814      */
815     Rdb_KeyInfo inserted;
816 
817     /**
818      * The {@link Rdb_KeyInfo} of updated rows.
819      */
820     Rdb_KeyInfo updated;
821 
822     /**
823      * The {@link Rdb_KeyInfo} of deleted rows.
824      */
825     Rdb_KeyInfo deleted;
826 } Rdb_ChangeInfo;
827 
828 /**
829  * @brief Indicates the subscribe type.
830  *
831  * @since 11
832  */
833 typedef enum Rdb_SubscribeType {
834     /**
835      * @brief Subscription to cloud data changes.
836      */
837     RDB_SUBSCRIBE_TYPE_CLOUD,
838 
839     /**
840      * @brief Subscription to cloud data change details.
841      */
842     RDB_SUBSCRIBE_TYPE_CLOUD_DETAILS,
843 
844     /**
845      * @brief Subscription to local data change details.
846      * @since 12
847      */
848     RDB_SUBSCRIBE_TYPE_LOCAL_DETAILS,
849 } Rdb_SubscribeType;
850 
851 /**
852  * @brief The callback function of cloud data change event.
853  *
854  * @param context Represents the context of data observer.
855  * @param values Indicates the cloud accounts that changed.
856  * @param count The count of changed cloud accounts.
857  * @see OH_VObject.
858  * @since 11
859  */
860 typedef void (*Rdb_BriefObserver)(void *context, const char *values[], uint32_t count);
861 
862 /**
863  * @brief The callback function of cloud data change details event.
864  *
865  * @param context Represents the context of data observer.
866  * @param changeInfo Indicates the {@link Rdb_ChangeInfo} of changed tables.
867  * @param count The count of changed tables.
868  * @see Rdb_ChangeInfo.
869  * @since 11
870  */
871 typedef void (*Rdb_DetailsObserver)(void *context, const Rdb_ChangeInfo **changeInfo, uint32_t count);
872 
873 /**
874  * @brief Indicates the callback functions.
875  *
876  * @since 11
877  */
878 typedef union Rdb_SubscribeCallback {
879     /**
880      * The callback function of cloud data change details event.
881      */
882     Rdb_DetailsObserver detailsObserver;
883 
884     /**
885      * The callback function of cloud data change event.
886      */
887     Rdb_BriefObserver briefObserver;
888 } Rdb_SubscribeCallback;
889 
890 /**
891  * @brief Indicates the observer of data.
892  *
893  * @since 11
894  */
895 typedef struct Rdb_DataObserver {
896     /**
897      * The context of data observer.
898      */
899     void *context;
900 
901     /**
902      * The callback of data observer.
903      */
904     Rdb_SubscribeCallback callback;
905 } Rdb_DataObserver;
906 
907 /**
908  * @brief Registers an observer for the database.
909  * When data in the distributed database changes, the callback will be invoked.
910  *
911  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
912  * @param type Indicates the subscription type, which is defined in {@link Rdb_SubscribeType}.
913  * @param observer The {@link Rdb_DataObserver} of change events in the database.
914  * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}.
915  *     {@link RDB_OK} - success.
916  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
917  * @see OH_Rdb_Store.
918  * @see Rdb_DataObserver.
919  * @since 11
920  */
921 int OH_Rdb_Subscribe(OH_Rdb_Store *store, Rdb_SubscribeType type, const Rdb_DataObserver *observer);
922 
923 /**
924  * @brief Remove specified observer of specified type from the database.
925  *
926  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
927  * @param type Indicates the subscription type, which is defined in {@link Rdb_SubscribeType}.
928  * @param observer The {@link Rdb_DataObserver} of change events in the database.
929  * If this is nullptr, remove all observers of the type.
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_DataObserver.
935  * @since 11
936  */
937 int OH_Rdb_Unsubscribe(OH_Rdb_Store *store, Rdb_SubscribeType type, const Rdb_DataObserver *observer);
938 
939 /**
940  * @brief Indicates the database synchronization mode.
941  *
942  * @since 11
943  */
944 typedef enum Rdb_SyncMode {
945     /**
946      * @brief Indicates that data is synchronized from the end with the closest modification time
947      * to the end with a more distant modification time.
948      */
949     RDB_SYNC_MODE_TIME_FIRST,
950     /**
951      * @brief Indicates that data is synchronized from local to cloud.
952      */
953     RDB_SYNC_MODE_NATIVE_FIRST,
954     /**
955      * @brief Indicates that data is synchronized from cloud to local.
956      */
957     RDB_SYNC_MODE_CLOUD_FIRST
958 } Rdb_SyncMode;
959 
960 /**
961  * @brief Describes the statistic of the cloud sync process.
962  *
963  * @since 11
964  */
965 typedef struct Rdb_Statistic {
966     /**
967      * Describes the total number of data to sync.
968      */
969     int total;
970 
971     /**
972      * Describes the number of successfully synced data.
973      */
974     int successful;
975 
976     /**
977      * Describes the number of data failed to sync.
978      */
979     int failed;
980 
981     /**
982      * Describes the number of data remained to sync.
983      */
984     int remained;
985 } Rdb_Statistic;
986 
987 /**
988  * @brief Describes the {@link Rdb_Statistic} details of the table.
989  *
990  * @since 11
991  */
992 typedef struct Rdb_TableDetails {
993     /**
994      * Indicates the name of changed table.
995      */
996     const char *table;
997 
998     /**
999      * Describes the {@link Rdb_Statistic} details of the upload process.
1000      */
1001     Rdb_Statistic upload;
1002 
1003     /**
1004      * Describes the {@link Rdb_Statistic} details of the download process.
1005      */
1006     Rdb_Statistic download;
1007 } Rdb_TableDetails;
1008 
1009 /**
1010  * The cloud sync progress
1011  *
1012  * @since 11
1013  */
1014 typedef enum Rdb_Progress {
1015     /**
1016      * @brief Means the sync process begin.
1017      */
1018     RDB_SYNC_BEGIN,
1019 
1020     /**
1021      * @brief Means the sync process is in progress
1022      */
1023     RDB_SYNC_IN_PROGRESS,
1024 
1025     /**
1026      * @brief Means the sync process is finished
1027      */
1028     RDB_SYNC_FINISH
1029 } Rdb_Progress;
1030 
1031 /**
1032    * Describes the status of cloud sync progress.
1033    *
1034    * @since 11
1035    */
1036 typedef enum Rdb_ProgressCode {
1037     /**
1038      * @brief Means the status of progress is success.
1039      */
1040     RDB_SUCCESS,
1041 
1042     /**
1043      * @brief Means the progress meets unknown error.
1044      */
1045     RDB_UNKNOWN_ERROR,
1046 
1047     /**
1048      * @brief Means the progress meets network error.
1049      */
1050     RDB_NETWORK_ERROR,
1051 
1052     /**
1053      * @brief Means cloud is disabled.
1054      */
1055     RDB_CLOUD_DISABLED,
1056 
1057     /**
1058      * @brief Means the progress is locked by others.
1059      */
1060     RDB_LOCKED_BY_OTHERS,
1061 
1062     /**
1063      * @brief Means the record exceeds the limit.
1064      */
1065     RDB_RECORD_LIMIT_EXCEEDED,
1066 
1067     /**
1068      * Means the cloud has no space for the asset.
1069      */
1070     RDB_NO_SPACE_FOR_ASSET
1071 } Rdb_ProgressCode;
1072 
1073 /**
1074  * @brief Indicates version of {@link Rdb_ProgressDetails}
1075  *
1076  * @since 11
1077  */
1078 #define DISTRIBUTED_PROGRESS_DETAIL_VERSION 1
1079 
1080 /**
1081  * @brief Describes detail of the cloud sync progress.
1082  *
1083  * @since 11
1084  */
1085 typedef struct Rdb_ProgressDetails {
1086     /**
1087      * The version used to uniquely identify the Rdb_ProgressDetails struct.
1088      */
1089     int version;
1090 
1091     /**
1092      * Describes the status of data sync progress. Defined in {@link Rdb_Progress}.
1093      */
1094     int schedule;
1095 
1096     /**
1097      * Describes the code of data sync progress. Defined in {@link Rdb_ProgressCode}.
1098      */
1099     int code;
1100 
1101     /**
1102      * Describes the length of changed tables in data sync progress.
1103      */
1104     int32_t tableLength;
1105 } Rdb_ProgressDetails;
1106 
1107 /**
1108  * @brief Get table details from progress details.
1109  *
1110  * @param progress Represents a pointer to an {@link Rdb_ProgressDetails} instance.
1111  * @param version Indicates the version of current {@link Rdb_ProgressDetails}.
1112  * @return If the operation is successful, a pointer to the instance of the {@link Rdb_TableDetails}
1113  * structure is returned.If get details is failed, nullptr is returned.
1114  * @see Rdb_ProgressDetails
1115  * @see Rdb_TableDetails
1116  * @since 11
1117  */
1118 Rdb_TableDetails *OH_Rdb_GetTableDetails(Rdb_ProgressDetails *progress, int32_t version);
1119 
1120 /**
1121  * @brief The callback function of progress.
1122  *
1123  * @param progressDetails The details of the sync progress.
1124  * @see Rdb_ProgressDetails.
1125  * @since 11
1126  */
1127 typedef void (*Rdb_ProgressCallback)(void *context, Rdb_ProgressDetails *progressDetails);
1128 
1129 /**
1130  * @brief The callback function of sync.
1131  *
1132  * @param progressDetails The details of the sync progress.
1133  * @see Rdb_ProgressDetails.
1134  * @since 11
1135  */
1136 typedef void (*Rdb_SyncCallback)(Rdb_ProgressDetails *progressDetails);
1137 
1138 /**
1139  * @brief The observer of progress.
1140  *
1141  * @since 11
1142  */
1143 typedef struct Rdb_ProgressObserver {
1144     /**
1145      * The context of progress observer.
1146      */
1147     void *context;
1148 
1149     /**
1150      * The callback function of progress observer.
1151      */
1152     Rdb_ProgressCallback callback;
1153 } Rdb_ProgressObserver;
1154 
1155 /**
1156  * @brief Sync data to cloud.
1157  *
1158  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
1159  * @param mode Represents the {@link Rdb_SyncMode} of sync progress.
1160  * @param tables Indicates the names of tables to sync.
1161  * @param count The count of tables to sync. If value equals 0, sync all tables of the store.
1162  * @param observer The {@link Rdb_ProgressObserver} of cloud sync progress.
1163  * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}.
1164  *     {@link RDB_OK} - success.
1165  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
1166  * @see OH_Rdb_Store.
1167  * @see Rdb_ProgressObserver.
1168  * @since 11
1169  */
1170 int OH_Rdb_CloudSync(OH_Rdb_Store *store, Rdb_SyncMode mode, const char *tables[], uint32_t count,
1171     const Rdb_ProgressObserver *observer);
1172 
1173 /**
1174 * @brief Subscribes to the automatic synchronization progress of an RDB store.
1175 * A callback will be invoked when there is a notification of the automatic synchronization progress.
1176 *
1177 * @param store Indicates the pointer to the target {@Link OH_Rdb_Store} instance.
1178 * @param observer The {@link Rdb_SyncObserver} for the automatic synchornizaiton progress
1179 * Indicates the callback invoked to return the automatic synchronization progress.
1180 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}.
1181 *     {@link RDB_OK} - success.
1182 *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
1183 * @see OH_Rdb_Store.
1184 * @see Rdb_ProgressObserver.
1185 * @since 11
1186 */
1187 int OH_Rdb_SubscribeAutoSyncProgress(OH_Rdb_Store *store, const Rdb_ProgressObserver *observer);
1188 
1189 /**
1190 * @brief Unsubscribes from the automatic synchronization progress of an RDB store.
1191 *
1192 * @param store Indicates the pointer to the target {@Link OH_Rdb_Store} instance.
1193 * @param observer Indicates the {@link Rdb_SyncObserver} callback for the automatic synchronization progress.
1194 * If it is a null pointer, all callbacks for the automatic synchronization progress will be unregistered.
1195 * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}.
1196 *     {@link RDB_OK} - success.
1197 *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
1198 * @see OH_Rdb_Store.
1199 * @see Rdb_ProgressObserver.
1200 * @since 11
1201 */
1202 int OH_Rdb_UnsubscribeAutoSyncProgress(OH_Rdb_Store *store, const Rdb_ProgressObserver *observer);
1203 
1204 /**
1205  * @brief Lock data from the database based on specified conditions.
1206  *
1207  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
1208  * @param predicates Represents a pointer to an {@link OH_Predicates} instance.
1209  * Indicates the specified lock condition.
1210  * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}.
1211  *     {@link RDB_OK} - success.
1212  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
1213  * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode.
1214  * @since 12
1215  */
1216 int OH_Rdb_LockRow(OH_Rdb_Store *store, OH_Predicates *predicates);
1217 
1218 /**
1219  * @brief Unlock data from the database based on specified conditions.
1220  *
1221  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
1222  * @param predicates Represents a pointer to an {@link OH_Predicates} instance.
1223  * Indicates the specified unlock condition.
1224  * @return Returns the status code of the execution. See {@link OH_Rdb_ErrCode}.
1225  *     {@link RDB_OK} - success.
1226  *     {@link RDB_E_INVALID_ARGS} - The error code for common invalid args.
1227  * @see OH_Rdb_Store, OH_Predicates, OH_Rdb_ErrCode.
1228  * @since 12
1229  */
1230 int OH_Rdb_UnlockRow(OH_Rdb_Store *store, OH_Predicates *predicates);
1231 
1232 /**
1233  * @brief Queries locked data in the database based on specified conditions.
1234  *
1235  * @param store Represents a pointer to an {@link OH_Rdb_Store} instance.
1236  * @param predicates Represents a pointer to an {@link OH_Predicates} instance.
1237  * Indicates the specified query condition.
1238  * @param columnNames Indicates the columns to query. If the value is empty array, the query applies to all columns.
1239  * @param length Indicates the length of columnNames.
1240  * @return If the query is successful, a pointer to the instance of the @link OH_Cursor} structure is returned.
1241  *         If Get store failed or resultSet is nullptr, nullptr is returned.
1242  * @see OH_Rdb_Store, OH_Predicates, OH_Cursor.
1243  * @since 12
1244  */
1245 OH_Cursor *OH_Rdb_QueryLockedRow(
1246     OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length);
1247 #ifdef __cplusplus
1248 };
1249 #endif
1250 
1251 #endif // RELATIONAL_STORE_H
1252