From: Neil T. <nt...@us...> - 2006-10-30 19:31:13
|
Update of /cvsroot/pgsqlformac/pgCocoaDB In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19764 Modified Files: Schema.m Log Message: Fix round trip function SQL. Index: Schema.m =================================================================== RCS file: /cvsroot/pgsqlformac/pgCocoaDB/Schema.m,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** Schema.m 14 Sep 2006 12:26:46 -0000 1.23 --- Schema.m 30 Oct 2006 19:30:32 -0000 1.24 *************** *** 420,424 **** char **argmodes = NULL; char **argnames = NULL; ! int argtypes_nitems = 0; int argmodes_nitems = 0; int argnames_nitems = 0; --- 420,424 ---- char **argmodes = NULL; char **argnames = NULL; ! int allargtypes_nitems = 0; int argmodes_nitems = 0; int argnames_nitems = 0; *************** *** 437,441 **** */ ! sql = [NSString stringWithFormat:@"SELECT p.proname, p.proretset, p.prosrc, p.probin, p.pronargs, p.proallargtypes, p.proargmodes, p.proargnames, \ p.provolatile, p.proisstrict, p.prosecdef, (SELECT lanname FROM pg_catalog.pg_language l WHERE l.oid = p.prolang) as lanname, \ (SELECT typname from pg_catalog.pg_type WHERE oid = p.prorettype) as rettype \ --- 437,443 ---- */ ! sql = [NSString stringWithFormat:@"SELECT p.proname, \ ! CASE WHEN p.proretset THEN 'setof ' ELSE '' END || pg_catalog.format_type(p.prorettype, NULL) as \"resulttype\", \ ! p.prosrc, p.probin, p.pronargs, p.proallargtypes, p.proargtypes, p.proargmodes, p.proargnames, \ p.provolatile, p.proisstrict, p.prosecdef, (SELECT lanname FROM pg_catalog.pg_language l WHERE l.oid = p.prolang) as lanname, \ (SELECT typname from pg_catalog.pg_type WHERE oid = p.prorettype) as rettype \ *************** *** 470,476 **** [sqlOutput appendFormat:@"CREATE or REPLACE FUNCTION %@.%@ ", schemaName, functionName]; //TODO get return values and parameters ! NSLog(@"args: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"proallargtypes"]); ! NSLog(@"args: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"proargmodes"]); ! NSLog(@"args: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"proargnames"]); /* 2006-06-17 16:41:42.405 Query Tool for Postgres[28356] args: {23,23,23,23} --- 472,480 ---- [sqlOutput appendFormat:@"CREATE or REPLACE FUNCTION %@.%@ ", schemaName, functionName]; //TODO get return values and parameters ! NSLog(@"proallargtypes: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"proallargtypes"]); ! NSLog(@" proargmodes: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"proargmodes"]); ! NSLog(@" proargnames: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"proargnames"]); ! NSLog(@" proargtypes: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"proargtypes"]); ! NSLog(@" pronargs: %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"pronargs"]); /* 2006-06-17 16:41:42.405 Query Tool for Postgres[28356] args: {23,23,23,23} *************** *** 479,497 **** 2006-06-17 16:41:42.406 Query Tool for Postgres[28356] args: 2 */ - numberOfArgs = [[[[results itemAtIndex: 0] fields] getValueFromName: @"pronargs"] intValue]; [[[[results itemAtIndex: 0] fields] getValueFromName: @"proallargtypes"] getCString:buffer maxLength:1024 encoding:NSASCIIStringEncoding]; ! if (!parsePGArray( buffer, &allargtypes, &argtypes_nitems)) { if (allargtypes) free(allargtypes); allargtypes = NULL; } [[[[results itemAtIndex: 0] fields] getValueFromName: @"proargmodes"] getCString:buffer maxLength:1024 encoding:NSASCIIStringEncoding]; if (!parsePGArray( buffer, &argmodes, &argmodes_nitems)) { ! if (argmodes) ! free(argmodes); argmodes = NULL; } --- 483,512 ---- 2006-06-17 16:41:42.406 Query Tool for Postgres[28356] args: 2 */ [[[[results itemAtIndex: 0] fields] getValueFromName: @"proallargtypes"] getCString:buffer maxLength:1024 encoding:NSASCIIStringEncoding]; ! if (!parsePGArray( buffer, &allargtypes, &allargtypes_nitems)) { if (allargtypes) + { free(allargtypes); + } allargtypes = NULL; } + + NSScanner *theScanner = [NSScanner scannerWithString: [[[results itemAtIndex: 0] fields] getValueFromName: @"proargtypes"]]; + NSMutableArray * argTypeArray = [NSMutableArray arrayWithCapacity:10]; + int anInt; + while ([theScanner scanInt: &anInt]) + { + [argTypeArray addObject:[NSNumber numberWithInt:anInt]]; + } [[[[results itemAtIndex: 0] fields] getValueFromName: @"proargmodes"] getCString:buffer maxLength:1024 encoding:NSASCIIStringEncoding]; if (!parsePGArray( buffer, &argmodes, &argmodes_nitems)) { ! if (argmodes) ! { ! free(argmodes); ! } argmodes = NULL; } *************** *** 501,515 **** { if (argnames) ! free(argnames); argnames = NULL; } ! if (argnames_nitems != 0) { int i; - [sqlOutput appendString:@"("]; for (i = 0; i< argnames_nitems; i++) { ! char * argmode; if (argmodes) { --- 516,537 ---- { if (argnames) ! { ! free(argnames); ! } argnames = NULL; } ! // determine if args are in proargtypes or proallargtypes ! numberOfArgs = [[[[results itemAtIndex: 0] fields] getValueFromName: @"pronargs"] intValue]; ! if (allargtypes_nitems > numberOfArgs) ! { ! numberOfArgs = allargtypes_nitems; ! } ! [sqlOutput appendString:@"("]; if (argnames_nitems != 0) { int i; for (i = 0; i< argnames_nitems; i++) { ! char * argmode = ""; if (argmodes) { *************** *** 517,521 **** { case 'i': ! argmode = "IN"; break; case 'o': --- 539,543 ---- { case 'i': ! argmode = "IN "; break; case 'o': *************** *** 527,542 **** default: NSLog(@"WARNING: bogus value in proargmodes array"); - argmode = ""; break; } } ! else ! argmode = ""; ! ! if (i != 0) ! { ! [sqlOutput appendString:@", "]; ! } ! sql = [NSString stringWithFormat:@"SELECT pg_catalog.format_type('%s'::pg_catalog.oid, NULL)", allargtypes[i]]; results1 = [connection execQueryNoLog:sql]; if ([results1 count] != 1) --- 549,559 ---- default: NSLog(@"WARNING: bogus value in proargmodes array"); break; } } ! [sqlOutput appendString: (i==0) ? @"" : @", "]; ! // FIXME allargtypes can be NULL, then this faults, need to understand the conditions that lead to this? ! sql = [NSString stringWithFormat:@"SELECT pg_catalog.format_type('%s'::pg_catalog.oid, NULL)", ! allargtypes ? allargtypes[i] : [[[argTypeArray objectAtIndex:i] stringValue] cStringUsingEncoding:NSASCIIStringEncoding] ]; results1 = [connection execQueryNoLog:sql]; if ([results1 count] != 1) *************** *** 547,552 **** [sqlOutput appendFormat:@"%s %s %@", argmode, argnames[i], [[[[results1 itemAtIndex: 0] fields] itemAtIndex:0] value]]; } - [sqlOutput appendString:@") "]; } [sqlOutput appendString:@" AS $$\n"]; [sqlOutput appendString:[[[results itemAtIndex: 0] fields] getValueFromName:@"prosrc"]]; --- 564,570 ---- [sqlOutput appendFormat:@"%s %s %@", argmode, argnames[i], [[[[results1 itemAtIndex: 0] fields] itemAtIndex:0] value]]; } } + [sqlOutput appendString:@") "]; + [sqlOutput appendFormat:@"RETURNS %@", [[[results itemAtIndex: 0] fields] getValueFromName: @"resulttype"]]; [sqlOutput appendString:@" AS $$\n"]; [sqlOutput appendString:[[[results itemAtIndex: 0] fields] getValueFromName:@"prosrc"]]; *************** *** 620,631 **** if (i != 0) { ! if (pretty) ! { ! [sqlOutput appendString:@",\n"]; ! } ! else ! { ! [sqlOutput appendString:@", "]; ! } } if (pretty) --- 638,642 ---- if (i != 0) { ! [sqlOutput appendString: (pretty) ? @",\n" : @", "]; } if (pretty) *************** *** 652,665 **** [sqlOutput appendFormat:@" %@", [[[results itemAtIndex: i] fields] getValueFromName:@"notnull"]]; } - - } - if (pretty) - { - [sqlOutput appendString:@"\n);"]; - } - else - { - [sqlOutput appendString:@" );"]; } return sqlOutput; } --- 663,668 ---- [sqlOutput appendFormat:@" %@", [[[results itemAtIndex: i] fields] getValueFromName:@"notnull"]]; } } + [sqlOutput appendString: (pretty) ? @"\n);" : @" );"]; return sqlOutput; } *************** *** 1008,1012 **** * We expect input in the form of "{item,item,item}" where any item is * either raw data, or surrounded by double quotes (in which case embedded ! * characters including backslashes and quotes are backslashed). * * We build the result as an array of pointers followed by the actual --- 1011,1015 ---- * We expect input in the form of "{item,item,item}" where any item is * either raw data, or surrounded by double quotes (in which case embedded ! * characters including backslashes and quotes are backslashed). * * We build the result as an array of pointers followed by the actual |