From: Neil T. <nt...@us...> - 2006-06-24 16:16:37
|
Update of /cvsroot/pgsqlformac/pgCocoaDB In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1931 Modified Files: Connection.h Connection.m ExplorerModel.m Schema.m Log Message: Add SQL log functions, correct bug in schema dealloc. Index: Schema.m =================================================================== RCS file: /cvsroot/pgsqlformac/pgCocoaDB/Schema.m,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** Schema.m 20 Jun 2006 16:26:51 -0000 1.14 --- Schema.m 24 Jun 2006 16:16:33 -0000 1.15 *************** *** 30,34 **** sql = [NSString stringWithFormat:@"%s", "Select version()"]; ! results = [connection execQuery:sql]; versionField = [[[results itemAtIndex:0] fields] itemAtIndex:0]; pgVersionFound = [[NSString alloc] initWithString: [versionField value]]; --- 30,34 ---- sql = [NSString stringWithFormat:@"%s", "Select version()"]; ! results = [connection execQueryNoLog:sql]; versionField = [[[results itemAtIndex:0] fields] itemAtIndex:0]; pgVersionFound = [[NSString alloc] initWithString: [versionField value]]; *************** *** 67,71 **** - (void)dealloc { - [connection disconnect]; [connection release]; connection = nil; --- 67,70 ---- *************** *** 106,110 **** #endif ! return [connection execQuery:sql]; } --- 105,109 ---- #endif ! return [connection execQueryNoLog:sql]; } *************** *** 118,122 **** #endif ! return [connection execQuery:sql]; } --- 117,121 ---- #endif ! return [connection execQueryNoLog:sql]; } *************** *** 132,136 **** #endif ! return [connection execQuery:sql]; } --- 131,135 ---- #endif ! return [connection execQueryNoLog:sql]; } *************** *** 147,151 **** #endif ! return [connection execQuery:sql]; } --- 146,150 ---- #endif ! return [connection execQueryNoLog:sql]; } *************** *** 158,162 **** #endif ! return [connection execQuery:sql]; } --- 157,161 ---- #endif ! return [connection execQueryNoLog:sql]; } *************** *** 179,183 **** NSLog(sql); #endif ! return [connection execQuery:sql]; } --- 178,182 ---- NSLog(sql); #endif ! return [connection execQueryNoLog:sql]; } *************** *** 238,242 **** NSLog(sql); #endif ! return [connection execQuery:sql]; } --- 237,241 ---- NSLog(sql); #endif ! return [connection execQueryNoLog:sql]; } *************** *** 271,275 **** NSLog(sql); #endif ! return [connection execQuery:sql]; } --- 270,274 ---- NSLog(sql); #endif ! return [connection execQueryNoLog:sql]; } *************** *** 288,292 **** #endif ! return [connection execQuery:sql]; } --- 287,291 ---- #endif ! return [connection execQueryNoLog:sql]; } *************** *** 310,314 **** #endif ! return [connection execQuery:sql]; } --- 309,313 ---- #endif ! return [connection execQueryNoLog:sql]; } *************** *** 356,360 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] != 1) --- 355,359 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] != 1) *************** *** 449,453 **** } sql = [NSString stringWithFormat:@"SELECT pg_catalog.format_type('%s'::pg_catalog.oid, NULL)", allargtypes[i]]; ! results1 = [connection execQuery:sql]; if ([results1 count] != 1) { --- 448,452 ---- } sql = [NSString stringWithFormat:@"SELECT pg_catalog.format_type('%s'::pg_catalog.oid, NULL)", allargtypes[i]]; ! results1 = [connection execQueryNoLog:sql]; if ([results1 count] != 1) { *************** *** 478,482 **** #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 477,481 ---- #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 501,505 **** #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 500,504 ---- #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 591,595 **** #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 590,594 ---- #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 615,619 **** #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 614,618 ---- #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 663,667 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 662,666 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 683,687 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 682,686 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 702,706 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 701,705 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 723,727 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 722,726 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 742,746 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 741,745 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 761,765 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 760,764 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 780,784 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 779,783 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 799,803 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 798,802 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 820,824 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 819,823 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 839,843 **** NSLog(sql); #endif ! results = [connection execQuery:sql]; if ([results count] == 1) { --- 838,842 ---- NSLog(sql); #endif ! results = [connection execQueryNoLog:sql]; if ([results count] == 1) { *************** *** 871,875 **** #endif ! return [connection execQuery:sql]; } --- 870,874 ---- #endif ! return [connection execQueryNoLog:sql]; } Index: Connection.m =================================================================== RCS file: /cvsroot/pgsqlformac/pgCocoaDB/Connection.m,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Connection.m 17 Jun 2006 23:03:33 -0000 1.9 --- Connection.m 24 Jun 2006 16:16:33 -0000 1.10 *************** *** 9,12 **** --- 9,23 ---- #import "Connection.h" #include "libpq-fe.h" + #import <sys/time.h> + + // When a pqlib notice is raised this function gets called + void + handle_pq_notice(void *arg, const char *message) + { + Connection *theConn = (Connection *) arg; + //NSLog(@"%s", message); + [theConn appendSQLLog:[NSString stringWithFormat: @"%s\n", message]]; + } + @implementation Connection *************** *** 29,32 **** --- 40,44 ---- errorDescription = [[[[NSString alloc] init] retain] autorelease]; + sqlLog = [[NSMutableString alloc] init]; return self; *************** *** 56,61 **** errorDescription = [[NSString alloc] initWithFormat:@"%s", PQerrorMessage(pgconn)]; [[errorDescription retain] autorelease]; PQfinish(pgconn); - connected = NO; return NO; --- 68,74 ---- errorDescription = [[NSString alloc] initWithFormat:@"%s", PQerrorMessage(pgconn)]; [[errorDescription retain] autorelease]; + [self appendSQLLog:[NSMutableString stringWithFormat:@"Connection to database %@ Failed.\n", dbName]]; + PQfinish(pgconn); connected = NO; return NO; *************** *** 65,68 **** --- 78,86 ---- [errorDescription release]; errorDescription = nil; + + // set up notification + PQsetNoticeProcessor(pgconn, handle_pq_notice, self); + + [self setSQLLog:[NSMutableString stringWithFormat:@"Connected to database %@.\n", dbName]]; connected = YES; return YES; *************** *** 94,101 **** } PQfinish(pgconn); - connected = NO; - return YES; } --- 112,118 ---- } + [self appendSQLLog:[NSMutableString stringWithString:@"Disconnected from database.\n"]]; PQfinish(pgconn); connected = NO; return YES; } *************** *** 261,264 **** --- 278,282 ---- } + - (NSString *)currentDatabase { *************** *** 280,292 **** } - /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - (RecordSet *)execQuery:(NSString *)sql { PGresult* res; if (pgconn == nil) { - [errorDescription release]; errorDescription = [[NSString alloc] initWithString:@"Object is not Connected."]; [[errorDescription retain] autorelease]; --- 298,355 ---- } + - (NSMutableString *)sqlLog; + { + return [[sqlLog retain] autorelease]; + } + + + - (void)setSQLLog:(NSString *)value + { + [sqlLog release]; + sqlLog = [NSMutableString stringWithString:value]; + [sqlLog retain]; + } + + - (void)appendSQLLog:(NSString *)value + { + if (sqlLog == nil) + { + sqlLog = [NSMutableString stringWithString:value]; + [sqlLog retain]; + } + else + { + [sqlLog appendString:value]; + } + } + + + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - (RecordSet *)execQuery:(NSString *)sql { + return [self execQuery:sql logInfo:1 logSQL:0]; + } + + + - (RecordSet *)execQueryNoLog:(NSString *)sql + { + return [self execQuery:sql logInfo:0 logSQL:0]; + } + + + - (RecordSet *)execQuery:(NSString *)sql logInfo:(bool)logInfo logSQL:(bool)logSQL + { + struct timeval start, finished; + double elapsed_time; + long seconds, usecs; PGresult* res; + [errorDescription release]; + errorDescription = nil; + if (pgconn == nil) { errorDescription = [[NSString alloc] initWithString:@"Object is not Connected."]; [[errorDescription retain] autorelease]; *************** *** 294,299 **** return nil; } ! res = PQexec(pgconn, [sql cString]); switch (PQresultStatus(res)) { --- 357,375 ---- return nil; } ! gettimeofday(&start, 0); res = PQexec(pgconn, [sql cString]); + if (logInfo) + { + gettimeofday(&finished, 0); + seconds = finished.tv_sec - start.tv_sec; + usecs = finished.tv_usec - start.tv_usec; + if (usecs < 0) + { + seconds--; + usecs = usecs + 1000000; + } + elapsed_time = (double) seconds *1000.0 + (double) usecs *0.001; + [self appendSQLLog: [NSString stringWithFormat: @"Completed in %d milliseconds.\n", (long) elapsed_time]]; + } switch (PQresultStatus(res)) { *************** *** 320,324 **** } } ! return rs; break; --- 396,404 ---- } } ! if (logInfo) ! { ! [self appendSQLLog:[NSString stringWithFormat: @"%d rows affected.\n", nRecords]]; ! } ! PQclear(res); return rs; break; *************** *** 327,331 **** case PGRES_COMMAND_OK: { ! errorDescription = nil; return nil; break; --- 407,419 ---- case PGRES_COMMAND_OK: { ! if (strlen(PQcmdStatus(res))) ! { ! [self appendSQLLog:[NSString stringWithFormat:@"%s\n", PQcmdStatus(res)]]; ! } ! if ((strlen(PQcmdTuples(res)) > 0) && (logInfo)) ! { ! [self appendSQLLog:[NSString stringWithFormat: @"%s rows affected.\n", PQcmdTuples(res)]]; ! } ! PQclear(res); return nil; break; *************** *** 334,346 **** case PGRES_EMPTY_QUERY: { ! errorDescription = nil; return nil; break; } ! default: { - [errorDescription release]; errorDescription = [[NSString alloc] initWithFormat:@"PostgreSQL Error: %s", PQresultErrorMessage(res)]; --- 422,441 ---- case PGRES_EMPTY_QUERY: { ! if (logInfo) ! { ! [self appendSQLLog:@"Postgres reported Empty Query\n"]; ! } ! PQclear(res); return nil; break; } ! case PGRES_COPY_OUT: ! case PGRES_COPY_IN: ! case PGRES_BAD_RESPONSE: ! case PGRES_NONFATAL_ERROR: ! case PGRES_FATAL_ERROR: default: { errorDescription = [[NSString alloc] initWithFormat:@"PostgreSQL Error: %s", PQresultErrorMessage(res)]; *************** *** 356,359 **** --- 451,455 ---- { PGresult* res; + NSString *results; if (pgconn == nil) *************** *** 375,381 **** return nil; } ! ! return [[[[NSString alloc] initWithCString:PQcmdTuples(res)] retain] autorelease]; ! //return 0; } --- 471,481 ---- return nil; } ! if (strlen(PQcmdStatus(res))) ! { ! [self appendSQLLog:[[NSString alloc] initWithFormat:@"%s\n", PQcmdStatus(res)]]; ! } ! results = [[[[NSString alloc] initWithCString:PQcmdTuples(res)] retain] autorelease]; ! PQclear(res); ! return results; } Index: ExplorerModel.m =================================================================== RCS file: /cvsroot/pgsqlformac/pgCocoaDB/ExplorerModel.m,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ExplorerModel.m 2 Jun 2006 11:48:17 -0000 1.5 --- ExplorerModel.m 24 Jun 2006 16:16:33 -0000 1.6 *************** *** 10,13 **** --- 10,14 ---- #import "RecordSet.h" + @implementation ExplorerModel *************** *** 58,90 **** [newNode release]; - } - } - - - - (void)createViewNodes:(ExplorerNode *)aParent fromSchemaName:(NSString *)schemaName - { - RecordSet * results; - ExplorerNode * newNode; - NSString * viewName; - int i; - - results = [schema getViewNamesFromSchema:schemaName]; - for (i = 0; i < [results count]; i++) - { - newNode = [[ExplorerNode alloc] init]; - viewName = [[[[results itemAtIndex: i] fields] itemAtIndex:0] value]; - [newNode setName:viewName]; - [newNode setBaseTable:viewName]; - [newNode setBaseSchema:schemaName]; - [newNode setExplorerType:@"View Name"]; - [newNode setParent:aParent]; - [newNode setDisplayColumn2:@""]; - // do columns - [self createColumnNodes:newNode fromSchemaName:schemaName fromTableName:viewName]; - - // TODO get indexes and other ?? - [aParent addChild:newNode]; - - [newNode release]; } } --- 59,62 ---- *************** *** 133,137 **** [newNode setBaseSchema:schemaName]; [titleNode addChild:newNode]; ! [newNode release]; } --- 105,109 ---- [newNode setBaseSchema:schemaName]; [titleNode addChild:newNode]; ! [newNode release]; } *************** *** 141,144 **** --- 113,145 ---- + - (void)createViewNodes:(ExplorerNode *)aParent fromSchemaName:(NSString *)schemaName + { + RecordSet * results; + ExplorerNode * newNode; + NSString * viewName; + int i; + + results = [schema getViewNamesFromSchema:schemaName]; + for (i = 0; i < [results count]; i++) + { + newNode = [[ExplorerNode alloc] init]; + viewName = [[[[results itemAtIndex: i] fields] itemAtIndex:0] value]; + [newNode setName:viewName]; + [newNode setBaseTable:viewName]; + [newNode setBaseSchema:schemaName]; + [newNode setExplorerType:@"View Name"]; + [newNode setParent:aParent]; + [newNode setDisplayColumn2:@""]; + // do columns + [self createColumnNodes:newNode fromSchemaName:schemaName fromTableName:viewName]; + + // TODO get indexes and other ?? + [aParent addChild:newNode]; + + [newNode release]; + } + } + + - (void)createTriggerNodes:(ExplorerNode *)aParent fromSchemaName:(NSString *)schemaName fromTableName:(NSString *)tableName { Index: Connection.h =================================================================== RCS file: /cvsroot/pgsqlformac/pgCocoaDB/Connection.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Connection.h 17 Jun 2006 23:03:33 -0000 1.4 --- Connection.h 24 Jun 2006 16:16:33 -0000 1.5 *************** *** 27,30 **** --- 27,31 ---- NSString *errorDescription; + NSMutableString *sqlLog; void *pgconn; *************** *** 64,68 **** --- 65,75 ---- - (NSString *)errorDescription; + - (NSMutableString *)sqlLog; + - (void)setSQLLog:(NSString *)value; + - (void)appendSQLLog:(NSString *)value; + - (RecordSet *)execQuery:(NSString *)sql; + - (RecordSet *)execQueryNoLog:(NSString *)sql; + - (RecordSet *)execQuery:(NSString *)sql logInfo:(bool)logInfo logSQL:(bool)logSQL; - (NSString *)execCommand:(NSString *)sql; |