-
Notifications
You must be signed in to change notification settings - Fork 315
/
ExportWindowController.mm
292 lines (275 loc) · 10.6 KB
/
ExportWindowController.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// ExportWindowController.m
// MongoHub
//
// Created by Syd on 10-6-22.
// Copyright 2010 ThePeppersStudio.COM. All rights reserved.
//
#import "ExportWindowController.h"
#import "Configure.h"
#import "DatabasesArrayController.h"
#import "Database.h"
#import "Connection.h"
#import "NSString+Extras.h"
#import "MongoDB.h"
#import <MCPKit_bundled/MCPKit_bundled.h>
#import "FieldMapTableController.h"
#import "FieldMapDataObject.h"
@implementation ExportWindowController
@synthesize dbname;
@synthesize conn;
@synthesize db;
@synthesize mongoDB;
@synthesize databasesArrayController;
@synthesize managedObjectContext;
@synthesize dbsArrayController;
@synthesize tablesArrayController;
@synthesize hostTextField;
@synthesize portTextField;
@synthesize userTextField;
@synthesize passwdTextField;
@synthesize collectionTextField;
@synthesize progressIndicator;
@synthesize tablesPopUpButton;
@synthesize fieldMapTableController;
- (id)init {
if (![super initWithWindowNibName:@"Export"]) return nil;
return self;
}
- (void)dealloc {
[dbname release];
[managedObjectContext release];
[databasesArrayController release];
[conn release];
[db release];
[mongoDB release];
[dbsArrayController release];
[tablesArrayController release];
[hostTextField release];
[portTextField release];
[userTextField release];
[passwdTextField release];
[collectionTextField release];
[progressIndicator release];
[tablesPopUpButton release];
[fieldMapTableController release];
[super dealloc];
}
- (void)windowDidLoad {
//NSLog(@"New Connection Window Loaded");
[super windowDidLoad];
}
- (void)windowWillClose:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:kExportWindowWillClose object:dbname];
dbname = nil;
db = nil;
[self initInterface];
}
- (IBAction)export:(id)sender {
[progressIndicator setUsesThreadedAnimation:YES];
[progressIndicator startAnimation: self];
[progressIndicator setDoubleValue:0];
NSString *collection = [[NSString alloc] initWithString:[collectionTextField stringValue]];
if (![collection isPresent]) {
NSRunAlertPanel(@"Error", @"Collection name could not be empty!", @"OK", nil, nil);
return;
}
NSString *tablename = [[NSString alloc] initWithString:[tablesPopUpButton titleOfSelectedItem]];
NSString *user=nil;
NSString *password=nil;
Database *mongodb = [databasesArrayController dbInfo:conn name:dbname];
if (mongodb) {
user = mongodb.user;
password = mongodb.password;
}
[mongodb release];
long long int total = [self exportCount:collection user:user password:password];
if (total == 0) {
return;
}
NSString *query = [[NSString alloc] initWithFormat:@"select * from %@ limit 1", tablename];
MCPResult *theResult = [db queryString:query];
[query release];
NSDictionary *fieldTypes = [theResult fetchTypesAsDictionary];
mongo::BSONObjBuilder fieldsBSONBuilder;
for(FieldMapDataObject *field in fieldMapTableController.nsMutaryDataObj)
{
fieldsBSONBuilder.append([field.mongoKey UTF8String], 1);
}
mongo::BSONObj fieldsBSONObj = fieldsBSONBuilder.obj();
std::auto_ptr<mongo::DBClientCursor> cursor = [mongoDB findAllCursorInDB:dbname collection:collection user:user password:password fields:fieldsBSONObj];
int i = 1;
while( cursor->more() )
{
mongo::BSONObj b = cursor->next();
[self doExportToTable:tablename data:b fieldTypes:fieldTypes];
[progressIndicator setDoubleValue:(double)i/total];
i ++;
}
[progressIndicator stopAnimation: self];
[tablename release];
[collection release];
}
- (long long int)exportCount:(NSString *)collection user:(NSString *)user password:(NSString *)password
{
long long int result = [mongoDB countInDB:dbname collection:collection user:user password:password critical:nil];
return result;
}
- (void)doExportToTable:(NSString *)tableName data:(mongo::BSONObj) bsonObj fieldTypes:(NSDictionary *)fieldTypes
{
int fieldsCount = [fieldMapTableController.nsMutaryDataObj count];
NSMutableArray *fields = [[NSMutableArray alloc] initWithCapacity:fieldsCount];
NSMutableArray *values = [[NSMutableArray alloc] initWithCapacity:fieldsCount];
for(FieldMapDataObject *field in fieldMapTableController.nsMutaryDataObj)
{
id value;
mongo::BSONElement e = bsonObj.getFieldDotted([field.mongoKey UTF8String]);
if (e.eoo() == true) {
continue;
}
if (e.type() == mongo::jstNULL) {
continue;
}else if (e.type() == mongo::Array) {
continue;
}else if (e.type() == mongo::Object) {
continue;
}else if (e.type() == mongo::Bool) {
if (e.boolean()) {
value = [[NSString alloc] initWithString:@"1" ];
}else {
value = [[NSString alloc] initWithString:@"0"];
}
}else if (e.type() == mongo::NumberDouble) {
value = [[NSNumber alloc] initWithDouble: e.numberDouble()];
}else if (e.type() == mongo::NumberInt) {
NSString *ft = [fieldTypes objectForKey:field.sqlKey];
if ([ft isEqualToString:@"date"] || [ft isEqualToString:@"datetime"]) {
value = [[NSDate alloc] initWithTimeIntervalSince1970:e.numberInt()];
}else {
value = [[NSNumber alloc] initWithInt: e.numberInt()];
}
}else if (e.type() == mongo::Date) {
mongo::Date_t dt = (time_t)e.date();
time_t timestamp = dt / 1000;
value = [[NSDate alloc] initWithTimeIntervalSince1970:timestamp];
}else if (e.type() == mongo::Timestamp) {
time_t timestamp = (time_t)e.timestampTime();
value = [[NSDate alloc] initWithTimeIntervalSince1970:timestamp];
}else if (e.type() == mongo::BinData) {
int binlen;
const char* data = e.binData(binlen);
value = [[NSData alloc] initWithBytes:data length:binlen];
}else if (e.type() == mongo::NumberLong) {
NSString *ft = [fieldTypes objectForKey:field.sqlKey];
if ([ft isEqualToString:@"date"] || [ft isEqualToString:@"datetime"]) {
value = [[NSDate alloc] initWithTimeIntervalSince1970:e.numberLong()];
}else {
value = [[NSNumber alloc] initWithLong: e.numberLong()];
}
}else if ([field.mongoKey isEqualToString:@"_id" ]) {
if (e.type() == mongo::jstOID)
{
value = [[NSString alloc] initWithUTF8String: e.__oid().str().c_str()];
}else {
value = [[NSString alloc] initWithUTF8String: e.str().c_str()];
}
}else {
value = [[NSString alloc] initWithUTF8String:e.str().c_str()];
}
NSString *sqlKey = [[NSString alloc] initWithString:field.sqlKey];
NSString *quotedValue = [[NSString alloc] initWithString:[db quoteObject:value]];
[value release];
[fields addObject:sqlKey];
[values addObject:quotedValue];
[quotedValue release];
[sqlKey release];
}
if ([fields count] > 0) {
NSString *query = [[NSString alloc] initWithFormat:@"INSERT INTO %@ (%@) values (%@)", tableName, [fields componentsJoinedByString:@","], [values componentsJoinedByString:@","]];
//NSLog(@"query: %@", query);
[db queryString:query];
[query release];
}
[fields release];
[values release];
}
- (IBAction)connect:(id)sender {
if (db) {
[self initInterface];
[db release];
}
db = [[MCPConnection alloc] initToHost:[hostTextField stringValue] withLogin:[userTextField stringValue] password:[passwdTextField stringValue] usingPort:[portTextField intValue] ];
NSLog(@"Connect: %d", [db isConnected]);
if (![db isConnected])
{
NSRunAlertPanel(@"Error", @"Could not connect to the mysql server!", @"OK", nil, nil);
}
[db queryString:@"SET NAMES utf8"];
[db queryString:@"SET CHARACTER SET utf8"];
[db queryString:@"SET COLLATION_CONNECTION='utf8_general_ci'"];
[db setEncoding:NSUTF8StringEncoding];
MCPResult *dbs = [db listDBs];
NSArray *row;
NSMutableArray *databases = [[NSMutableArray alloc] initWithCapacity:[dbs numOfRows]];
while (row = [dbs fetchRowAsArray]) {
NSDictionary *database = [[NSDictionary alloc] initWithObjectsAndKeys:[row objectAtIndex:0], @"name", nil];
[databases addObject:database];
[database release];
}
[dbsArrayController setContent:databases];
[databases release];
//[self showTables:nil];
}
- (IBAction)showTables:(id)sender
{
NSString *dbn;
if (sender == nil && [[dbsArrayController arrangedObjects] count] > 0) {
dbn = [[[dbsArrayController arrangedObjects] objectAtIndex:0] objectForKey:@"name"];
}else {
NSPopUpButton *pb = sender;
dbn = [[NSString alloc] initWithString:[pb titleOfSelectedItem]];
}
if (![dbn isPresent]) {
[dbn release];
return;
}
[db selectDB:dbn];
[dbn release];
MCPResult *tbs = [db listTables];
NSArray *row;
NSMutableArray *tables = [[NSMutableArray alloc] initWithCapacity:[tbs numOfRows]];
while (row = [tbs fetchRowAsArray]) {
NSDictionary *table = [[NSDictionary alloc] initWithObjectsAndKeys:[row objectAtIndex:0], @"name", nil];
[tables addObject:table];
[table release];
}
[tablesArrayController setContent:tables];
[tables release];
[self showFields:nil];
}
- (IBAction)showFields:(id)sender
{
NSString *tablename = [[NSString alloc] initWithString:[tablesPopUpButton titleOfSelectedItem]];
MCPResult *theResult = [db queryString:[NSString stringWithFormat:@"select * from %@ limit 1", tablename]];
[tablename release];
NSArray *theFields = [theResult fetchFieldNames];
NSMutableArray *fields = [[NSMutableArray alloc] initWithCapacity:[theFields count] ];
for (int i=0; i<[theFields count]; i++) {
NSString *fieldName = [theFields objectAtIndex:i];
FieldMapDataObject *fd = [[FieldMapDataObject alloc] initWithSqlKey:fieldName andMongoKey:fieldName];
[fields addObject:fd];
[fd release];
}
[fieldMapTableController setNsMutaryDataObj:fields];
[fieldMapTableController.idTableView reloadData];
[fields release];
}
- (void)initInterface
{
[dbsArrayController setContent:nil];
[tablesArrayController setContent:nil];
[progressIndicator setDoubleValue:0.0];
[fieldMapTableController.nsMutaryDataObj removeAllObjects];
[fieldMapTableController.idTableView reloadData];
}
@end