home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.1 (Developer) [x86] / NeXT Step 3.1 Intel dev.cdr.dmg / NextDeveloper / Examples / DatabaseKit / Binder / BinderHandler.m < prev    next >
Encoding:
Text File  |  1992-12-20  |  6.1 KB  |  243 lines

  1. /* BinderHanlder.m:
  2.  * You may freely copy, distribute, and reuse the code in this example.
  3.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  4.  * fitness for any particular use.
  5.  *
  6.  * Written by: Mai Nguyen, NeXT Developer Support
  7.  *
  8.  *
  9.  */
  10.  
  11. #import "BinderHandler.h"
  12. #import <appkit/appkit.h>
  13. #import <dbkit/dbkit.h>
  14. #import <libc.h>
  15.  
  16.     /* Define localized strings */
  17. #define FAILURE NXLocalizedString("Failure:", NULL, "Message given to user when an operation has failed.")
  18. #define CANNOT_CONNECT NXLocalizedString("Couldn't connect to database", NULL, "Message given to user to explain what fails. ")
  19. #define OK NXLocalizedString("OK", NULL, "Okay to continue ")
  20. #define NO_BINDER_OBJECT NXLocalizedString("Binder object not properly allocated", NULL, "Message given to user about a system problem.")
  21.  
  22.     /* Global to be used by Author object */
  23. static id myHandler;
  24. @implementation BinderHandler
  25.  
  26. - appDidInit:sender
  27. {
  28.     /* Connect to the Sybase Adaptor */
  29.     if ( myDatabase == nil ) {
  30.         myDatabase = [[DBDatabase alloc]init];
  31.         [myDatabase connectUsingAdaptor:"SybaseAdaptor"
  32.                                  andString:"sa@SYBASE/pubs"];
  33.         }
  34.     
  35.         /* TESTING */
  36.     if (![myDatabase isConnected]) {
  37.         NXRunAlertPanel(FAILURE, CANNOT_CONNECT,OK, NULL, NULL);
  38.         return self;
  39.         }
  40.     [myDatabase setDelegate:self];
  41.     authorEntity = [myDatabase entityNamed:"authors"];
  42.  
  43.     myHandler = self;
  44.  
  45.       return self;
  46. }
  47.  
  48. /* Stuff the binder with attributes from the authors table
  49.  */
  50. - initBinder
  51. {
  52.     
  53.     authorPrototype = [[Author alloc]init];
  54.     propertyList = [[List alloc] init];
  55.     containerList = [[List alloc] init];
  56.     myBinder = [[DBBinder alloc] init];
  57.     
  58.     [myBinder setDatabase:myDatabase];
  59.     [myBinder setProperties:propertyList];
  60.     [myBinder setRecordPrototype:authorPrototype];
  61.  
  62.     [myBinder associateRecordIvar:"first"
  63.         withProperty: [[DBExpression alloc] initForEntity:authorEntity
  64.                                             fromDescription:"au_fname"]];
  65.       [myBinder associateRecordIvar:"last" 
  66.         withProperty: [[DBExpression alloc] initForEntity:authorEntity
  67.                                             fromDescription:"au_lname"]];
  68.     [myBinder associateRecordIvar:"address"
  69.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  70.                                             fromDescription:"address"]];
  71.     [myBinder associateRecordIvar:"state"
  72.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  73.                                             fromDescription:"state"]];
  74.     [myBinder associateRecordIvar:"zip"
  75.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  76.                                             fromDescription:"zip"]];
  77.     [myBinder associateRecordIvar:"phone"
  78.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  79.                                             fromDescription:"phone"]];
  80.      
  81.     return self;
  82. }
  83.  
  84.  
  85. - free
  86. {    
  87.     if (authorPrototype)
  88.         [authorPrototype free];
  89.     if (propertyList)
  90.         [propertyList free];
  91.     if (containerList)
  92.         [containerList free];
  93.     if (myBinder)
  94.         [myBinder free];
  95.  
  96.     return [super free];
  97. }
  98.         
  99. - showAllRecords:sender
  100. {
  101.     [self initBinder];
  102.     [self findAllRecords];
  103.  
  104.     return self;
  105. }
  106.  
  107.  
  108. - findAllRecords
  109. {
  110.     int  recordCount;
  111.     
  112.     if ( myBinder == nil )    {
  113.         NXRunAlertPanel(FAILURE, NO_BINDER_OBJECT,
  114.             "Binder object not properly allocated",
  115.                 "OK", NULL, NULL);
  116.         return self;
  117.     }
  118.         
  119.     [myBinder setContainer:containerList];        
  120.     [myBinder select];
  121.     
  122.      recordCount = [containerList count];
  123.     sprintf(buf, "\n------------------------\n");
  124.     [self appendToText:buf];
  125.     sprintf(buf, "%s\t%d\n", "Total Number of Records:", recordCount);
  126.     [self appendToText:buf];
  127.       [containerList makeObjectsPerform:@selector(printSelf)];
  128.  
  129.     return self;
  130. }
  131.  
  132.  
  133. /* Build a simple SQL query with DBQualifier and retrieve the data via the
  134.  * binder object.
  135.  */
  136. - findByName:sender
  137. {
  138.     const char * name;
  139.     id             aProp;
  140.     id             theQualifier;
  141.     
  142.     name = [lastNameField stringValue];
  143.     
  144.     aProp = [[DBExpression alloc] initForEntity:authorEntity
  145.                                             fromDescription:"au_lname"];
  146.     theQualifier = [[DBQualifier allocFromZone:[self zone] ] 
  147.         initForEntity:authorEntity
  148.         fromDescription:"%@ LIKE %s", aProp, name];
  149.  
  150.         /* Must allocate a new binder object */    
  151.     [self initBinder];
  152.     [myBinder setQualifier:theQualifier];
  153.     [self findAllRecords];
  154.     [aProp free];
  155.     [theQualifier free];
  156.     return self;
  157. }
  158.  
  159.  
  160. - findByState:sender
  161. {
  162.     const char * state;
  163.     id             aProp;
  164.     id             theQualifier;
  165.         
  166.     
  167.     state = [stateField stringValue];
  168.     
  169.     aProp = [[DBExpression alloc] initForEntity:authorEntity
  170.                                             fromDescription:"state"];
  171.     theQualifier = [[DBQualifier allocFromZone:[self zone] ] 
  172.         initForEntity:authorEntity
  173.         fromDescription:"%@ LIKE %s", aProp, state];
  174.         
  175.         /* Must allocate a new binder object with a different qualifier */
  176.     [self initBinder];
  177.     [myBinder setQualifier:theQualifier];
  178.     [self findAllRecords];
  179.     [aProp free];
  180.     [theQualifier free];
  181.     return self;
  182. }
  183.  
  184.  
  185. /* Appends the string passed to the doc view of the text view
  186.  */
  187. - appendToText:(const char *)newText
  188. {
  189.     int currentLength = [theTextView textLength];
  190.     [theTextView setSel:currentLength :currentLength];
  191.     [theTextView replaceSel:newText];
  192.     [theTextView scrollSelToVisible];
  193.     return self;
  194. }
  195.  
  196.  
  197. /* DBDatabase delegate methods to log error messages and SQL queries */
  198.  
  199. - (BOOL)db:aDb willEvaluateString:(const unsigned char*)aString usingBinder:aBinder
  200. {
  201.     [self appendToText:"SQL Query:\n"];
  202.     [self appendToText:aString];
  203.     [self appendToText:"\n"];
  204.     return YES;
  205. }
  206.  
  207. @end
  208.  
  209. @implementation Author
  210.  
  211. /* copyFromZone: is VERY important...this is how the prototype
  212.  *  object is turned into records!
  213.  */
  214.  
  215. - copyFromZone:(NXZone*)z
  216. {
  217.   Author *theCopy = [[Author allocFromZone:z] init];
  218.   theCopy->first = NXCopyStringBufferFromZone(first, z);
  219.   theCopy->last = NXCopyStringBufferFromZone(last, z);
  220.   theCopy->address = NXCopyStringBufferFromZone(address, z);
  221.   theCopy->state = NXCopyStringBufferFromZone(state, z);
  222.   theCopy->zip = NXCopyStringBufferFromZone(zip, z);
  223.   theCopy->phone = NXCopyStringBufferFromZone(phone, z);
  224.   return theCopy;
  225. }
  226.  
  227.  
  228. /* Print data stored in the author object
  229.  */ 
  230. - printSelf
  231. {
  232.  char buf[200];
  233.  
  234.  sprintf(buf, "%s %s\n%s \t%s %s\n%s\n", first, last, address, state, zip,
  235.                                                                       phone);
  236.  if (myHandler != nil)
  237.      [myHandler appendToText: buf];
  238.   return self;
  239. }
  240.  
  241.  
  242. @end
  243.