diff options
-rw-r--r-- | Source/SPDBActionCommons.h | 16 | ||||
-rw-r--r-- | Source/SPDBActionCommons.m | 1 | ||||
-rw-r--r-- | Source/SPDatabaseCopy.h | 19 | ||||
-rw-r--r-- | Source/SPDatabaseCopy.m | 39 | ||||
-rw-r--r-- | Source/SPDatabaseDocument.m | 1 | ||||
-rw-r--r-- | Source/SPDatabaseInfo.h | 55 | ||||
-rw-r--r-- | Source/SPDatabaseInfo.m | 48 | ||||
-rw-r--r-- | Source/SPDatabaseRename.h | 38 | ||||
-rw-r--r-- | Source/SPDatabaseRename.m | 123 | ||||
-rw-r--r-- | Source/SPTableCopy.h | 6 | ||||
-rw-r--r-- | Source/SPTableCopy.m | 40 | ||||
-rw-r--r-- | Source/SPTablesList.m | 2 | ||||
-rw-r--r-- | Source/SPViewCopy.h | 37 | ||||
-rw-r--r-- | Source/SPViewCopy.m | 90 | ||||
-rw-r--r-- | UnitTests/SPDatabaseCopyTest.h | 8 | ||||
-rw-r--r-- | UnitTests/SPDatabaseCopyTest.m | 2 | ||||
-rw-r--r-- | UnitTests/SPDatabaseInfoTest.h | 36 | ||||
-rw-r--r-- | UnitTests/SPDatabaseInfoTest.m | 92 | ||||
-rw-r--r-- | UnitTests/SPDatabaseRenameTest.h | 4 | ||||
-rw-r--r-- | UnitTests/SPDatabaseRenameTest.m | 2 | ||||
-rw-r--r-- | UnitTests/SPTableCopyTest.h | 5 | ||||
-rw-r--r-- | sequel-pro.xcodeproj/project.pbxproj | 24 |
22 files changed, 263 insertions, 425 deletions
diff --git a/Source/SPDBActionCommons.h b/Source/SPDBActionCommons.h index 7ae7ec79..5b467e1d 100644 --- a/Source/SPDBActionCommons.h +++ b/Source/SPDBActionCommons.h @@ -22,22 +22,30 @@ // // More info at <http://code.google.com/p/sequel-pro/> -@class SPMySQLConnection; +#import <SPMySQL/SPMySQL.h> + +@class SPTablesList; @interface SPDBActionCommons : NSObject { - SPMySQLConnection *connection; NSWindow *messageWindow; + SPTablesList *tablesList; + SPMySQLConnection *connection; } /** * @property connection References the SPMySQL.framework MySQL connection; it has to be set. */ -@property (retain) SPMySQLConnection *connection; +@property (readwrite, assign) SPMySQLConnection *connection; /** * @property messageWindow The NSWindow instance to send message sheets to. */ -@property (assign) NSWindow *messageWindow; +@property (readwrite, assign) NSWindow *messageWindow; + +/** + * @property tablesList + */ +@property (readwrite, assign) SPTablesList *tablesList; @end diff --git a/Source/SPDBActionCommons.m b/Source/SPDBActionCommons.m index 969ee0ad..c12751cf 100644 --- a/Source/SPDBActionCommons.m +++ b/Source/SPDBActionCommons.m @@ -28,5 +28,6 @@ @synthesize connection; @synthesize messageWindow; +@synthesize tablesList; @end diff --git a/Source/SPDatabaseCopy.h b/Source/SPDatabaseCopy.h index 56a7e26a..41a22ff0 100644 --- a/Source/SPDatabaseCopy.h +++ b/Source/SPDatabaseCopy.h @@ -23,28 +23,11 @@ // More info at <http://code.google.com/p/sequel-pro/> #import "SPDBActionCommons.h" -#import "SPDatabaseInfo.h" /** * The SPDatabaseCopy class povides functionality to create a copy of a database. */ -@interface SPDatabaseCopy : SPDBActionCommons -{ - SPDatabaseInfo *dbInfo; -} - -/** - * @property SPDatabaseInfo an instance of the database info class - */ -@property (retain) SPDatabaseInfo *dbInfo; - -/** - * This method retrieves the dbInfo object if it exists; otherwise it is generated and the - * connection is passed to it. - * - * @result SPDatabaseInfo dbInfo object - */ -- (SPDatabaseInfo *)getDBInfoObject; +@interface SPDatabaseCopy : SPDBActionCommons /** * This method clones an existing database. diff --git a/Source/SPDatabaseCopy.m b/Source/SPDatabaseCopy.m index ea45c4af..130f5d99 100644 --- a/Source/SPDatabaseCopy.m +++ b/Source/SPDatabaseCopy.m @@ -25,36 +25,18 @@ #import "SPDBActionCommons.h" #import "SPDatabaseCopy.h" #import "SPTableCopy.h" + #import <SPMySQL/SPMySQL.h> @implementation SPDatabaseCopy -@synthesize dbInfo; - -- (SPDatabaseInfo *)getDBInfoObject -{ - if (dbInfo != nil) { - return dbInfo; - } - else { - dbInfo = [[SPDatabaseInfo alloc] init]; - - [dbInfo setConnection:[self connection]]; - [dbInfo setMessageWindow:messageWindow]; - } - - return dbInfo; -} - - (BOOL)copyDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName withContent:(BOOL)copyWithContent { NSArray *tables = nil; - - SPDatabaseInfo *databaseInfo = [self getDBInfoObject]; - - // Check, whether the source database exists and the target database doesn't. - BOOL sourceExists = [databaseInfo databaseExists:sourceDatabaseName]; - BOOL targetExists = [databaseInfo databaseExists:targetDatabaseName]; + + // Check whether the source database exists and the target database doesn't. + BOOL sourceExists = [[connection databases] containsObject:sourceDatabaseName]; + BOOL targetExists = [[connection databases] containsObject:targetDatabaseName]; if (sourceExists && !targetExists) { @@ -65,9 +47,8 @@ return NO; } - //abort here if database creation failed - if(![self createDatabase:targetDatabaseName]) - return NO; + // Abort if database creation failed + if (![self createDatabase:targetDatabaseName]) return NO; SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init]; @@ -91,10 +72,4 @@ return YES; } -- (void)dealloc -{ - [dbInfo release], dbInfo = nil; - [super dealloc]; -} - @end diff --git a/Source/SPDatabaseDocument.m b/Source/SPDatabaseDocument.m index 2f2d8f1b..75af2ed1 100644 --- a/Source/SPDatabaseDocument.m +++ b/Source/SPDatabaseDocument.m @@ -5773,6 +5773,7 @@ static NSString *SPRenameDatabaseAction = @"SPRenameDatabase"; SPDatabaseRename *dbActionRename = [[SPDatabaseRename alloc] init]; + [dbActionRename setTablesList:tablesListInstance]; [dbActionRename setConnection:[self getConnection]]; [dbActionRename setMessageWindow:parentWindow]; diff --git a/Source/SPDatabaseInfo.h b/Source/SPDatabaseInfo.h deleted file mode 100644 index 3ae0319a..00000000 --- a/Source/SPDatabaseInfo.h +++ /dev/null @@ -1,55 +0,0 @@ -// -// $Id$ -// -// SPDatabaseInfo.h -// sequel-pro -// -// Created by David Rekowski on Apr 13, 2010 -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// More info at <http://code.google.com/p/sequel-pro/> - -#import "SPDBActionCommons.h" - -/* - * The SPDatabaseInfo class provides means of retrieving a list of database names - */ -@interface SPDatabaseInfo : SPDBActionCommons - -/** - * This method checks, whether a database exists. - * - * @param databaseName the name of the database to check - * @result TRUE if it exists, otherwise FALSE - */ --(BOOL)databaseExists:(NSString *)databaseName; - -/** - * This method retrieves a list of all databases. - * - * @result NSArray databaseNames - */ -- (NSArray *)listDBs; - -/** - * This method retrieves a list of databases like the given string - * - * @param NSString dbsName name of the database substring to match - * @result NSArray databaseNames - */ -- (NSArray *)listDBsLike:(NSString *)dbsName; - -@end diff --git a/Source/SPDatabaseInfo.m b/Source/SPDatabaseInfo.m deleted file mode 100644 index ea43f403..00000000 --- a/Source/SPDatabaseInfo.m +++ /dev/null @@ -1,48 +0,0 @@ -// -// $Id$ -// -// SPDatbaseInfo.h -// sequel-pro -// -// Created by David Rekowski on Apr 13, 2010 -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// More info at <http://code.google.com/p/sequel-pro/> - -#import "SPDBActionCommons.h" -#import "SPDatabaseInfo.h" -#import <SPMySQL/SPMySQL.h> - -@implementation SPDatabaseInfo - --(BOOL)databaseExists:(NSString *)databaseName -{ - NSArray *names = [self listDBs]; - - return [names containsObject:databaseName]; -} - -- (NSArray *)listDBs -{ - return [connection databases]; -} - -- (NSArray *)listDBsLike:(NSString *)dbsName -{ - return [connection databasesLike:dbsName]; -} - -@end diff --git a/Source/SPDatabaseRename.h b/Source/SPDatabaseRename.h index e679f476..e097c741 100644 --- a/Source/SPDatabaseRename.h +++ b/Source/SPDatabaseRename.h @@ -24,28 +24,10 @@ #import "SPDBActionCommons.h" -@class SPDatabaseInfo; - /** * The SPDatabaseRename class povides functionality to rename a database. */ -@interface SPDatabaseRename : SPDBActionCommons -{ - SPDatabaseInfo *dbInfo; -} - -/** - * @property SPDatabaseInfo an instance of the database info class - */ -@property (retain) SPDatabaseInfo *dbInfo; - -/** - * This method retrieves the dbInfo object if it exists; otherwise it is generated and the - * connection is passed to it. - * - * @result SPDatabaseInfo dbInfo object - */ -- (SPDatabaseInfo *)getDBInfoObject; +@interface SPDatabaseRename : SPDBActionCommons /** * This method renames an existing database. @@ -54,22 +36,6 @@ * @param NSString targetDatabaseName the name of the target database * @result BOOL success */ -- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName; - -/** - * This method creates a new database. - * - * @param NSString newDatabaseName name of the new database to be created - * @return BOOL YES on success, otherwise NO - */ -- (BOOL)createDatabase:(NSString *)newDatabaseName; - -/** - * This method drops a database. - * - * @param NSString databaseName name of the database to drop - * @return BOOL YES on success, otherwise NO - */ -- (BOOL)dropDatabase:(NSString *)databaseName; +- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabase to:(NSString *)targetDatabase; @end diff --git a/Source/SPDatabaseRename.m b/Source/SPDatabaseRename.m index 61186678..85763c7f 100644 --- a/Source/SPDatabaseRename.m +++ b/Source/SPDatabaseRename.m @@ -25,95 +25,108 @@ #import "SPDBActionCommons.h" #import "SPDatabaseRename.h" #import "SPTableCopy.h" -#import "SPDatabaseInfo.h" +#import "SPViewCopy.h" +#import "SPTablesList.h" + #import <SPMySQL/SPMySQL.h> -@implementation SPDatabaseRename +@interface SPDatabaseRename () -@synthesize dbInfo; +- (BOOL)_createDatabase:(NSString *)database; +- (BOOL)_dropDatabase:(NSString *)database; -- (SPDatabaseInfo *)getDBInfoObject -{ - if (dbInfo != nil) { - return dbInfo; - } - else { - dbInfo = [[SPDatabaseInfo alloc] init]; - - [dbInfo setConnection:[self connection]]; - [dbInfo setMessageWindow:messageWindow]; - } - - return dbInfo; -} +- (void)_moveTables:(NSArray *)tables fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase; +- (void)_moveViews:(NSArray *)views fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase; -- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabaseName to:(NSString *)targetDatabaseName -{ - SPDatabaseInfo *databaseInfo = [self getDBInfoObject]; +@end + +@implementation SPDatabaseRename - // Check, whether the source database exists and the target database doesn't. - NSArray *tables = nil; +- (BOOL)renameDatabaseFrom:(NSString *)sourceDatabase to:(NSString *)targetDatabase +{ + NSArray *tables = nil; + NSArray *views = nil; - BOOL sourceExists = [databaseInfo databaseExists:sourceDatabaseName]; - BOOL targetExists = [databaseInfo databaseExists:targetDatabaseName]; + // Check, whether the source database exists and the target database doesn't + BOOL sourceExists = [[connection databases] containsObject:sourceDatabase]; + BOOL targetExists = [[connection databases] containsObject:targetDatabase]; if (sourceExists && !targetExists) { - - // Retrieve the list of tables/views/funcs/triggers from the source database - tables = [connection tablesFromDatabase:sourceDatabaseName]; + tables = [tablesList allTableNames]; + views = [tablesList allViewNames]; } else { return NO; } - BOOL success = [self createDatabase:targetDatabaseName]; - - SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init]; - - [dbActionTableCopy setConnection:connection]; + BOOL success = [self _createDatabase:targetDatabase]; - for (NSString *currentTable in tables) - { - success = [dbActionTableCopy moveTable:currentTable from:sourceDatabaseName to:targetDatabaseName]; - } + [self _moveTables:tables fromDatabase:sourceDatabase toDatabase:targetDatabase]; - [dbActionTableCopy release]; - - tables = [connection tablesFromDatabase:sourceDatabaseName]; + tables = [connection tablesFromDatabase:sourceDatabase]; if ([tables count] == 0) { - [self dropDatabase:sourceDatabaseName]; + [self _dropDatabase:sourceDatabase]; } return success; } -- (BOOL)createDatabase:(NSString *)newDatabaseName -{ - NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE %@", [newDatabaseName backtickQuotedString]]; - - [connection queryString:createStatement]; +#pragma mark - +#pragma mark Private API + +/** + * This method creates a new database. + * + * @param NSString newDatabaseName name of the new database to be created + * @return BOOL YES on success, otherwise NO + */ +- (BOOL)_createDatabase:(NSString *)database +{ + [connection queryString:[NSString stringWithFormat:@"CREATE DATABASE %@", [database backtickQuotedString]]]; - if ([connection queryErrored]) return NO; + return ![connection queryErrored]; +} + +/** + * This method drops a database. + * + * @param NSString databaseName name of the database to drop + * @return BOOL YES on success, otherwise NO + */ +- (BOOL)_dropDatabase:(NSString *)database +{ + [connection queryString:[NSString stringWithFormat:@"DROP DATABASE %@", [database backtickQuotedString]]]; - return YES; + return ![connection queryErrored]; } -- (BOOL)dropDatabase:(NSString *)databaseName +- (void)_moveTables:(NSArray *)tables fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase { - NSString *dropStatement = [NSString stringWithFormat:@"DROP DATABASE %@", [databaseName backtickQuotedString]]; + SPTableCopy *dbActionTableCopy = [[SPTableCopy alloc] init]; - [connection queryString:dropStatement]; + [dbActionTableCopy setConnection:connection]; - if ([connection queryErrored]) return NO; + for (NSString *table in tables) + { + [dbActionTableCopy moveTable:table from:sourceDatabase to:targetDatabase]; + } - return YES; + [dbActionTableCopy release]; } -- (void)dealloc +- (void)_moveViews:(NSArray *)views fromDatabase:(NSString *)sourceDatabase toDatabase:(NSString *)targetDatabase { - [dbInfo release], dbInfo = nil; - [super dealloc]; + SPViewCopy *dbActionViewCopy = [[SPViewCopy alloc] init]; + + [dbActionViewCopy setConnection:connection]; + + for (NSString *view in views) + { + [dbActionViewCopy moveView:view from:sourceDatabase to:targetDatabase]; + } + + [dbActionViewCopy release]; } @end diff --git a/Source/SPTableCopy.h b/Source/SPTableCopy.h index 56e1da8c..e32560aa 100644 --- a/Source/SPTableCopy.h +++ b/Source/SPTableCopy.h @@ -37,7 +37,7 @@ * @param targetDB name of the target database * @return YES on success, NO on any kind of error (unspecified) */ -- (BOOL)copyTable:(NSString *)name from: (NSString *)sourceDB to: (NSString *)targetDB; +- (BOOL)copyTable:(NSString *)name from:(NSString *)sourceDB to:(NSString *)targetDB; /** * This method moves a table from one db to another. @@ -46,7 +46,7 @@ * @param sourceDB name of the source database * @param targetDB name of the target database */ -- (BOOL)moveTable:(NSString *)name from: (NSString *)sourceDB to: (NSString *)targetDB; +- (BOOL)moveTable:(NSString *)name from:(NSString *)sourceDB to:(NSString *)targetDB; /** * This method copies a table including its data from one db to another. @@ -57,7 +57,7 @@ * @param copyWithContent whether to copy the content too, otherwise only structure * @return YES on success, NO on any kind of error (unspecified) */ -- (BOOL)copyTable:(NSString *)tableName from: (NSString *)sourceDB to: (NSString *)targetDB withContent:(BOOL)copyWithContent; +- (BOOL)copyTable:(NSString *)tableName from:(NSString *)sourceDB to: (NSString *)targetDB withContent:(BOOL)copyWithContent; /** * This method copies a bunch of tables including their data from one db to another. diff --git a/Source/SPTableCopy.m b/Source/SPTableCopy.m index fd9b9055..e629e481 100644 --- a/Source/SPTableCopy.m +++ b/Source/SPTableCopy.m @@ -24,26 +24,21 @@ #import "SPDBActionCommons.h" #import "SPTableCopy.h" + #import <SPMySQL/SPMySQL.h> -@implementation SPTableCopy +@interface SPTableCopy () -- (NSString *)getCreateTableStatementFor:(NSString *)tableName inDB:(NSString *)sourceDB -{ - NSString *showCreateTableStatment = [NSString stringWithFormat:@"SHOW CREATE TABLE %@.%@", [sourceDB backtickQuotedString], [tableName backtickQuotedString]]; - - SPMySQLResult *theResult = [connection queryString:showCreateTableStatment]; - - if ([theResult numberOfRows] != 0) { - return [[theResult getRowAsArray] objectAtIndex:1]; - } - - return @""; -} +- (NSString *)_createTableStatementFor:(NSString *)tableName inDatabase:(NSString *)sourceDatabase; + +@end + + +@implementation SPTableCopy - (BOOL)copyTable:(NSString *)tableName from:(NSString *)sourceDB to:(NSString *)targetDB { - NSString *createTableResult = [self getCreateTableStatementFor:tableName inDB:sourceDB]; + NSString *createTableResult = [self _createTableStatementFor:tableName inDatabase:sourceDB]; NSMutableString *createTableStatement = [[NSMutableString alloc] initWithString:createTableResult]; if ([[createTableStatement substringToIndex:12] isEqualToString:@"CREATE TABLE"]) { @@ -115,18 +110,29 @@ return success; } -- (BOOL)moveTable:(NSString *)tableName from:(NSString *)sourceDB to:(NSString *)targetDB +- (BOOL)moveTable:(NSString *)tableName from:(NSString *)sourceDB to:(NSString *)targetDB { NSString *moveStatement = [NSString stringWithFormat:@"RENAME TABLE %@.%@ TO %@.%@", [sourceDB backtickQuotedString], [tableName backtickQuotedString], [targetDB backtickQuotedString], - [tableName backtickQuotedString] - ]; + [tableName backtickQuotedString]]; [connection queryString:moveStatement]; return ![connection queryErrored]; } +#pragma mark - +#pragma mark Private API + +- (NSString *)_createTableStatementFor:(NSString *)tableName inDatabase:(NSString *)sourceDatabase +{ + NSString *showCreateTableStatment = [NSString stringWithFormat:@"SHOW CREATE TABLE %@.%@", [sourceDatabase backtickQuotedString], [tableName backtickQuotedString]]; + + SPMySQLResult *theResult = [connection queryString:showCreateTableStatment]; + + return [theResult numberOfRows] > 0 ? [[theResult getRowAsArray] objectAtIndex:1] : @""; +} + @end diff --git a/Source/SPTablesList.m b/Source/SPTablesList.m index 690b30d9..3a866bf7 100644 --- a/Source/SPTablesList.m +++ b/Source/SPTablesList.m @@ -1170,7 +1170,7 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; /** * Returns the currently selected table type, or -1 if no table or multiple tables are selected */ -- (SPTableType) tableType +- (SPTableType)tableType { return selectedTableType; } diff --git a/Source/SPViewCopy.h b/Source/SPViewCopy.h new file mode 100644 index 00000000..6f1be956 --- /dev/null +++ b/Source/SPViewCopy.h @@ -0,0 +1,37 @@ +// +// $Id$ +// +// SPViewCopy.h +// Sequel Pro +// +// Created by Stuart Connolly (stuconnolly.com) on May 3, 2012 +// Copyright (c) 2012 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// More info at <http://code.google.com/p/sequel-pro/> + +@interface SPViewCopy : SPDBActionCommons + +- (BOOL)moveView:(NSString *)view from:(NSString *)sourceDatabase to:(NSString *)targetDatabase; + +@end diff --git a/Source/SPViewCopy.m b/Source/SPViewCopy.m new file mode 100644 index 00000000..b7fde195 --- /dev/null +++ b/Source/SPViewCopy.m @@ -0,0 +1,90 @@ +// +// $Id$ +// +// SPViewCopy.m +// Sequel Pro +// +// Created by Stuart Connolly (stuconnolly.com) on May 3, 2012 +// Copyright (c) 2012 Stuart Connolly. All rights reserved. +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// +// More info at <http://code.google.com/p/sequel-pro/> + +#import "SPDBActionCommons.h" +#import "SPViewCopy.h" + +#import <SPMySQL/SPMySQL.h> + +@interface SPViewCopy () + +- (NSString *)_createViewStatementFor:(NSString *)view inDatabase:(NSString *)sourceDatabase; + +@end + +@implementation SPViewCopy + +- (BOOL)moveView:(NSString *)view from:(NSString *)sourceDatabase to:(NSString *)targetDatabase +{ + NSMutableString *createStatement = [[NSMutableString alloc] initWithString:[self _createViewStatementFor:view inDatabase:sourceDatabase]]; + + NSString *search = [NSString stringWithFormat:@"VIEW %@", [view backtickQuotedString]]; + + NSRange range = [createStatement rangeOfString:search]; + + if (range.location != NSNotFound) { + + NSUInteger replaced = [createStatement replaceOccurrencesOfString:search withString:[NSString stringWithFormat:@"VIEW %@.%@", [targetDatabase backtickQuotedString], [view backtickQuotedString]] options:0 range:range]; + + if (replaced != 1) return NO; + + // Replace all occurrences of the old database name + [createStatement replaceOccurrencesOfString:[sourceDatabase backtickQuotedString] + withString:[targetDatabase backtickQuotedString] + options:0 + range:NSMakeRange(0, [createStatement length])]; + + [connection queryString:createStatement]; + + [createStatement release]; + + return ![connection queryErrored]; + } + + [createStatement release]; + + return NO; +} + +#pragma mark - +#pragma mark Private API + +- (NSString *)_createViewStatementFor:(NSString *)view inDatabase:(NSString *)sourceDatabase +{ + NSString *createStatement = [NSString stringWithFormat:@"SHOW CREATE VIEW %@.%@", [sourceDatabase backtickQuotedString], [view backtickQuotedString]]; + + SPMySQLResult *theResult = [connection queryString:createStatement]; + + return [theResult numberOfRows] > 0 ? [[theResult getRowAsArray] objectAtIndex:1] : @""; +} + +@end diff --git a/UnitTests/SPDatabaseCopyTest.h b/UnitTests/SPDatabaseCopyTest.h index 641b3cdd..ba882ee1 100644 --- a/UnitTests/SPDatabaseCopyTest.h +++ b/UnitTests/SPDatabaseCopyTest.h @@ -27,9 +27,9 @@ #import <SenTestingKit/SenTestingKit.h> -@interface SPDatabaseCopyTest : SenTestCase { -} -- (void) testCopyDatabase; -- (void) testCreateDatabase; +@interface SPDatabaseCopyTest : SenTestCase + +- (void)testCopyDatabase; +- (void)testCreateDatabase; @end diff --git a/UnitTests/SPDatabaseCopyTest.m b/UnitTests/SPDatabaseCopyTest.m index 9ba7d5b7..df4807b9 100644 --- a/UnitTests/SPDatabaseCopyTest.m +++ b/UnitTests/SPDatabaseCopyTest.m @@ -55,7 +55,7 @@ return mockDBInfo; } -- (void) testCopyDatabase { +- (void)testCopyDatabase { SPDatabaseCopy *dbCopy = [self getDatabaseCopyFixture]; id mockConnection = [self getMockConnection]; diff --git a/UnitTests/SPDatabaseInfoTest.h b/UnitTests/SPDatabaseInfoTest.h deleted file mode 100644 index ed0093c5..00000000 --- a/UnitTests/SPDatabaseInfoTest.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// $Id$ -// -// SPDatabaseInfoTest.h -// sequel-pro -// -// Created by David Rekowski -// Copyright (c) 2010 David Rekowski. All rights reserved. -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// More info at <http://code.google.com/p/sequel-pro/> - -#import <SenTestingKit/SenTestingKit.h> - -#define USE_APPLICATION_UNIT_TEST 1 - -@interface SPDatabaseInfoTest : SenTestCase - -- (void)testDatabaseExists; -- (void)testListDBs; -- (void)testListDBsLike; - -@end diff --git a/UnitTests/SPDatabaseInfoTest.m b/UnitTests/SPDatabaseInfoTest.m deleted file mode 100644 index fc2309e3..00000000 --- a/UnitTests/SPDatabaseInfoTest.m +++ /dev/null @@ -1,92 +0,0 @@ -// -// $Id$ -// -// SPDatabaseInfoTest.m -// sequel-pro -// -// Created by David Rekowski -// Copyright (c) 2010 David Rekowski. All rights reserved. -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// More info at <http://code.google.com/p/sequel-pro/> - -#import <OCMock/OCMock.h> - -#import "SPDatabaseInfo.h" -#import "SPDatabaseInfoTest.h" - -@implementation SPDatabaseInfoTest - -- (SPDatabaseInfo *)getDatabaseInfoFixture -{ - SPDatabaseInfo *dbInfo = [[SPDatabaseInfo alloc] init]; - - return dbInfo; -} - -- (id) getMockConnection -{ - id mockConnection = [OCMockObject niceMockForClass:[MCPConnection class]]; - - return mockConnection; -} - -- (id) getMockMCPResult -{ - id mockResult = [OCMockObject niceMockForClass:[MCPResult class]]; - - return mockResult; -} - -- (void)testDatabaseExists -{ - SPDatabaseInfo *dbInfo = [self getDatabaseInfoFixture]; - - NSArray *tables = [[NSArray alloc] initWithObjects: @"db_one", nil]; - id mockMCPResult = [self getMockMCPResult]; - [[mockMCPResult expect] numOfRows]; - [[[mockMCPResult stub] andReturn:[[NSNumber alloc] initWithInt:1]] numOfRows]; - [[mockMCPResult expect] fetchRowAsArray]; - [[[mockMCPResult stub] andReturn:tables] fetchRowAsArray]; - id mockConnection = [self getMockConnection]; - - [[[mockConnection expect] andReturn:mockMCPResult] queryString:@"SHOW DATABASES"]; - [dbInfo setConnection:mockConnection]; - [dbInfo databaseExists:@"db_one"]; - [mockConnection verify]; -} - -- (void)testListDBs -{ - SPDatabaseInfo *dbInfo = [self getDatabaseInfoFixture]; - id mockConnection = [self getMockConnection]; - [[mockConnection expect] queryString:@"SHOW DATABASES"]; - [dbInfo setConnection:mockConnection]; - [dbInfo listDBs]; - [mockConnection verify]; -} - -- (void)testListDBsLike -{ - SPDatabaseInfo *dbInfo = [self getDatabaseInfoFixture]; - id mockConnection = [self getMockConnection]; - [[mockConnection expect] queryString:@"SHOW DATABASES LIKE `test_db`"]; - [dbInfo setConnection:mockConnection]; - [dbInfo listDBsLike:@"test_db"]; - [mockConnection verify]; -} - -@end diff --git a/UnitTests/SPDatabaseRenameTest.h b/UnitTests/SPDatabaseRenameTest.h index e3ac19cc..d012e8b9 100644 --- a/UnitTests/SPDatabaseRenameTest.h +++ b/UnitTests/SPDatabaseRenameTest.h @@ -29,7 +29,7 @@ @interface SPDatabaseRenameTest : SenTestCase -- (void) testRenameDatabase; -- (void) testCreateDatabase; +- (void)testRenameDatabase; +- (void)testCreateDatabase; @end diff --git a/UnitTests/SPDatabaseRenameTest.m b/UnitTests/SPDatabaseRenameTest.m index 9a1c7b1d..335e9f34 100644 --- a/UnitTests/SPDatabaseRenameTest.m +++ b/UnitTests/SPDatabaseRenameTest.m @@ -34,7 +34,7 @@ @implementation SPDatabaseRenameTest -- (SPDatabaseRename *) getDatabaseRenameFixture +- (SPDatabaseRename *)getDatabaseRenameFixture { SPDatabaseRename *dbRename = [[SPDatabaseRename alloc] init]; diff --git a/UnitTests/SPTableCopyTest.h b/UnitTests/SPTableCopyTest.h index 25d0da7b..62ebbaaf 100644 --- a/UnitTests/SPTableCopyTest.h +++ b/UnitTests/SPTableCopyTest.h @@ -27,11 +27,8 @@ #import <SenTestingKit/SenTestingKit.h> -@interface SPTableCopyTest : SenTestCase { - -} +@interface SPTableCopyTest : SenTestCase - (void)testCopyTableFromToWithData; - @end diff --git a/sequel-pro.xcodeproj/project.pbxproj b/sequel-pro.xcodeproj/project.pbxproj index 0b02478c..d06e20dc 100644 --- a/sequel-pro.xcodeproj/project.pbxproj +++ b/sequel-pro.xcodeproj/project.pbxproj @@ -25,8 +25,6 @@ /* Begin PBXBuildFile section */ 1141A389117BBFF200126A28 /* SPTableCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = 1141A388117BBFF200126A28 /* SPTableCopy.m */; }; 1141A38A117BBFF200126A28 /* SPTableCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = 1141A388117BBFF200126A28 /* SPTableCopy.m */; }; - 115D63E2117CBC5900419057 /* SPDatabaseInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 115D63E1117CBC5900419057 /* SPDatabaseInfo.m */; }; - 115D63E3117CBC5900419057 /* SPDatabaseInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 115D63E1117CBC5900419057 /* SPDatabaseInfo.m */; }; 1198F5B31174EDD500670590 /* SPDatabaseCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = 1198F5B21174EDD500670590 /* SPDatabaseCopy.m */; }; 1198F7541174FFCF00670590 /* SPDatabaseCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = 1198F5B21174EDD500670590 /* SPDatabaseCopy.m */; }; 11B55BFE1189E3B2009EF465 /* SPDBActionCommons.m in Sources */ = {isa = PBXBuildFile; fileRef = 11B55BFD1189E3B2009EF465 /* SPDBActionCommons.m */; }; @@ -64,7 +62,6 @@ 175EC63512733B36009A7C0F /* SPExportControllerDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 175EC63412733B36009A7C0F /* SPExportControllerDelegate.m */; }; 1760599F1336199D0098E162 /* SPMenuAdditionsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1760599E1336199D0098E162 /* SPMenuAdditionsTests.m */; }; 176059B713361D380098E162 /* SPDatabaseRenameTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 11C210DE1180E9B800758039 /* SPDatabaseRenameTest.m */; }; - 176059B813361D380098E162 /* SPDatabaseInfoTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 1127305911807894000737FD /* SPDatabaseInfoTest.m */; }; 176059B913361D390098E162 /* SPTableCopyTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 112730551180788A000737FD /* SPTableCopyTest.m */; }; 176059BA13361D3A0098E162 /* SPDatabaseCopyTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 1198F5C31174EF3F00670590 /* SPDatabaseCopyTest.m */; }; 1761FD480EF03A6F00331368 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1761FD460EF03A6F00331368 /* MainMenu.xib */; }; @@ -117,6 +114,7 @@ 17D3C671128AD8160047709F /* SPSingleton.m in Sources */ = {isa = PBXBuildFile; fileRef = 17D3C670128AD8160047709F /* SPSingleton.m */; }; 17D3C6D3128B1C900047709F /* SPFavoritesOutlineView.m in Sources */ = {isa = PBXBuildFile; fileRef = 17D3C6D2128B1C900047709F /* SPFavoritesOutlineView.m */; }; 17D3DC201281816E002A163A /* SPDatabaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 17D3DC1F1281816E002A163A /* SPDatabaseViewController.m */; }; + 17D5B49E1553059F00EF3BB3 /* SPViewCopy.m in Sources */ = {isa = PBXBuildFile; fileRef = 17D5B49D1553059F00EF3BB3 /* SPViewCopy.m */; }; 17DD52B7115071D0007D8950 /* SPPrintTemplate.html in Resources */ = {isa = PBXBuildFile; fileRef = 17DD52B6115071D0007D8950 /* SPPrintTemplate.html */; }; 17DD52C3115074B3007D8950 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 17DD52C1115074B3007D8950 /* InfoPlist.strings */; }; 17DD52C6115074CB007D8950 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 17DD52C4115074CB007D8950 /* Localizable.strings */; }; @@ -593,12 +591,8 @@ 1058C7A7FEA54F5311CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; }; 112730541180788A000737FD /* SPTableCopyTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTableCopyTest.h; sourceTree = "<group>"; }; 112730551180788A000737FD /* SPTableCopyTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTableCopyTest.m; sourceTree = "<group>"; }; - 1127305811807894000737FD /* SPDatabaseInfoTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPDatabaseInfoTest.h; sourceTree = "<group>"; }; - 1127305911807894000737FD /* SPDatabaseInfoTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPDatabaseInfoTest.m; sourceTree = "<group>"; }; 1141A387117BBFF200126A28 /* SPTableCopy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTableCopy.h; sourceTree = "<group>"; }; 1141A388117BBFF200126A28 /* SPTableCopy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTableCopy.m; sourceTree = "<group>"; }; - 115D63E0117CBC5900419057 /* SPDatabaseInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPDatabaseInfo.h; sourceTree = "<group>"; }; - 115D63E1117CBC5900419057 /* SPDatabaseInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPDatabaseInfo.m; sourceTree = "<group>"; }; 1198F5B11174EDD500670590 /* SPDatabaseCopy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPDatabaseCopy.h; sourceTree = "<group>"; }; 1198F5B21174EDD500670590 /* SPDatabaseCopy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPDatabaseCopy.m; sourceTree = "<group>"; }; 1198F5C21174EF3F00670590 /* SPDatabaseCopyTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPDatabaseCopyTest.h; sourceTree = "<group>"; }; @@ -780,6 +774,8 @@ 17D3C6D2128B1C900047709F /* SPFavoritesOutlineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPFavoritesOutlineView.m; sourceTree = "<group>"; }; 17D3DC1E1281816E002A163A /* SPDatabaseViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPDatabaseViewController.h; sourceTree = "<group>"; }; 17D3DC1F1281816E002A163A /* SPDatabaseViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPDatabaseViewController.m; sourceTree = "<group>"; }; + 17D5B49C1553059F00EF3BB3 /* SPViewCopy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPViewCopy.h; sourceTree = "<group>"; }; + 17D5B49D1553059F00EF3BB3 /* SPViewCopy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPViewCopy.m; sourceTree = "<group>"; }; 17DA04EA0FC1A7DA00D66140 /* Unit Tests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "Unit Tests-Info.plist"; path = "Plists/Unit Tests-Info.plist"; sourceTree = "<group>"; }; 17DD52B6115071D0007D8950 /* SPPrintTemplate.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = SPPrintTemplate.html; path = Templates/SPPrintTemplate.html; sourceTree = "<group>"; }; 17DD52B811507217007D8950 /* English */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = English; path = English.lproj/Credits.rtf; sourceTree = "<group>"; }; @@ -1396,12 +1392,12 @@ 1198F5B01174EDA700670590 /* Database Actions */ = { isa = PBXGroup; children = ( - 1198F5B11174EDD500670590 /* SPDatabaseCopy.h */, - 1198F5B21174EDD500670590 /* SPDatabaseCopy.m */, + 17D5B49C1553059F00EF3BB3 /* SPViewCopy.h */, + 17D5B49D1553059F00EF3BB3 /* SPViewCopy.m */, 1141A387117BBFF200126A28 /* SPTableCopy.h */, 1141A388117BBFF200126A28 /* SPTableCopy.m */, - 115D63E0117CBC5900419057 /* SPDatabaseInfo.h */, - 115D63E1117CBC5900419057 /* SPDatabaseInfo.m */, + 1198F5B11174EDD500670590 /* SPDatabaseCopy.h */, + 1198F5B21174EDD500670590 /* SPDatabaseCopy.m */, 11C2109C1180E70800758039 /* SPDatabaseRename.h */, 11C2109D1180E70800758039 /* SPDatabaseRename.m */, 11B55BFC1189E3B2009EF465 /* SPDBActionCommons.h */, @@ -1417,8 +1413,6 @@ 1198F5C31174EF3F00670590 /* SPDatabaseCopyTest.m */, 112730541180788A000737FD /* SPTableCopyTest.h */, 112730551180788A000737FD /* SPTableCopyTest.m */, - 1127305811807894000737FD /* SPDatabaseInfoTest.h */, - 1127305911807894000737FD /* SPDatabaseInfoTest.m */, 11C210DD1180E9B800758039 /* SPDatabaseRenameTest.h */, 11C210DE1180E9B800758039 /* SPDatabaseRenameTest.m */, ); @@ -3085,12 +3079,10 @@ files = ( 11C211271180EC0400758039 /* SPDatabaseRename.m in Sources */, 1141A38A117BBFF200126A28 /* SPTableCopy.m in Sources */, - 115D63E3117CBC5900419057 /* SPDatabaseInfo.m in Sources */, 1198F7541174FFCF00670590 /* SPDatabaseCopy.m in Sources */, 380F4EF50FC0B68F00B0BFD7 /* SPStringAdditionsTest.m in Sources */, 1760599F1336199D0098E162 /* SPMenuAdditionsTests.m in Sources */, 176059B713361D380098E162 /* SPDatabaseRenameTest.m in Sources */, - 176059B813361D380098E162 /* SPDatabaseInfoTest.m in Sources */, 176059B913361D390098E162 /* SPTableCopyTest.m in Sources */, 176059BA13361D3A0098E162 /* SPDatabaseCopyTest.m in Sources */, 1798F1C4155018E2004B0AB8 /* SPMutableArrayAdditionsTest.m in Sources */, @@ -3240,7 +3232,6 @@ 5885CF4A116A63B200A85ACB /* SPFileHandle.m in Sources */, 1198F5B31174EDD500670590 /* SPDatabaseCopy.m in Sources */, 1141A389117BBFF200126A28 /* SPTableCopy.m in Sources */, - 115D63E2117CBC5900419057 /* SPDatabaseInfo.m in Sources */, 11B55BFE1189E3B2009EF465 /* SPDBActionCommons.m in Sources */, 58A8A79111A036C000B95749 /* SPWindowController.m in Sources */, 5806B76411A991EC00813A88 /* SPDocumentController.m in Sources */, @@ -3317,6 +3308,7 @@ 1798F19815501838004B0AB8 /* SPMutableArrayAdditions.m in Sources */, 1798F19B1550185B004B0AB8 /* SPTreeNode.m in Sources */, 1798F19E15501892004B0AB8 /* SPFlippedView.m in Sources */, + 17D5B49E1553059F00EF3BB3 /* SPViewCopy.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; |