Dynamic Database Switching: Difference between revisions

From DataFlex Wiki
Jump to navigationJump to search
m
Correcting INT file entry
m (Tidying)
m (Correcting INT file entry)
 
(6 intermediate revisions by the same user not shown)
Line 16: Line 16:
Some applications - those used by the customers - will only access one database, perhaps defined by a user's login, while others - those used by administrators perhaps - will need to be able to switch between databases. However certain kinds of application, even as used by the customers, may require the capability to dynamically switch database: web applications (especially if using process pooling) are an example of this.
Some applications - those used by the customers - will only access one database, perhaps defined by a user's login, while others - those used by administrators perhaps - will need to be able to switch between databases. However certain kinds of application, even as used by the customers, may require the capability to dynamically switch database: web applications (especially if using process pooling) are an example of this.


This article will look at a technique for handling this using [[MySQL]] and the [[Mertech]] database driver for that database.
==Technique using a MySQL Database==


==Technique using MySQL Database==
This article will look at a technique for handling this using [[MySQL]] and the [[Mertech]] database driver for that database.  The techniques for other database servers may be added later.


===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.
The first 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 90: Line 90:
       String sCurr
       String sCurr
        
        
       Get_Database_Name to sCurr  // Note: Mertech docs say not supported on MySQl,
       Get_Database_Name to sCurr
                                  // but seems to work.
   
       Move (Trim(sDB)) to sDB
      Move (Uppercase(Trim(sDB)))  to sDB
       Move (Uppercase(Trim(sCurr))) to sCurr
       If (sDB = sCurr) Procedure_Return  // Don't do more than we have to.
       If (sDB = sCurr) Procedure_Return  // Don't do more than we have to.
        
        
Line 139: Line 140:
   
   
<source lang="vdf">
<source lang="vdf">
Use DataDict.pkg  //  
Use DataDict.pkg  // The Data Access DD class


// We want to ensure that the global handle is defined, so that this
// We want to ensure that the global handle is defined, so that this
Line 199: Line 200:
===Without Data Dictionaries===
===Without Data Dictionaries===


If you need to have the database switching capability for tables you are opening directly, instead of using data dictionaries for them, you can just use the same call that was place in the data dictionary sub-class, but using the table's File_Number rather than the Main_File property:
If you need to have the database switching capability for tables you are opening directly, instead of using data dictionaries for them, you can just use the same call that was placed in the data dictionary sub-class, but using the table's File_Number rather than the Main_File property:


<source lang="vdf">
<source lang="vdf">
Line 218: Line 219:
Register_Procedure SwitchDB String sDatabase
Register_Procedure SwitchDB String sDatabase
</source>
</source>
(''Possibly this too might be placed in a package file of its own, including registering the AddCustomerTable procedure as well, since effectively it has already been used three times.'')


Then switching database becomes a simple matter of invoking that procedure:
Then switching database becomes a simple matter of invoking that procedure:
Line 225: Line 228:
</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]] 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.
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 maintained 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.
 
===In the .INT files===
 
The Mertech Set_Database_Name command, on which the underlying functionality is based, will only be effective if the database is '''not''' set in the .INT file - so you should '''remove''' the line "'''DATABASE_SPACE_NAME XXXXX'''" from the appropriate .INT files (and indeed probably from all the .INT files). Then the .TD files should be deleted so that they will be freshly regenerated on next access from the .INT files.
 
[[Category:Database Connectivity]]
[[Category:Tutorials]]
[[Category:Cookbook]]

Navigation menu