Dynamic Database Switching: Difference between revisions
From DataFlex Wiki
Jump to navigationJump to search
m
Tidying
(Adding) |
m (Tidying) |
||
Line 21: | Line 21: | ||
===The Database Switching Object=== | ===The Database Switching Object=== | ||
The fist thing that we need is a global object (global, at least partly because database connections and DataFlex file buffers are also global) to handle the actual database switching. This is probably best created in a package containing both the class definition and an object instance of that class - thus only the first "'''Use'''" statement in any program will actually include it and cause the object to be created. | |||
<source lang="vdf"> | <source lang="vdf"> | ||
Line 84: | Line 86: | ||
End_Procedure // CloseCustomerTables | End_Procedure // CloseCustomerTables | ||
// Switches database: | // Switches database - this is the only fully public message in the class: | ||
Procedure SwitchDB String sDB | Procedure SwitchDB String sDB | ||
String sCurr | String sCurr | ||
Line 98: | Line 100: | ||
End_Procedure | End_Procedure | ||
// Add a table to the list of "customer" tables: | // Add a table to the list of "customer" tables (semi-private): | ||
Procedure AddCustomerTable Integer iTable | Procedure AddCustomerTable Integer iTable | ||
Integer i | Integer i | ||
Line 131: | Line 133: | ||
===DataDictionary Sub-Class=== | ===DataDictionary Sub-Class=== | ||
We then need to define which tables will be "customer" tables - that is the ones which will exist as different versions in different databases. To do this it is most convenient to have a subclass of the Data Access DataDictionary class, | We then need to define which tables will be "customer" tables - that is the ones which will exist as different versions in different databases. To do this it is most convenient to have a subclass of the Data Access DataDictionary class, either just in the workspace, or in a [[library workspace]] if it is to be used in more than one project (placed in the AppSrc directory in either case). In this we will: | ||
* Create a property in Construct_Object | * Create a property in Construct_Object | ||
Line 139: | Line 141: | ||
Use DataDict.pkg // | Use DataDict.pkg // | ||
// We want to | // We want to ensure that the global handle is defined, so that this | ||
// class will work even if the database switch object is not in use | // class will work even if the database switch object is not in use: | ||
#IFDEF ghoDbSwitch | #IFDEF ghoDbSwitch | ||
#ELSE | #ELSE | ||
Line 147: | Line 149: | ||
#ENDIF | #ENDIF | ||
// Register the procedure we are (maybe) going to call, so | // Register the procedure we are (maybe) going to call, so class will work in | ||
// all cases | // all cases: | ||
Register_Procedure AddCustomerTable Integer iTab | Register_Procedure AddCustomerTable Integer iTab | ||
Line 165: | Line 167: | ||
// IF this is a customer table AND we have the switcher object, call | // IF this is a customer table AND we have the switcher object, call | ||
// the procedure passing it the file number: | |||
If (pbCustomerTable(Self) and | If (pbCustomerTable(Self) and ghoDbSwitch) ; | ||
Send AddCustomerTable of ghoDbSwitch (Main_File(Self)) | Send AddCustomerTable of ghoDbSwitch (Main_File(Self)) | ||
End_Procedure // End_Construct_Object | End_Procedure // End_Construct_Object | ||
Line 175: | Line 177: | ||
In the [[Visual DataFlex Studio]] you should then set this sub-class to be the super-class for all of your data dictionaries: Tools -> Configure Workspace -> Class Preferences tab -> DataDictionary and put cMyDatadictionary (or whatever you are calling yours) and cMyDataDictionary.pkg in the two columns. | In the [[Visual DataFlex Studio]] you should then set this sub-class to be the super-class for all of your data dictionaries: Tools -> Configure Workspace -> Class Preferences tab -> DataDictionary and put cMyDatadictionary (or whatever you are calling yours) and cMyDataDictionary.pkg in the two columns. | ||
===Data | ===Data Dictionaries=== | ||
In the data dictionary class file for each of your tables, you should ensure that these are based on your data dictionary sub-class, then, if the table to be marked as a "customer" table, set that property in the Construct_Object procedure: | In the data dictionary class file for each of your tables, you should ensure that these are based on your data dictionary sub-class, then, if the table to be marked as a "customer" table, set that property in the Construct_Object procedure: | ||
Line 200: | Line 202: | ||
<source lang="vdf"> | <source lang="vdf"> | ||
If | If ghoDbSwitch Send AddCustomerTable of ghoDbSwitch Customer.File_Number | ||
</source> | </source> | ||
Line 223: | Line 225: | ||
</source> | </source> | ||
This might be invoked in the login module of some applications, where the database to use is determined by the user's identity, or in the Session Manager module of an [[Ajax]] [[Web Application]], or simply in the change of a visual control (such as a Combo Form) in an application in which the user can choose between many databases to work on. | This might be invoked in the login module of some applications, where the database to use is determined by the user's identity, or in the Session Manager module of an [[Ajax]] [[Web Application]] where the database to use is mainteined in the Session record, or simply in the change of a visual control (such as a Combo Form) in an application in which the user can choose between many databases to work on. |