⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdbbasedatadict.class.php

📁 一个用PHP编写的
💻 PHP
📖 第 1 页 / 共 3 页
字号:
                $ftype = false;
                $fsize = false;
                $fprec = false;
                $fprimary = false;
                $fnoquote = false;
                $fdefts = false;
                $fdefdate = false;
                $fconstraint = false;
                $fnotnull = false;
                $funsigned = false;
                
                //-----------------
                // Parse attributes
                foreach($fld as $attr => $v) {
                    if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
                    else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
                    
                    switch($attr) {
                    case '0':
                    case 'NAME': 	$fname = $v; break;
                    case '1':
                    case 'TYPE': 	$ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
                    
                    case 'SIZE': 	
                                    $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
                                    if ($dotat === false) $fsize = $v;
                                    else {
                                        $fsize = substr($v,0,$dotat);
                                        $fprec = substr($v,$dotat+1);
                                    }
                                    break;
                    case 'UNSIGNED': $funsigned = true; break;
                    case 'AUTOINCREMENT':
                    case 'AUTO':	$fautoinc = true; $fnotnull = true; break;
                    case 'KEY':
                    case 'PRIMARY':	$fprimary = $v; $fnotnull = true; break;
                    case 'DEF':
                    case 'DEFAULT': $fdefault = $v; break;
                    case 'NOTNULL': $fnotnull = $v; break;
                    case 'NOQUOTE': $fnoquote = $v; break;
                    case 'DEFDATE': $fdefdate = $v; break;
                    case 'DEFTIMESTAMP': $fdefts = $v; break;
                    case 'CONSTRAINT': $fconstraint = $v; break;
                    } //switch
                } // foreach $fld
                
                //--------------------
                // VALIDATE FIELD INFO
                if (!strlen($fname)) {
                    if ($this->debug) print("Undefined NAME");
                    return false;
                }
                
                $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
                $fname = $this->NameQuote($fname);
                
                if (!strlen($ftype)) {
                    if ($this->debug) print("Undefined TYPE for field '$fname'");
                    return false;
                } else {
                    $ftype = strtoupper($ftype);
                }
                
                $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
                
                if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
                
                if ($fprimary) $pkey[] = $fname;
                
                // some databases do not allow blobs to have defaults
                if ($ty == 'X') $fdefault = false;
                
                //--------------------
                // CONSTRUCT FIELD SQL
                if ($fdefts) {
                    if (substr($this->connection->_type,0,5) == 'mysql') {
                        $ftype = 'TIMESTAMP';
                    } else {
                        $fdefault = $this->connection->sysTimeStamp;
                    }
                } else if ($fdefdate) {
                    if (substr($this->connection->_type,0,5) == 'mysql') {
                        $ftype = 'TIMESTAMP';
                    } else {
                        $fdefault = $this->connection->sysDate;
                    }
                } else if (strlen($fdefault) && !$fnoquote) {
                    if ($ty == 'C' or $ty == 'X' or 
                        ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault)))
                        if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ') 
                            $fdefault = trim($fdefault);
                        else if (strtolower($fdefault) != 'null') {
                            //$fdefault = $this->connection->qstr($fdefault);
							$fdefault = "'".$fdefault."'";
						}
				}
                $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
                
                if ($widespacing) $fname = str_pad($fname,24);
                $lines[$fid] = $fname.' '.$ftype.$suffix;
                
                if ($fautoinc) $this->autoIncrement = true;
            } // foreach $flds
            
            return array($lines,$pkey);
        }
		
		/**
		 * @private
		 */		
        function _IndexesSQL($tabname,$flds)
        {
            if (is_string($flds)) {
                $padding = '     ';
                $txt = $flds.$padding;
                $flds = array();
                $flds0 = PDbBaseDataDict::Lens_ParseArgs($txt,',');
                $hasparam = false;
                foreach($flds0 as $f0) {
				
					// ignore non-index fields
					if( !in_array( $f0[0], Array( "INDEX", "FULLTEXT", "HASH", "UNIQUE", "CLUSTERED", "BITMAP", "DROP" )))
						continue;
						
                    $flds[] = $f0;
                }
            }
            $lines = array();
            foreach($flds as $fld) {
				$typeStr = $fld[0];
				if( $typeStr == "INDEX" )
					$type = false;
				else
					$type = Array( $typeStr );			
									
                $fld = PDbBaseDataDict::_array_change_key_case($fld);
				$sql = $this->CreateIndexSQL( $fld[1], $tabname, $fld[2], $type );
				
				foreach( $sql as $s )
					$lines[] = $s;
			}
                        
            return($lines);
        }
				
		/**
		 * @private
		 */
        function _GetSize($ftype, $ty, $fsize, $fprec)
        {
            if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
                $ftype .= "(".$fsize;
                if (strlen($fprec)) $ftype .= ",".$fprec;
                $ftype .= ')';
            }
            return $ftype;
        }
        
        
        // return string must begin with space
        function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
        {	
            $suffix = '';
            if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
            if ($fnotnull) $suffix .= ' NOT NULL';
            if ($fconstraint) $suffix .= ' '.$fconstraint;
            return $suffix;
        }

		/**
		 * @private
		 */        
        function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
        {
            $sql = array();
            
            if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
                $sql[] = sprintf ($this->dropIndex, $idxname);
                if ( isset($idxoptions['DROP']) )
                    return $sql;
            }
            
            if ( empty ($flds) ) {
                return $sql;
            }
            
            $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
        
            $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
            
            if ( isset($idxoptions[$this->upperName]) )
                $s .= $idxoptions[$this->upperName];
            
            if ( is_array($flds) )
                $flds = implode(', ',$flds);
            $s .= '(' . $flds . ')';
            $sql[] = $s;
            
            return $sql;
        }

		/**
		 * @private
		 */        
        function _DropAutoIncrement($tabname)
        {
            return false;
        }
        
		/**
		 * @private
		 */
        function _TableSQL($tabname,$lines,$pkey,$tableoptions)
        {
            $sql = array();
            
            if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
                $sql[] = sprintf($this->dropTable,$tabname);
                if ($this->autoIncrement) {
                    $sInc = $this->_DropAutoIncrement($tabname);
                    if ($sInc) $sql[] = $sInc;
                }
                if ( isset ($tableoptions['DROP']) ) {
                    return $sql;
                }
            }
            $s = "CREATE TABLE $tabname (\n";
            $s .= implode(",\n", $lines);
            if (sizeof($pkey)>0) {
                $s .= ",\n                 PRIMARY KEY (";
                $s .= implode(", ",$pkey).")";
            }
            if (isset($tableoptions['CONSTRAINTS'])) 
                $s .= "\n".$tableoptions['CONSTRAINTS'];
            
            if (isset($tableoptions[$this->upperName.'_CONSTRAINTS'])) 
                $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
            
            $s .= "\n) ";
            if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
            $sql[] = $s;
            
            return $sql;
        }
        
        /**
         * Sanitize options, so that array elements with no keys are promoted to keys
		 *
		 * @private
         */
        function _Options($opts)
        {
            if (!is_array($opts)) return array();
            $newopts = array();
            foreach($opts as $k => $v) {
                if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
                else $newopts[strtoupper($k)] = $v;
            }
            return $newopts;
        }
        
        /**
         * Generate the SQL to modify the schema of a table
		 *
		 * @param tabname Name of the table
		 * @param flds The new table schema
		 * @param tableoptions Any extra options needed
		 * @return An array with SQL code to modify the schema of the given table
         */        
        function ChangeTableSQL($tablename, $flds, $tableoptions = false)
        {
            // check table exists
            $cols = &$this->MetaColumns($tablename);
            if ( empty($cols)) { 
                return $this->CreateTableSQL($tablename, $flds, $tableoptions);
            }
            
            // already exists, alter table instead
            list($lines,$pkey) = $this->_GenFields($flds);
            $alter = 'ALTER TABLE ' . $this->TableName($tablename);
            foreach ( $lines as $id => $v ) {
                if ( isset($cols[$id]) && is_object($cols[$id]) ) {
                
                    $flds = PDbBaseDataDict::Lens_ParseArgs($v,',');
                    
                    //  We are trying to change the size of the field, if not allowed, simply ignore the request.
                    if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) continue;	 
                 
                    $sql[] = $alter . $this->alterCol . ' ' . $v;
                } else {
                    $sql[] = $alter . $this->addCol . ' ' . $v;
                }
            }
            
            return $sql;
        }		
    }
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -