Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.11.5

Request #3765 Database prefixes in auto generated classes
Submitted: 2005-03-10 14:56 UTC
From: therion_5 at hotmail dot com Assigned:
Status: Suspended Package: DB_DataObject
PHP Version: 4.3.10 OS: Any
Roadmaps: (Not assigned)    
Subscription  


 [2005-03-10 14:56 UTC] therion_5 at hotmail dot com
Description: ------------ If i want to use multiple databases, which has similar tables then autogenerator will overwrite class definition from first database with table from latest database. I think it should be possible to define class prefix for each database. We need two support two modified properties class_location_* and class_prefix_* database_foo = mysql://.../foo database_bar = mysql://.../bar class_prefix_foo = FOO_ class_prefix_bar = BAR_ class_location_foo = ... class_location_bar = ... ... After that it should be possible to load tables by appending to them after '@' database name $foo_user = DB_DataObject::factory('user@foo'); $bar_user = DB_DataObject::factory('user@bar'); I have changed my local DB_DataObject and Generator classes ... it works without visible side-effects. Please contact me by email and I will provide changed scripts.

Comments

 [2005-03-12 03:33 UTC] alan_k
factory should follow DBDO's format eventually, eg. $obj = DBDO::factory('databasealias','table'); (eg. if 2 args assume the first is the project/database alias name.) can you email me the patch. I'll see if I can merge it in..
 [2005-03-15 08:23 UTC] therion_5 at hotmail dot com
can't send you on alan@akbkhome.com (Delivery delayed). Please give me another email
 [2005-03-15 12:59 UTC] alan_k
I've got it ok, it would be alot easier if you used diff -u old/DataObject.php DataObject.php
 [2005-05-17 23:19 UTC] alan_k
This does need looking at again, the patches you sent had some problems that need looking at/discussing when I get time.
 [2005-05-25 08:38 UTC] therion_5 at hotmail dot com
Give me more information, i have no probs with my implementation, it works for me fine. If you want to follow DBDO guidelines then we have to think about additional argument for several methods. After that i can try to fix it in current version.
 [2005-08-24 17:15 UTC] robert at osuosl dot org
I was on here looking to recommend almost this exact patch. It would be a wonderful feature. I'm just curious what the status of this is, since the last comment was in May. Thanks.
 [2005-08-25 01:58 UTC] alan_k
This is the last patch set for reference.. - I really do need to look at this again. --- old/DataObject.php Tue Mar 15 18:41:45 2005 +++ DataObject.php Tue Mar 15 19:45:24 2005 @@ -92,6 +92,7 @@ */ require_once 'PEAR.php'; +require_once 'DB/DataObject/ConfigHelper.php'; /** * these are constants for the get_table array @@ -325,7 +326,7 @@ DB_DataObject::_loadConfig(); } - + list($class, $database) = DB_DataObject_ConfigHelper::decodeDbName($class); $key = "$k:$v"; if ($v === null) { @@ -341,7 +342,10 @@ DB_DataObject::debug("$class $key","STATIC GET - NOT IN CACHE"); } - $obj = DB_DataObject::factory(substr($class,strlen($_DB_DATAOBJECT['CONFIG']['class_prefix']))); + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); + + $obj = DB_DataObject::factory(substr($class,strlen($class_prefix))); if (PEAR::isError($obj)) { DB_DataObject::raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS); return false; @@ -2270,10 +2274,17 @@ if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } - $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? - $_DB_DATAOBJECT['CONFIG']['class_prefix'] : ''; + + list($table, $database) = DB_DataObject_ConfigHelper::decodeDbName($table); + + $p = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); + $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); - $class = (class_exists($class)) ? $class : DB_DataObject::_autoloadClass($class); + $class = (class_exists($class)) ? $class : + DB_DataObject::_autoloadClass( + DB_DataObject_ConfigHelper::encodeDbName($class, $database)); + return $class; } @@ -2303,6 +2314,11 @@ DB_DataObject::_loadConfig(); } + $database = ''; + + if(is_a($this,'DB_DataObject') && strlen($this->_database)) + $database = $this->_database; + if ($table === '') { if (is_a($this,'DB_DataObject') && strlen($this->__table)) { $table = $this->__table; @@ -2312,13 +2328,18 @@ DB_DATAOBJECT_ERROR_INVALIDARGS); } } + else { + list($table, $database) = DB_DataObject_ConfigHelper::decodeDbName($table); + } - $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? - $_DB_DATAOBJECT['CONFIG']['class_prefix'] : ''; + $p = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); - $class = (class_exists($class)) ? $class : DB_DataObject::_autoloadClass($class); + $class = (class_exists($class)) ? $class : + DB_DataObject::_autoloadClass( + DB_DataObject_ConfigHelper::encodeDbName($class, $database)); // proxy = full|light if (!$class && isset($_DB_DATAOBJECT['CONFIG']['proxy'])) { @@ -2356,16 +2377,25 @@ if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } - $table = substr($class,strlen($_DB_DATAOBJECT['CONFIG']['class_prefix'])); + + list($class, $database) = DB_DataObject_ConfigHelper::decodeDbName($class); + + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); + + $class_location = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_location',$_DB_DATAOBJECT['CONFIG'], $database); + + $table = substr($class,strlen($class_prefix)); // only include the file if it exists - and barf badly if it has parse errors :) - if (!empty($_DB_DATAOBJECT['CONFIG']['proxy']) && empty($_DB_DATAOBJECT['CONFIG']['class_location'])) { + if (!empty($_DB_DATAOBJECT['CONFIG']['proxy']) && empty($class_location)) { return false; } - if (strpos($_DB_DATAOBJECT['CONFIG']['class_location'],'%s') !== false) { - $file = sprintf($_DB_DATAOBJECT['CONFIG']['class_location'], preg_replace('/[^A-Z0-9]/i','_',ucfirst($table))); + if (strpos($class_location,'%s') !== false) { + $file = sprintf($class_location, preg_replace('/[^A-Z0-9]/i','_',ucfirst($table))); } else { - $file = $_DB_DATAOBJECT['CONFIG']['class_location'].'/'.preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)).".php"; + $file = $class_location.'/'.preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)).".php"; } if (!file_exists($file)) { --- old/DataObject/Generator.php Tue Mar 15 18:41:45 2005 +++ DataObject/Generator.php Tue Mar 15 19:52:29 2005 @@ -36,6 +36,7 @@ * Needed classes */ require_once 'DB/DataObject.php'; +require_once 'DB/DataObject/ConfigHelper.php'; //require_once('Config.php'); /** @@ -455,7 +456,13 @@ { //echo "Generating Class files: \n"; $options = &PEAR::getStaticProperty('DB_DataObject','options'); - $base = $options['class_location']; + + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$options, $this->_database); + + $base = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_location',$options, $this->_database); + if (strpos($base,'%s') !== false) { $base = dirname($base); } @@ -465,7 +472,7 @@ require_once 'System.php'; System::mkdir(array('-p',$base)); } - $class_prefix = $options['class_prefix']; + if ($extends = @$options['extends']) { $this->_extends = $extends; $this->_extendsFile = $options['extends_location']; @@ -476,8 +483,8 @@ $this->classname = $class_prefix.preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)); $i = ''; - if (strpos($options['class_location'],'%s') !== false) { - $outfilename = sprintf($options['class_location'], preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table))); + if (strpos($base,'%s') !== false) { + $outfilename = sprintf($base, preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table))); } else { $outfilename = "{$base}/".preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)).".php"; } @@ -599,7 +606,7 @@ // simple creation tools ! (static stuff!) $body .= "\n"; $body .= " /* Static get */\n"; - $body .= " function staticGet(\$k,\$v=NULL) { return DB_DataObject::staticGet('{$this->classname}',\$k,\$v); }\n"; + $body .= " function staticGet(\$k,\$v=NULL) { return DB_DataObject::staticGet('{$this->classname}@{$this->_database}',\$k,\$v); }\n"; // generate getter and setter methods $body .= $this->_generateGetters($input); @@ -732,7 +739,9 @@ $options = &PEAR::getStaticProperty('DB_DataObject','options'); - $class_prefix = $options['class_prefix']; + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$options, $database); + if ($extends = @$options['extends']) { $this->_extends = $extends; <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Andriy Vyedyeneyev <therion_5@hotmail.com> | // +----------------------------------------------------------------------+ // // Prototype Helper class for property operation. // /** * * @abstract Help with some specific property access operations. * * @version */ class DB_DataObject_ConfigHelper { /** * Decode value that can contain database name in following * encoding: * <tablename>@<databasename>, or * <classname>@<databasename> * * @return mixed array of decoded elements */ function decodeDbName($encValue) { $decValues = array(); if(strchr($encValue, '@')) { // table|class name $decValues[] = substr($encValue, 0, strpos($encValue, '@')); // databasename $decValues[] = substr($encValue, strpos($encValue, '@')+1); } else { $decValues[] = $encValue; $decValues[] = ''; } return $decValues; } /** * Encode value (tablename or classname) with database name in following * format: * <tablename>@<databasename>, or * <classname>@<databasename> * * @return encoded value */ function encodeDbName($value, $databaseName) { if(strlen($databaseName)) return $value.'@'.$databaseName; else return $value; } /** * Get config property value dependent from database name. If * such property doesn't exist method will try to get * database independent (default) property value * * @return value of config property, false if property doesn't exists */ function getDbDependentProperty($propertyName, $properties, $database) { if(isset($properties[$propertyName.'_'.$database])) return $properties[$propertyName.'_'.$database]; else if(isset($properties[$propertyName])) return $properties[$propertyName]; return ''; } }
 [2005-09-05 14:30 UTC] developer at ckiweb dot com
Just wanted to let you know that I would like this implemented. I took the diff you gave and inserted it into my code, and things look to be running pretty good. I am wondering if you are plannig to implement this in with the calling format as table@database, or as 'table', 'database' (2 arguments)? Thanksg.
 [2005-09-06 09:57 UTC] therion_5 at hotmail dot com
Yeah it would be nice to implement it like Alan suggested, but I have implemented it before i heard it from him, and after that i had no real time(need) to reimplement it again, BUT it should be really not a problem. Just one thing ... it will possibly affect existing code, because currently we are using DB_DataObject::factory('tablename') ... and switching to DB_DataObject::factory('databasealias', 'tablename'). If it's not a problem i would suggest DB_DataObject::factory('tablename','databasealias'), where second param is optional. So actually we are waiting for Alan decision, if he accept it and may be specify some requirements, then no prob we can implement it in actual version :)
 [2009-10-13 05:51 UTC] alan_k (Alan Knowles)
patch in readable format --- old/DataObject.php Tue Mar 15 18:41:45 2005 +++ DataObject.php Tue Mar 15 19:45:24 2005 @@ -92,6 +92,7 @@ */ require_once 'PEAR.php'; +require_once 'DB/DataObject/ConfigHelper.php'; /** * these are constants for the get_table array @@ -325,7 +326,7 @@ DB_DataObject::_loadConfig(); } - + list($class, $database) = DB_DataObject_ConfigHelper::decodeDbName($class); $key = "$k:$v"; if ($v === null) { @@ -341,7 +342,10 @@ DB_DataObject::debug("$class $key","STATIC GET - NOT IN CACHE"); } - $obj = DB_DataObject::factory(substr($class,strlen($_DB_DATAOBJECT['CONFIG']['class_prefix']))); + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); + + $obj = DB_DataObject::factory(substr($class,strlen($class_prefix))); if (PEAR::isError($obj)) { DB_DataObject::raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS); return false; @@ -2270,10 +2274,17 @@ if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } - $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? - $_DB_DATAOBJECT['CONFIG']['class_prefix'] : ''; + + list($table, $database) = DB_DataObject_ConfigHelper::decodeDbName($table); + + $p = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); + $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); - $class = (class_exists($class)) ? $class : DB_DataObject::_autoloadClass($class); + $class = (class_exists($class)) ? $class : + DB_DataObject::_autoloadClass( + DB_DataObject_ConfigHelper::encodeDbName($class, $database)); + return $class; } @@ -2303,6 +2314,11 @@ DB_DataObject::_loadConfig(); } + $database = ''; + + if(is_a($this,'DB_DataObject') && strlen($this->_database)) + $database = $this->_database; + if ($table === '') { if (is_a($this,'DB_DataObject') && strlen($this->__table)) { $table = $this->__table; @@ -2312,13 +2328,18 @@ DB_DATAOBJECT_ERROR_INVALIDARGS); } } + else { + list($table, $database) = DB_DataObject_ConfigHelper::decodeDbName($table); + } - $p = isset($_DB_DATAOBJECT['CONFIG']['class_prefix']) ? - $_DB_DATAOBJECT['CONFIG']['class_prefix'] : ''; + $p = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); $class = $p . preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)); - $class = (class_exists($class)) ? $class : DB_DataObject::_autoloadClass($class); + $class = (class_exists($class)) ? $class : + DB_DataObject::_autoloadClass( + DB_DataObject_ConfigHelper::encodeDbName($class, $database)); // proxy = full|light if (!$class && isset($_DB_DATAOBJECT['CONFIG']['proxy'])) { @@ -2356,16 +2377,25 @@ if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } - $table = substr($class,strlen($_DB_DATAOBJECT['CONFIG']['class_prefix'])); + + list($class, $database) = DB_DataObject_ConfigHelper::decodeDbName($class); + + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$_DB_DATAOBJECT['CONFIG'], $database); + + $class_location = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_location',$_DB_DATAOBJECT['CONFIG'], $database); + + $table = substr($class,strlen($class_prefix)); // only include the file if it exists - and barf badly if it has parse errors :) - if (!empty($_DB_DATAOBJECT['CONFIG']['proxy']) && empty($_DB_DATAOBJECT['CONFIG']['class_location'])) { + if (!empty($_DB_DATAOBJECT['CONFIG']['proxy']) && empty($class_location)) { return false; } - if (strpos($_DB_DATAOBJECT['CONFIG']['class_location'],'%s') !== false) { - $file = sprintf($_DB_DATAOBJECT['CONFIG']['class_location'], preg_replace('/[^A-Z0-9]/i','_',ucfirst($table))); + if (strpos($class_location,'%s') !== false) { + $file = sprintf($class_location, preg_replace('/[^A-Z0-9]/i','_',ucfirst($table))); } else { - $file = $_DB_DATAOBJECT['CONFIG']['class_location'].'/'.preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)).".php"; + $file = $class_location.'/'.preg_replace('/[^A-Z0-9]/i','_',ucfirst($table)).".php"; } if (!file_exists($file)) { --- old/DataObject/Generator.php Tue Mar 15 18:41:45 2005 +++ DataObject/Generator.php Tue Mar 15 19:52:29 2005 @@ -36,6 +36,7 @@ * Needed classes */ require_once 'DB/DataObject.php'; +require_once 'DB/DataObject/ConfigHelper.php'; //require_once('Config.php'); /** @@ -455,7 +456,13 @@ { //echo "Generating Class files: \n"; $options = &PEAR::getStaticProperty('DB_DataObject','options'); - $base = $options['class_location']; + + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$options, $this->_database); + + $base = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_location',$options, $this->_database); + if (strpos($base,'%s') !== false) { $base = dirname($base); } @@ -465,7 +472,7 @@ require_once 'System.php'; System::mkdir(array('-p',$base)); } - $class_prefix = $options['class_prefix']; + if ($extends = @$options['extends']) { $this->_extends = $extends; $this->_extendsFile = $options['extends_location']; @@ -476,8 +483,8 @@ $this->classname = $class_prefix.preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)); $i = ''; - if (strpos($options['class_location'],'%s') !== false) { - $outfilename = sprintf($options['class_location'], preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table))); + if (strpos($base,'%s') !== false) { + $outfilename = sprintf($base, preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table))); } else { $outfilename = "{$base}/".preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)).".php"; } @@ -599,7 +606,7 @@ // simple creation tools ! (static stuff!) $body .= "\n"; $body .= " /* Static get */\n"; - $body .= " function staticGet(\$k,\$v=NULL) { return DB_DataObject::staticGet('{$this->classname}',\$k,\$v); }\n"; + $body .= " function staticGet(\$k,\$v=NULL) { return DB_DataObject::staticGet('{$this->classname}@{$this->_database}',\$k,\$v); }\n"; // generate getter and setter methods $body .= $this->_generateGetters($input); @@ -732,7 +739,9 @@ $options = &PEAR::getStaticProperty('DB_DataObject','options'); - $class_prefix = $options['class_prefix']; + $class_prefix = DB_DataObject_ConfigHelper::getDbDependentProperty( + 'class_prefix',$options, $database); + if ($extends = @$options['extends']) { $this->_extends = $extends; <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | <a href="http://www.php.net/license/2_02.txt">http://www.php.net/license/2_02.txt</a>. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Andriy Vyedyeneyev <therion_5@hotmail.com> | // +----------------------------------------------------------------------+ // // Prototype Helper class for property operation. // /** * * @abstract Help with some specific property access operations. * * @version */ class DB_DataObject_ConfigHelper { /** * Decode value that can contain database name in following * encoding: * <tablename>@<databasename>, or * <classname>@<databasename> * * @return mixed array of decoded elements */ function decodeDbName($encValue) { $decValues = array(); if(strchr($encValue, '@')) { // table|class name $decValues[] = substr($encValue, 0, strpos($encValue, '@')); // databasename $decValues[] = substr($encValue, strpos($encValue, '@')+1); } else { $decValues[] = $encValue; $decValues[] = ''; } return $decValues; } /** * Encode value (tablename or classname) with database name in following * format: * <tablename>@<databasename>, or * <classname>@<databasename> * * @return encoded value */ function encodeDbName($value, $databaseName) { if(strlen($databaseName)) return $value.'@'.$databaseName; else return $value; } /** * Get config property value dependent from database name. If * such property doesn't exist method will try to get * database independent (default) property value * * @return value of config property, false if property doesn't exists */ function getDbDependentProperty($propertyName, $properties, $database) { if(isset($properties[$propertyName.'_'.$database])) return $properties[$propertyName.'_'.$database]; else if(isset($properties[$propertyName])) return $properties[$propertyName]; return ''; }
 [2009-10-13 05:52 UTC] alan_k (Alan Knowles)
-Status: Verified +Status: Suspended
This really needs to be an optional Load, rather than a necessary load.