FLEXTableListViewController.m 3.0 KB

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