Create commands data models and add ObjectMapper.

Reviewed By: lblasa

Differential Revision: D48188172

fbshipit-source-id: 10c99250f993c71976c693a691586385d111a4fd
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 9db2c74632
commit ce85f44a33
16 changed files with 462 additions and 163 deletions

View File

@@ -0,0 +1,27 @@
/*
* 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>
@interface DatabaseExecuteSqlResponse : NSObject
@property(nonatomic, strong) NSString* type;
@property(nonatomic, strong) NSArray* columns;
@property(nonatomic, strong) NSArray* values;
@property(nonatomic, strong) NSNumber* insertedId;
@property(nonatomic, assign) NSInteger affectedCount;
@end
@interface DatabaseExecuteSqlRequest : NSObject
@property(nonatomic, assign, readonly) NSInteger databaseId;
@property(nonatomic, copy, readonly) NSString* value;
- (instancetype)initWithDatabaseId:(NSInteger)databaseId value:(NSString*)value;
@end

View File

@@ -0,0 +1,42 @@
/*
* 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.
*/
#include "DatabaseExecuteSql.h"
@implementation DatabaseExecuteSqlResponse
- (instancetype)initWithType:(NSString*)type
columns:(NSArray*)columns
values:(NSArray*)values
insertedId:(NSNumber*)insertedId
affectedCount:(NSInteger)affectedCount {
self = [super init];
if (self) {
_type = type;
_columns = [columns copy];
_values = [values copy];
_insertedId = insertedId;
_affectedCount = affectedCount;
}
return self;
}
@end
@implementation DatabaseExecuteSqlRequest
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
value:(NSString*)value {
self = [super init];
if (self) {
_databaseId = databaseId;
_value = [value copy];
}
return self;
}
@end

View File

@@ -0,0 +1,42 @@
/*
* 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>
@interface DatabaseGetTableDataResponse : NSObject
@property(nonatomic, strong, readonly) NSArray<NSString*>* columns;
@property(nonatomic, strong, readonly) NSArray<NSArray*>* values;
@property(nonatomic, assign, readonly) NSInteger start;
@property(nonatomic, assign, readonly) NSInteger count;
@property(nonatomic, assign, readonly) NSInteger total;
- (instancetype)initWithColumns:(NSArray<NSString*>*)columns
values:(NSArray<NSArray*>*)values
start:(NSInteger)start
count:(NSInteger)count
total:(NSInteger)total;
@end
@interface DatabaseGetTableDataRequest : 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

View File

@@ -0,0 +1,50 @@
/*
* 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 "DatabaseGetTableData.h"
@implementation DatabaseGetTableDataResponse
- (instancetype)initWithColumns:(NSArray<NSString*>*)columns
values:(NSArray<NSArray*>*)values
start:(NSInteger)start
count:(NSInteger)count
total:(NSInteger)total {
self = [super init];
if (self) {
_columns = [columns copy];
_values = [values copy];
_start = start;
_count = count;
_total = total;
}
return self;
}
@end
@implementation DatabaseGetTableDataRequest
- (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

View File

@@ -0,0 +1,25 @@
/*
* 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>
@interface DatabaseGetTableInfoResponse : NSObject
@property(nonatomic, strong) NSString* definition;
- (instancetype)initWithDefinition:(NSString*)definition;
@end
@interface DatabaseGetTableInfoRequest : 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,34 @@
/*
* 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 "DatabaseGetTableInfo.h"
@implementation DatabaseGetTableInfoResponse
- (instancetype)initWithDefinition:(NSString*)definition {
self = [super init];
if (self) {
_definition = definition;
}
return self;
}
@end
@implementation DatabaseGetTableInfoRequest
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
table:(NSString*)table {
self = [super init];
if (self) {
_databaseId = databaseId;
_table = [table copy];
}
return self;
}
@end

View File

@@ -0,0 +1,34 @@
/*
* 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>
@interface DatabaseGetTableStructureResponse : NSObject
@property(nonatomic, strong, readonly) NSArray<NSString*>* structureColumns;
@property(nonatomic, strong, readonly)
NSArray<NSArray<NSString*>*>* structureValues;
@property(nonatomic, strong, readonly) NSArray<NSString*>* indexesColumns;
@property(nonatomic, strong, readonly)
NSArray<NSArray<NSString*>*>* indexesValues;
- (instancetype)
initWithStructureColumns:(NSArray<NSString*>*)structureColumns
structureValues:(NSArray<NSArray<NSString*>*>*)structureValues
indexesColumns:(NSArray<NSString*>*)indexesColumns
indexesValues:(NSArray<NSArray<NSString*>*>*)indexesValues;
@end
@interface DatabaseGetTableStructureRequest : 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,41 @@
/*
* 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 "DatabaseGetTableStructure.h"
@implementation DatabaseGetTableStructureResponse
- (instancetype)
initWithStructureColumns:(NSArray<NSString*>*)structureColumns
structureValues:(NSArray<NSArray<NSString*>*>*)structureValues
indexesColumns:(NSArray<NSString*>*)indexesColumns
indexesValues:(NSArray<NSArray<NSString*>*>*)indexesValues {
self = [super init];
if (self) {
_structureColumns = [structureColumns copy];
_structureValues = [structureValues copy];
_indexesColumns = [indexesColumns copy];
_indexesValues = [indexesValues copy];
}
return self;
}
@end
@implementation DatabaseGetTableStructureRequest
- (instancetype)initWithDatabaseId:(NSInteger)databaseId
table:(NSString*)table {
self = [super init];
if (self) {
_databaseId = databaseId;
_table = [table copy];
}
return self;
}
@end

View File

@@ -0,0 +1,23 @@
/*
* 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>
#import "DatabaseDescriptor.h"
#import "DatabaseDriver.h"
@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

View File

@@ -0,0 +1,24 @@
/*
* 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 "DatabaseDescriptorHolder.h"
@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

View File

@@ -0,0 +1,21 @@
/*
* 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>
typedef NS_ENUM(NSInteger, DatabasesErrorCodes) {
DatabasesErrorCodesInvalidRequest = 1,
DatabasesErrorCodesDatabaseInvalid = 2,
DatabasesErrorCodesSqlExecutionException = 3,
};
static NSString* const kDatabasesErrorCodesInvalidRequestMessage =
@"The request received was invalid";
static NSString* const kDatabasesErrorCodesDatabaseInvalidMessage =
@"Could not access database";
static NSString* const kDatabasesErrorCodesSqlExecutionExceptionMessage =
@"SQL execution exception: ";

View File

@@ -23,61 +23,3 @@
- (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

@@ -11,7 +11,11 @@
#import <FlipperKit/FlipperResponder.h>
#include <Foundation/Foundation.h>
#import "DatabaseDescriptor.h"
#import "DatabaseDescriptorHolder.h"
#import "DatabaseDriver.h"
#import "DatabaseErrorCodes.h"
#import "DatabaseGetTableStructure.h"
#import "ObjectMapper.h"
@interface DatabasesManager ()
@@ -73,7 +77,7 @@
}
}
NSDictionary* result = [DatabasesManager
NSDictionary* result = [ObjectMapper
databaseListToDictionary:self.databaseDescriptorHolderSet];
[responder success:result];
}];
@@ -89,7 +93,7 @@
}];
[self.connection
receive:@"getTableInfo"
receive:@"getTableStructure"
withBlock:^(NSDictionary* params, id<FlipperResponder> responder){
}];
@@ -99,107 +103,4 @@
}];
}
+ (NSDictionary*)databaseListToDictionary:
(NSSet<DatabaseDescriptorHolder*>*)databaseDescriptorHolderSet {
NSMutableDictionary* resultDict = [NSMutableDictionary new];
for (DatabaseDescriptorHolder* descriptorHolder in
databaseDescriptorHolderSet) {
NSArray<NSString*>* tableNameList = [descriptorHolder.databaseDriver
getTableNames:descriptorHolder.databaseDescriptor];
NSArray<NSString*>* sortedTableNames =
[tableNameList sortedArrayUsingSelector:@selector(compare:)];
NSString* idString =
[NSString stringWithFormat:@"%ld", descriptorHolder.identifier];
NSDictionary* databaseDict = @{
@"id" : idString,
@"name" : descriptorHolder.databaseDescriptor.name,
@"tables" : sortedTableNames
};
[resultDict setObject:databaseDict forKey:idString];
}
return resultDict;
}
@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

@@ -0,0 +1,30 @@
/*
* 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>
@class DatabaseDescriptorHolder;
@class DatabaseExecuteSqlResponse;
@class DatabaseGetTableDataResponse;
@class DatabaseGetTableInfoResponse;
@class DatabaseGetTableStructureResponse;
@interface ObjectMapper : NSObject
+ (NSDictionary*)databaseListToDictionary:
(NSMutableSet<DatabaseDescriptorHolder*>*)databaseDescriptorHolderSet;
+ (NSDictionary*)databaseGetTableDataResponseToDictionary:
(DatabaseGetTableDataResponse*)response;
+ (NSDictionary*)databaseGetTableStructureResponseToDictionary:
(DatabaseGetTableStructureResponse*)response;
+ (NSDictionary*)databaseGetTableInfoResponseToDictionary:
(DatabaseGetTableInfoResponse*)response;
+ (NSDictionary*)databaseExecuteSqlResponseToDictionary:
(DatabaseExecuteSqlResponse*)response;
+ (NSDictionary*)errorWithCode:(NSInteger)code message:(NSString*)message;
@end

View File

@@ -0,0 +1,63 @@
/*
* 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 "ObjectMapper.h"
#include <Foundation/Foundation.h>
#import "DatabaseDescriptorHolder.h"
#import "DatabaseExecuteSql.h"
#import "DatabaseGetTableData.h"
#import "DatabaseGetTableInfo.h"
#import "DatabaseGetTableStructure.h"
@implementation ObjectMapper
+ (NSDictionary*)databaseListToDictionary:
(NSMutableSet<DatabaseDescriptorHolder*>*)databaseDescriptorHolderSet {
NSMutableDictionary* result = [NSMutableDictionary new];
for (DatabaseDescriptorHolder* holder in databaseDescriptorHolderSet) {
NSArray<NSString*>* tables =
[holder.databaseDriver getTableNames:holder.databaseDescriptor];
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];
}
return result;
}
+ (NSDictionary*)databaseGetTableDataResponseToDictionary:
(DatabaseGetTableDataResponse*)response {
return @{};
}
+ (NSDictionary*)errorWithCode:(NSInteger)code message:(NSString*)message {
return @{@"code" : @(code), @"message" : message};
}
+ (NSDictionary*)databaseGetTableStructureResponseToDictionary:
(DatabaseGetTableStructureResponse*)response {
return @{};
}
+ (NSDictionary*)databaseGetTableInfoResponseToDictionary:
(DatabaseGetTableInfoResponse*)response {
return @{};
}
+ (NSDictionary*)databaseExecuteSqlResponseToDictionary:
(DatabaseExecuteSqlResponse*)response {
return @{};
}
@end