@@ -29,8 +29,14 @@ use crate::{
29
29
CBL_DeleteDatabase , CBLEncryptionKey_FromPassword , FLString , kCBLMaintenanceTypeCompact,
30
30
kCBLEncryptionNone, kCBLMaintenanceTypeFullOptimize, kCBLMaintenanceTypeIntegrityCheck,
31
31
kCBLMaintenanceTypeOptimize, kCBLMaintenanceTypeReindex, CBL_CopyDatabase ,
32
+ CBLDatabase_ScopeNames , CBLDatabase_CollectionNames , CBLDatabase_Scope ,
33
+ CBLDatabase_Collection , CBLDatabase_CreateCollection , CBLDatabase_DeleteCollection ,
34
+ CBLDatabase_DefaultScope , CBLDatabase_DefaultCollection ,
32
35
} ,
33
36
Listener , check_error, Error , CouchbaseLiteError ,
37
+ collection:: Collection ,
38
+ scope:: Scope ,
39
+ MutableArray ,
34
40
} ;
35
41
use std:: path:: { Path , PathBuf } ;
36
42
use std:: ptr;
@@ -89,7 +95,8 @@ enum_from_primitive! {
89
95
}
90
96
91
97
/** A database change listener callback, invoked after one or more documents are changed on disk. */
92
- type ChangeListener = Box < dyn Fn ( & Database , Vec < String > ) > ;
98
+ #[ deprecated( note = "please use `CollectionChangeListener` on default collection instead" ) ]
99
+ type DatabaseChangeListener = Box < dyn Fn ( & Database , Vec < String > ) > ;
93
100
94
101
#[ no_mangle]
95
102
unsafe extern "C" fn c_database_change_listener (
@@ -98,7 +105,7 @@ unsafe extern "C" fn c_database_change_listener(
98
105
num_docs : :: std:: os:: raw:: c_uint ,
99
106
c_doc_ids : * mut FLString ,
100
107
) {
101
- let callback = context as * const ChangeListener ;
108
+ let callback = context as * const DatabaseChangeListener ;
102
109
let database = Database :: retain ( db as * mut CBLDatabase ) ;
103
110
104
111
let doc_ids = std:: slice:: from_raw_parts ( c_doc_ids, num_docs as usize )
@@ -324,10 +331,157 @@ impl Database {
324
331
}
325
332
326
333
/** Returns the number of documents in the database. */
334
+ #[ deprecated( note = "please use `count` on the default collection instead" ) ]
327
335
pub fn count ( & self ) -> u64 {
328
336
unsafe { CBLDatabase_Count ( self . get_ref ( ) ) }
329
337
}
330
338
339
+ /** Returns the names of all existing scopes in the database.
340
+ The scope exists when there is at least one collection created under the scope.
341
+ The default scope is exceptional in that it will always exists even there are no collections under it. */
342
+ pub fn scope_names ( & self ) -> Result < Vec < String > > {
343
+ let mut error = CBLError :: default ( ) ;
344
+ let array = unsafe { CBLDatabase_ScopeNames ( self . get_ref ( ) , & mut error) } ;
345
+
346
+ check_error ( & error) . map ( |( ) | unsafe {
347
+ MutableArray :: adopt ( array)
348
+ . iter ( )
349
+ . map ( |v| v. as_string ( ) . unwrap_or ( "" ) . to_string ( ) )
350
+ . collect ( )
351
+ } )
352
+ }
353
+
354
+ /** Returns the names of all collections in the scope. */
355
+ pub fn collection_names ( & self , scope_name : String ) -> Result < Vec < String > > {
356
+ let scope_name = from_str ( & scope_name) ;
357
+ let mut error = CBLError :: default ( ) ;
358
+ let array = unsafe {
359
+ CBLDatabase_CollectionNames ( self . get_ref ( ) , scope_name. get_ref ( ) , & mut error)
360
+ } ;
361
+
362
+ check_error ( & error) . map ( |( ) | unsafe {
363
+ MutableArray :: adopt ( array)
364
+ . iter ( )
365
+ . map ( |v| v. as_string ( ) . unwrap_or ( "" ) . to_string ( ) )
366
+ . collect ( )
367
+ } )
368
+ }
369
+
370
+ /** Returns an existing scope with the given name.
371
+ The scope exists when there is at least one collection created under the scope.
372
+ The default scope is exception in that it will always exists even there are no collections under it. */
373
+ pub fn scope ( & self , scope_name : String ) -> Result < Option < Scope > > {
374
+ let scope_name = from_str ( & scope_name) ;
375
+ let mut error = CBLError :: default ( ) ;
376
+ let scope = unsafe { CBLDatabase_Scope ( self . get_ref ( ) , scope_name. get_ref ( ) , & mut error) } ;
377
+
378
+ check_error ( & error) . map ( |( ) | {
379
+ if scope. is_null ( ) {
380
+ None
381
+ } else {
382
+ Some ( Scope :: retain ( scope) )
383
+ }
384
+ } )
385
+ }
386
+
387
+ /** Returns the existing collection with the given name and scope. */
388
+ pub fn collection (
389
+ & self ,
390
+ collection_name : String ,
391
+ scope_name : String ,
392
+ ) -> Result < Option < Collection > > {
393
+ let collection_name = from_str ( & collection_name) ;
394
+ let scope_name = from_str ( & scope_name) ;
395
+ let mut error = CBLError :: default ( ) ;
396
+ let collection = unsafe {
397
+ CBLDatabase_Collection (
398
+ self . get_ref ( ) ,
399
+ collection_name. get_ref ( ) ,
400
+ scope_name. get_ref ( ) ,
401
+ & mut error,
402
+ )
403
+ } ;
404
+
405
+ check_error ( & error) . map ( |( ) | {
406
+ if collection. is_null ( ) {
407
+ None
408
+ } else {
409
+ Some ( Collection :: retain ( collection) )
410
+ }
411
+ } )
412
+ }
413
+
414
+ /** Create a new collection.
415
+ The naming rules of the collections and scopes are as follows:
416
+ - Must be between 1 and 251 characters in length.
417
+ - Can only contain the characters A-Z, a-z, 0-9, and the symbols _, -, and %.
418
+ - Cannot start with _ or %.
419
+ - Both scope and collection names are case sensitive.
420
+ If the collection already exists, the existing collection will be returned. */
421
+ pub fn create_collection (
422
+ & self ,
423
+ collection_name : String ,
424
+ scope_name : String ,
425
+ ) -> Result < Collection > {
426
+ let collection_name = from_str ( & collection_name) ;
427
+ let scope_name = from_str ( & scope_name) ;
428
+ let mut error = CBLError :: default ( ) ;
429
+ let collection = unsafe {
430
+ CBLDatabase_CreateCollection (
431
+ self . get_ref ( ) ,
432
+ collection_name. get_ref ( ) ,
433
+ scope_name. get_ref ( ) ,
434
+ & mut error,
435
+ )
436
+ } ;
437
+
438
+ check_error ( & error) . map ( |( ) | Collection :: retain ( collection) )
439
+ }
440
+
441
+ /** Delete an existing collection.
442
+ @note The default collection cannot be deleted.
443
+ @param db The database.
444
+ @param collectionName The name of the collection.
445
+ @param scopeName The name of the scope.
446
+ @param outError On failure, the error will be written here.
447
+ @return True if success, or False if an error occurred. */
448
+ pub fn delete_collection ( & self , collection_name : String , scope_name : String ) -> Result < ( ) > {
449
+ let collection_name = from_str ( & collection_name) ;
450
+ let scope_name = from_str ( & scope_name) ;
451
+ unsafe {
452
+ check_bool ( |error| {
453
+ CBLDatabase_DeleteCollection (
454
+ self . get_ref ( ) ,
455
+ collection_name. get_ref ( ) ,
456
+ scope_name. get_ref ( ) ,
457
+ error,
458
+ )
459
+ } )
460
+ }
461
+ }
462
+
463
+ /** Returns the default scope. */
464
+ pub fn default_scope ( & self ) -> Result < Scope > {
465
+ let mut error = CBLError :: default ( ) ;
466
+ let scope = unsafe { CBLDatabase_DefaultScope ( self . get_ref ( ) , & mut error) } ;
467
+
468
+ check_error ( & error) . map ( |( ) | Scope :: retain ( scope) )
469
+ }
470
+
471
+ /** Returns the default collection. */
472
+ pub fn default_collection ( & self ) -> Result < Option < Collection > > {
473
+ let mut error = CBLError :: default ( ) ;
474
+ let collection = unsafe { CBLDatabase_DefaultCollection ( self . get_ref ( ) , & mut error) } ;
475
+
476
+ check_error ( & error) . map ( |( ) | {
477
+ if collection. is_null ( ) {
478
+ None
479
+ } else {
480
+ Some ( Collection :: retain ( collection) )
481
+ }
482
+ } )
483
+ }
484
+
331
485
//////// NOTIFICATIONS:
332
486
333
487
/** Registers a database change listener function. It will be called after one or more
@@ -340,7 +494,11 @@ impl Database {
340
494
You must keep the `Listener` object as long as you need it.
341
495
*/
342
496
#[ must_use]
343
- pub fn add_listener ( & mut self , listener : ChangeListener ) -> Listener < ChangeListener > {
497
+ #[ deprecated( note = "please use `add_listener` on default collection instead" ) ]
498
+ pub fn add_listener (
499
+ & mut self ,
500
+ listener : DatabaseChangeListener ,
501
+ ) -> Listener < DatabaseChangeListener > {
344
502
unsafe {
345
503
let listener = Box :: new ( listener) ;
346
504
let ptr = Box :: into_raw ( listener) ;
0 commit comments