generator.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 930 行 · 第 1/3 页
PHP
930 行
<?php/** * Generation tools for DB_DataObject * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Database * @package DB_DataObject * @author Alan Knowles <alan@akbkhome.com> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: Generator.php,v 1.96 2005/06/16 02:03:45 alan_k Exp $ * @link http://pear.php.net/package/DB_DataObject */ /** * * Config _$ptions * [DB_DataObject_Generator] * ; optional default = DB/DataObject.php * extends_location = * ; optional default = DB_DataObject * extends = * ; alter the extends field when updating a class (defaults to only replacing DB_DataObject) * generator_class_rewrite = ANY|specific_name // default is DB_DataObject * *//** * Needed classes */require_once 'DB/DataObject.php';//require_once('Config.php');/** * Generator class * * @package DB_DataObject */class DB_DataObject_Generator extends DB_DataObject{ /* =========================================================== */ /* Utility functions - for building db config files */ /* =========================================================== */ /** * Array of table names * * @var array * @access private */ var $tables; /** * associative array table -> array of table row objects * * @var array * @access private */ var $_definitions; /** * active table being output * * @var string * @access private */ var $table; // active tablename /** * The 'starter' = call this to start the process * * @access public * @return none */ function start() { $options = &PEAR::getStaticProperty('DB_DataObject','options'); $databases = array(); foreach($options as $k=>$v) { if (substr($k,0,9) == 'database_') { $databases[substr($k,9)] = $v; } } if (@$options['database']) { require_once 'DB.php'; $dsn = DB::parseDSN($options['database']); if (!isset($database[$dsn['database']])) { $databases[$dsn['database']] = $options['database']; } } foreach($databases as $databasename => $database) { if (!$database) { continue; } $this->debug("CREATING FOR $databasename\n"); $class = get_class($this); $t = new $class; $t->_database_dsn = $database; $t->_database = $databasename; $dsn = DB::parseDSN($database); if (($dsn['phptype'] == 'sqlite') && is_file($databasename)) { $t->_database = basename($t->_database); } $t->_createTableList(); foreach(get_class_methods($class) as $method) { if (substr($method,0,8 ) != 'generate') { continue; } $this->debug("calling $method"); $t->$method(); } } $this->debug("DONE\n\n"); } /** * Output File was config object, now just string * Used to generate the Tables * * @var string outputbuffer for table definitions * @access private */ var $_newConfig; /** * Build a list of tables; * Currently this is very Mysql Specific - ideas for more generic stiff welcome * * @access private * @return none */ function _createTableList() { $this->_connect(); $options = &PEAR::getStaticProperty('DB_DataObject','options'); $__DB= &$GLOBALS['_DB_DATAOBJECT']['CONNECTIONS'][$this->_database_dsn_md5]; // try getting a list of schema tables first. (postgres) $__DB->expectError(DB_ERROR_UNSUPPORTED); $this->tables = $__DB->getListOf('schema.tables'); $__DB->popExpect(); if (empty($this->tables) || is_a($this->tables , 'PEAR_Error')) { //if that fails fall back to clasic tables list. $this->tables = $__DB->getListOf('tables'); } if (is_a($this->tables , 'PEAR_Error')) { return PEAR::raiseError($this->tables->toString(), null, PEAR_ERROR_DIE); } // build views as well if asked to. if (!empty($options['build_views'])) { $views = $__DB->getListOf('views'); if (is_a($views,'PEAR_Error')) { return PEAR::raiseError( 'Error getting Views (check the PEAR bug database for the fix to DB), ' . $views->toString(), null, PEAR_ERROR_DIE ); } $this->tables = array_merge ($this->tables, $views); } // declare a temporary table to be filled with matching tables names $tmp_table = array(); foreach($this->tables as $table) { if (isset($options['generator_include_regex']) && !preg_match($options['generator_include_regex'],$table)) { continue; } else if (isset($options['generator_exclude_regex']) && preg_match($options['generator_exclude_regex'],$table)) { continue; } // postgres strip the schema bit from the if (!empty($options['generator_strip_schema'])) { $bits = explode('.', $table,2); $table = $bits[0]; if (count($bits) > 1) { $table = $bits[1]; } } $defs = $__DB->tableInfo($table); if (is_a($defs,'PEAR_Error')) { echo $defs->toString(); exit; } // cast all definitions to objects - as we deal with that better. foreach($defs as $def) { if (!is_array($def)) { continue; } $this->_definitions[$table][] = (object) $def; } // we find a matching table, just store it into a temporary array $tmp_table[] = $table; } // the temporary table array is now the right one (tables names matching // with regex expressions have been removed) $this->tables = $tmp_table; //print_r($this->_definitions); } /** * Auto generation of table data. * * it will output to db_oo_{database} the table definitions * * @access private * @return none */ function generateDefinitions() { $this->debug("Generating Definitions file: "); if (!$this->tables) { $this->debug("-- NO TABLES -- \n"); return; } $options = &PEAR::getStaticProperty('DB_DataObject','options'); //$this->_newConfig = new Config('IniFile'); $this->_newConfig = ''; foreach($this->tables as $this->table) { $this->_generateDefinitionsTable(); } $this->_connect(); // dont generate a schema if location is not set // it's created on the fly! if (!@$options['schema_location'] && @!$options["ini_{$this->_database}"] ) { return; } $base = @$options['schema_location']; if (isset($options["ini_{$this->_database}"])) { $file = $options["ini_{$this->_database}"]; } else { $file = "{$base}/{$this->_database}.ini"; } if (!file_exists(dirname($file))) { require_once 'System.php'; System::mkdir(array('-p','-m',0755,dirname($file))); } $this->debug("Writing ini as {$file}\n"); touch($file); //print_r($this->_newConfig); $fh = fopen($file,'w'); fwrite($fh,$this->_newConfig); fclose($fh); //$ret = $this->_newConfig->writeInput($file,false); //if (PEAR::isError($ret) ) { // return PEAR::raiseError($ret->message,null,PEAR_ERROR_DIE); // } } /** * The table geneation part * * @access private * @return tabledef and keys array. */ function _generateDefinitionsTable() { global $_DB_DATAOBJECT; $defs = $this->_definitions[$this->table]; $this->_newConfig .= "\n[{$this->table}]\n"; $keys_out = "\n[{$this->table}__keys]\n"; $keys_out_primary = ''; $keys_out_secondary = ''; if (@$_DB_DATAOBJECT['CONFIG']['debug'] > 2) { echo "TABLE STRUCTURE FOR {$this->table}\n"; print_r($defs); } $DB = $this->getDatabaseConnection(); $dbtype = $DB->phptype; $ret = array( 'table' => array(), 'keys' => array(), ); $ret_keys_primary = array(); $ret_keys_secondary = array();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?