// ReadListWithWhere.cpp : Defines the entry point for the console application. // #include "StdAfx.h" #include #include #include "TSServer.h" // Create these before we start because when a memory allocation // fails it will bee too late (no memory to create new stuff). static const char * memoryMessage = "Memory allocation failed."; static const std::runtime_error bad_alloc( memoryMessage ); // Define a function to be called if new fails to allocate memory. int TSNewHandler( size_t size ) { printf( "Throwing: %s\n", memoryMessage ); throw bad_alloc; return NULL; } // For a change, this example requires the user to enter the // connection and query data on the command line. The /h option // still works. void PrintUsage() { printf(" Usage:\n"); printf(" ReadListWithWhere \"where clause (w/o the word where)\"\n"); } int main(int argc, char* argv[]) { // Set the failure handler for new to be the TSNewHandler. _set_new_handler( TSNewHandler ); TSServer teamtrack; /* * Use the convenience function to initialize Winsock. */ #ifdef TS_USE_WINSOCK TSInitializeWinsock(); #endif if(argc < 6) { PrintUsage(); return 1; } if (( argc >= 2 ) && ( strcmp(argv[1],"/h") == 0 )) { PrintUsage(); return 0; } /* * Connect to the teamtrack server. */ if(teamtrack.Connect(argv[2],argv[3],argv[1]) != TS_OK) { printf("Error connecting to \"%s\" as user \"%s\"\n", argv[1], argv[2] ); if( teamtrack.GetLastErrorMessage() != NULL ) { printf("%s\n", teamtrack.GetLastErrorMessage() ); } return 2; } int tableId = atol(argv[4]); /* * Read the record list */ TSRecordList list; try { if(teamtrack.ReadRecordListWithWhere(&list, tableId, argv[5]) != TS_OK) { printf("Couldn't retrieve record list from table %d\n",tableId); if( teamtrack.GetLastErrorMessage() != NULL ) { printf("%s\n", teamtrack.GetLastErrorMessage() ); } return 3; } /* * Use the built in Dump() function to create a nicely formatted * string for outputing the record list. */ TSString s = list.StringDump(TRUE, " "); printf("\n\nRecord from table %d:\n%s\n", tableId, s.GetBuffer()); printf("Record count = %d", list.Length() ); list.EmptyAndDestroyList(); if( teamtrack.GetLastErrorMessage() != NULL ) { printf("%s\n", teamtrack.GetLastErrorMessage() ); } } catch (exception &e) { list.EmptyAndDestroyList(); printf("Caught an C++ exception:\n%s\n", e.what() ); } catch (...) { printf("Unhandled exception occurred.\n" ); } return 0; }