123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //
- // PTTableListViewController.m
- // PTDatabaseReader
- //
- // Created by Peng Tao on 15/11/23.
- // Copyright © 2015年 Peng Tao. All rights reserved.
- //
- #import "FLEXTableListViewController.h"
- #import "FLEXDatabaseManager.h"
- #import "FLEXSQLiteDatabaseManager.h"
- #import "FLEXRealmDatabaseManager.h"
- #import "FLEXTableContentViewController.h"
- #import "FLEXMutableListSection.h"
- #import "NSArray+FLEX.h"
- #import "FLEXAlert.h"
- @interface FLEXTableListViewController ()
- @property (nonatomic, readonly) id<FLEXDatabaseManager> dbm;
- @property (nonatomic, readonly) NSString *path;
- @property (nonatomic, readonly) FLEXMutableListSection<NSString *> *tables;
- + (NSArray<NSString *> *)supportedSQLiteExtensions;
- + (NSArray<NSString *> *)supportedRealmExtensions;
- @end
- @implementation FLEXTableListViewController
- - (instancetype)initWithPath:(NSString *)path {
- self = [super initWithStyle:UITableViewStyleGrouped];
- if (self) {
- _path = path.copy;
- _dbm = [self databaseManagerForFileAtPath:path];
- }
-
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.showsSearchBar = YES;
-
- // Compose query button //
- UIBarButtonItem *composeQuery = [[UIBarButtonItem alloc]
- initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
- target:self
- action:@selector(queryButtonPressed)
- ];
- // Cannot run custom queries on realm databases
- composeQuery.enabled = [self.dbm
- respondsToSelector:@selector(executeStatement:)
- ];
-
- [self addToolbarItems:@[composeQuery]];
- }
- - (NSArray<FLEXTableViewSection *> *)makeSections {
- _tables = [FLEXMutableListSection list:[self.dbm queryAllTables]
- cellConfiguration:^(__kindof UITableViewCell *cell, NSString *tableName, NSInteger row) {
- cell.textLabel.text = tableName;
- } filterMatcher:^BOOL(NSString *filterText, NSString *tableName) {
- return [tableName localizedCaseInsensitiveContainsString:filterText];
- }
- ];
-
- self.tables.selectionHandler = ^(FLEXTableListViewController *host, NSString *tableName) {
- NSArray *rows = [host.dbm queryAllDataInTable:tableName];
- NSArray *columns = [host.dbm queryAllColumnsOfTable:tableName];
-
- UIViewController *resultsScreen = [FLEXTableContentViewController columns:columns rows:rows];
- resultsScreen.title = tableName;
- [host.navigationController pushViewController:resultsScreen animated:YES];
- };
-
- return @[self.tables];
- }
- - (void)reloadData {
- self.tables.customTitle = [NSString
- stringWithFormat:@"Tables (%@)", @(self.tables.filteredList.count)
- ];
-
- [super reloadData];
- }
-
- - (void)queryButtonPressed {
- FLEXSQLiteDatabaseManager *database = self.dbm;
-
- [FLEXAlert makeAlert:^(FLEXAlert *make) {
- make.title(@"Execute an SQL query");
- make.textField(nil);
- make.button(@"Run").handler(^(NSArray<NSString *> *strings) {
- FLEXSQLResult *result = [database executeStatement:strings[0]];
-
- if (result.message) {
- [FLEXAlert showAlert:@"Message" message:result.message from:self];
- } else {
- UIViewController *resultsScreen = [FLEXTableContentViewController
- columns:result.columns rows:result.rows
- ];
-
- [self.navigationController pushViewController:resultsScreen animated:YES];
- }
- });
- make.button(@"Cancel").cancelStyle();
- } showFrom:self];
- }
-
- - (id<FLEXDatabaseManager>)databaseManagerForFileAtPath:(NSString *)path {
- NSString *pathExtension = path.pathExtension.lowercaseString;
-
- NSArray<NSString *> *sqliteExtensions = FLEXTableListViewController.supportedSQLiteExtensions;
- if ([sqliteExtensions indexOfObject:pathExtension] != NSNotFound) {
- return [FLEXSQLiteDatabaseManager managerForDatabase:path];
- }
-
- NSArray<NSString *> *realmExtensions = FLEXTableListViewController.supportedRealmExtensions;
- if (realmExtensions != nil && [realmExtensions indexOfObject:pathExtension] != NSNotFound) {
- return [FLEXRealmDatabaseManager managerForDatabase:path];
- }
-
- return nil;
- }
- #pragma mark - FLEXTableListViewController
- + (BOOL)supportsExtension:(NSString *)extension {
- extension = extension.lowercaseString;
-
- NSArray<NSString *> *sqliteExtensions = FLEXTableListViewController.supportedSQLiteExtensions;
- if (sqliteExtensions.count > 0 && [sqliteExtensions indexOfObject:extension] != NSNotFound) {
- return YES;
- }
-
- NSArray<NSString *> *realmExtensions = FLEXTableListViewController.supportedRealmExtensions;
- if (realmExtensions.count > 0 && [realmExtensions indexOfObject:extension] != NSNotFound) {
- return YES;
- }
-
- return NO;
- }
- + (NSArray<NSString *> *)supportedSQLiteExtensions {
- return @[@"db", @"sqlite", @"sqlite3"];
- }
- + (NSArray<NSString *> *)supportedRealmExtensions {
- if (NSClassFromString(@"RLMRealm") == nil) {
- return nil;
- }
-
- return @[@"realm"];
- }
- @end
|