home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / DatabaseKit / Binder / BinderHandler.m < prev    next >
Text File  |  1992-07-09  |  6KB  |  214 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.  
  41.     myHandler = self;
  42.  
  43.       return self;
  44. }
  45.  
  46. /* Stuff the binder with attributes from the authors table
  47.  */
  48. - initBinder
  49. {
  50.     
  51.     id authorPrototype = [[Author alloc]init];
  52.     propertyList = [[List alloc] init];
  53.  
  54.     containerList = [[List alloc] init];
  55.     
  56.     myBinder = [[DBBinder alloc] init];
  57.     
  58.     author@gty = [myDatabase entityNamed:"authors"];
  59.     [myBinder setDatabase:myDatabase];
  60.     [myBinder setProperties:propertyList];
  61.     [myBinder setRecordPrototype:authorPrototype];
  62.  
  63.     [myBinder associateRecordIvar:"first"
  64.         withProperty: [[DBExpression alloc] initForEntity:authorEntity
  65.                                             fromDescription:"au_fname"]];
  66.       [myBinder associateRecordIvar:"last" 
  67.         withProperty: [[DBExpression alloc] initForEntity:authorEntity
  68.                                             fromDescription:"au_lname"]];
  69.     [myBinder associateRecordIvar:"address"
  70.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  71.                                             fromDescription:"address"]];
  72.     [myBinder associateRecordIvar:"state"
  73.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  74.                                             fromDescription:"state"]];
  75.     [myBinder associateRecordIvar:"zip"
  76.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  77.                                             fromDescription:"zip"]];
  78.     [myBinder associateRecordIvar:"phone"
  79.         withProperty:[[DBExpression alloc] initForEntity:authorEntity
  80.                                             fromDescription:"phone"]];
  81.      
  82.     return self;
  83. }
  84.  
  85.  
  86. - showAllRecords:sender
  87. {
  88.     [self initBinder];
  89.     [self findAllRecords];
  90.  
  91.     return self;
  92. }
  93.  
  94.  
  95. - findAllRecords
  96. {
  97.     int  recordCount;
  98.     
  99.     if ( myBinder == nil )    {
  100.         NXRunAlertPanel(FAILURE, NO_BINDER_OBJECT,
  101.             "Binder object not properly allocated",
  102.                 "OK", NULL, NULL);
  103.         return self;
  104.     }
  105.         
  106.     [myBinder setContainer:containerList];        
  107.     [myBinder select];
  108.     
  109.      recordCount = [containerList count];
  110.     sprintf(buf, "\n------------------------\n");
  111.     [self appendToText:buf];
  112.     sprintf(buf, "%s\t%d\n", "Total Number of Records:", recordCount);
  113.     [self appendToText:buf];
  114.       [containerList makeObjectsPerform:@selector(printSelf)];
  115.  
  116.     return self;
  117. }
  118.  
  119.  
  120. /* Build a simple SQL query with DBQualifier and retrieve the data via the
  121.  * binder object.
  122.  */
  123. - findByName:sender
  124. {
  125.     const char * name;
  126.     id theQualifier;
  127.     id aProp;
  128.     
  129.     name = [lastNameField stringValue];
  130.     
  131.     aProp = [[DBExpression alloc] initForEntity:authorEntity
  132.                                             fromDescription:"au_lname"];
  133.     theQualifier = [[DBQualifier allocFromZone:[self zone] ] 
  134.         initForEntity:authorEntity
  135.         fromDescription:"%@ LIKE %s", aProp, name];
  136.  
  137.         /* Must allocate a new binder object */
  138.     [self initBinder];
  139.     [myBinder setQualifier:theQualifier];
  140.     [self findAllR@hds];
  141.     return self;
  142. }
  143.  
  144.  
  145. - findByState:sender
  146. {
  147.     const char * state;
  148.     id theQualifier;
  149.     id aProp;
  150.     
  151.     state = [stateField stringValue];
  152.     
  153.     aProp = [[DBExpression alloc] initForEntity:authorEntity
  154.                                             fromDescription:"state"];
  155.     theQualifier = [[DBQualifier allocFromZone:[self zone] ] 
  156.         initForEntity:authorEntity
  157.         fromDescription:"%@ LIKE %s", aProp, state];
  158.         
  159.         /* Must allocate a new binder object with a different qualifier */
  160.     [self initBinder];
  161.     [myBinder setQualifier:theQualifier];
  162.     [self findAllRecords];
  163.     return self;
  164. }
  165.  
  166.  
  167. /* Appends the string passed to the doc view of the text view
  168.  */
  169. - appendToText:(const char *)newText
  170. {
  171.     int currentLength = [theTextView textLength];
  172.     [theTextView setSel:currentLength :currentLength];
  173.     [theTextView replaceSel:newText];
  174.     [theTextView scrollSelToVisible];
  175.     return self;
  176. }
  177.  
  178. @end
  179.  
  180. @implementation Author
  181.  
  182. /* copyFromZone: is VERY important...this is how the prototype
  183.  *  object is turned into records!
  184.  */
  185.  
  186. - copyFromZone:(NXZone*)z
  187. {
  188.   Author *theCopy = [[Author allocFromZone:z] init];
  189.   theCopy->first = NXCopyStringBufferFromZone(first, z);
  190.   theCopy->last = NXCopyStringBufferFromZone(last, z);
  191.   theCopy->address = NXCopyStringBufferFromZone(address, z);
  192.   theCopy->state = NXCopyStringBufferFromZone(state, z);
  193.   theCopy->zip = NXCopyStringBufferFromZone(zip, z);
  194.   theCopy->phone = NXCopyStringBufferFromZone(phone, z);
  195.   return theCopy;
  196. }
  197.  
  198.  
  199. /* Print data stored in the author object
  200.  */ 
  201. - printSelf
  202. {
  203.  char buf[200];
  204.  
  205.  sprintf(buf, "%s %s\n%s \t%s %s\n%s\n", first, last, address, state, zip,
  206.                                                                       phone);
  207.  if (myHandler != nil)
  208.      [myHandler appendToText: buf];
  209.   return self;
  210. }
  211.  
  212.  
  213. @end
  214.