generator.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 930 行 · 第 1/3 页
PHP
930 行
foreach($defs as $t) { $n=0; switch (strtoupper($t->type)) { case 'INT': case 'INT2': // postgres case 'INT4': // postgres case 'INT8': // postgres case 'SERIAL4': // postgres case 'SERIAL8': // postgres case 'INTEGER': case 'TINYINT': case 'SMALLINT': case 'MEDIUMINT': case 'BIGINT': $type = DB_DATAOBJECT_INT; if ($t->len == 1) { $type += DB_DATAOBJECT_BOOL; } break; case 'REAL': case 'DOUBLE': case 'FLOAT': case 'FLOAT8': // double precision (postgres) case 'DECIMAL': case 'NUMERIC': case 'NUMBER': // oci8 $type = DB_DATAOBJECT_INT; // should really by FLOAT!!! / MONEY... break; case 'YEAR': $type = DB_DATAOBJECT_INT; break; case 'BIT': case 'BOOL': case 'BOOLEAN': $type = DB_DATAOBJECT_BOOL; // postgres needs to quote '0' if ($dbtype == 'pgsql') { $type += DB_DATAOBJECT_STR; } break; case 'STRING': case 'CHAR': case 'VARCHAR': case 'VARCHAR2': case 'TINYTEXT': case 'ENUM': case 'SET': // not really but oh well case 'TIMESTAMPTZ': // postgres case 'BPCHAR': // postgres case 'INTERVAL': // postgres (eg. '12 days') case 'CIDR': // postgres IP net spec case 'INET': // postgres IP case 'MACADDR': // postgress network Mac address. $type = DB_DATAOBJECT_STR; break; case 'TEXT': case 'MEDIUMTEXT': case 'LONGTEXT': $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_TXT; break; case 'DATE': $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE; break; case 'TIME': $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_TIME; break; case 'DATETIME': $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME; break; case 'TIMESTAMP': // do other databases use this??? $type = ($dbtype == 'mysql') ? DB_DATAOBJECT_MYSQLTIMESTAMP : DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME; break; case 'TINYBLOB': case 'BLOB': /// these should really be ignored!!!??? case 'MEDIUMBLOB': case 'LONGBLOB': case 'BYTEA': // postgres blob support.. $type = DB_DATAOBJECT_STR + DB_DATAOBJECT_BLOB; break; } if (!strlen(trim($t->name))) { continue; } if (preg_match('/not_null/i',$t->flags)) { $type += DB_DATAOBJECT_NOTNULL; } $write_ini = true; if (in_array($t->name,array('null','yes','no','true','false'))) { echo "*****************************************************************\n". "** WARNING **\n". "** Found column '{$t->name}', which is invalid in an .ini file **\n". "** This line will not be writen to the file - you will have **\n". "** define the keys()/method manually. **\n". "*****************************************************************\n"; $write_ini = false; } else { $this->_newConfig .= "{$t->name} = $type\n"; } $ret['table'][$t->name] = $type; // i've no idea if this will work well on other databases? // only use primary key or nextval(), cause the setFrom blocks you setting all key items... // if no keys exist fall back to using unique //echo "\n{$t->name} => {$t->flags}\n"; if (preg_match("/(auto_increment|nextval\()/i",rawurldecode($t->flags))) { // native sequences = 2 if ($write_ini) { $keys_out_primary .= "{$t->name} = N\n"; } $ret_keys_primary[$t->name] = 'N'; } else if (preg_match("/(primary|unique)/i",$t->flags)) { // keys.. = 1 if ($write_ini) { $keys_out_secondary .= "{$t->name} = K\n"; } $ret_keys_secondary[$t->name] = 'K'; } } $this->_newConfig .= $keys_out . (empty($keys_out_primary) ? $keys_out_secondary : $keys_out_primary); $ret['keys'] = empty($keys_out_primary) ? $ret_keys_secondary : $ret_keys_primary; if (@$_DB_DATAOBJECT['CONFIG']['debug'] > 2) { print_r(array("dump for {$this->table}", $ret)); } return $ret; } /* * building the class files * for each of the tables output a file! */ function generateClasses() { //echo "Generating Class files: \n"; $options = &PEAR::getStaticProperty('DB_DataObject','options'); $base = $options['class_location']; if (strpos($base,'%s') !== false) { $base = dirname($base); } if (!file_exists($base)) { 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']; } foreach($this->tables as $this->table) { $this->table = trim($this->table); $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))); } else { $outfilename = "{$base}/".preg_replace('/[^A-Z0-9]/i','_',ucfirst($this->table)).".php"; } $oldcontents = ''; if (file_exists($outfilename)) { // file_get_contents??? $oldcontents = implode('',file($outfilename)); } $out = $this->_generateClassTable($oldcontents); $this->debug( "writing $this->classname\n"); $fh = fopen($outfilename, "w"); fputs($fh,$out); fclose($fh); } //echo $out; } /** * class being extended (can be overridden by [DB_DataObject_Generator] extends=xxxx * * @var string * @access private */ var $_extends = 'DB_DataObject'; /** * line to use for require('DB/DataObject.php'); * * @var string * @access private */ var $_extendsFile = "DB/DataObject.php"; /** * class being generated * * @var string * @access private */ var $_className; /** * 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 */\n"; // requires $head .= "require_once '{$this->_extendsFile}';\n\n"; // add dummy class header in... // class $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)); if ($padding < 2) $padding =2; $p = str_repeat(' ',$padding) ; $options = &PEAR::getStaticProperty('DB_DataObject','options'); $var = (substr(phpversion(),0,1) > 4) ? 'public' : '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 (isset($options["database_{$this->_database}"])) { $body .= " {$var} \$_database = '{$this->_database}'; {$p}// database name (used with database_{*} config)\n"; } $var = (substr(phpversion(),0,1) > 4) ? 'public' : 'var'; 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; } $padding = (30 - strlen($t->name)); if ($padding < 2) $padding =2; $p = str_repeat(' ',$padding) ; $body .=" {$var} \${$t->name}; {$p}// {$t->type}({$t->len}) {$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.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?