FLEXTableListViewController.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // PTTableListViewController.m
  3. // PTDatabaseReader
  4. //
  5. // Created by Peng Tao on 15/11/23.
  6. // Copyright © 2015年 Peng Tao. All rights reserved.
  7. //
  8. #import "FLEXTableListViewController.h"
  9. #import "FLEXDatabaseManager.h"
  10. #import "FLEXSQLiteDatabaseManager.h"
  11. #import "FLEXRealmDatabaseManager.h"
  12. #import "FLEXTableContentViewController.h"
  13. @interface FLEXTableListViewController ()
  14. {
  15. id<FLEXDatabaseManager> _dbm;
  16. NSString *_databasePath;
  17. }
  18. @property (nonatomic, strong) NSArray *tables;
  19. @end
  20. @implementation FLEXTableListViewController
  21. - (instancetype)initWithPath:(NSString *)path
  22. {
  23. self = [super initWithStyle:UITableViewStyleGrouped];
  24. if (self) {
  25. _databasePath = [path copy];
  26. _dbm = [self databaseManagerForFileAtPath:_databasePath];
  27. [_dbm open];
  28. [self getAllTables];
  29. }
  30. return self;
  31. }
  32. - (id<FLEXDatabaseManager>)databaseManagerForFileAtPath:(NSString *)path
  33. {
  34. NSString *pathExtension = path.pathExtension.lowercaseString;
  35. if ([@[@"db", @"sqlite", @"sqlite3"] indexOfObject:pathExtension] != NSNotFound) {
  36. return [[FLEXSQLiteDatabaseManager alloc] initWithPath:path];
  37. }
  38. else if ([pathExtension isEqualToString:@"realm"]) {
  39. return [[FLEXRealmDatabaseManager alloc] initWithPath:path];
  40. }
  41. return nil;
  42. }
  43. - (void)getAllTables
  44. {
  45. NSArray *resultArray = [_dbm queryAllTables];
  46. NSMutableArray *array = [NSMutableArray array];
  47. for (NSDictionary *dict in resultArray) {
  48. [array addObject:dict[@"name"]];
  49. }
  50. self.tables = array;
  51. }
  52. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  53. {
  54. return self.tables.count;
  55. }
  56. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  57. {
  58. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLEXTableListViewControllerCell"];
  59. if (!cell) {
  60. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  61. reuseIdentifier:@"FLEXTableListViewControllerCell"];
  62. }
  63. cell.textLabel.text = self.tables[indexPath.row];
  64. return cell;
  65. }
  66. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  67. {
  68. FLEXTableContentViewController *contentViewController = [[FLEXTableContentViewController alloc] init];
  69. contentViewController.contentsArray = [_dbm queryAllDataWithTableName:self.tables[indexPath.row]];
  70. contentViewController.columnsArray = [_dbm queryAllColumnsWithTableName:self.tables[indexPath.row]];
  71. contentViewController.title = self.tables[indexPath.row];
  72. [self.navigationController pushViewController:contentViewController animated:YES];
  73. }
  74. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  75. {
  76. return [NSString stringWithFormat:@"%lu tables", (unsigned long)self.tables.count];
  77. }
  78. @end