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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
//
// MQParameters.m
// MySQL QueryBrowser
//
// Created by Alfredo Kojima on 05/6/11.
// Copyright 2005 MySQL AB. All rights reserved.
//
#import "MQParameters.h"
#import <MySQLToolsCommon/MQResultSetView.h>
#import <MySQLToolsCommon/MQTableView.h>
@implementation MQParameters
- (void)loadGlobals
{
NSString *path= [NSString stringWithFormat:@"%@/global_params.xml", [[NSUserDefaults standardUserDefaults] stringForKey:@"QBDirectory"]];
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
id plist;
plistData = [NSData dataWithContentsOfFile:path];
if (plistData)
{
plist = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListMutableContainers
format:&format
errorDescription:&error];
if (!plist)
{
NSLog(error);
[error release];
}
_globalParams= [plist retain];
}
else
_globalParams= nil;
}
- (void)saveGlobals
{
NSString *path= [NSString stringWithFormat:@"%@/global_params.xml", [[NSUserDefaults standardUserDefaults] stringForKey:@"QBDirectory"]];
NSData *xmlData;
NSString *error;
xmlData = [NSPropertyListSerialization dataFromPropertyList:_globalParams
format:NSPropertyListXMLFormat_v1_0
errorDescription:&error];
if (xmlData)
{
[xmlData writeToFile:path atomically:YES];
}
else
{
NSLog(error);
[error release];
}
}
- (id)init
{
self= [super init];
if (self)
{
_normalParams= [[NSMutableArray alloc] init];
_localParams= [[NSMutableArray alloc] init];
[self loadGlobals];
if (!_globalParams)
_globalParams= [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[self saveGlobals];
[_normalParams release];
[_globalParams release];
[_localParams release];
[super dealloc];
}
- (id)localParameters
{
return _localParams;
}
- (void)setLocalParameters:(id)params
{
if (_localParams != params)
{
[_localParams release];
if (params)
_localParams= [params retain];
else
_localParams= [[NSMutableArray alloc] init];
}
}
- (void)setResultset:(MYX_RESULTSET*)rs row:(int)rowIndex
{
unsigned int i;
[_normalParams removeAllObjects];
if (rs)
{
if (rowIndex < 0)
_row= NULL;
else
_row= rs->rows+rowIndex;
for (i= 0; i < rs->columns_num_to_display; i++)
{
[_normalParams addObject:[NSString stringWithUTF8String:rs->columns[i].name]];
if (_row && _row->fields[i].value)
[_normalParams addObject:[NSString stringWithUTF8String:_row->fields[i].value]];
else
[_normalParams addObject:@""];
[_normalParams addObject:[NSNumber numberWithLongLong:(1LL<<32)|i]];
}
}
else
_row= NULL;
}
- (BOOL)itemCanBeDeleted:(id)item
{
if ([item isKindOfClass:[NSString class]])
return NO;
else if (([item longLongValue] >> 32) > 1)
return YES;
else
return NO;
}
- (BOOL)itemCanBeAdded:(id)item
{
if ([item isEqualTo:@"local"] || [item isEqualTo:@"global"])
return YES;
else if ([item isEqualTo:@"normal"])
return NO;
else if (([item longLongValue] >> 32) > 1)
return YES;
else
return NO;
}
- (BOOL)itemCanBeEdited:(id)item
{
if ([item isKindOfClass:[NSString class]])
return NO;
else if (([item longLongValue] >> 32) > 1)
return YES;
else
return NO;
}
- (void)addParameter:(id)item
{
if ([item isKindOfClass:[NSString class]])
{
if ([item isEqualTo:@"local"])
{
[_localParams addObject:@"new_param"];
[_localParams addObject:@""];
[_localParams addObject:[NSNumber numberWithLongLong:(2LL<<32)|[_localParams count]/3]];
}
else if ([item isEqualTo:@"global"])
{
[_globalParams addObject:@"new_param"];
[_globalParams addObject:@""];
[_globalParams addObject:[NSNumber numberWithLongLong:(3LL<<32)|[_globalParams count]/3]];
}
}
else if (([item longLongValue] >> 32) != 1)
{
if ([item longLongValue] >> 32 == 2)
{
[_localParams addObject:@"new_param"];
[_localParams addObject:@""];
[_localParams addObject:[NSNumber numberWithLongLong:(2LL<<32)|[_localParams count]/3]];
}
else if ([item longLongValue] >> 32 == 3)
{
[_globalParams addObject:@"new_param"];
[_globalParams addObject:@""];
[_globalParams addObject:[NSNumber numberWithLongLong:(3LL<<32)|[_globalParams count]/3]];
}
}
}
- (void)deleteParameter:(id)item
{
if ([item longLongValue] >> 32 == 2)
{
int index= [item longLongValue]&0xffffffffL;
[_localParams removeObjectAtIndex:index*3+2];
[_localParams removeObjectAtIndex:index*3+1];
[_localParams removeObjectAtIndex:index*3];
}
else if ([item longLongValue] >> 32 == 3)
{
int index= [item longLongValue]&0xffffffffL;
[_globalParams removeObjectAtIndex:index*3+2];
[_globalParams removeObjectAtIndex:index*3+1];
[_globalParams removeObjectAtIndex:index*3];
}
}
- (MYX_STRINGLIST*)parametersFromMaster:(MQResultSetView*)master
{
MYX_STRINGLIST *sl= g_new0(MYX_STRINGLIST, 1);
unsigned int idx, i, c;
MYX_RESULTSET *rs= [master resultset];
int rowIndex= [[master tableView] selectedRow];
if (rs && rowIndex >= 0)
sl->strings_num= ([_globalParams count]+[_localParams count])/3+rs->columns_num_to_display;
else
sl->strings_num= ([_globalParams count]+[_localParams count])/3;
sl->strings= g_new0(char*, sl->strings_num);
idx= 0;
c= [_globalParams count]/3;
for (i= 0; i < c; i++)
{
sl->strings[idx++]= g_strdup_printf("%s=%s",
[[_globalParams objectAtIndex:i*3] UTF8String],
[[_globalParams objectAtIndex:i*3+1] UTF8String]);
}
c= [_localParams count]/3;
for (i= 0; i < c; i++)
{
sl->strings[idx++]= g_strdup_printf("%s=%s",
[[_localParams objectAtIndex:i*3] UTF8String],
[[_localParams objectAtIndex:i*3+1] UTF8String]);
}
if (rs && rowIndex >= 0)
{
MYX_RS_ROW *row= rs->rows+rowIndex;
for (i= 0; i < rs->columns_num_to_display; i++)
{
sl->strings[idx++]= g_strdup_printf("%s=%s",
rs->columns[i].name,
row->fields[i].value?:"NULL");
}
}
return sl;
}
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if ([item isKindOfClass:[NSNumber class]])
{
int type= [item longLongValue]>>32;
int index= [item longLongValue]&0xffffffffLL;
NSMutableArray *array= type == 2 ? _localParams : _globalParams;
if ([[tableColumn identifier] isEqualTo:@"name"])
[array replaceObjectAtIndex:index*3
withObject:object];
else
[array replaceObjectAtIndex:index*3+1
withObject:object];
}
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if ([item isKindOfClass:[NSString class]])
{
if ([[tableColumn identifier] isEqualTo:@"name"])
{
if ([item isEqualTo:@"normal"])
return NSLocalizedString(@"From Master", @"Parameter Sidebar");
else if ([item isEqualTo:@"local"])
return NSLocalizedString(@"Local", @"Parameter Sidebar");
else
return NSLocalizedString(@"Global", @"Parameter Sidebar");
}
}
else
{
int type= [item longLongValue]>>32;
int index= [item longLongValue]&0xffffffffLL;
if ([[tableColumn identifier] isEqualTo:@"name"])
{
switch (type)
{
case 1:
return [_normalParams objectAtIndex:index*3];
case 2:
return [_localParams objectAtIndex:index*3];
case 3:
return [_globalParams objectAtIndex:index*3];
}
}
else
{
switch (type)
{
case 1:
return [_normalParams objectAtIndex:index*3+1];
case 2:
return [_localParams objectAtIndex:index*3+1];
case 3:
return [_globalParams objectAtIndex:index*3+1];
}
}
}
return nil;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
{
if (!item)
{
switch (index)
{
case 0:
return @"normal";
case 1:
return @"local";
case 2:
return @"global";
}
return @"???";
}
else
{
if ([item isEqualTo:@"normal"])
return [_normalParams objectAtIndex:index*3+2];
else if ([item isEqualTo:@"local"])
return [_localParams objectAtIndex:index*3+2];
else
return [_globalParams objectAtIndex:index*3+2];
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
if (!item)
return YES;
else if ([item isKindOfClass:[NSString class]])
return YES;
else
return NO;
}
- (int)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
if (!item)
return 3;
else
{
if ([item isEqual:@"normal"])
return [_normalParams count]/3;
else if ([item isEqual:@"local"])
return [_localParams count]/3;
else if ([item isEqual:@"global"])
return [_globalParams count]/3;
else
return 0;
}
}
@end
|