📄 pdbbasedatadict.class.php
字号:
<?php
/**
V4.54 5 Nov 2004 (c) 2000-2004 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.
*/
// compatibility stuff
if (!function_exists('ctype_alnum')) {
function ctype_alnum($text) {
return preg_match('/^[a-z0-9]*$/i', $text);
}
}
/**
* Base class for data dictionaries. Data dictionaries allow to use a meta-language to describe a database schema, which
* will be processed by the data dictionary and which will allow to automatically modify a database. This meta-language
* is independent of the database being used, so it can be used in different database environments.
*
* Data dictionaries in PDb work in exactly the same way as in ADOdb, please see more details about data dictionaries here:
* http://phplens.com/lens/adodb/docs-datadict.htm.
*
* \ingroup PDb
*/
class PDbBaseDataDict
{
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 $schema = false;
var $serverInfo = array();
var $autoIncrement = false;
var $dataProvider;
var $upperName;
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.
/**
* 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();
$tokarr = array();
$tokens[$stmtno] = array();
$max = strlen($args);
$quoted = false;
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;
}
/**
* Given a field, return its metatype based on its real type, i.e. 'VARCHAR' would return 'C',
* 'TIMESTAMP' would return 'T' and so on. This method must be implemented by child classes as each
* database has its own data types.
*
* @param t The type
* @param len the field length
* @param fieldobj
* @return Returns a string with the meta type
*/
function MetaType($t,$len=-1,$fieldobj=false)
{
// to be implemented by child classes
}
/**
* Returns information about the tables in the current connection (a list of them)
*
* @return An array with the tables
*/
function &MetaTables()
{
if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaTables();
}
/**
* Returns information (the table schema) about the given table
*
* @param tab Name of the table
* @param upper
* @param schema
* @return An array with the table schema
*/
function &MetaColumns($tab, $upper=true, $schema=false)
{
if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema);
}
/**
* Returns information about primary keys in the given table
*
* @param tab Name of the table
* @param upper
* @param schema
* @return An array with information about the primary keys
*/
function &MetaPrimaryKeys($tab,$owner=false,$intkey=false)
{
if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
}
/**
* Returns information about indexes in the given table
*
* @param tab Name of the table
* @param upper
* @param schema
* @return An array with information about the indexes
*/
function &MetaIndexes($table, $primary = false, $owner = false)
{
if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner);
}
/**
* Adds quotes around a name if necessary
*
* @param name The name that should be quoted
* @return The quoted name if it needed any quotes, or unquoted if it didn't
*/
function NameQuote($name = NULL)
{
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
if ( !preg_match('/^[' . $this->nameRegex . ']+$/', $name) ) {
return $quote . $name . $quote;
}
return $name;
}
/**
* tbd
*/
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
*
* @param sql The SQL code to be executed, passed as an array of queries
* @param continueOnError Whether to stop after an error or not
* @return True if successful or false otherwise
*/
function ExecuteSQLArray($sql, $continueOnError = true)
{
$rez = 2;
$conn = &$this->connection;
//$saved = $conn->debug;
foreach($sql as $line) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -