home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / DBkit / AssociationExample / Controller.m < prev    next >
Encoding:
Text File  |  1992-09-16  |  2.2 KB  |  78 lines

  1. /* Controller.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. #import    <dbkit/dbkit.h>
  10. #import <libc.h>
  11. #import "Controller.h"
  12. #import "QualifiedAssociation.h"
  13.  
  14. /* Define localized strings */
  15. #define INSTALL_MODEL NXLocalizedString("Please install OracleDemo.dbmodel into your ~/Library/Databases directory and restart.", NULL, "Notify user that SybaseDemo.dbmodel must be installed in his local Databases directory.")
  16.  
  17. @implementation Controller
  18.  
  19. /* At init time, the proper association is set up between the master fetchgroup
  20.  * and the detail fetchgroup, such that whenever a fetch happens, that fetch
  21.  * will be done with the specified qualifier.
  22.  */ 
  23. -appDidInit:sender
  24. {
  25.     id    dbDatabase;
  26.  
  27.         /* Notify the user if the database can't be found */
  28.     if ( (dbDatabase = [DBDatabase findDatabaseNamed:"OracleDemo" connect:YES]) == nil) {
  29.         NXRunAlertPanel(NULL, INSTALL_MODEL, "OK", NULL, NULL);
  30.         return self;
  31.     }
  32.     [dbDatabase setDelegate:self];
  33.     
  34.     detailFetchGroup = [departmentTable fetchGroupNamed:"employees"];
  35.  
  36.           /* Set up the new association.
  37.            Note that the caller of this function is responsible
  38.            for freeing the new association. */
  39.     newAssociation = setUpAssociation(detailFetchGroup);
  40.     dbQualifier = [[DBQualifier alloc] 
  41.             initForEntity:[detailFetchGroup entity]
  42.             fromDescription: "%p > 1000", "salary"];
  43.     [newAssociation setQualifier: dbQualifier];
  44.     [departmentTable fetchAllRecords:sender];
  45.     [theWindow makeKeyAndOrderFront:nil];
  46.     return self;
  47. }
  48.  
  49. - changeQualifier:sender
  50. {
  51.     dbQualifier = [[DBQualifier alloc] 
  52.             initForEntity:[detailFetchGroup entity]
  53.             fromDescription: "%p > %d", "salary", (int)[salaryField intValue]];
  54.     [newAssociation setQualifier: dbQualifier];
  55.     [departmentTable fetchAllRecords:sender];
  56.     return self;
  57. }
  58.  
  59. - free
  60. {
  61.     if (dbQualifier)
  62.         [dbQualifier free];
  63.     if (newAssociation)
  64.         [newAssociation free];
  65.     return [super free];
  66. }
  67.  
  68. /* For debugging purpose */
  69. - (BOOL)db:aDb willEvaluateString:(const unsigned char*)aString
  70.      usingBinder:aBinder
  71. {
  72.     fprintf(stderr, "SQL Query: %s\n", (char *)aString);
  73.     return YES;
  74. }
  75.     
  76. @end
  77.  
  78.