?? adodb-datadict.inc.php
字號(hào):
<?php/** V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence. Set tabs to 4 for best viewing. DOCUMENTATION: See adodb/tests/test-datadict.php for docs and examples.*//* Test script for parser*/// security - hide pathsif (!defined('ADODB_DIR')) die();function Lens_ParseTest(){$str = "`zcol ACOL` NUMBER(32,2) DEFAULT 'The \"cow\" (and Jim''s dog) jumps over the moon' PRIMARY, INTI INT AUTO DEFAULT 0, zcol2\"afs ds";print "<p>$str</p>";$a= Lens_ParseArgs($str);print "<pre>";print_r($a);print "</pre>";}if (!function_exists('ctype_alnum')) { function ctype_alnum($text) { return preg_match('/^[a-z0-9]*$/i', $text); }}//Lens_ParseTest();/** Parse arguments, treat "text" (text) and 'text' as quotation marks. To escape, use "" or '' or )) Will read in "abc def" sans quotes, as: abc def Same with 'abc def'. However if `abc def`, then will read in as `abc def` @param endstmtchar Character that indicates end of statement @param tokenchars Include the following characters in tokens apart from A-Z and 0-9 @returns 2 dimensional array containing parsed tokens.*/function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-'){ $pos = 0; $intoken = false; $stmtno = 0; $endquote = false; $tokens = array(); $tokens[$stmtno] = array(); $max = strlen($args); $quoted = false; $tokarr = array(); while ($pos < $max) { $ch = substr($args,$pos,1); switch($ch) { case ' ': case "\t": case "\n": case "\r": if (!$quoted) { if ($intoken) { $intoken = false; $tokens[$stmtno][] = implode('',$tokarr); } break; } $tokarr[] = $ch; break; case '`': if ($intoken) $tokarr[] = $ch; case '(': case ')': case '"': case "'": if ($intoken) { if (empty($endquote)) { $tokens[$stmtno][] = implode('',$tokarr); if ($ch == '(') $endquote = ')'; else $endquote = $ch; $quoted = true; $intoken = true; $tokarr = array(); } else if ($endquote == $ch) { $ch2 = substr($args,$pos+1,1); if ($ch2 == $endquote) { $pos += 1; $tokarr[] = $ch2; } else { $quoted = false; $intoken = false; $tokens[$stmtno][] = implode('',$tokarr); $endquote = ''; } } else $tokarr[] = $ch; }else { if ($ch == '(') $endquote = ')'; else $endquote = $ch; $quoted = true; $intoken = true; $tokarr = array(); if ($ch == '`') $tokarr[] = '`'; } break; default: if (!$intoken) { if ($ch == $endstmtchar) { $stmtno += 1; $tokens[$stmtno] = array(); break; } $intoken = true; $quoted = false; $endquote = false; $tokarr = array(); } if ($quoted) $tokarr[] = $ch; else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch; else { if ($ch == $endstmtchar) { $tokens[$stmtno][] = implode('',$tokarr); $stmtno += 1; $tokens[$stmtno] = array(); $intoken = false; $tokarr = array(); break; } $tokens[$stmtno][] = implode('',$tokarr); $tokens[$stmtno][] = $ch; $intoken = false; } } $pos += 1; } if ($intoken) $tokens[$stmtno][] = implode('',$tokarr); return $tokens;}class ADODB_DataDict { var $connection; var $debug = false; var $dropTable = 'DROP TABLE %s'; var $renameTable = 'RENAME TABLE %s TO %s'; var $dropIndex = 'DROP INDEX %s'; var $addCol = ' ADD'; var $alterCol = ' ALTER COLUMN'; var $dropCol = ' DROP COLUMN'; var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s'; // table, old-column, new-column, column-definitions (not used by default) var $nameRegex = '\w'; var $nameRegexBrackets = 'a-zA-Z0-9_\(\)'; var $schema = false; var $serverInfo = array(); var $autoIncrement = false; var $dataProvider; var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changetablesql var $blobSize = 100; /// any varchar/char field this size or greater is treated as a blob /// in other words, we use a text area for editting. function GetCommentSQL($table,$col) { return false; } function SetCommentSQL($table,$col,$cmt) { return false; } function MetaTables() { if (!$this->connection->IsConnected()) return array(); return $this->connection->MetaTables(); } function MetaColumns($tab, $upper=true, $schema=false) { if (!$this->connection->IsConnected()) return array(); return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema); } function MetaPrimaryKeys($tab,$owner=false,$intkey=false) { if (!$this->connection->IsConnected()) return array(); return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey); } function MetaIndexes($table, $primary = false, $owner = false) { if (!$this->connection->IsConnected()) return array(); return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner); } function MetaType($t,$len=-1,$fieldobj=false) { return ADORecordSet::MetaType($t,$len,$fieldobj); } function NameQuote($name = NULL,$allowBrackets=false) { if (!is_string($name)) { return FALSE; } $name = trim($name); if ( !is_object($this->connection) ) { return $name; } $quote = $this->connection->nameQuote; // if name is of the form `name`, quote it if ( preg_match('/^`(.+)`$/', $name, $matches) ) { return $quote . $matches[1] . $quote; } // if name contains special characters, quote it $regex = ($allowBrackets) ? $this->nameRegexBrackets : $this->nameRegex; if ( !preg_match('/^[' . $regex . ']+$/', $name) ) { return $quote . $name . $quote; } return $name; } function TableName($name) { if ( $this->schema ) { return $this->NameQuote($this->schema) .'.'. $this->NameQuote($name); } return $this->NameQuote($name); } // Executes the sql array returned by GetTableSQL and GetIndexSQL function ExecuteSQLArray($sql, $continueOnError = true) { $rez = 2; $conn = &$this->connection; $saved = $conn->debug; foreach($sql as $line) { if ($this->debug) $conn->debug = true; $ok = $conn->Execute($line); $conn->debug = $saved; if (!$ok) { if ($this->debug) ADOConnection::outp($conn->ErrorMsg()); if (!$continueOnError) return 0; $rez = 1; } } return $rez; } /** Returns the actual type given a character code. 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 */ function ActualType($meta) { return $meta; } 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. 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) { # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32) $flds[$key] = $this->NameQuote($fld,$allowBrackets=true); } return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions)); } function DropIndexSQL ($idxname, $tabname = NULL) { return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname))); } function SetSchema($schema) { $this->schema = $schema; } function AddColumnSQL($tabname, $flds) { $tabname = $this->TableName ($tabname); $sql = array(); list($lines,$pkey,$idxs) = $this->_GenFields($flds); // genfields can return FALSE at times if ($lines == null) $lines = array(); $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' '; foreach($lines as $v) { $sql[] = $alter . $v; } if (is_array($idxs)) { foreach($idxs as $idx => $idxdef) { $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); $sql = array_merge($sql, $sql_idxs); } } 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,$idxs) = $this->_GenFields($flds); // genfields can return FALSE at times if ($lines == null) $lines = array(); $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' '; foreach($lines as $v) { $sql[] = $alter . $v; } if (is_array($idxs)) { foreach($idxs as $idx => $idxdef) { $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); $sql = array_merge($sql, $sql_idxs); } } 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,$idxs) = $this->_GenFields($flds); // genfields can return FALSE at times if ($lines == null) $lines = array(); 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; } function DropTableSQL($tabname) { return array (sprintf($this->dropTable, $this->TableName($tabname))); }
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -