Manuel Pear

Modules

Modules -- Loading and calling modules

Description

MDB2 follows a modular concept to provide functionality beyond the basic ability to send queries to the database and fetch result sets. Currently the following modules are available:

  • Datatype module (API) - handles datatype abstraction via the MDB2_Datatype_Common class

  • Extended module (API) - provides numerous high-level methods via the MDB2_Extended class

  • Function module (API) - handles SQL function abstraction via the MDB2_Function_Common class

  • Manager module (API) - handles data definition language (DDL) abstraction and schema listing via the MDB2_Manager_Common class

  • Native module (API) - handles RDBMS specific functions via the MDB2_Native_Common class

  • Reverse module (API) - handles schema reverse engineering abstraction via the MDB2_Reverse_Common class

A module is loaded using the loadModule() method. This method returns the module instance, but also stores the instance in a property. The name of the property is either the lowercased name of the module passed in as the first parameter, or optionally the non null value of the second parameter. The optional third parameter is used to differentiate modules that depend on a specific RDBMS (like the Datatype module) and those that do not (like the Extended module). The method can also be used to load custom modules that are installed.

Avertissement

The third parameter is automatically detected if it is not set. On hosts that have 'safe_mode' enabled automatic detection does however require silenced falls to fopen(). Error handling and error handlers should be configured accordingly.

Exemple 39-1. Loading a module

  1. <?php
  2. require_once 'MDB2.php';  
  3.  
  4. $dsn = 'pgsql://someuser:apasswd@localhost/thedb';  
  5. $options = array( 
  6.    'debug' => 2, 
  7.    'result_buffering' => false,  
  8. );  
  9.  
  10. $mdb2 =& MDB2::connect($dsn, $options);  
  11. if (PEAR::isError($mdb2)) { 
  12.    die($mdb2->getMessage());  
  13. }  
  14.  
  15. // ...
  16.  
  17. $mdb2->loadModule('Manager');  
  18. // specifically stating that the module that is being loaded is RDBMS independent
  19. // this works around some needless internal calls
  20. $mdb2->loadModule('Extended', null, false);  
  21.  
  22. ?> 

Exemple 39-2. Loading a custom module that is RDBMS independent

  1. <?php
  2. // ...
  3.  
  4. // file must reside in [peardir]/MDB2/MyModule.php
  5. class MDB2_MyModule extends MDB2_Module_Common  
  6. {  
  7. function myMethod()  
  8. { 
  9.    $db =& $this->getDBInstance(); 
  10.    if (PEAR::isError($db)) { 
  11.       return $db; 
  12.    } 
  13.    ...  
  14. }  
  15. }  
  16. ?>
  17.  
  18. <?php
  19. // ...
  20.  
  21. // file must reside in [peardir]/MDB2/MyModule.php
  22. $mdb2->loadModule('MyModule');  
  23.  
  24.  
  25. ?> 

Exemple 39-3. Loading a custom module that is RDBMS dependent

  1. <?php
  2. // ...
  3.  
  4. // file must reside in [peardir]/MDB2/Driver/MyRDBMSModule/pgsql.php
  5. // this is the class that would get loaded for an MDB2 PostgreSQL instance
  6. // equivalent classes for other backends would need to implemented,
  7. // potentially making use of a common base class
  8. class MDB2_Driver_MyRDBMSModule_pgsql extends MDB2_Module_Common  
  9. {  
  10. function myRDBMSMethod()  
  11. { 
  12.    $db =& $this->getDBInstance(); 
  13.    if (PEAR::isError($db)) { 
  14.       return $db; 
  15.    } 
  16.    ...  
  17. }  
  18. }  
  19. ?>
  20.  
  21. <?php
  22. // ...
  23.  
  24. // file must reside in [peardir]/MDB2/Driver/MyRDBMSModule/[phptype].php
  25. $mdb2->loadModule('MyRDBMSModule');  
  26.  
  27.  
  28. ?> 

Exemple 39-4. Using a loaded module

  1. <?php
  2. // ...
  3.  
  4. // loading into default name
  5. $mdb2->loadModule('Manager');  
  6. $tables = $mdb2->manager->listTables();  
  7.  
  8. // loading into non standard property $foo
  9. $mdb2->loadModule('Function', 'foo');  
  10. $tables = $mdb2->foo->concat($str1, $str2);  
  11.  
  12.  
  13. ?> 

On PHP5 users can also rely on overloading to load and call modules.

Exemple 39-5. Using the 'modules' option with PHP5 overloading

  1. <?php
  2. require_once 'MDB2.php';  
  3.  
  4. $dsn = 'pgsql://someuser:apasswd@localhost/thedb';  
  5. $options = array( 
  6.    'debug' => 2, 
  7.    'result_buffering' => false,  
  8. );  
  9.  
  10. $mdb2 =& MDB2::connect($dsn, $options);  
  11. if (PEAR::isError($mdb2)) { 
  12.    die($mdb2->getMessage());  
  13. }  
  14.  
  15. // ...
  16.  
  17. $module_shorthands = $mdb2->getOptions('modules');  
  18.  
  19. // use the shorthand key for the given module as a prefix for the method name
  20. // where the first letter of the original method name is uppercased
  21. $tables = $mdb2->mgListTables();  
  22.  
  23. ?> 

Exemple 39-6. Calling a method on a loaded module with PHP5 overloading

  1. <?php
  2. require_once 'MDB2.php';  
  3.  
  4. $dsn = 'pgsql://someuser:apasswd@localhost/thedb';  
  5. $options = array( 
  6.    'debug' => 2, 
  7.    'result_buffering' => false,  
  8. );  
  9.  
  10. $mdb2 =& MDB2::connect($dsn, $options);  
  11. if (PEAR::isError($mdb2)) { 
  12.    die($mdb2->getMessage());  
  13. }  
  14.  
  15. // ...
  16.  
  17. $mdb2->loadModule('Manager');  
  18.  
  19. // since the manager module is already loaded we can call the listTable() method
  20. $tables = $mdb2->manager->listTables();  
  21.  
  22. // NB: on PHP5, where __autoload() is available,
  23. // the above line can be rewritten as:
  24. $tables = $mdb2->listTables();  
  25.  
  26. ?> 

Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique