FLEXTableListViewController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #import "FLEXMutableListSection.h"
  14. #import "NSArray+FLEX.h"
  15. #import "FLEXAlert.h"
  16. @interface FLEXTableListViewController ()
  17. @property (nonatomic, readonly) id<FLEXDatabaseManager> dbm;
  18. @property (nonatomic, readonly) NSString *path;
  19. @property (nonatomic, readonly) FLEXMutableListSection<NSString *> *tables;
  20. + (NSArray<NSString *> *)supportedSQLiteExtensions;
  21. + (NSArray<NSString *> *)supportedRealmExtensions;
  22. @end
  23. @implementation FLEXTableListViewController
  24. - (instancetype)initWithPath:(NSString *)path {
  25. self = [super initWithStyle:UITableViewStyleGrouped];
  26. if (self) {
  27. _path = path.copy;
  28. _dbm = [self databaseManagerForFileAtPath:path];
  29. }
  30. return self;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.showsSearchBar = YES;
  35. // Compose query button //
  36. UIBarButtonItem *composeQuery = [[UIBarButtonItem alloc]
  37. initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
  38. target:self
  39. action:@selector(queryButtonPressed)
  40. ];
  41. // Cannot run custom queries on realm databases
  42. composeQuery.enabled = [self.dbm
  43. respondsToSelector:@selector(executeStatement:)
  44. ];
  45. [self addToolbarItems:@[composeQuery]];
  46. }
  47. - (NSArray<FLEXTableViewSection *> *)makeSections {
  48. _tables = [FLEXMutableListSection list:[self.dbm queryAllTables]
  49. cellConfiguration:^(__kindof UITableViewCell *cell, NSString *tableName, NSInteger row) {
  50. cell.textLabel.text = tableName;
  51. } filterMatcher:^BOOL(NSString *filterText, NSString *tableName) {
  52. return [tableName localizedCaseInsensitiveContainsString:filterText];
  53. }
  54. ];
  55. self.tables.selectionHandler = ^(FLEXTableListViewController *host, NSString *tableName) {
  56. NSArray *rows = [host.dbm queryAllDataInTable:tableName];
  57. NSArray *columns = [host.dbm queryAllColumnsOfTable:tableName];
  58. UIViewController *resultsScreen = [FLEXTableContentViewController columns:columns rows:rows];
  59. resultsScreen.title = tableName;
  60. [host.navigationController pushViewController:resultsScreen animated:YES];
  61. };
  62. return @[self.tables];
  63. }
  64. - (void)reloadData {
  65. self.tables.customTitle = [NSString
  66. stringWithFormat:@"Tables (%@)", @(self.tables.filteredList.count)
  67. ];
  68. [super reloadData];
  69. }
  70. - (void)queryButtonPressed {
  71. FLEXSQLiteDatabaseManager *database = self.dbm;
  72. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  73. make.title(@"Execute an SQL query");
  74. make.textField(nil);
  75. make.button(@"Run").handler(^(NSArray<NSString *> *strings) {
  76. FLEXSQLResult *result = [database executeStatement:strings[0]];
  77. if (result.message) {
  78. [FLEXAlert showAlert:@"Message" message:result.message from:self];
  79. } else {
  80. UIViewController *resultsScreen = [FLEXTableContentViewController
  81. columns:result.columns rows:result.rows
  82. ];
  83. [self.navigationController pushViewController:resultsScreen animated:YES];
  84. }
  85. });
  86. make.button(@"Cancel").cancelStyle();
  87. } showFrom:self];
  88. }
  89. - (id<FLEXDatabaseManager>)databaseManagerForFileAtPath:(NSString *)path {
  90. NSString *pathExtension = path.pathExtension.lowercaseString;
  91. NSArray<NSString *> *sqliteExtensions = FLEXTableListViewController.supportedSQLiteExtensions;
  92. if ([sqliteExtensions indexOfObject:pathExtension] != NSNotFound) {
  93. return [FLEXSQLiteDatabaseManager managerForDatabase:path];
  94. }
  95. NSArray<NSString *> *realmExtensions = FLEXTableListViewController.supportedRealmExtensions;
  96. if (realmExtensions != nil && [realmExtensions indexOfObject:pathExtension] != NSNotFound) {
  97. return [FLEXRealmDatabaseManager managerForDatabase:path];
  98. }
  99. return nil;
  100. }
  101. #pragma mark - FLEXTableListViewController
  102. + (BOOL)supportsExtension:(NSString *)extension {
  103. extension = extension.lowercaseString;
  104. NSArray<NSString *> *sqliteExtensions = FLEXTableListViewController.supportedSQLiteExtensions;
  105. if (sqliteExtensions.count > 0 && [sqliteExtensions indexOfObject:extension] != NSNotFound) {
  106. return YES;
  107. }
  108. NSArray<NSString *> *realmExtensions = FLEXTableListViewController.supportedRealmExtensions;
  109. if (realmExtensions.count > 0 && [realmExtensions indexOfObject:extension] != NSNotFound) {
  110. return YES;
  111. }
  112. return NO;
  113. }
  114. + (NSArray<NSString *> *)supportedSQLiteExtensions {
  115. return @[@"db", @"sqlite", @"sqlite3"];
  116. }
  117. + (NSArray<NSString *> *)supportedRealmExtensions {
  118. if (NSClassFromString(@"RLMRealm") == nil) {
  119. return nil;
  120. }
  121. return @[@"realm"];
  122. }
  123. @end