26
26
#define RCNTableNameMain " main"
27
27
#define RCNTableNameMainActive " main_active"
28
28
#define RCNTableNameMainDefault " main_default"
29
- #define RCNTableNameMetadata " fetch_metadata"
29
+ #define RCNTableNameMetadataDeprecated " fetch_metadata"
30
+ #define RCNTableNameMetadata " fetch_metadata_v2"
30
31
#define RCNTableNameInternalMetadata " internal_metadata"
31
32
#define RCNTableNameExperiment " experiment"
32
33
#define RCNTableNamePersonalization " personalization"
@@ -98,9 +99,9 @@ static BOOL RemoteConfigCreateFilePathIfNotExist(NSString *filePath) {
98
99
99
100
static NSArray *RemoteConfigMetadataTableColumnsInOrder () {
100
101
return @[
101
- RCNKeyBundleIdentifier, RCNKeyFetchTime, RCNKeyDigestPerNamespace, RCNKeyDeviceContext ,
102
- RCNKeyAppContext, RCNKeySuccessFetchTime, RCNKeyFailureFetchTime, RCNKeyLastFetchStatus ,
103
- RCNKeyLastFetchError, RCNKeyLastApplyTime, RCNKeyLastSetDefaultsTime
102
+ RCNKeyBundleIdentifier, RCNKeyNamespace, RCNKeyFetchTime, RCNKeyDigestPerNamespace ,
103
+ RCNKeyDeviceContext, RCNKeyAppContext, RCNKeySuccessFetchTime, RCNKeyFailureFetchTime,
104
+ RCNKeyLastFetchStatus, RCNKeyLastFetchError, RCNKeyLastApplyTime, RCNKeyLastSetDefaultsTime
104
105
];
105
106
}
106
107
@@ -267,8 +268,8 @@ - (BOOL)createTableSchema {
267
268
268
269
static const char *createTableMetadata =
269
270
" create TABLE IF NOT EXISTS " RCNTableNameMetadata
270
- " (_id INTEGER PRIMARY KEY, bundle_identifier"
271
- " TEXT, fetch_time INTEGER, digest_per_ns BLOB, device_context BLOB, app_context BLOB, "
271
+ " (_id INTEGER PRIMARY KEY, bundle_identifier TEXT, namespace TEXT, "
272
+ " fetch_time INTEGER, digest_per_ns BLOB, device_context BLOB, app_context BLOB, "
272
273
" success_fetch_time BLOB, failure_fetch_time BLOB, last_fetch_status INTEGER, "
273
274
" last_fetch_error INTEGER, last_apply_time INTEGER, last_set_defaults_time INTEGER)" ;
274
275
@@ -354,9 +355,9 @@ - (BOOL)insertMetadataTableWithValues:(NSDictionary *)columnNameToValue {
354
355
RCN_MUST_NOT_BE_MAIN_THREAD ();
355
356
static const char *SQL =
356
357
" INSERT INTO " RCNTableNameMetadata
357
- " (bundle_identifier, fetch_time, digest_per_ns, device_context, "
358
+ " (bundle_identifier, namespace, fetch_time, digest_per_ns, device_context, "
358
359
" app_context, success_fetch_time, failure_fetch_time, last_fetch_status, "
359
- " last_fetch_error, last_apply_time, last_set_defaults_time) values (?, ?, ?, ?, ?, "
360
+ " last_fetch_error, last_apply_time, last_set_defaults_time) values (?, ?, ?, ?, ?, ?, "
360
361
" ?, ?, ?, ?, ?, ?)" ;
361
362
362
363
sqlite3_stmt *statement = [self prepareSQL: SQL];
@@ -368,7 +369,8 @@ - (BOOL)insertMetadataTableWithValues:(NSDictionary *)columnNameToValue {
368
369
NSArray *columns = RemoteConfigMetadataTableColumnsInOrder ();
369
370
int index = 0 ;
370
371
for (NSString *columnName in columns) {
371
- if ([columnName isEqualToString: RCNKeyBundleIdentifier]) {
372
+ if ([columnName isEqualToString: RCNKeyBundleIdentifier] ||
373
+ [columnName isEqualToString: RCNKeyNamespace]) {
372
374
NSString *value = columnNameToValue[columnName];
373
375
if (![self bindStringToStatement: statement index: ++index string: value]) {
374
376
return [self logErrorWithSQL: SQL finalizeStatement: statement returnValue: NO ];
@@ -618,10 +620,11 @@ - (BOOL)insertOrUpdatePersonalizationConfig:(NSDictionary *)dataValue
618
620
#pragma mark - update
619
621
620
622
- (void )updateMetadataWithOption : (RCNUpdateOption)option
623
+ namespace : (NSString *)namespace
621
624
values : (NSArray *)values
622
625
completionHandler : (RCNDBCompletion)handler {
623
626
dispatch_async (_databaseOperationQueue, ^{
624
- BOOL success = [self updateMetadataTableWithOption: option andValues: values];
627
+ BOOL success = [self updateMetadataTableWithOption: option namespace: namespace andValues: values];
625
628
if (handler) {
626
629
dispatch_async (dispatch_get_main_queue (), ^{
627
630
handler (success, nil );
@@ -630,17 +633,20 @@ - (void)updateMetadataWithOption:(RCNUpdateOption)option
630
633
});
631
634
}
632
635
633
- - (BOOL )updateMetadataTableWithOption : (RCNUpdateOption)option andValues : (NSArray *)values {
636
+ - (BOOL )updateMetadataTableWithOption : (RCNUpdateOption)option
637
+ namespace : (NSString *)namespace
638
+ andValues : (NSArray *)values {
634
639
RCN_MUST_NOT_BE_MAIN_THREAD ();
635
640
static const char *SQL =
636
641
" UPDATE " RCNTableNameMetadata " (last_fetch_status, last_fetch_error, last_apply_time, "
637
- " last_set_defaults_time) values (?, ?, ?, ?)" ;
642
+ " last_set_defaults_time) values (?, ?, ?, ?) WHERE namespace = ? " ;
638
643
if (option == RCNUpdateOptionFetchStatus) {
639
- SQL = " UPDATE " RCNTableNameMetadata " SET last_fetch_status = ?, last_fetch_error = ?" ;
644
+ SQL = " UPDATE " RCNTableNameMetadata
645
+ " SET last_fetch_status = ?, last_fetch_error = ? WHERE namespace = ?" ;
640
646
} else if (option == RCNUpdateOptionApplyTime) {
641
- SQL = " UPDATE " RCNTableNameMetadata " SET last_apply_time = ?" ;
647
+ SQL = " UPDATE " RCNTableNameMetadata " SET last_apply_time = ? WHERE namespace = ? " ;
642
648
} else if (option == RCNUpdateOptionDefaultTime) {
643
- SQL = " UPDATE " RCNTableNameMetadata " SET last_set_defaults_time = ?" ;
649
+ SQL = " UPDATE " RCNTableNameMetadata " SET last_set_defaults_time = ? WHERE namespace = ? " ;
644
650
} else {
645
651
return NO ;
646
652
}
@@ -666,6 +672,12 @@ - (BOOL)updateMetadataTableWithOption:(RCNUpdateOption)option andValues:(NSArray
666
672
return [self logErrorWithSQL: SQL finalizeStatement: statement returnValue: NO ];
667
673
}
668
674
}
675
+ // bind namespace to query
676
+ if (sqlite3_bind_text (statement, ++index, [namespace UTF8String ], -1 , SQLITE_TRANSIENT) !=
677
+ SQLITE_OK) {
678
+ return [self logErrorWithSQL: SQL finalizeStatement: statement returnValue: NO ];
679
+ }
680
+
669
681
if (sqlite3_step (statement) != SQLITE_DONE) {
670
682
return [self logErrorWithSQL: SQL finalizeStatement: statement returnValue: NO ];
671
683
}
@@ -674,31 +686,34 @@ - (BOOL)updateMetadataTableWithOption:(RCNUpdateOption)option andValues:(NSArray
674
686
}
675
687
#pragma mark - read from DB
676
688
677
- - (NSDictionary *)loadMetadataWithBundleIdentifier : (NSString *)bundleIdentifier {
689
+ - (NSDictionary *)loadMetadataWithBundleIdentifier : (NSString *)bundleIdentifier
690
+ namespace : (NSString *)namespace {
678
691
__block NSDictionary *metadataTableResult;
679
692
__weak RCNConfigDBManager *weakSelf = self;
680
693
dispatch_sync (_databaseOperationQueue, ^{
681
- metadataTableResult = [weakSelf loadMetadataTableWithBundleIdentifier: bundleIdentifier];
694
+ metadataTableResult = [weakSelf loadMetadataTableWithBundleIdentifier: bundleIdentifier
695
+ namespace: namespace];
682
696
});
683
697
if (metadataTableResult) {
684
698
return metadataTableResult;
685
699
}
686
700
return [[NSDictionary alloc ] init ];
687
701
}
688
702
689
- - (NSMutableDictionary *)loadMetadataTableWithBundleIdentifier : (NSString *)bundleIdentifier {
703
+ - (NSMutableDictionary *)loadMetadataTableWithBundleIdentifier : (NSString *)bundleIdentifier
704
+ namespace : (NSString *)namespace {
690
705
NSMutableDictionary *dict = [[NSMutableDictionary alloc ] init ];
691
706
const char *SQL =
692
707
" SELECT bundle_identifier, fetch_time, digest_per_ns, device_context, app_context, "
693
708
" success_fetch_time, failure_fetch_time , last_fetch_status, "
694
709
" last_fetch_error, last_apply_time, last_set_defaults_time FROM " RCNTableNameMetadata
695
- " WHERE bundle_identifier = ?" ;
710
+ " WHERE bundle_identifier = ? and namespace = ? " ;
696
711
sqlite3_stmt *statement = [self prepareSQL: SQL];
697
712
if (!statement) {
698
713
return nil ;
699
714
}
700
715
701
- NSArray *params = @[ bundleIdentifier ];
716
+ NSArray *params = @[ bundleIdentifier, namespace ];
702
717
[self bindStringsToStatement: statement stringArray: params];
703
718
704
719
while (sqlite3_step (statement) == SQLITE_ROW) {
@@ -1043,6 +1058,7 @@ - (void)deleteRecordFromMainTableWithNamespace:(NSString *)namespace_p
1043
1058
}
1044
1059
1045
1060
- (void )deleteRecordWithBundleIdentifier : (NSString *)bundleIdentifier
1061
+ namespace : (NSString *)namespace
1046
1062
isInternalDB : (BOOL )isInternalDB {
1047
1063
__weak RCNConfigDBManager *weakSelf = self;
1048
1064
dispatch_async (_databaseOperationQueue, ^{
@@ -1051,10 +1067,11 @@ - (void)deleteRecordWithBundleIdentifier:(NSString *)bundleIdentifier
1051
1067
return ;
1052
1068
}
1053
1069
const char *SQL = " DELETE FROM " RCNTableNameInternalMetadata " WHERE key LIKE ?" ;
1070
+ NSArray *params = @[ bundleIdentifier ];
1054
1071
if (!isInternalDB) {
1055
- SQL = " DELETE FROM " RCNTableNameMetadata " WHERE bundle_identifier = ?" ;
1072
+ SQL = " DELETE FROM " RCNTableNameMetadata " WHERE bundle_identifier = ? and namespace = ?" ;
1073
+ params = @[ bundleIdentifier, namespace ];
1056
1074
}
1057
- NSArray *params = @[ bundleIdentifier ];
1058
1075
[strongSelf executeQuery: SQL withParams: params];
1059
1076
});
1060
1077
}
0 commit comments