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

📄 pdbbasedatadict.class.php

📁 一个用PHP编写的
💻 PHP
📖 第 1 页 / 共 3 页
字号:
                if ($this->debug) $conn->debug = true;
                $ok = $conn->Execute($line);
                //$conn->debug = $saved;
                if (!$ok) {
                    if ($this->debug) print($conn->ErrorMsg());
                    if (!$continueOnError) return 0;
                    $rez = 1;
                }
            }
            return $rez;
        }        

		/**
		 * Given a metatype, return its equivalent database type
         *   
		 * @param meta The meta-type:
         * - C:  varchar
         * - X:  CLOB (character large object) or largest varchar size if CLOB is not supported
         * - C2: Multibyte varchar
         * - X2: Multibyte CLOB 
         * - B:  BLOB (binary large object) 
         * - D:  Date
         * - T:  Date-time 
         * - L:  Integer field suitable for storing booleans (0 or 1)
         * - I:  Integer
         * - F:  Floating point number
         * - N:  Numeric or decimal number
		 * @return Returns a string with the real type
         */        
        function ActualType($meta)
        {
            return $meta;
        }
        
		/**
		 * Returns the SQL code necessary to create the given database
		 *
		 * @param dbname Name of the new database
		 * @param options Any additional options needed to create the database, or empty as default
		 * @return An array with SQL code needed to create the database
		 */
        function CreateDatabase($dbname,$options=false)
        {
            $options = $this->_Options($options);
            $sql = array();
            
            $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
            if (isset($options[$this->upperName]))
                $s .= ' '.$options[$this->upperName];
            
            $sql[] = $s;
            return $sql;
        }
        
        /**
         * Generates the SQL to create index.
 		 *
		 * @param idxname Name of the index
		 * @param tabname Name of the table where the index will be created
		 * @param flds Names of the fields on which the index will work
		 * @param idxoptions Options needed to create the index
		 * @return Returns an array of sql strings.
         */
        function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
        {
            if (!is_array($flds)) {
                $flds = explode(',',$flds);
            }
            
            foreach($flds as $key => $fld) {
                $flds[$key] = $this->NameQuote($fld);
            }
            
            return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
        }
        
		/**
		 * Removes an index from a table
		 * 
		 * @param idxname Name of the index
		 * @param tabname Name of the table in which the index exists
		 * @return Returns an array of SQL strings needed to remove an index
		 */
        function DropIndexSQL ($idxname, $tabname = NULL)
        {
            return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname)));
        }
        
		/**
		 * tbd
		 */
        function SetSchema($schema)
        {
            $this->schema = $schema;
        }
        
		/**
		 * Returns SQL code needed to add a colum to the database table
		 * 
		 * @param tabname Name of the table
		 * @param flds An array with all the fields that will be added to the database
		 * @return An array with SQL code
        function AddColumnSQL($tabname, $flds)
        {
            $tabname = $this->TableName ($tabname);
            $sql = array();
            list($lines,$pkey) = $this->_GenFields($flds);
            $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
            foreach($lines as $v) {
                $sql[] = $alter . $v;
            }
            return $sql;
        }
        
        /**
         * Change the definition of one column
         *
         * As some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
         * to allow, recreating the table and copying the content over to the new table
         * @param string $tabname table-name
         * @param string $flds column-name and type for the changed column
         * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
         * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
         * @return array with SQL strings
         */
        function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
        {
            $tabname = $this->TableName ($tabname);
            $sql = array();
            list($lines,$pkey) = $this->_GenFields($flds);
            $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
            foreach($lines as $v) {
                $sql[] = $alter . $v;
            }
            return $sql;
        }
        
        /**
         * Rename one column
         *
         * Some DBM's can only do this together with changeing the type of the column (even if that stays the same, eg. mysql)
         * @param string $tabname table-name
         * @param string $oldcolumn column-name to be renamed
         * @param string $newcolumn new column-name
         * @param string $flds='' complete column-defintion-string like for AddColumnSQL, only used by mysql atm., default=''
         * @return array with SQL strings
         */
        function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
        {
            $tabname = $this->TableName ($tabname);
            if ($flds) {
                list($lines,$pkey) = $this->_GenFields($flds);
                list(,$first) = each($lines);
                list(,$column_def) = split("[\t ]+",$first,2);
            }
            return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
        }
            
        /**
         * Drop one column
         *
         * Some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
         * to allow, recreating the table and copying the content over to the new table
         * @param string $tabname table-name
         * @param string $flds column-name and type for the changed column
         * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
         * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
         * @return array with SQL strings
         */
        function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
        {
            $tabname = $this->TableName ($tabname);
            if (!is_array($flds)) $flds = explode(',',$flds);
            $sql = array();
            $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
            foreach($flds as $v) {
                $sql[] = $alter . $this->NameQuote($v);
            }
            return $sql;
        }

        /**
         * Generate the SQL to drop a table
		 *
		 * @param tabname Name of the table
		 * @return An array with SQL code to drop the table.
         */        
        function DropTableSQL($tabname)
        {
            return array (sprintf($this->dropTable, $this->TableName($tabname)));
        }

        /**
         * Generate the SQL to rename a table
		 *
		 * @param tabname Name of the table
		 * @return An array with SQL code to rename the table.
         */                
        function RenameTableSQL($tabname,$newname)
        {
            return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
        }	
        
        /**
         * Generate the SQL to create table. Returns an array of sql strings.
		 *
		 * @param tabname Name of the table
		 * @param flds Table schema
		 * @param tableoptions Any extra options needed to create the table
		 * @return An array with SQL code to create the table according to the schema
         */
        function CreateTableSQL($tabname, $flds, $tableoptions=false)
        {
            if (!$tableoptions) $tableoptions = array();
            
            list($lines,$pkey) = $this->_GenFields($flds, true);
            
            $taboptions = $this->_Options($tableoptions);
            $tabname = $this->TableName ($tabname);
            $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
			$idxs = $this->_IndexesSQL($tabname,$flds);
            
            $tsql = $this->_Triggers($tabname,$taboptions);
            foreach($tsql as $s) $sql[] = $s;
			foreach($idxs as $i) $sql[] = $i;
            
            return $sql;
        }
		
		/**
		 * @private
		 */		
		function _Triggers($tabname,$taboptions)
		{
			return array();
		}		
        
		/**
		 * @private
		 */
        function _array_change_key_case($an_array)
        {
            if (is_array($an_array)) {
                $new_array = array();
                foreach($an_array as $key=>$value)
                    $new_array[strtoupper($key)] = $value;
        
                   return $new_array;
           }
        
            return $an_array;
        }
        
		/**
		 * @private
		 */
        function _GenFields($flds,$widespacing=false)
        {
            if (is_string($flds)) {
                $padding = '     ';
                $txt = $flds.$padding;
                $flds = array();
                $flds0 = PDbBaseDataDict::Lens_ParseArgs($txt,',');
                $hasparam = false;
                foreach($flds0 as $f0) {
				
					// ignore index fields
					if( in_array( $f0[0], Array( "INDEX", "FULLTEXT", "HASH", "UNIQUE", "CLUSTERED", "BITMAP", "DROP" )))
						continue;
				
                    $f1 = array();
                    foreach($f0 as $token) {
                        switch (strtoupper($token)) {
                        case 'CONSTRAINT':
                        case 'DEFAULT': 
                            $hasparam = $token;
                            break;
                        default:
                            if ($hasparam) $f1[$hasparam] = $token;
                            else $f1[] = $token;
                            $hasparam = false;
                            break;
                        }
                    }
                    $flds[] = $f1;
                    
                }
            }
            $this->autoIncrement = false;
            $lines = array();
            $pkey = array();
            foreach($flds as $fld) {
			
                $fld = PDbBaseDataDict::_array_change_key_case($fld);
            
                $fname = false;
                $fdefault = false;
                $fautoinc = false;

⌨️ 快捷键说明

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