aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
Diffstat (limited to 'Source')
-rw-r--r--Source/SPAppController.m7
-rw-r--r--Source/SPCopyTable.m37
-rw-r--r--Source/SPDatabaseDocument.h1
-rw-r--r--Source/SPDatabaseDocument.m52
-rw-r--r--Source/SPStringAdditions.m64
-rw-r--r--Source/SPTextView.m29
-rw-r--r--Source/SPTextViewAdditions.m35
7 files changed, 123 insertions, 102 deletions
diff --git a/Source/SPAppController.m b/Source/SPAppController.m
index eb013e48..c3698db0 100644
--- a/Source/SPAppController.m
+++ b/Source/SPAppController.m
@@ -905,7 +905,7 @@
[bundleKeyEquivalents setObject:[NSMutableDictionary dictionary] forKey:scope];
}
- if([cmdData objectForKey:SPBundleFileCategoryKey] && ![[bundleCategories objectForKey:scope] containsObject:[cmdData objectForKey:SPBundleFileCategoryKey]])
+ if([cmdData objectForKey:SPBundleFileCategoryKey] && [[cmdData objectForKey:SPBundleFileCategoryKey] length] && ![[bundleCategories objectForKey:scope] containsObject:[cmdData objectForKey:SPBundleFileCategoryKey]])
[[bundleCategories objectForKey:scope] addObject:[cmdData objectForKey:SPBundleFileCategoryKey]];
}
@@ -983,7 +983,10 @@
NSArray *bundleCategories = [[NSApp delegate] bundleCategoriesForScope:scope];
NSArray *bundleItems = [[NSApp delegate] bundleItemsForScope:scope];
- if(![bundleItems count]) continue;
+ if(![bundleItems count]) {
+ k++;
+ continue;
+ }
NSMenu *bundleMenu = [[[NSMenu alloc] init] autorelease];
NSMenuItem *bundleSubMenuItem = [[NSMenuItem alloc] initWithTitle:[scopeTitles objectAtIndex:k] action:nil keyEquivalent:@""];
diff --git a/Source/SPCopyTable.m b/Source/SPCopyTable.m
index 84c43640..5574ff33 100644
--- a/Source/SPCopyTable.m
+++ b/Source/SPCopyTable.m
@@ -748,39 +748,8 @@ NSInteger MENU_EDIT_COPY_AS_SQL = 2003;
[env setObject:[infoPath stringByDeletingLastPathComponent] forKey:@"SP_BUNDLE_PATH"];
[env setObject:SPBundleTaskInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"];
- id tablesListInstance = [[self delegate] valueForKeyPath:@"tablesListInstance"];
- id tableDocumentInstance = [[self delegate] valueForKeyPath:@"tableDocumentInstance"];
-
- if (tablesListInstance) {
- if([tablesListInstance selectedDatabase])
- [env setObject:[tablesListInstance selectedDatabase] forKey:@"SP_SELECTED_DATABASE"];
-
- if ([tablesListInstance allDatabaseNames])
- [env setObject:[[tablesListInstance allDatabaseNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_DATABASES"];
-
- if ([tablesListInstance allTableNames])
- [env setObject:[[tablesListInstance allTableNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_TABLES"];
-
- if ([tablesListInstance allViewNames])
- [env setObject:[[tablesListInstance allViewNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_VIEWS"];
-
- if ([tablesListInstance allFunctionNames])
- [env setObject:[[tablesListInstance allFunctionNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_FUNCTIONS"];
-
- if ([tablesListInstance allProcedureNames])
- [env setObject:[[tablesListInstance allProcedureNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_PROCEDURES"];
-
- if ([tablesListInstance tableName])
- [env setObject:[tablesListInstance tableName] forKey:@"SP_SELECTED_TABLE"];
-
- }
-
- if(tableDocumentInstance && [tableDocumentInstance mySQLVersion])
- [env setObject:[tableDocumentInstance mySQLVersion] forKey:@"SP_RDBMS_VERSION"];
-
- if(1)
- [env setObject:@"mysql" forKey:@"SP_RDBMS_TYPE"];
-
+ if([[self delegate] respondsToSelector:@selector(usedQuery)] && [[self delegate] usedQuery])
+ [env setObject:[[self delegate] usedQuery] forKey:@"SP_USED_QUERY_FOR_TABLE"];
// NSError *inputFileError = nil;
// NSString *input = [NSString stringWithString:[[self string] substringWithRange:replaceRange]];
@@ -801,7 +770,7 @@ NSInteger MENU_EDIT_COPY_AS_SQL = 2003;
[[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil];
- if(err == nil && [cmdData objectForKey:SPBundleFileOutputActionKey]) {
+ if(err == nil && output && [cmdData objectForKey:SPBundleFileOutputActionKey]) {
if([[cmdData objectForKey:SPBundleFileOutputActionKey] length]
&& ![[cmdData objectForKey:SPBundleFileOutputActionKey] isEqualToString:SPBundleOutputActionNone]) {
NSString *action = [[cmdData objectForKey:SPBundleFileOutputActionKey] lowercaseString];
diff --git a/Source/SPDatabaseDocument.h b/Source/SPDatabaseDocument.h
index aeeaa302..9563fd58 100644
--- a/Source/SPDatabaseDocument.h
+++ b/Source/SPDatabaseDocument.h
@@ -355,5 +355,6 @@
// Scripting
- (void)handleSchemeCommand:(NSDictionary*)commandDict;
+- (NSDictionary*)shellVariables;
@end
diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m
index 837c5387..4ef38ef8 100644
--- a/Source/SPDatabaseDocument.m
+++ b/Source/SPDatabaseDocument.m
@@ -4548,6 +4548,58 @@
NSLog(@"received: %@", commandDict);
}
+- (NSDictionary*)shellVariables
+{
+ NSMutableDictionary *env = [NSMutableDictionary dictionary];
+
+ if (tablesListInstance) {
+ if([tablesListInstance selectedDatabase])
+ [env setObject:[tablesListInstance selectedDatabase] forKey:@"SP_SELECTED_DATABASE"];
+
+ if ([tablesListInstance tableName])
+ [env setObject:[tablesListInstance tableName] forKey:@"SP_SELECTED_TABLE"];
+
+ if ([tablesListInstance selectedTableItems])
+ [env setObject:[[tablesListInstance selectedTableItems] componentsJoinedByString:@"\t"] forKey:@"SP_SELECTED_TABLES"];
+
+ if ([tablesListInstance allDatabaseNames])
+ [env setObject:[[tablesListInstance allDatabaseNames] componentsJoinedByString:@"\t"] forKey:@"SP_ALL_DATABASES"];
+
+ if ([tablesListInstance allTableNames])
+ [env setObject:[[tablesListInstance allTableNames] componentsJoinedByString:@"\t"] forKey:@"SP_ALL_TABLES"];
+
+ if ([tablesListInstance allViewNames])
+ [env setObject:[[tablesListInstance allViewNames] componentsJoinedByString:@"\t"] forKey:@"SP_ALL_VIEWS"];
+
+ if ([tablesListInstance allFunctionNames])
+ [env setObject:[[tablesListInstance allFunctionNames] componentsJoinedByString:@"\t"] forKey:@"SP_ALL_FUNCTIONS"];
+
+ if ([tablesListInstance allProcedureNames])
+ [env setObject:[[tablesListInstance allProcedureNames] componentsJoinedByString:@"\t"] forKey:@"SP_ALL_PROCEDURES"];
+
+ if ([self user])
+ [env setObject:[self user] forKey:@"SP_CURRENT_USER"];
+
+ if ([self host])
+ [env setObject:[self host] forKey:@"SP_CURRENT_HOST"];
+
+ if ([self port])
+ [env setObject:[self port] forKey:@"SP_CURRENT_PORT"];
+
+ if ([self databaseEncoding])
+ [env setObject:[self databaseEncoding] forKey:@"SP_DATABASE_ENCODING"];
+
+ }
+
+ if(1)
+ [env setObject:@"mysql" forKey:@"SP_RDBMS_TYPE"];
+
+ if([self mySQLVersion])
+ [env setObject:[self mySQLVersion] forKey:@"SP_RDBMS_VERSION"];
+
+ return (NSDictionary*)env;
+}
+
#pragma mark -
#pragma mark Text field delegate methods
diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m
index 7909bd70..7f93659e 100644
--- a/Source/SPStringAdditions.m
+++ b/Source/SPStringAdditions.m
@@ -414,10 +414,50 @@
*/
- (NSString *)runBashCommandWithEnvironment:(NSDictionary*)shellEnvironment atCurrentDirectoryPath:(NSString*)path error:(NSError**)theError
{
+
BOOL userTerminated = NO;
+ BOOL redirectForScript = NO;
+ NSMutableArray *scriptHeaderArguments = [NSMutableArray array];
+ NSString *scriptPath = @"";
+ NSString *scriptContentPath = @"/tmp/SP_SCRIPT_COMMAND";
+
+ [[NSFileManager defaultManager] removeItemAtPath:scriptContentPath error:nil];
+
+ if([self length] > 3 && [self hasPrefix:@"#!"] && [shellEnvironment objectForKey:@"SP_BUNDLE_PATH"]) {
+
+ NSRange firstLineRange = NSMakeRange(2, [self rangeOfString:@"\n"].location - 2);
+
+ [scriptHeaderArguments setArray:[[self substringWithRange:firstLineRange] componentsSeparatedByString:@" "]];
+
+ while([scriptHeaderArguments containsObject:@""])
+ [scriptHeaderArguments removeObject:@""];
+
+ if([scriptHeaderArguments count]) {
+ scriptPath = [scriptHeaderArguments objectAtIndex:0];
+ [scriptHeaderArguments removeObject:scriptPath];
+ }
+
+ BOOL isDir = NO;
+ if([scriptPath hasPrefix:@"/"] && [[NSFileManager defaultManager] fileExistsAtPath:scriptPath isDirectory:&isDir] && !isDir) {
+ NSString *script = [self substringWithRange:NSMakeRange(NSMaxRange(firstLineRange), [self length] - NSMaxRange(firstLineRange))];
+ NSError *writeError = nil;
+ [script writeToFile:scriptContentPath atomically:YES encoding:NSUTF8StringEncoding error:writeError];
+ if(writeError == nil) {
+ redirectForScript = YES;
+ [scriptHeaderArguments insertObject:scriptContentPath atIndex:0];
+ } else {
+ NSBeep();
+ NSLog(@"Couldn't write script file.");
+ }
+
+ }
+ }
NSTask *bashTask = [[NSTask alloc] init];
- [bashTask setLaunchPath: @"/bin/bash"];
+ if(redirectForScript)
+ [bashTask setLaunchPath:scriptPath];
+ else
+ [bashTask setLaunchPath:@"/bin/bash"];
NSMutableDictionary *theEnv = [NSMutableDictionary dictionary];
[theEnv setDictionary:shellEnvironment];
@@ -427,14 +467,27 @@
// Furthermore this id is used to communicate with the called command as file name.
NSString *processID = [NSString stringWithNewUUID];
[theEnv setObject:processID forKey:@"SP_PROCESS_ID"];
- [[[[NSApp mainWindow] delegate] selectedTableDocument] setProcessID:processID];
- if(shellEnvironment != nil && [shellEnvironment isKindOfClass:[NSDictionary class]] && [shellEnvironment count])
+ id doc = [[[NSApp mainWindow] delegate] selectedTableDocument];
+ if(!doc) {
+ NSBeep();
+ NSLog(@"No active document found for bash command.");
+ return;
+ }
+ [doc setProcessID:processID];
+
+ if([doc shellVariables])
+ [theEnv addEntriesFromDictionary:[doc shellVariables]];
+
+ if(theEnv != nil && [theEnv count])
[bashTask setEnvironment:theEnv];
if(path != nil)
[bashTask setCurrentDirectoryPath:path];
- [bashTask setArguments:[NSArray arrayWithObjects: @"-c", self, nil]];
+ if(redirectForScript)
+ [bashTask setArguments:scriptHeaderArguments];
+ else
+ [bashTask setArguments:[NSArray arrayWithObjects:@"-c", self, nil]];
NSPipe *stdout_pipe = [NSPipe pipe];
[bashTask setStandardOutput:stdout_pipe];
@@ -476,6 +529,9 @@
return @"";
}
+ if(redirectForScript)
+ [[NSFileManager defaultManager] removeItemAtPath:scriptContentPath error:nil];
+
// If return from bash re-activate Sequel Pro
[NSApp activateIgnoringOtherApps:YES];
diff --git a/Source/SPTextView.m b/Source/SPTextView.m
index f83b2549..0af0b2ff 100644
--- a/Source/SPTextView.m
+++ b/Source/SPTextView.m
@@ -3499,33 +3499,6 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
if(currentSelectionRange.length)
[env setObject:[[self string] substringWithRange:currentSelectionRange] forKey:@"SP_SELECTED_TEXT"];
- if (tablesListInstance && [tablesListInstance selectedDatabase])
- [env setObject:[tablesListInstance selectedDatabase] forKey:@"SP_SELECTED_DATABASE"];
-
- if (tablesListInstance && [tablesListInstance allDatabaseNames])
- [env setObject:[[tablesListInstance allDatabaseNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_DATABASES"];
-
- if (tablesListInstance && [tablesListInstance allTableNames])
- [env setObject:[[tablesListInstance allTableNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_TABLES"];
-
- if (tablesListInstance && [tablesListInstance allViewNames])
- [env setObject:[[tablesListInstance allViewNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_VIEWS"];
-
- if (tablesListInstance && [tablesListInstance allFunctionNames])
- [env setObject:[[tablesListInstance allFunctionNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_FUNCTIONS"];
-
- if (tablesListInstance && [tablesListInstance allProcedureNames])
- [env setObject:[[tablesListInstance allProcedureNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_PROCEDURES"];
-
- if(tableDocumentInstance && [tableDocumentInstance mySQLVersion])
- [env setObject:[tableDocumentInstance mySQLVersion] forKey:@"SP_RDBMS_VERSION"];
-
- if(1)
- [env setObject:@"mysql" forKey:@"SP_RDBMS_TYPE"];
-
- if (tablesListInstance && [tablesListInstance tableName])
- [env setObject:[tablesListInstance tableName] forKey:@"SP_SELECTED_TABLE"];
-
if(customQueryInstance && [customQueryInstance currentQueryRange].length)
[env setObject:[[self string] substringWithRange:[customQueryInstance currentQueryRange]] forKey:@"SP_CURRENT_QUERY"];
@@ -3554,7 +3527,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse)
[[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil];
- if(err == nil && [cmdData objectForKey:SPBundleFileOutputActionKey]) {
+ if(err == nil && output && [cmdData objectForKey:SPBundleFileOutputActionKey]) {
if([[cmdData objectForKey:SPBundleFileOutputActionKey] length]
&& ![[cmdData objectForKey:SPBundleFileOutputActionKey] isEqualToString:SPBundleOutputActionNone]) {
NSString *action = [[cmdData objectForKey:SPBundleFileOutputActionKey] lowercaseString];
diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m
index 3084138d..fdf2318c 100644
--- a/Source/SPTextViewAdditions.m
+++ b/Source/SPTextViewAdditions.m
@@ -564,39 +564,6 @@
if(currentSelectionRange.length)
[env setObject:[[self string] substringWithRange:currentSelectionRange] forKey:@"SP_SELECTED_TEXT"];
- if(![[[[[[NSApp delegate] frontDocumentWindow] delegate] selectedTableDocument] connectionID] isEqualToString:@"_"]) {
-
- id tablesListInstance = [[[[[NSApp delegate] frontDocumentWindow] delegate] selectedTableDocument] valueForKeyPath:@"tablesListInstance"];
-
- if (tablesListInstance && [tablesListInstance selectedDatabase])
- [env setObject:[tablesListInstance selectedDatabase] forKey:@"SP_SELECTED_DATABASE"];
-
- if (tablesListInstance && [tablesListInstance allDatabaseNames])
- [env setObject:[[tablesListInstance allDatabaseNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_DATABASES"];
-
- if (tablesListInstance && [tablesListInstance allTableNames])
- [env setObject:[[tablesListInstance allTableNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_TABLES"];
-
- if (tablesListInstance && [tablesListInstance allViewNames])
- [env setObject:[[tablesListInstance allViewNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_VIEWS"];
-
- if (tablesListInstance && [tablesListInstance allFunctionNames])
- [env setObject:[[tablesListInstance allFunctionNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_FUNCTIONS"];
-
- if (tablesListInstance && [tablesListInstance allProcedureNames])
- [env setObject:[[tablesListInstance allProcedureNames] componentsJoinedBySpacesAndQuoted] forKey:@"SP_ALL_PROCEDURES"];
-
- if (tablesListInstance && [tablesListInstance tableName])
- [env setObject:[tablesListInstance tableName] forKey:@"SP_SELECTED_TABLE"];
-
- if([[[[NSApp delegate] frontDocumentWindow] delegate] selectedTableDocument] && [[[[[NSApp delegate] frontDocumentWindow] delegate] selectedTableDocument] mySQLVersion])
- [env setObject:[[[[[NSApp delegate] frontDocumentWindow] delegate] selectedTableDocument] mySQLVersion] forKey:@"SP_RDBMS_VERSION"];
-
- }
-
- if(1)
- [env setObject:@"mysql" forKey:@"SP_RDBMS_TYPE"];
-
if(currentWordRange.length)
[env setObject:[[self string] substringWithRange:currentWordRange] forKey:@"SP_CURRENT_WORD"];
@@ -622,7 +589,7 @@
[[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil];
- if(err == nil && [cmdData objectForKey:SPBundleFileOutputActionKey]) {
+ if(err == nil && output && [cmdData objectForKey:SPBundleFileOutputActionKey]) {
if([[cmdData objectForKey:SPBundleFileOutputActionKey] length]
&& ![[cmdData objectForKey:SPBundleFileOutputActionKey] isEqualToString:SPBundleOutputActionNone]) {
NSString *action = [[cmdData objectForKey:SPBundleFileOutputActionKey] lowercaseString];