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 oh_predicates.h 30 * 31 * @brief Declared predicate 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 OH_PREDICATES_H 40 #define OH_PREDICATES_H 41 42 #ifdef __cplusplus 43 #include <cstdint> 44 #else 45 #include <stdint.h> 46 #endif 47 48 #include <stddef.h> 49 #include "database/rdb/oh_value_object.h" 50 #include "database/data/oh_data_values.h" 51 52 #ifdef __cplusplus 53 extern "C" { 54 #endif 55 56 /** 57 * @brief Result set sort type. 58 * 59 * @since 10 60 */ 61 typedef enum OH_OrderType { 62 /** 63 * Ascend order. 64 */ 65 ASC = 0, 66 /** 67 * Descend order. 68 */ 69 DESC = 1, 70 } OH_OrderType; 71 72 /** 73 * @brief Define the OH_Predicates structure type. 74 * 75 * @since 10 76 */ 77 typedef struct OH_Predicates OH_Predicates; 78 79 /** 80 * @brief Define the OH_Predicates structure type. 81 * 82 * @since 10 83 */ 84 struct OH_Predicates { 85 /** 86 * The id used to uniquely identify the OH_Predicates struct. 87 */ 88 int64_t id; 89 90 /** 91 * @brief Function pointer. Restricts the value of the field to be equal to the specified value to the predicates. 92 * 93 * This method is similar to = of the SQL statement. 94 * 95 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 96 * @param field Indicates the column name in the database table. 97 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 98 * @return Returns the self. 99 * @see OH_Predicates, OH_VObject. 100 * @since 10 101 */ 102 OH_Predicates *(*equalTo)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 103 104 /** 105 * @brief Function pointer. 106 * Restricts the value of the field to be not equal to the specified value to the predicates. 107 * 108 * This method is similar to != of the SQL statement. 109 * 110 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 111 * @param field Indicates the column name in the database table. 112 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 113 * @return Returns the self. 114 * @see OH_Predicates, OH_VObject. 115 * @since 10 116 */ 117 OH_Predicates *(*notEqualTo)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 118 119 /** 120 * @brief Function pointer. Add left parenthesis to predicate. 121 * 122 * This method is similar to ( of the SQL statement. 123 * 124 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 125 * @return Returns the self. 126 * @see OH_Predicates. 127 * @since 10 128 */ 129 OH_Predicates *(*beginWrap)(OH_Predicates *predicates); 130 131 /** 132 * @brief Function pointer. Add right parenthesis to predicate. 133 * 134 * This method is similar to ) of the SQL statement. 135 * 136 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 137 * @return Returns the self. 138 * @see OH_Predicates. 139 * @since 10 140 */ 141 OH_Predicates *(*endWrap)(OH_Predicates *predicates); 142 143 /** 144 * @brief Function pointer. Adds an or condition to the predicates. 145 * 146 * This method is similar to OR of the SQL statement. 147 * 148 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 149 * @return Returns the self. 150 * @see OH_Predicates. 151 * @since 10 152 */ 153 OH_Predicates *(*orOperate)(OH_Predicates *predicates); 154 155 /** 156 * @brief Function pointer. Adds an and condition to the predicates. 157 * 158 * This method is similar to AND of the SQL statement. 159 * 160 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 161 * @return Returns the self. 162 * @see OH_Predicates. 163 * @since 10 164 */ 165 OH_Predicates *(*andOperate)(OH_Predicates *predicates); 166 167 /** 168 * @brief Function pointer. Restricts the value of the field which is null to the predicates. 169 * 170 * This method is similar to IS NULL of the SQL statement. 171 * 172 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 173 * @param field Indicates the column name in the database table. 174 * @return Returns the self. 175 * @see OH_Predicates. 176 * @since 10 177 */ 178 OH_Predicates *(*isNull)(OH_Predicates *predicates, const char *field); 179 180 /** 181 * @brief Function pointer. Restricts the value of the field which is not null to the predicates. 182 * 183 * This method is similar to IS NOT NULL of the SQL statement. 184 * 185 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 186 * @param field Indicates the column name in the database table. 187 * @return Returns the self. 188 * @see OH_Predicates. 189 * @since 10 190 */ 191 OH_Predicates *(*isNotNull)(OH_Predicates *predicates, const char *field); 192 193 /** 194 * @brief Function pointer. Restricts the value of the field to be like the specified value to the predicates. 195 * 196 * This method is similar to LIKE of the SQL statement. 197 * 198 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 199 * @param field Indicates the column name in the database table. 200 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 201 * @return Returns the self. 202 * @see OH_Predicates, OH_VObject. 203 * @since 10 204 */ 205 OH_Predicates *(*like)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 206 207 /** 208 * @brief Function pointer. Restricts the value of the field to be between the specified value to the predicates. 209 * 210 * This method is similar to BETWEEN of the SQL statement. 211 * 212 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 213 * @param field Indicates the column name in the database table. 214 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 215 * @return Returns the self. 216 * @see OH_Predicates, OH_VObject. 217 * @since 10 218 */ 219 OH_Predicates *(*between)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 220 221 /** 222 * @brief Function pointer. 223 * Restricts the value of the field to be not between the specified value to the predicates. 224 * 225 * This method is similar to NOT BETWEEN of the SQL statement. 226 * 227 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 228 * @param field Indicates the column name in the database table. 229 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 230 * @return Returns the self. 231 * @see OH_Predicates, OH_VObject. 232 * @since 10 233 */ 234 OH_Predicates *(*notBetween)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 235 236 /** 237 * @brief Function pointer. 238 * Restricts the value of the field to be greater than the specified value to the predicates. 239 * 240 * This method is similar to > of the SQL statement. 241 * 242 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 243 * @param field Indicates the column name in the database table. 244 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 245 * @return Returns the self. 246 * @see OH_Predicates, OH_VObject. 247 * @since 10 248 */ 249 OH_Predicates *(*greaterThan)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 250 251 /** 252 * @brief Function pointer. 253 * Restricts the value of the field to be less than the specified value to the predicates. 254 * 255 * This method is similar to < of the SQL statement. 256 * 257 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 258 * @param field Indicates the column name in the database table. 259 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 260 * @return Returns the self. 261 * @see OH_Predicates, OH_VObject. 262 * @since 10 263 */ 264 OH_Predicates *(*lessThan)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 265 266 /** 267 * @brief Function pointer. 268 * Restricts the value of the field to be greater than or equal to the specified value to the predicates. 269 * 270 * This method is similar to >= of the SQL statement. 271 * 272 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 273 * @param field Indicates the column name in the database table. 274 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 275 * @return Returns the self. 276 * @see OH_Predicates, OH_VObject. 277 * @since 10 278 */ 279 OH_Predicates *(*greaterThanOrEqualTo)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 280 281 /** 282 * @brief Function pointer. 283 * Restricts the value of the field to be less than or equal to the specified value to the predicates. 284 * 285 * This method is similar to <= of the SQL statement. 286 * 287 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 288 * @param field Indicates the column name in the database table. 289 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 290 * @return Returns the self. 291 * @see OH_Predicates, OH_VObject. 292 * @since 10 293 */ 294 OH_Predicates *(*lessThanOrEqualTo)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 295 296 /** 297 * @brief Function pointer. Restricts the ascending or descending order of the return list. 298 * When there are several orders, the one close to the head has the highest priority. 299 * 300 * This method is similar ORDER BY the SQL statement. 301 * 302 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 303 * @param field Indicates the column name in the database table. 304 * @param type Indicates the sort {@link OH_OrderType} type. 305 * @return Returns the self. 306 * @see OH_Predicates, OH_OrderType. 307 * @since 10 308 */ 309 OH_Predicates *(*orderBy)(OH_Predicates *predicates, const char *field, OH_OrderType type); 310 311 /** 312 * @brief Function pointer. Configure predicates to filter duplicate records and retain only one of them. 313 * 314 * This method is similar DISTINCT the SQL statement. 315 * 316 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 317 * @return Returns the self. 318 * @see OH_Predicates. 319 * @since 10 320 */ 321 OH_Predicates *(*distinct)(OH_Predicates *predicates); 322 323 /** 324 * @brief Function pointer. Predicate for setting the maximum number of data records. 325 * 326 * This method is similar LIMIT the SQL statement. 327 * 328 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 329 * @param value Indicates the maximum number of records. 330 * @return Returns the self. 331 * @see OH_Predicates. 332 * @since 10 333 */ 334 OH_Predicates *(*limit)(OH_Predicates *predicates, unsigned int value); 335 336 /** 337 * @brief Function pointer. Configure the predicate to specify the starting position of the returned result. 338 * 339 * This method is similar OFFSET the SQL statement. 340 * 341 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 342 * @param rowOffset Indicates the number of rows to offset from the beginning. The value is a positive integer. 343 * @return Returns the self. 344 * @see OH_Predicates. 345 * @since 10 346 */ 347 OH_Predicates *(*offset)(OH_Predicates *predicates, unsigned int rowOffset); 348 349 /** 350 * @brief Function pointer. Configure predicates to group query results by specified columns. 351 * 352 * This method is similar GROUP BY the SQL statement. 353 * 354 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 355 * @param fields Indicates the column names that the grouping depends on. 356 * @param length Indicates the length of fields. 357 * @return Returns the self. 358 * @see OH_Predicates. 359 * @since 10 360 */ 361 OH_Predicates *(*groupBy)(OH_Predicates *predicates, char const *const *fields, int length); 362 363 /** 364 * @brief Function pointer. 365 * Configure the predicate to match the specified field and the value within the given array range. 366 * 367 * This method is similar IN the SQL statement. 368 * 369 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 370 * @param field Indicates the column name in the database table. 371 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 372 * @return Returns the self. 373 * @see OH_Predicates, OH_VObject. 374 * @since 10 375 */ 376 OH_Predicates *(*in)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 377 378 /** 379 * @brief Function pointer. 380 * Configure the predicate to match the specified field and the value not within the given array range. 381 * 382 * This method is similar NOT IN the SQL statement. 383 * 384 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 385 * @param field Indicates the column name in the database table. 386 * @param valueObject Represents a pointer to an {@link OH_VObject} instance. 387 * @return Returns the self. 388 * @see OH_Predicates, OH_VObject. 389 * @since 10 390 */ 391 OH_Predicates *(*notIn)(OH_Predicates *predicates, const char *field, OH_VObject *valueObject); 392 393 /** 394 * @brief Function pointer. Initialize OH_Predicates object. 395 * 396 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 397 * @return Returns the self. 398 * @see OH_Predicates. 399 * @since 10 400 */ 401 OH_Predicates *(*clear)(OH_Predicates *predicates); 402 403 /** 404 * @brief Destroy the {@link OH_Predicates} object and reclaim the memory occupied by the object. 405 * 406 * @param predicates Represents a pointer to an {@link OH_Predicates} instance. 407 * @return Returns the status code of the execution.. 408 * @see OH_Predicates. 409 * @since 10 410 */ 411 int (*destroy)(OH_Predicates *predicates); 412 }; 413 414 /** 415 * @brief Sets the OH_Predicates to match the field whose data type is string and value is not like the specified value. 416 * This method is similar to "Not like" of the SQL statement. 417 * 418 * @param predicates Represents a pointer to an instance of OH_Predicates. 419 * @param field Indicates the column name in the database table. 420 * @param pattern Indicates the value to compare against. 421 * @return Returns the error code. 422 * Returns {@link RDB_OK} if the execution is successful. 423 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 424 * @since 20 425 */ 426 int OH_Predicates_NotLike(OH_Predicates *predicates, const char *field, const char *pattern); 427 428 /** 429 * @brief Sets the OH_Predicates to match the specified field whose data type is string and the value contains 430 * a wildcard. Different from like, the input parameters of this method are case-sensitive. 431 * 432 * @param predicates Represents a pointer to an instance of OH_Predicates. 433 * @param field Indicates the column name in the database table. 434 * @param pattern Indicates the value to match with the predicate. 435 * @return Returns the error code. 436 * Returns {@link RDB_OK} if the execution is successful. 437 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 438 * @since 20 439 */ 440 int OH_Predicates_Glob(OH_Predicates *predicates, const char *field, const char *pattern); 441 442 /** 443 * @brief Sets the OH_Predicates to not match the specified field whose data type is string and the value contains 444 * a wildcard. Different from not like, the input parameters of this method are case-sensitive. 445 * 446 * @param predicates Represents a pointer to an instance of OH_Predicates. 447 * @param field Indicates the column name in the database table. 448 * @param pattern Indicates the value to compare against. 449 * @return Returns the error code. 450 * Returns {@link RDB_OK} if the execution is successful. 451 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 452 * @since 20 453 */ 454 int OH_Predicates_NotGlob(OH_Predicates *predicates, const char *field, const char *pattern); 455 456 /** 457 * @brief Sets the OH_Predicates to specify conditions to filter grouped results that will appear in the final result. 458 * 459 * @param predicates Represents a pointer to an instance of OH_Predicates. 460 * @param conditions Indicates filter conditions in the having clause. 461 * @param values Indicates a pointer to an instance of OH_Data_Values. 462 * @return Returns the error code. 463 * Returns {@link RDB_OK} if the execution is successful. 464 * Returns {@link RDB_E_INVALID_ARGS} if invalid input parameter. 465 * @since 20 466 */ 467 int OH_Predicates_Having(OH_Predicates *predicates, const char *conditions, const OH_Data_Values *values); 468 #ifdef __cplusplus 469 }; 470 #endif 471 472 /** @} */ 473 474 #endif // OH_PREDICATES_H 475