public function _loadConfig() { // avoid those annoying PEAR::DB strict standards warnings it causes $old = error_reporting(); error_reporting(error_reporting() & ~E_STRICT); $res = parent::_loadConfig(); // reset error_reporting($old); return $res; }
function _getDbDsn() { global $_DB_DATAOBJECT; if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } $options =& $_DB_DATAOBJECT['CONFIG']; // if the databse dsn dis defined in the object.. $dsn = isset($this->_database_dsn) ? $this->_database_dsn : null; if (!$dsn) { if (!$this->_database) { $this->_database = isset($options["table_{$this->__table}"]) ? $options["table_{$this->__table}"] : null; } if ($this->_database && !empty($options["database_{$this->_database}"])) { $dsn = $options["database_{$this->_database}"]; } else { if (!empty($options['database'])) { $dsn = $options['database']; } } } if (!$dsn) { // TRANS: Exception thrown when database name or Data Source Name could not be found. throw new Exception(_('No database name or DSN found anywhere.')); } return $dsn; }
/** * sets and returns debug level * eg. DB_DataObject::debugLevel(4); * * @param int $v level * @access public * @return none */ function debugLevel($v = null) { global $_DB_DATAOBJECT; if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } if ($v !== null) { $r = isset($_DB_DATAOBJECT['CONFIG']['debug']) ? $_DB_DATAOBJECT['CONFIG']['debug'] : 0; $_DB_DATAOBJECT['CONFIG']['debug'] = $v; return $r; } return isset($_DB_DATAOBJECT['CONFIG']['debug']) ? $_DB_DATAOBJECT['CONFIG']['debug'] : 0; }
/** * Work around memory-leak bugs... * Had to copy-paste the whole function in order to patch a couple lines of it. * Would be nice if this code was better factored. * * @param optional string name of database to assign / read * @param optional array structure of database, and keys * @param optional array table links * * @access public * @return true or PEAR:error on wrong paramenters.. or false if no file exists.. * or the array(tablename => array(column_name=>type)) if called with 1 argument.. (databasename) */ function databaseStructure() { global $_DB_DATAOBJECT; // Assignment code if ($args = func_get_args()) { if (count($args) == 1) { // this returns all the tables and their structure.. if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug("Loading Generator as databaseStructure called with args", 1); } $x = new DB_DataObject(); $x->_database = $args[0]; $this->_connect(); $DB =& $_DB_DATAOBJECT['CONNECTIONS'][$this->_database_dsn_md5]; $tables = $DB->getListOf('tables'); class_exists('DB_DataObject_Generator') ? '' : (require_once 'DB/DataObject/Generator.php'); foreach ($tables as $table) { $y = new DB_DataObject_Generator(); $y->fillTableSchema($x->_database, $table); } return $_DB_DATAOBJECT['INI'][$x->_database]; } else { $_DB_DATAOBJECT['INI'][$args[0]] = isset($_DB_DATAOBJECT['INI'][$args[0]]) ? $_DB_DATAOBJECT['INI'][$args[0]] + $args[1] : $args[1]; if (isset($args[1])) { $_DB_DATAOBJECT['LINKS'][$args[0]] = isset($_DB_DATAOBJECT['LINKS'][$args[0]]) ? $_DB_DATAOBJECT['LINKS'][$args[0]] + $args[2] : $args[2]; } return true; } } if (!$this->_database) { $this->_connect(); } // loaded already? if (!empty($_DB_DATAOBJECT['INI'][$this->_database])) { // database loaded - but this is table is not available.. if (empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table]) && !empty($_DB_DATAOBJECT['CONFIG']['proxy'])) { if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { $this->debug("Loading Generator to fetch Schema", 1); } class_exists('DB_DataObject_Generator') ? '' : (require_once 'DB/DataObject/Generator.php'); $x = new DB_DataObject_Generator(); $x->fillTableSchema($this->_database, $this->__table); } return true; } if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } // if you supply this with arguments, then it will take those // as the database and links array... $schemas = isset($_DB_DATAOBJECT['CONFIG']['schema_location']) ? array("{$_DB_DATAOBJECT['CONFIG']['schema_location']}/{$this->_database}.ini") : array(); if (isset($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"])) { $schemas = is_array($_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]) ? $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"] : explode(PATH_SEPARATOR, $_DB_DATAOBJECT['CONFIG']["ini_{$this->_database}"]); } /* BEGIN CHANGED FROM UPSTREAM */ $_DB_DATAOBJECT['INI'][$this->_database] = $this->parseIniFiles($schemas); /* END CHANGED FROM UPSTREAM */ // now have we loaded the structure.. if (!empty($_DB_DATAOBJECT['INI'][$this->_database][$this->__table])) { return true; } // - if not try building it.. if (!empty($_DB_DATAOBJECT['CONFIG']['proxy'])) { class_exists('DB_DataObject_Generator') ? '' : (require_once 'DB/DataObject/Generator.php'); $x = new DB_DataObject_Generator(); $x->fillTableSchema($this->_database, $this->__table); // should this fail!!!??? return true; } $this->debug("Cant find database schema: {$this->_database}/{$this->__table} \n" . "in links file data: " . print_r($_DB_DATAOBJECT['INI'], true), "databaseStructure", 5); // we have to die here!! - it causes chaos if we don't (including looping forever!) // Low level exception. No need for i18n as discussed with Brion. $this->raiseError("Unable to load schema for database and table (turn debugging up to 5 for full error message)", DB_DATAOBJECT_ERROR_INVALIDARGS, PEAR_ERROR_DIE); return false; }
function _loadConfig() { global $_DB_DATAOBJECT; if (!isset($_DB_DATAOBJECT['CONFIG'])) { parent::_loadConfig(); } // Set DB Driver as MDB2 $_DB_DATAOBJECT['CONFIG']['db_driver'] = 'MDB2'; }
/** * The table class geneation part - single file. * * @access private * @return none */ function _generateClassTable($input = '') { // title = expand me! $foot = ""; $head = "<?php\n/**\n * Table Definition for {$this->table}\n"; $head .= $this->derivedHookPageLevelDocBlock(); $head .= " */\n"; $head .= $this->derivedHookExtendsDocBlock(); // requires $head .= "require_once '{$this->_extendsFile}';\n\n"; // add dummy class header in... // class $head .= $this->derivedHookClassDocBlock(); $head .= "class {$this->classname} extends {$this->_extends} \n{"; $body = "\n ###START_AUTOCODE\n"; $body .= " /* the code below is auto generated do not remove the above tag */\n\n"; // table $padding = 30 - strlen($this->table); $padding = $padding < 2 ? 2 : $padding; $p = str_repeat(' ', $padding); $options =& PEAR::getStaticProperty('DB_DataObject', 'options'); $var = substr(phpversion(), 0, 1) > 4 ? 'public' : 'var'; $var = !empty($options['generator_var_keyword']) ? $options['generator_var_keyword'] : $var; $body .= " {$var} \$__table = '{$this->table}'; {$p}// table name\n"; // if we are using the option database_{databasename} = dsn // then we should add var $_database = here // as database names may not always match.. if (empty($GLOBALS['_DB_DATAOBJECT']['CONFIG'])) { DB_DataObject::_loadConfig(); } // Only include the $_database property if the omit_database_var is unset or false if (isset($options["database_{$this->_database}"]) && empty($GLOBALS['_DB_DATAOBJECT']['CONFIG']['generator_omit_database_var'])) { $body .= " {$var} \$_database = '{$this->_database}'; {$p}// database name (used with database_{*} config)\n"; } if (!empty($options['generator_novars'])) { $var = '//' . $var; } $defs = $this->_definitions[$this->table]; // show nice information! $connections = array(); $sets = array(); foreach ($defs as $t) { if (!strlen(trim($t->name))) { continue; } if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $t->name)) { echo "*****************************************************************\n" . "** WARNING COLUMN NAME UNUSABLE **\n" . "** Found column '{$t->name}', of type '{$t->type}' **\n" . "** Since this column name can't be converted to a php variable **\n" . "** name, and the whole idea of mapping would result in a mess **\n" . "** This column has been ignored... **\n" . "*****************************************************************\n"; continue; } $padding = 30 - strlen($t->name); if ($padding < 2) { $padding = 2; } $p = str_repeat(' ', $padding); $length = empty($t->len) ? '' : '(' . $t->len . ')'; $body .= " {$var} \${$t->name}; {$p}// {$t->type}{$length} {$t->flags}\n"; // can not do set as PEAR::DB table info doesnt support it. //if (substr($t->Type,0,3) == "set") // $sets[$t->Field] = "array".substr($t->Type,3); $body .= $this->derivedHookVar($t, $padding); } // THIS IS TOTALLY BORKED old FC creation // IT WILL BE REMOVED!!!!! in DataObjects 1.6 // grep -r __clone * to find all it's uses // and replace them with $x = clone($y); // due to the change in the PHP5 clone design. if (substr(phpversion(), 0, 1) < 5) { $body .= "\n"; $body .= " /* ZE2 compatibility trick*/\n"; $body .= " function __clone() { return \$this;}\n"; } // 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"; // generate getter and setter methods $body .= $this->_generateGetters($input); $body .= $this->_generateSetters($input); /* theoretically there is scope here to introduce 'list' methods based up 'xxxx_up' column!!! for heiracitcal trees.. */ // set methods //foreach ($sets as $k=>$v) { // $kk = strtoupper($k); // $body .=" function getSets{$k}() { return {$v}; }\n"; //} if (!empty($options['generator_no_ini'])) { $def = $this->_generateDefinitionsTable(); // simplify this!? $body .= $this->_generateTableFunction($def['table']); $body .= $this->_generateKeysFunction($def['keys']); $body .= $this->_generateSequenceKeyFunction($def); $body .= $this->_generateDefaultsFunction($this->table, $def['table']); } else { if (!empty($options['generator_add_defaults'])) { // I dont really like doing it this way (adding another option) // but it helps on older projects. $def = $this->_generateDefinitionsTable(); // simplify this!? $body .= $this->_generateDefaultsFunction($this->table, $def['table']); } } $body .= $this->derivedHookFunctions($input); $body .= "\n /* the code above is auto generated do not remove the tag below */"; $body .= "\n ###END_AUTOCODE\n"; // stubs.. if (!empty($options['generator_add_validate_stubs'])) { foreach ($defs as $t) { if (!strlen(trim($t->name))) { continue; } $validate_fname = 'validate' . $this->getMethodNameFromColumnName($t->name); // dont re-add it.. if (preg_match('/\\s+function\\s+' . $validate_fname . '\\s*\\(/i', $input)) { continue; } $body .= "\n function {$validate_fname}()\n {\n return false;\n }\n"; } } $foot .= "}\n"; $full = $head . $body . $foot; if (!$input) { return $full; } if (!preg_match('/(\\n|\\r\\n)\\s*###START_AUTOCODE(\\n|\\r\\n)/s', $input)) { return $full; } if (!preg_match('/(\\n|\\r\\n)\\s*###END_AUTOCODE(\\n|\\r\\n)/s', $input)) { return $full; } /* this will only replace extends DB_DataObject by default, unless use set generator_class_rewrite to ANY or a name*/ $class_rewrite = 'DB_DataObject'; $options =& PEAR::getStaticProperty('DB_DataObject', 'options'); if (empty($options['generator_class_rewrite']) || !($class_rewrite = $options['generator_class_rewrite'])) { $class_rewrite = 'DB_DataObject'; } if ($class_rewrite == 'ANY') { $class_rewrite = '[a-z_]+'; } $input = preg_replace('/(\\n|\\r\\n)class\\s*[a-z0-9_]+\\s*extends\\s*' . $class_rewrite . '\\s*(\\n|\\r\\n)\\{(\\n|\\r\\n)/si', "\nclass {$this->classname} extends {$this->_extends} \n{\n", $input); $ret = preg_replace('/(\\n|\\r\\n)\\s*###START_AUTOCODE(\\n|\\r\\n).*(\\n|\\r\\n)\\s*###END_AUTOCODE(\\n|\\r\\n)/s', $body, $input); if (!strlen($ret)) { return PEAR::raiseError("PREG_REPLACE failed to replace body, - you probably need to set these in your php.ini\n" . "pcre.backtrack_limit=1000000\n" . "pcre.recursion_limit=1000000\n", null, PEAR_ERROR_DIE); } return $ret; }
/** * sets and returns debug level * eg. DB_DataObject::debugLevel(4); * * @param int $v level * @access public * @return none */ function debugLevel($v = NULL) { global $_DB_DATAOBJECT; if (empty($_DB_DATAOBJECT['CONFIG'])) { DB_DataObject::_loadConfig(); } if ($v !== NULL) { $_DB_DATAOBJECT['CONFIG']['debug'] = $v; } return @$_DB_DATAOBJECT['CONFIG']['debug']; }