aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPTablesList.m
diff options
context:
space:
mode:
authorsqlprodev <sqlprodev@northofthree.com>2011-04-11 20:29:49 +0000
committersqlprodev <sqlprodev@northofthree.com>2011-04-11 20:29:49 +0000
commit0ce916eb0583ec6b062786f91bee0133906671ce (patch)
tree68a5767ded966f4eadf53ba9ce85bcaf6d31b951 /Source/SPTablesList.m
parent2621e79c75a0119a54c669b263caa82183bd19c8 (diff)
downloadsequelpro-0ce916eb0583ec6b062786f91bee0133906671ce.tar.gz
sequelpro-0ce916eb0583ec6b062786f91bee0133906671ce.tar.bz2
sequelpro-0ce916eb0583ec6b062786f91bee0133906671ce.zip
initial commit of SP_REFACTOR ifdefs, the start of a long quest to separate UI code from functional code
Diffstat (limited to 'Source/SPTablesList.m')
-rw-r--r--Source/SPTablesList.m139
1 files changed, 135 insertions, 4 deletions
diff --git a/Source/SPTablesList.m b/Source/SPTablesList.m
index fa358b23..c7f3bc1f 100644
--- a/Source/SPTablesList.m
+++ b/Source/SPTablesList.m
@@ -24,11 +24,18 @@
// More info at <http://code.google.com/p/sequel-pro/>
#import "SPTablesList.h"
+#ifndef SP_REFACTOR /* headers */
+#import "SPConnectionController.h"
+#endif
#import "SPDatabaseDocument.h"
-#import "SPDatabaseViewController.h"
#import "SPTableStructure.h"
+#import "SPDatabaseViewController.h"
+
+#ifndef SP_REFACTOR /* headers */
#import "SPTableContent.h"
+#endif
#import "SPTableData.h"
+#ifndef SP_REFACTOR /* headers */
#import "SPTableInfo.h"
#import "SPDataImport.h"
#import "SPTableView.h"
@@ -51,6 +58,7 @@
- (void)renameTableOfType:(SPTableType)tableType from:(NSString *)oldTableName to:(NSString *)newTableName;
@end
+#endif
@implementation SPTablesList
@@ -67,10 +75,13 @@
NSUInteger i;
NSString *previousSelectedTable = nil;
NSString *previousFilterString = nil;
+#ifndef SP_REFACTOR /* table list filtering */
BOOL previousTableListIsSelectable = tableListIsSelectable;
+#endif
BOOL changeEncoding = ![[mySQLConnection encoding] isEqualToString:@"utf8"];
if (selectedTableName) previousSelectedTable = [[NSString alloc] initWithString:selectedTableName];
+#ifndef SP_REFACTOR /* table list filtering */
if (isTableListFiltered) {
previousFilterString = [[NSString alloc] initWithString:[listFilterField stringValue]];
if (filteredTables) [filteredTables release];
@@ -85,6 +96,7 @@
tableListIsSelectable = YES;
[[tablesListView onMainThread] deselectAll:self];
tableListIsSelectable = previousTableListIsSelectable;
+#endif
[tables removeAllObjects];
[tableTypes removeAllObjects];
@@ -132,6 +144,7 @@
// Reorder the tables in alphabetical order
[tables sortArrayUsingSelector:@selector(localizedCompare:) withPairedMutableArrays:tableTypes, nil];
+#ifndef SP_REFACTOR /* table procedures and functions */
/* Grab the procedures and functions
*
* Using information_schema gives us more info (for information window perhaps?) but breaks
@@ -171,7 +184,7 @@
}
}
}
- /*
+#endif /*
BOOL addedPFHeader = FALSE;
NSString *pQuery = [NSString stringWithFormat:@"SHOW PROCEDURE STATUS WHERE db = '%@'",[tableDocumentInstance database]];
theResult = [mySQLConnection queryString:pQuery];
@@ -240,7 +253,9 @@
[tableTypes insertObject:[NSNumber numberWithInteger:SPTableTypeNone] atIndex:0];
+#ifndef SP_REFACTOR /* ui manipulation */
[[tablesListView onMainThread] reloadData];
+#endif
// if the previous selected table still exists, select it
// but not if the update was called from SPTableData since it calls that method
@@ -248,9 +263,11 @@
// or if the table name contains characters which are not supported by the current set encoding
if ( ![sender isKindOfClass:[SPTableData class]] && previousSelectedTable != nil && [tables indexOfObject:previousSelectedTable] < [tables count]) {
NSInteger itemToReselect = [tables indexOfObject:previousSelectedTable];
+#ifndef SP_REFACTOR /* ui manipulation */
tableListIsSelectable = YES;
[[tablesListView onMainThread] selectRowIndexes:[NSIndexSet indexSetWithIndex:itemToReselect] byExtendingSelection:NO];
tableListIsSelectable = previousTableListIsSelectable;
+#endif
if (selectedTableName) [selectedTableName release];
selectedTableName = [[NSString alloc] initWithString:[tables objectAtIndex:itemToReselect]];
selectedTableType = (SPTableType)[[tableTypes objectAtIndex:itemToReselect] integerValue];
@@ -261,6 +278,7 @@
selectedTableType = SPTableTypeNone;
}
+#ifndef SP_REFACTOR /* table list filtering */
// Determine whether or not to preserve the existing filter, and whether to
// show or hide the list filter based on the number of tables
if ([tables count] > 20) {
@@ -277,6 +295,7 @@
if ([tableDocumentInstance database]) {
[[[listFilterField cell] onMainThread] setPlaceholderString:NSLocalizedString(@"Filter", @"filter label")];
}
+#endif
if (previousSelectedTable) [previousSelectedTable release];
if (previousFilterString) [previousFilterString release];
@@ -289,6 +308,7 @@
// User press refresh button ergo force update
[NSThread detachNewThreadSelector:@selector(queryDbStructureWithUserInfo:) toTarget:mySQLConnection withObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"forceUpdate", [NSNumber numberWithBool:YES], @"cancelQuerying", nil]];
}
+#ifndef SP_REFACTOR /* whole table operations */
/**
* Adds a new table to the tables-array (no changes in mysql-db)
@@ -658,6 +678,8 @@
#pragma mark -
#pragma mark Additional methods
+#endif
+
/**
* Sets the connection (received from SPDatabaseDocument) and makes things that have to be done only once
*/
@@ -667,6 +689,8 @@
[self updateTables:self];
}
+#ifndef SP_REFACTOR /* ui validation */
+
/**
* Performs interface validation for various controls.
*/
@@ -702,6 +726,7 @@
[copyTableButton performClick:object];
}
}
+#endif
/**
* Updates application state to match the current selection, including
@@ -715,17 +740,23 @@
{
// First handle empty or multiple selections
if (!selectionDetails || ![selectionDetails objectForKey:@"name"]) {
+#ifndef SP_REFACTOR /* ui manipulation */
NSIndexSet *indexes = [tablesListView selectedRowIndexes];
-
+#endif
// Update the selected table name and type
if (selectedTableName) [selectedTableName release];
+#ifndef SP_REFACTOR /* ui manipulation */
if ([indexes count]) {
selectedTableName = [[NSString alloc] initWithString:@""];
}
else {
+#endif
selectedTableName = nil;
+#ifndef SP_REFACTOR /* ui manipulation */
}
+#endif
+#ifndef SP_REFACTOR /* ui manipulation */
// Set gear menu items Remove/Duplicate table/view according to the table types
// if at least one item is selected
@@ -826,6 +857,7 @@
[[tableSubMenu itemAtIndex:10] setHidden:NO];
return;
+#endif
}
// If a new selection has been provided, store variables and update the interface to match
@@ -837,6 +869,7 @@
selectedTableName = [[NSString alloc] initWithString:selectedItemName];
selectedTableType = selectedItemType;
+#ifndef SP_REFACTOR /* ui manipulation */
// Remove the "current selection" item for filtered lists if appropriate
if (isTableListFiltered && [tablesListView selectedRow] < (NSInteger)[filteredTables count] - 2 && [filteredTables count] > 2
&& [[filteredTableTypes objectAtIndex:[filteredTableTypes count]-2] integerValue] == SPTableTypeNone
@@ -1007,8 +1040,11 @@
[showCreateSyntaxContextMenuItem setHidden:NO];
[showCreateSyntaxContextMenuItem setTitle:NSLocalizedString(@"Show Create Function Syntax...", @"show create func syntax menu item")];
}
+#endif
}
+#ifndef SP_REFACTOR /* getters */
+
#pragma mark -
#pragma mark Getter methods
@@ -1055,6 +1091,7 @@
}
return selTables;
}
+#endif
/**
* Returns the currently selected table or nil if no table or mulitple tables are selected
@@ -1203,6 +1240,7 @@
// If no match found, return failure
if (itemIndex == NSNotFound) return NO;
+#ifndef SP_REFACTOR /* table list filtering */
if (!isTableListFiltered) {
[tablesListView selectRowIndexes:[NSIndexSet indexSetWithIndex:itemIndex] byExtendingSelection:NO];
} else {
@@ -1211,19 +1249,26 @@
[tablesListView selectRowIndexes:[NSIndexSet indexSetWithIndex:filteredIndex] byExtendingSelection:NO];
} else {
[tablesListView deselectAll:nil];
+#endif
if (selectedTableName) [selectedTableName release];
selectedTableName = [[NSString alloc] initWithString:[tables objectAtIndex:itemIndex]];
- selectedTableType = (SPTableType)[[tableTypes objectAtIndex:itemIndex] integerValue];
+ selectedTableType = [[tableTypes objectAtIndex:itemIndex] integerValue];
+#ifndef SP_REFACTOR /* table list filtering */
[self updateFilter:self];
+#endif
[tableDocumentInstance loadTable:selectedTableName ofType:selectedTableType];
+#ifndef SP_REFACTOR /* table list filtering */
}
}
[[tablesListView onMainThread] scrollRowToVisible:[tablesListView selectedRow]];
+#endif
return YES;
}
+#ifndef SP_REFACTOR /* tableView datasource/delegate */
+
/**
* Try to select items using the provided names in theNames; returns YES if at least
* one item could be seleceted, otherwise NO.
@@ -1361,6 +1406,9 @@
return ![tableDocumentInstance isWorking];
}
+
+#ifndef SP_REFACTOR
+
/**
* Renames a table (in tables-array and mysql-db).
*/
@@ -1420,6 +1468,7 @@
// Query the structure of all databases in the background (mainly for completion)
[NSThread detachNewThreadSelector:@selector(queryDbStructureWithUserInfo:) toTarget:mySQLConnection withObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], @"forceUpdate", nil]];
}
+#endif
#pragma mark -
#pragma mark TableView delegate methods
@@ -1849,6 +1898,7 @@
{
return (splitView == tableListSplitView ? NSZeroRect : proposedEffectiveRect);
}
+#endif
#pragma mark -
@@ -1861,21 +1911,28 @@
{
if ((self = [super init])) {
tables = [[NSMutableArray alloc] init];
+#ifndef SP_REFACTOR
filteredTables = tables;
+#endif
tableTypes = [[NSMutableArray alloc] init];
+#ifndef SP_REFACTOR
filteredTableTypes = tableTypes;
isTableListFiltered = NO;
tableListIsSelectable = YES;
+#endif
tableListContainsViews = NO;
selectedTableType = SPTableTypeNone;
selectedTableName = nil;
[tables addObject:NSLocalizedString(@"TABLES",@"header for table list")];
+#ifndef SP_REFACTOR /* font */
smallSystemFont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
+#endif
}
return self;
}
+#ifndef SP_REFACTOR /* awakeFromNib */
/**
* Standard awakeFromNib method for interface loading.
*/
@@ -1922,6 +1979,7 @@
[tablesListView registerForDraggedTypes:[NSArray arrayWithObjects:@"SPDragTableDataFromNavigatorPboardType", nil]];
}
+#endif
/**
* Standard dealloc method.
@@ -1932,13 +1990,24 @@
[tables release];
[tableTypes release];
+#ifndef SP_REFACTOR
if (isTableListFiltered && filteredTables) [filteredTables release];
if (isTableListFiltered && filteredTableTypes) [filteredTableTypes release];
+#endif
if (selectedTableName) [selectedTableName release];
[super dealloc];
}
+
+#ifdef SP_REFACTOR /* glue */
+- (void)setDatabaseDocument:(SPDatabaseDocument*)val
+{
+ tableDocumentInstance = val;
+}
+#endif
+
+#ifndef SP_REFACTOR /* operations performed on whole tables */
@end
@implementation SPTablesList (PrivateAPI)
@@ -2445,5 +2514,67 @@
[NSException raise:@"Object of unknown type" format:NSLocalizedString(@"An error occured while renaming. '%@' is of an unknown type.", @"rename error - don't know what type the renamed thing is"), oldTableName];
}
+#endif
+
+/**
+ * Check tableName for length and if the tableName doesn't match
+ * against current database table/view names (case-insensitive).
+ */
+- (BOOL)isTableNameValid:(NSString *)tableName forType:(SPTableType)tableType
+{
+ return [self isTableNameValid:tableName forType:tableType ignoringSelectedTable:NO];
+}
+
+/**
+ * Check tableName for length and if the tableName doesn't match
+ * against current database table/view names (case-insensitive).
+ */
+- (BOOL)isTableNameValid:(NSString *)tableName forType:(SPTableType)tableType ignoringSelectedTable:(BOOL)ignoreSelectedTable
+{
+ BOOL isValid = YES;
+
+ // delete trailing whitespaces since 'foo ' or ' ' are not valid table names
+ NSString *fieldStr = [tableName stringByMatching:@"(.*?)\\s*$" capture:1];
+ NSString *lowercaseFieldStr = [fieldStr lowercaseString];
+
+ // If table name has trailing whitespaces return 'no valid'
+ if([fieldStr length] != [tableName length]) return NO;
+
+ // empty table names are invalid
+ if([fieldStr length] == 0) return NO;
+
+
+ NSArray *similarTables;
+ switch (tableType) {
+ case SPTableTypeView:
+ case SPTableTypeTable:
+ similarTables = [self allTableAndViewNames];
+ break;
+ case SPTableTypeProc:
+ similarTables = [self allProcedureNames];
+ break;
+ case SPTableTypeFunc:
+ similarTables = [self allFunctionNames];
+ break;
+ default:
+ // if some other table type is given, just return yes
+ // better a mysql error than not being able to change something at all
+ return YES;
+ }
+
+ for(id table in similarTables) {
+ //compare case insensitive here
+ if([lowercaseFieldStr isEqualToString:[table lowercaseString]]) {
+ if (ignoreSelectedTable) {
+ // if table is the selectedTable, ignore it
+ // we must compare CASE SENSITIVE here!
+ if ([table isEqualToString:selectedTableName]) continue;
+ }
+ isValid = NO;
+ break;
+ }
+ }
+ return isValid;
+}
@end