提交 6f4a860d 编写于 作者: M mizikoi53

getTableData done

上级 2dc2f79e
......@@ -122,6 +122,13 @@
return [weakSelf getAllTable:request];
}];
[self addHandlerForMethod:@"GET"
path:@"/getTableData"
requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse * _Nullable(__kindof GCDWebServerRequest * _Nonnull request) {
return [weakSelf getTableData:request];
}];
[self addHandlerForMethod:@"POST"
path:@"/insertRow"
requestClass:[GCDWebServerDataRequest class]
......@@ -155,6 +162,50 @@
return response;
}
- (GCDWebServerResponse *)getTableData:(GCDWebServerRequest *)request {
NSDictionary *query = request.query;
NSString *dirPath = query[@"dirPath"];
NSString *fileName = query[@"fileName"];
NSString *tableName = query[@"tableName"];
NSString *rootPath = NSHomeDirectory();
NSString *targetPath = [NSString stringWithFormat:@"%@/%@/%@", rootPath, dirPath, fileName];
if (![_fm fileExistsAtPath:targetPath]) {
return [self responseWhenFailed];
}
FMDatabase *db = [FMDatabase databaseWithPath:targetPath];
if (![db open]) {
return [self responseWhenFailed];
}
NSMutableArray *fieldInfo = @[].mutableCopy;
NSMutableArray *rows = @[].mutableCopy;
FMResultSet *tableInfo = [db executeQuery:[NSString stringWithFormat:@"PRAGMA table_info(%@)", tableName]];
while ([tableInfo next]) {
NSString *title = [tableInfo stringForColumn:@"name"];
BOOL isPrimary = [tableInfo boolForColumn:@"pk"];
if (title) {
[fieldInfo addObject:@{@"title" : title,
@"isPrimary" : @(isPrimary)}];
}
}
FMResultSet *set = [db executeQuery:[NSString stringWithFormat:@"SELECT * FROM %@;", tableName]];
while ([set next]) {
NSDictionary *row = [NSDictionary dictionaryWithDictionary:set.resultDictionary];
[rows addObject:row];
}
[db close];
GCDWebServerResponse *response = [GCDWebServerDataResponse responseWithJSONObject:@{@"fieldInfo" : fieldInfo,
@"rows" : rows}];
[response setValue:@"*" forAdditionalHeader:@"Access-Control-Allow-Origin"];
return response;
}
- (GCDWebServerResponse *)insertRow:(GCDWebServerDataRequest *)request {
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:request.data options:0 error:nil];
NSString *dirPath = data[@"dirPath"];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册