Implement similar structure we have in Java plugin.

Reviewed By: lblasa

Differential Revision: D48172579

fbshipit-source-id: b2127507b04cdb85f426313e71405a81103e42bd
This commit is contained in:
Fúlvio Abrahão de Paula
2023-08-14 11:07:07 -07:00
committed by Facebook GitHub Bot
parent 3003330067
commit fea326be79
6 changed files with 260 additions and 48 deletions

View File

@@ -0,0 +1,10 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
@protocol DatabaseDescriptor<NSObject>
- (NSString*)name;
@end

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
@protocol DatabaseDescriptor;
@protocol DatabaseDriver<NSObject>
@property(nonatomic, strong, readonly) id<DatabaseDescriptor>
databaseDescriptor;
- (NSArray<id<DatabaseDescriptor>>*)getDatabases;
- (NSArray<NSString*>*)getTableNames:(id<DatabaseDescriptor>)databaseDescriptor;
@end

View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
@protocol DatabaseDriver;
@protocol DatabaseDescriptor;
@protocol FlipperConnection;
@interface DatabasesManager : NSObject
@property(nonatomic, strong) id<FlipperConnection> connection;
@property(nonatomic, strong, readonly)
NSArray<id<DatabaseDriver>>* databaseDrivers;
- (instancetype)initWithDatabaseDrivers:
(NSArray<id<DatabaseDriver>>*)databaseDrivers;
- (void)setConnection:(id<FlipperConnection>)connection;
- (BOOL)isConnected;
@end
@interface DatabaseDescriptorHolder : NSObject
@property(nonatomic, assign, readonly) NSInteger identifier;
@property(nonatomic, strong, readonly) id<DatabaseDriver> databaseDriver;
@property(nonatomic, strong, readonly) id<DatabaseDescriptor>
databaseDescriptor;
- (instancetype)initWithIdentifier:(NSInteger)identifier
databaseDriver:(id<DatabaseDriver>)databaseDriver
databaseDescriptor:(id<DatabaseDescriptor>)databaseDescriptor;
@end
@interface ExecuteSqlRequest : NSObject
@property(nonatomic, assign, readonly) NSInteger databaseId;
@property(nonatomic, copy, readonly) NSString* value;
- (instancetype)initWithDatabaseId:(NSInteger)databaseId value:(NSString*)value;
@end
@interface GetTableDataRequest : NSObject
@property(nonatomic, assign, readonly) NSInteger databaseId;
@property(nonatomic, copy, readonly) NSString* table;
@property(nonatomic, copy, readonly) NSString* order;
@property(nonatomic, assign, readonly) BOOL reverse;
@property(nonatomic, assign, readonly) NSInteger start;
@property(nonatomic, assign, readonly) NSInteger count;
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
table:(NSString*)table
order:(NSString*)order
reverse:(BOOL)reverse
start:(NSInteger)start
count:(NSInteger)count;
@end
@interface GetTableStructureRequest : NSObject
@property(nonatomic, assign, readonly) NSInteger databaseId;
@property(nonatomic, copy, readonly) NSString* table;
- (instancetype)initWithDatabaseId:(NSInteger)databaseId table:(NSString*)table;
@end
@interface GetTableInfoRequest : NSObject
@property(nonatomic, assign, readonly) NSInteger databaseId;
@property(nonatomic, copy, readonly) NSString* table;
- (instancetype)initWithDatabaseId:(NSInteger)databaseId table:(NSString*)table;
@end

View File

@@ -0,0 +1,136 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "DatabasesManager.h"
#import <FlipperKit/FlipperClient.h>
#import <FlipperKit/FlipperConnection.h>
#import <FlipperKit/FlipperResponder.h>
#import "DatabaseDriver.h"
@interface DatabasesManager ()
@property(nonatomic, strong)
NSMutableDictionary<NSNumber*, DatabaseDescriptorHolder*>*
databaseDescriptorHolders;
@property(nonatomic, strong)
NSMutableSet<DatabaseDescriptorHolder*>* databaseDescriptorHolderSet;
@end
@implementation DatabasesManager
- (instancetype)initWithDatabaseDrivers:
(NSArray<id<DatabaseDriver>>*)databaseDrivers {
self = [super init];
if (self) {
_databaseDrivers = [databaseDrivers copy];
_databaseDescriptorHolders = [[NSMutableDictionary alloc] init];
_databaseDescriptorHolderSet = [[NSMutableSet alloc] init];
}
return self;
}
- (void)setConnection:(id<FlipperConnection>)connection {
_connection = connection;
if (connection) {
[self listenForCommands];
}
}
- (BOOL)isConnected {
return _connection != nil;
}
- (void)listenForCommands {
// TODO(fulvioabrahao) implement commands.
[self.connection
receive:@"databaseList"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder){
}];
}
@end
@implementation DatabaseDescriptorHolder
- (instancetype)initWithIdentifier:(NSInteger)identifier
databaseDriver:(id<DatabaseDriver>)databaseDriver
databaseDescriptor:(id<DatabaseDescriptor>)databaseDescriptor {
self = [super init];
if (self) {
_identifier = identifier;
_databaseDriver = databaseDriver;
_databaseDescriptor = databaseDescriptor;
}
return self;
}
@end
@implementation ExecuteSqlRequest
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
value:(NSString*)value {
self = [super init];
if (self) {
_databaseId = databaseId;
_value = [value copy];
}
return self;
}
@end
@implementation GetTableDataRequest
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
table:(NSString*)table
order:(NSString*)order
reverse:(BOOL)reverse
start:(NSInteger)start
count:(NSInteger)count {
self = [super init];
if (self) {
_databaseId = databaseId;
_table = [table copy];
_order = [order copy];
_reverse = reverse;
_start = start;
_count = count;
}
return self;
}
@end
@implementation GetTableStructureRequest
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
table:(NSString*)table {
self = [super init];
if (self) {
_databaseId = databaseId;
_table = [table copy];
}
return self;
}
@end
@implementation GetTableInfoRequest
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
table:(NSString*)table {
self = [super init];
if (self) {
_databaseId = databaseId;
_table = [table copy];
}
return self;
}
@end

View File

@@ -8,7 +8,11 @@
#import <FlipperKit/FlipperPlugin.h>
#import <Foundation/Foundation.h>
@class DatabasesManager;
@interface FlipperKitDatabasesPlugin : NSObject<FlipperPlugin>
@property(nonatomic, strong) DatabasesManager* databasesManager;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)sharedInstance;

View File

@@ -10,6 +10,8 @@
#import <FlipperKit/FlipperClient.h>
#import <FlipperKit/FlipperConnection.h>
#import <FlipperKit/FlipperResponder.h>
#import "DatabaseDriver.h"
#import "DatabasesManager.h"
@interface FlipperKitDatabasesPlugin ()
@property(strong, nonatomic) id<FlipperConnection> connection;
@@ -20,6 +22,9 @@
- (instancetype)init {
if (self = [super init]) {
NSArray<id<DatabaseDriver>>* databaseDrivers = @[];
_databasesManager =
[[DatabasesManager alloc] initWithDatabaseDrivers:databaseDrivers];
}
return self;
}
@@ -37,56 +42,11 @@
- (void)didConnect:(id<FlipperConnection>)connection {
self.connection = connection;
// Define connection event handlers
// databaseList
[connection
receive:@"databaseList"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder) {
NSMutableDictionary* response = [NSMutableDictionary dictionary];
response[@1] = @{
@"id" : @1,
@"name" : @"Provider1",
@"tables" : @[ @"Table1_1", @"Table1_2", @"Table1_3" ]
};
response[@2] = @{
@"id" : @2,
@"name" : @"Provider2",
@"tables" : @[ @"Table2_1", @"Table2_2" ]
};
response[@3] =
@{@"id" : @3,
@"name" : @"Provider3",
@"tables" : @[ @"Table3_1" ]};
[responder success:response];
}];
// getTableData
[connection receive:@"getTableData"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder){
}];
// getTableStructure
[connection receive:@"getTableStructure"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder){
}];
// getTableInfo
[connection receive:@"getTableInfo"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder){
}];
// execute
[connection receive:@"execute"
withBlock:^(NSDictionary*, id<FlipperResponder> responder){
}];
[self.databasesManager setConnection:connection];
}
- (void)didDisconnect {
self.connection = nil;
[self.databasesManager setConnection:nil];
}
- (NSString*)identifier {
@@ -94,7 +54,7 @@
}
- (BOOL)runInBackground {
return YES;
return NO;
}
@end