diff options
author | stuconnolly <stuart02@gmail.com> | 2012-05-04 14:36:30 +0000 |
---|---|---|
committer | stuconnolly <stuart02@gmail.com> | 2012-05-04 14:36:30 +0000 |
commit | 5544489096885e4e4be1ab6e54160caaa44c03dc (patch) | |
tree | 52b2201bf491d1e3495d506e7c282b3509e33808 /Source | |
parent | 830790b9251a36f3d1290a3f9ead62195d61f00a (diff) | |
download | sequelpro-5544489096885e4e4be1ab6e54160caaa44c03dc.tar.gz sequelpro-5544489096885e4e4be1ab6e54160caaa44c03dc.tar.bz2 sequelpro-5544489096885e4e4be1ab6e54160caaa44c03dc.zip |
Add initial support of moving views when renaming a database. Currently not hooked up yet. Part of issue #1235.
Diffstat (limited to 'Source')
-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 |
14 files changed, 246 insertions, 269 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 |