Implement getTableStructure command.

Reviewed By: lblasa

Differential Revision: D48191484

fbshipit-source-id: 6838590f4e10b4613074e9e9d112a62d4688f739
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 ce85f44a33
commit 453b8f7b96
11 changed files with 121 additions and 38 deletions

View File

@@ -30,5 +30,7 @@
@property(nonatomic, copy, readonly) NSString* table;
- (instancetype)initWithDatabaseId:(NSInteger)databaseId table:(NSString*)table;
+ (DatabaseGetTableStructureRequest*)getTableStructureRequestFromDictionary:
(NSDictionary*)dictionary;
@end

View File

@@ -38,4 +38,15 @@
return self;
}
+ (DatabaseGetTableStructureRequest*)getTableStructureRequestFromDictionary:
(NSDictionary*)params {
int databaseId = [params[@"databaseId"] integerValue];
NSString* table = params[@"table"];
if (databaseId <= 0 || !table) {
return nil;
}
return [[DatabaseGetTableStructureRequest alloc] initWithDatabaseId:databaseId
table:table];
}
@end

View File

@@ -8,12 +8,14 @@
#import <Foundation/Foundation.h>
@protocol DatabaseDescriptor;
@class DatabaseGetTableStructureRequest;
@class DatabaseGetTableStructureResponse;
@protocol DatabaseDriver<NSObject>
@property(nonatomic, strong, readonly) id<DatabaseDescriptor>
databaseDescriptor;
- (NSArray<id<DatabaseDescriptor>>*)getDatabases;
- (NSArray<NSString*>*)getTableNames:(id<DatabaseDescriptor>)databaseDescriptor;
- (DatabaseGetTableStructureResponse*)
getTableStructureWithDatabaseDescriptor:
(id<DatabaseDescriptor>)databaseDescriptor
forTable:(NSString*)tableName;
@end

View File

@@ -77,19 +77,58 @@
}
}
NSDictionary* result = [ObjectMapper
databaseListToDictionary:self.databaseDescriptorHolderSet];
id result = [ObjectMapper
databaseListToFlipperArray:self.databaseDescriptorHolderSet];
[responder success:result];
}];
[self.connection
receive:@"getTableData"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder){
}];
[self.connection
receive:@"getTableStructure"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder){
withBlock:^(NSDictionary* params, id<FlipperResponder> responder) {
DatabaseGetTableStructureRequest* request =
[DatabaseGetTableStructureRequest
getTableStructureRequestFromDictionary:params];
if (!request) {
NSDictionary* errorResponse = [ObjectMapper
errorWithCode:DatabasesErrorCodesInvalidRequest
message:kDatabasesErrorCodesInvalidRequestMessage];
[responder error:errorResponse];
return;
}
DatabaseDescriptorHolder* descriptorHolder =
self.databaseDescriptorHolders[@(request.databaseId)];
if (!descriptorHolder) {
NSDictionary* errorResponse = [ObjectMapper
errorWithCode:DatabasesErrorCodesDatabaseInvalid
message:kDatabasesErrorCodesDatabaseInvalidMessage];
[responder error:errorResponse];
return;
}
@try {
DatabaseGetTableStructureResponse* tableStructure =
[descriptorHolder.databaseDriver
getTableStructureWithDatabaseDescriptor:
descriptorHolder.databaseDescriptor
forTable:request.table];
NSDictionary* response = [ObjectMapper
databaseGetTableStructureResponseToDictionary:tableStructure];
[responder success:response];
} @catch (NSException* exception) {
NSDictionary* errorResponse = [ObjectMapper
errorWithCode:DatabasesErrorCodesSqlExecutionException
message:[kDatabasesErrorCodesSqlExecutionExceptionMessage
stringByAppendingString:exception.reason]];
[responder error:errorResponse];
}
}];
[self.connection

View File

@@ -0,0 +1,49 @@
/*
* 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 "MockDatabaseDriver.h"
#import "DatabaseGetTableStructure.h"
#import "MockDatabaseDescriptor.h"
@implementation MockDatabaseDriver
- (NSArray<id<DatabaseDescriptor>>*)getDatabases {
MockDatabaseDescriptor* mockDescriptor =
[[MockDatabaseDescriptor alloc] init];
return @[ mockDescriptor ];
}
- (NSArray<NSString*>*)getTableNames:
(id<DatabaseDescriptor>)databaseDescriptor {
return @[ @"MockTable1", @"MockTable2" ];
}
- (DatabaseGetTableStructureResponse*)
getTableStructureWithDatabaseDescriptor:
(id<DatabaseDescriptor>)databaseDescriptor
forTable:(NSString*)tableName {
NSMutableArray<NSString*>* structureColumns =
[NSMutableArray arrayWithObjects:@"id", @"name", @"age", nil];
NSMutableArray<NSArray<NSString*>*>* structureValues =
[NSMutableArray arrayWithObjects:@[ @"1", @"John", @"25" ],
@[ @"2", @"Jane", @"30" ],
nil];
NSMutableArray<NSString*>* indexesColumns = [NSMutableArray
arrayWithObjects:@"index_name", @"unique", @"indexed_column_name", nil];
NSMutableArray<NSArray<NSString*>*>* indexesValues = [NSMutableArray
arrayWithObjects:@[ @"index_name1", @"false", @"id,name" ],
@[ @"index_name2", @"true", @"age" ],
nil];
return [[DatabaseGetTableStructureResponse alloc]
initWithStructureColumns:[structureColumns copy]
structureValues:[structureValues copy]
indexesColumns:[indexesColumns copy]
indexesValues:[indexesValues copy]];
}
@end

View File

@@ -1,26 +0,0 @@
/*
* 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 "MockDatabaseDriver.h"
#import "MockDatabaseDescriptor.h"
@implementation MockDatabaseDriver
- (NSArray<id<DatabaseDescriptor>>*)getDatabases {
MockDatabaseDescriptor* mockDescriptor =
[[MockDatabaseDescriptor alloc] init];
return @[ mockDescriptor ];
}
- (NSArray<NSString*>*)getTableNames:
(id<DatabaseDescriptor>)databaseDescriptor {
return @[ @"MockTable1", @"MockTable2" ];
}
@synthesize databaseDescriptor;
@end

View File

@@ -15,7 +15,7 @@
@interface ObjectMapper : NSObject
+ (NSDictionary*)databaseListToDictionary:
+ (NSMutableArray*)databaseListToFlipperArray:
(NSMutableSet<DatabaseDescriptorHolder*>*)databaseDescriptorHolderSet;
+ (NSDictionary*)databaseGetTableDataResponseToDictionary:
(DatabaseGetTableDataResponse*)response;

View File

@@ -15,9 +15,9 @@
@implementation ObjectMapper
+ (NSDictionary*)databaseListToDictionary:
+ (NSMutableArray*)databaseListToFlipperArray:
(NSMutableSet<DatabaseDescriptorHolder*>*)databaseDescriptorHolderSet {
NSMutableDictionary* result = [NSMutableDictionary new];
NSMutableArray* result = [NSMutableArray new];
for (DatabaseDescriptorHolder* holder in databaseDescriptorHolderSet) {
NSArray<NSString*>* tables =
@@ -25,12 +25,13 @@
NSArray<NSString*>* sortedTableNames =
[tables sortedArrayUsingSelector:@selector(compare:)];
NSString* idString = [NSString stringWithFormat:@"%ld", holder.identifier];
NSDictionary* databaseInfo = @{
@"id" : idString,
@"name" : holder.databaseDescriptor.name,
@"tables" : sortedTableNames
};
[result setObject:databaseInfo forKey:idString];
[result addObject:databaseInfo];
}
return result;
@@ -47,7 +48,12 @@
+ (NSDictionary*)databaseGetTableStructureResponseToDictionary:
(DatabaseGetTableStructureResponse*)response {
return @{};
return @{
@"structureColumns" : response.structureColumns,
@"structureValues" : response.structureValues,
@"indexesColumns" : response.indexesColumns,
@"indexesValues" : response.indexesValues
};
}
+ (NSDictionary*)databaseGetTableInfoResponseToDictionary: