home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / OpenStepConversion / 3.3Headers / eoaccess / EODatabaseContext.h < prev    next >
Encoding:
Text File  |  1994-09-08  |  8.8 KB  |  220 lines

  1. // EODatabaseContext.h 
  2. // Copyright (c) 1994, NeXT Computer, Inc.  All rights reserved.
  3.  
  4. #import <foundation/NSArray.h>
  5. #import <foundation/NSData.h>
  6. #import <foundation/NSDictionary.h>
  7. #import <foundation/NSObject.h>
  8. #import <foundation/NSString.h>
  9.  
  10. @class EODatabase;
  11. @class EOQualifier;
  12. @class EOAdaptorContext;
  13. @class EOEntity;
  14.  
  15. // An EODatabaseContext represents a single transaction scope on the database
  16. // server, and determines the updating and locking stragegy used by the
  17. // database layer.  It's managed by an EODatabase object, which represents the
  18. // main connection to the server.  If the server supports multiple concurrent
  19. // transaction sessions, the EODatabase may have several EODatabaseContexts.
  20. // An EODatabaseContext may in turn have several EODatabaseChannels, which
  21. // handle actual access to the data on the server.  An EODatabaseContext by
  22. // default has no EODatabaseChannels; to create a new EODatabaseChannel,
  23. // allocate and initialize one with -initWithDatabaseContext:.  See
  24. // EODatabase.h and EODatabase Channel.h for more information.
  25. //
  26. // Not all adaptors support multiple contexts per database object.
  27. //
  28. // An EODatabaseContext creates an EOAdaptorContext when initialized, and uses
  29. // this object to actually communicate with the database server.
  30. //
  31. // UPDATING AND LOCKING STRATEGIES
  32. //
  33. // EODatabaseContext supports four updating stategies, one of which is
  34. // actually a read-only or "no update" strategy.  EODatabaseContext's updating
  35. // behavior is closely tied to the snapshot strategy of its associated
  36. // EODatabase.  As long as snapshotting is enabled for the EODatabaseContext's
  37. // parent database (the default behavior), snapshots are retained for the
  38. // duration of an Enterprise Object's existence.  Snapshot information is used
  39. // for optimistic locking and for updating only the properties of an
  40. // Enterprise Object that have changed.  The channel also uses a snapshot to
  41. // find the row in the database the needs to be updated.  This allows you to
  42. // change even the primary key of an object.
  43. //
  44. // When you disable snapshotting for the parent database, the context produces
  45. // snapshots within a given transaction to allow you to update objects fetched
  46. // within that transaction.
  47. //
  48. // The following sections describe each of the update modes in detail.
  49. // Remember, all three of the modes that support updating have the same
  50. // behavior when snapshotting is NOT set for the parent database: they only
  51. // permit updates within the current transaction.
  52. //
  53. // EOUpdateWithPessimisticLocking:
  54. //
  55. // Under pessimistic locking, objects are locked when they're selected.  This
  56. // ensures that no one else can modify the objects until your transaction ends.
  57. // However, this doesn't necessarily mean that YOUR update will succeed.
  58. // Other settings at the adaptor or physical database level can cause your
  59. // update to fail.
  60. //
  61. // If the parent database keeps snapshots, you can also update objects that
  62. // you haven't fetched during the current transaction.  Since the objects
  63. // haven't been locked, however, the optimistic strategy is used.
  64. //
  65. // EOUpdateWithOptimisticLocking:
  66. //
  67. // Under optimistic locking, objects aren't locked on the server: When you
  68. // attempt to update an object in the database, the object's snapshot is used
  69. // to ensure that the values in the corresponding database row have not
  70. // changed since the object was fetched.  As long as the snapshot matches the
  71. // database, the update is allowed to proceed.  When snapshotting is set in
  72. // the parent database, you can update objects even if they weren't fetched
  73. // in the current transaction.  EOUpdateWithOptimisticLocking is the default
  74. // update strategy.
  75. //
  76. // EOUpdateWithNoLocking:
  77. //
  78. // Under the no-locking strategy, objects are never locked.  No comparisons
  79. // are made between the snapshot and the row to ensure that the values in the
  80. // row haven't changed; they are simply overwritten with the new values.  When
  81. // snapshotting is set in the parent database, you can update objects even if
  82. // they weren't fetched in the current transaction.
  83. //
  84. // EONoUpdate:
  85. //
  86. // EONoUpdate is useful when you want to retrieve data with as little overhead
  87. // as possible and don't need to perform updates.  To reap the benefits of
  88. // this mode, snapshotting in the parent database should be turned off.  When
  89. // the context is in no update mode and the parent database is not keeping
  90. // snapshots, the least amount of overhead is used to get data into enterprise
  91. // objects and back to the client program.
  92.  
  93.  
  94. typedef enum {
  95.     EOUpdateWithOptimisticLocking,
  96.     EOUpdateWithPessimisticLocking,
  97.     EOUpdateWithNoLocking,
  98.     EONoUpdate,
  99. } EOUpdateStrategy;
  100.  
  101.  
  102. @interface EODatabaseContext:NSObject
  103. {
  104.     EODatabase *_database;
  105.     EOAdaptorContext *_adaptorContext;
  106.     EOUpdateStrategy _updateStrategy;
  107.     NSMutableArray *_uniqueStack;
  108.     NSMutableArray *_deleteStack;
  109.     NSMutableArray *_modifiedObjects;
  110.     void *_lockedObjects;
  111.     struct {
  112.     unsigned int keepsLocalSnapshots:1;
  113.     unsigned int _RESERVED:31;
  114.     } _flags;
  115. }
  116.  
  117. - initWithDatabase:(EODatabase *)database;
  118.     // Initializes a newly allocated EODatabaseChannel with database as the
  119.     // EODatabase object it works with.  Returns self, or nil if no more
  120.     // contexts can be associated with database.
  121.  
  122. - (BOOL)hasBusyChannels;
  123.     // Returns YES if the receiver has channels that have outstanding
  124.     // operations (that is, have a fetch in progress), NO otherwise.
  125.  
  126. - (EODatabase *)database;
  127.     // Returns the EODatabase that the context works with.
  128.  
  129. - (EOAdaptorContext *)adaptorContext;
  130.     // Returns the EOAdaptorContext used by the EODatabaseContext for
  131.     // communication with the database server.
  132.  
  133. - (void)setUpdateStrategy:(EOUpdateStrategy)strategy;
  134. - (EOUpdateStrategy)updateStrategy;
  135.     // These methods set/return the update strategy used by the context.  The
  136.     // default is EOUpdateWithOptimisticLocking.
  137.  
  138. - (BOOL)keepsSnapshots;
  139.     // Returns NO if the context's locking strategy is EONoUpdate and the
  140.     // parent database object keeps snapshots, YES otherwise.
  141.  
  142. @end
  143.  
  144.  
  145. // This category defines an interface for a basic transaction model
  146. // that incorporates begin, commit and rollback operations.
  147.  
  148. @interface EODatabaseContext (EOTransactions)
  149.  
  150. - (BOOL)beginTransaction;
  151.     // Attempts to begin a new transaction, nested within the current one if
  152.     // nested transaction are supported, returning YES if successful and NO
  153.     // if not.
  154.  
  155. - (BOOL)commitTransaction;
  156.     // Attempts to commit the last transaction begun, returning YES if
  157.     // successful and NO if not. This method will raise an exception if any
  158.     // of the context's channels have a fetch in progress or if the
  159.     // transactionNestingLevel of the databaseContext is out of sync with
  160.     // the adaptorContext.
  161.  
  162. - (BOOL)rollbackTransaction;
  163.     // Attempts to roll back the last transaction begun, returning YES if
  164.     // successful and NO if not. This method will raise an exception if any
  165.     // of the context's channels have a fetch in progress or if the
  166.     // transactionNestingLevel of the databaseContext is out of sync with
  167.     // the adaptorContext.
  168.  
  169. - (BOOL)canNestTransactions;
  170.     // Returns YES if the database server can nest transactions, NO
  171.     // otherwise.
  172.  
  173. - (unsigned)transactionNestingLevel; 
  174.     // Returns the number of transactions in progress. If transactions
  175.     // can nest, this number may be greater than one.
  176.  
  177. // Since databases vary widely in their ability to report transactions
  178. // generated automatically--for example through stored procedures--the
  179. // following notification methods help the caller keep the transaction objects
  180. // synchronized with the database.  These methods are also invoked by the
  181. // -beginTransaction, -commitTransaction, and -rollbackTransaction methods.
  182.  
  183. - (void)transactionDidBegin;
  184.     // Informs the receiver that the server has begun a transaction.
  185.  
  186. - (void)transactionDidCommit;
  187.     // Informs the receiver that the server has committed a transaction.
  188.  
  189. - (void)transactionDidRollback;
  190.     // Informs the receiver that the server has rolled back a transaction.
  191.  
  192. @end
  193.  
  194.  
  195. // The uniquing methods on the context are for maintaining unique objects
  196. // during a transaction.
  197.  
  198. @interface EODatabaseContext (EOUniquing)
  199.  
  200. - (void)recordObject:eo primaryKey:(NSDictionary *)key 
  201.     snapshot:(NSDictionary *)snapshot;
  202.     // If uniquing is turned on, records eo under the primary key.  Also,
  203.     // if snapshotting is turned on this method records the eo's snapshot.
  204.  
  205. - (void)forgetObject:eo;
  206.     // If uniquing is turned on, removes the eo associated with the
  207.     // primary key from the uniquing table, and if snapshotting is turned on
  208.     // the eo's snapshot is also destroyed.
  209.  
  210. - (NSDictionary *)snapshotForObject:eo;
  211.     // Returns the snapshot assocated with eo, if there is one; else
  212.     // returns nil.
  213.  
  214. - objectForPrimaryKey:(NSDictionary *)key entity:(EOEntity *)entity;
  215.     // Returns the unique eo for the given primary key and entity, or nil
  216.     // if one can't be found.
  217.  
  218. @end
  219.  
  220.