fix for 5.4 PHP Strict Standards: Declaration of ADODB2_mysql::_CreateSuffix() should be compatible with ADODB_DataDict::_CreateSuffix

This commit is contained in:
Ralf Becker 2012-07-20 16:30:26 +00:00
parent 3918f2606c
commit 13bcf73178
3 changed files with 217 additions and 217 deletions

View File

@ -2,14 +2,14 @@
/** /**
V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved. V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license. Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses, Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence. the BSD license will take precedence.
Set tabs to 4 for best viewing. Set tabs to 4 for best viewing.
DOCUMENTATION: DOCUMENTATION:
See adodb/tests/test-datadict.php for docs and examples. See adodb/tests/test-datadict.php for docs and examples.
*/ */
@ -42,13 +42,13 @@ if (!function_exists('ctype_alnum')) {
/** /**
Parse arguments, treat "text" (text) and 'text' as quotation marks. Parse arguments, treat "text" (text) and 'text' as quotation marks.
To escape, use "" or '' or )) To escape, use "" or '' or ))
Will read in "abc def" sans quotes, as: abc def Will read in "abc def" sans quotes, as: abc def
Same with 'abc def'. Same with 'abc def'.
However if `abc def`, then will read in as `abc def` However if `abc def`, then will read in as `abc def`
@param endstmtchar Character that indicates end of statement @param endstmtchar Character that indicates end of statement
@param tokenchars Include the following characters in tokens apart from A-Z and 0-9 @param tokenchars Include the following characters in tokens apart from A-Z and 0-9
@returns 2 dimensional array containing parsed tokens. @returns 2 dimensional array containing parsed tokens.
*/ */
function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-') function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
@ -61,7 +61,7 @@ function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
$tokens[$stmtno] = array(); $tokens[$stmtno] = array();
$max = strlen($args); $max = strlen($args);
$quoted = false; $quoted = false;
while ($pos < $max) { while ($pos < $max) {
$ch = substr($args,$pos,1); $ch = substr($args,$pos,1);
switch($ch) { switch($ch) {
@ -76,17 +76,17 @@ function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
} }
break; break;
} }
$tokarr[] = $ch; $tokarr[] = $ch;
break; break;
case '`': case '`':
if ($intoken) $tokarr[] = $ch; if ($intoken) $tokarr[] = $ch;
case '(': case '(':
case ')': case ')':
case '"': case '"':
case "'": case "'":
if ($intoken) { if ($intoken) {
if (empty($endquote)) { if (empty($endquote)) {
$tokens[$stmtno][] = implode('',$tokarr); $tokens[$stmtno][] = implode('',$tokarr);
@ -108,9 +108,9 @@ function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
} }
} else } else
$tokarr[] = $ch; $tokarr[] = $ch;
}else { }else {
if ($ch == '(') $endquote = ')'; if ($ch == '(') $endquote = ')';
else $endquote = $ch; else $endquote = $ch;
$quoted = true; $quoted = true;
@ -119,27 +119,27 @@ function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
if ($ch == '`') $tokarr[] = '`'; if ($ch == '`') $tokarr[] = '`';
} }
break; break;
default: default:
if (!$intoken) { if (!$intoken) {
if ($ch == $endstmtchar) { if ($ch == $endstmtchar) {
$stmtno += 1; $stmtno += 1;
$tokens[$stmtno] = array(); $tokens[$stmtno] = array();
break; break;
} }
$intoken = true; $intoken = true;
$quoted = false; $quoted = false;
$endquote = false; $endquote = false;
$tokarr = array(); $tokarr = array();
} }
if ($quoted) $tokarr[] = $ch; if ($quoted) $tokarr[] = $ch;
else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch; else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
else { else {
if ($ch == $endstmtchar) { if ($ch == $endstmtchar) {
$tokens[$stmtno][] = implode('',$tokarr); $tokens[$stmtno][] = implode('',$tokarr);
$stmtno += 1; $stmtno += 1;
$tokens[$stmtno] = array(); $tokens[$stmtno] = array();
@ -155,7 +155,7 @@ function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
$pos += 1; $pos += 1;
} }
if ($intoken) $tokens[$stmtno][] = implode('',$tokarr); if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
return $tokens; return $tokens;
} }
@ -164,7 +164,7 @@ class ADODB_DataDict {
var $connection; var $connection;
var $debug = false; var $debug = false;
var $dropTable = 'DROP TABLE %s'; var $dropTable = 'DROP TABLE %s';
var $renameTable = 'RENAME TABLE %s TO %s'; var $renameTable = 'RENAME TABLE %s TO %s';
var $dropIndex = 'DROP INDEX %s'; var $dropIndex = 'DROP INDEX %s';
var $addCol = ' ADD'; var $addCol = ' ADD';
var $alterCol = ' ALTER COLUMN'; var $alterCol = ' ALTER COLUMN';
@ -179,66 +179,66 @@ class ADODB_DataDict {
var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changetablesql 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 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. /// in other words, we use a text area for editting.
function GetCommentSQL($table,$col) function GetCommentSQL($table,$col)
{ {
return false; return false;
} }
function SetCommentSQL($table,$col,$cmt) function SetCommentSQL($table,$col,$cmt)
{ {
return false; return false;
} }
function MetaTables() function MetaTables()
{ {
if (!$this->connection->IsConnected()) return array(); if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaTables(); return $this->connection->MetaTables();
} }
function MetaColumns($tab, $upper=true, $schema=false) function MetaColumns($tab, $upper=true, $schema=false)
{ {
if (!$this->connection->IsConnected()) return array(); if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema); return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema);
} }
function MetaPrimaryKeys($tab,$owner=false,$intkey=false) function MetaPrimaryKeys($tab,$owner=false,$intkey=false)
{ {
if (!$this->connection->IsConnected()) return array(); if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey); return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
} }
function MetaIndexes($table, $primary = false, $owner = false) function MetaIndexes($table, $primary = false, $owner = false)
{ {
if (!$this->connection->IsConnected()) return array(); if (!$this->connection->IsConnected()) return array();
return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner); return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner);
} }
function MetaType($t,$len=-1,$fieldobj=false) function MetaType($t,$len=-1,$fieldobj=false)
{ {
return ADORecordSet::MetaType($t,$len,$fieldobj); return ADORecordSet::MetaType($t,$len,$fieldobj);
} }
function NameQuote($name = NULL,$allowBrackets=false) function NameQuote($name = NULL,$allowBrackets=false)
{ {
if (!is_string($name)) { if (!is_string($name)) {
return FALSE; return FALSE;
} }
$name = trim($name); $name = trim($name);
if ( !is_object($this->connection) ) { if ( !is_object($this->connection) ) {
return $name; return $name;
} }
$quote = $this->connection->nameQuote; $quote = $this->connection->nameQuote;
// if name is of the form `name`, quote it // if name is of the form `name`, quote it
if ( preg_match('/^`(.+)`$/', $name, $matches) ) { if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
return $quote . $matches[1] . $quote; return $quote . $matches[1] . $quote;
} }
// if brackets are allowed, quote only the rest, // if brackets are allowed, quote only the rest,
// to allow to limit indexes on colums, eg "column(32)" // to allow to limit indexes on colums, eg "column(32)"
if ($allowBrackets && preg_match('/^(.*) *(\(\d+\))$/',$name,$matches)) { if ($allowBrackets && preg_match('/^(.*) *(\(\d+\))$/',$name,$matches)) {
return $quote . $matches[1] . $quote . ' '. $matches[2]; return $quote . $matches[1] . $quote . ' '. $matches[2];
@ -247,17 +247,17 @@ class ADODB_DataDict {
// not used stock ADOdb code, which only quotes names with special chars, // not used stock ADOdb code, which only quotes names with special chars,
// which does not help with column names using reserved words, eg. "timestamp" in phpfreechat // which does not help with column names using reserved words, eg. "timestamp" in phpfreechat
// if name contains special characters, quote it // if name contains special characters, quote it
$regex = ($allowBrackets) ? $this->nameRegexBrackets : $this->nameRegex; $regex = ($allowBrackets) ? $this->nameRegexBrackets : $this->nameRegex;
if ( !preg_match('/^[' . $regex . ']+$/', $name) ) { if ( !preg_match('/^[' . $regex . ']+$/', $name) ) {
return $quote . $name . $quote; return $quote . $name . $quote;
} }
return $name; return $name;
} }
function TableName($name) function TableName($name)
{ {
if ( $this->schema ) { if ( $this->schema ) {
@ -265,7 +265,7 @@ class ADODB_DataDict {
} }
return $this->NameQuote($name); return $this->NameQuote($name);
} }
// Executes the sql array returned by GetTableSQL and GetIndexSQL // Executes the sql array returned by GetTableSQL and GetIndexSQL
function ExecuteSQLArray($sql, $continueOnError = true) function ExecuteSQLArray($sql, $continueOnError = true)
{ {
@ -273,7 +273,7 @@ class ADODB_DataDict {
$conn = &$this->connection; $conn = &$this->connection;
$saved = $conn->debug; $saved = $conn->debug;
foreach($sql as $line) { foreach($sql as $line) {
if ($this->debug) $conn->debug = true; if ($this->debug) $conn->debug = true;
$ok = $conn->Execute($line); $ok = $conn->Execute($line);
$conn->debug = $saved; $conn->debug = $saved;
@ -285,43 +285,43 @@ class ADODB_DataDict {
} }
return $rez; return $rez;
} }
/* /*
Returns the actual type given a character code. Returns the actual type given a character code.
C: varchar C: varchar
X: CLOB (character large object) or largest varchar size if CLOB is not supported X: CLOB (character large object) or largest varchar size if CLOB is not supported
C2: Multibyte varchar C2: Multibyte varchar
X2: Multibyte CLOB X2: Multibyte CLOB
B: BLOB (binary large object) B: BLOB (binary large object)
D: Date D: Date
T: Date-time T: Date-time
L: Integer field suitable for storing booleans (0 or 1) L: Integer field suitable for storing booleans (0 or 1)
I: Integer I: Integer
F: Floating point number F: Floating point number
N: Numeric or decimal number N: Numeric or decimal number
*/ */
function ActualType($meta) function ActualType($meta)
{ {
return $meta; return $meta;
} }
function CreateDatabase($dbname,$options=false) function CreateDatabase($dbname,$options=false)
{ {
$options = $this->_Options($options); $options = $this->_Options($options);
$sql = array(); $sql = array();
$s = 'CREATE DATABASE ' . $this->NameQuote($dbname); $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
if (isset($options[$this->upperName])) if (isset($options[$this->upperName]))
$s .= ' '.$options[$this->upperName]; $s .= ' '.$options[$this->upperName];
$sql[] = $s; $sql[] = $s;
return $sql; return $sql;
} }
/* /*
Generates the SQL to create index. Returns an array of sql strings. Generates the SQL to create index. Returns an array of sql strings.
*/ */
@ -330,25 +330,25 @@ class ADODB_DataDict {
if (!is_array($flds)) { if (!is_array($flds)) {
$flds = explode(',',$flds); $flds = explode(',',$flds);
} }
foreach($flds as $key => $fld) { foreach($flds as $key => $fld) {
# some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32) # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
$flds[$key] = $this->NameQuote($fld,$allowBrackets=true); $flds[$key] = $this->NameQuote($fld,$allowBrackets=true);
} }
return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions)); return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
} }
function DropIndexSQL ($idxname, $tabname = NULL) function DropIndexSQL ($idxname, $tabname = NULL)
{ {
return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname))); return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname)));
} }
function SetSchema($schema) function SetSchema($schema)
{ {
$this->schema = $schema; $this->schema = $schema;
} }
function AddColumnSQL($tabname, $flds) function AddColumnSQL($tabname, $flds)
{ {
$tabname = $this->TableName ($tabname); $tabname = $this->TableName ($tabname);
@ -360,7 +360,7 @@ class ADODB_DataDict {
} }
return $sql; return $sql;
} }
/** /**
* Change the definition of one column * Change the definition of one column
* *
@ -383,7 +383,7 @@ class ADODB_DataDict {
} }
return $sql; return $sql;
} }
/** /**
* Rename one column * Rename one column
* *
@ -404,7 +404,7 @@ class ADODB_DataDict {
} }
return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def)); return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
} }
/** /**
* Drop one column * Drop one column
* *
@ -427,36 +427,36 @@ class ADODB_DataDict {
} }
return $sql; return $sql;
} }
function DropTableSQL($tabname) function DropTableSQL($tabname)
{ {
return array (sprintf($this->dropTable, $this->TableName($tabname))); return array (sprintf($this->dropTable, $this->TableName($tabname)));
} }
function RenameTableSQL($tabname,$newname) function RenameTableSQL($tabname,$newname)
{ {
return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname))); return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
} }
/* /*
Generate the SQL to create table. Returns an array of sql strings. Generate the SQL to create table. Returns an array of sql strings.
*/ */
function CreateTableSQL($tabname, $flds, $tableoptions=false) function CreateTableSQL($tabname, $flds, $tableoptions=false)
{ {
if (!$tableoptions) $tableoptions = array(); if (!$tableoptions) $tableoptions = array();
list($lines,$pkey) = $this->_GenFields($flds, true); list($lines,$pkey) = $this->_GenFields($flds, true);
$taboptions = $this->_Options($tableoptions); $taboptions = $this->_Options($tableoptions);
$tabname = $this->TableName ($tabname); $tabname = $this->TableName ($tabname);
$sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions); $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
$tsql = $this->_Triggers($tabname,$taboptions); $tsql = $this->_Triggers($tabname,$taboptions);
foreach($tsql as $s) $sql[] = $s; foreach($tsql as $s) $sql[] = $s;
return $sql; return $sql;
} }
function _GenFields($flds,$widespacing=false) function _GenFields($flds,$widespacing=false)
{ {
if (is_string($flds)) { if (is_string($flds)) {
@ -470,7 +470,7 @@ class ADODB_DataDict {
foreach($f0 as $token) { foreach($f0 as $token) {
switch (strtoupper($token)) { switch (strtoupper($token)) {
case 'CONSTRAINT': case 'CONSTRAINT':
case 'DEFAULT': case 'DEFAULT':
$hasparam = $token; $hasparam = $token;
break; break;
default: default:
@ -481,7 +481,7 @@ class ADODB_DataDict {
} }
} }
$flds[] = $f1; $flds[] = $f1;
} }
} }
$this->autoIncrement = false; $this->autoIncrement = false;
@ -489,7 +489,7 @@ class ADODB_DataDict {
$pkey = array(); $pkey = array();
foreach($flds as $fld) { foreach($flds as $fld) {
$fld = _array_change_key_case($fld); $fld = _array_change_key_case($fld);
$fname = false; $fname = false;
$fdefault = false; $fdefault = false;
$fautoinc = false; $fautoinc = false;
@ -503,20 +503,20 @@ class ADODB_DataDict {
$fconstraint = false; $fconstraint = false;
$fnotnull = false; $fnotnull = false;
$funsigned = false; $funsigned = false;
//----------------- //-----------------
// Parse attributes // Parse attributes
foreach($fld as $attr => $v) { foreach($fld as $attr => $v) {
if ($attr == 2 && is_numeric($v)) $attr = 'SIZE'; if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v); else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
switch($attr) { switch($attr) {
case '0': case '0':
case 'NAME': $fname = $v; break; case 'NAME': $fname = $v; break;
case '1': case '1':
case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break; case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
case 'SIZE': case 'SIZE':
$dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,','); $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
if ($dotat === false) $fsize = $v; if ($dotat === false) $fsize = $v;
else { else {
@ -538,33 +538,33 @@ class ADODB_DataDict {
case 'CONSTRAINT': $fconstraint = $v; break; case 'CONSTRAINT': $fconstraint = $v; break;
} //switch } //switch
} // foreach $fld } // foreach $fld
//-------------------- //--------------------
// VALIDATE FIELD INFO // VALIDATE FIELD INFO
if (!strlen($fname)) { if (!strlen($fname)) {
if ($this->debug) ADOConnection::outp("Undefined NAME"); if ($this->debug) ADOConnection::outp("Undefined NAME");
return false; return false;
} }
$fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname)); $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
$fname = $this->NameQuote($fname); $fname = $this->NameQuote($fname);
if (!strlen($ftype)) { if (!strlen($ftype)) {
if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'"); if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'");
return false; return false;
} else { } else {
$ftype = strtoupper($ftype); $ftype = strtoupper($ftype);
} }
$ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec); $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
if ($fprimary) $pkey[] = $fname; if ($fprimary) $pkey[] = $fname;
// some databases do not allow blobs to have defaults // some databases do not allow blobs to have defaults
if ($ty == 'X') $fdefault = false; if ($ty == 'X') $fdefault = false;
//-------------------- //--------------------
// CONSTRUCT FIELD SQL // CONSTRUCT FIELD SQL
if ($fdefts) { if ($fdefts) {
@ -580,20 +580,20 @@ class ADODB_DataDict {
$fdefault = $this->connection->sysDate; $fdefault = $this->connection->sysDate;
} }
} else if ($fdefault !== false && !$fnoquote) } else if ($fdefault !== false && !$fnoquote)
if ($ty == 'C' or $ty == 'X' or if ($ty == 'C' or $ty == 'X' or
( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault)))
if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ') if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
$fdefault = trim($fdefault); $fdefault = trim($fdefault);
else if (strtolower($fdefault) != 'null') else if (strtolower($fdefault) != 'null')
$fdefault = $this->connection->qstr($fdefault); $fdefault = $this->connection->qstr($fdefault);
$suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned); $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
if ($widespacing) $fname = str_pad($fname,24); if ($widespacing) $fname = str_pad($fname,24);
$lines[$fid] = $fname.' '.$ftype.$suffix; $lines[$fid] = $fname.' '.$ftype.$suffix;
if ($fautoinc) $this->autoIncrement = true; if ($fautoinc) $this->autoIncrement = true;
} // foreach $flds } // foreach $flds
return array($lines,$pkey); return array($lines,$pkey);
} }
/* /*
@ -610,56 +610,56 @@ class ADODB_DataDict {
} }
return $ftype; return $ftype;
} }
// return string must begin with space // return string must begin with space
function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint) function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
{ {
$suffix = ''; $suffix = '';
if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault"; if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
if ($fnotnull) $suffix .= ' NOT NULL'; if ($fnotnull) $suffix .= ' NOT NULL';
if ($fconstraint) $suffix .= ' '.$fconstraint; if ($fconstraint) $suffix .= ' '.$fconstraint;
return $suffix; return $suffix;
} }
function _IndexSQL($idxname, $tabname, $flds, $idxoptions) function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
{ {
$sql = array(); $sql = array();
if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) { if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
$sql[] = sprintf ($this->dropIndex, $idxname); $sql[] = sprintf ($this->dropIndex, $idxname);
if ( isset($idxoptions['DROP']) ) if ( isset($idxoptions['DROP']) )
return $sql; return $sql;
} }
if ( empty ($flds) ) { if ( empty ($flds) ) {
return $sql; return $sql;
} }
$unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : ''; $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
$s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' '; $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
if ( isset($idxoptions[$this->upperName]) ) if ( isset($idxoptions[$this->upperName]) )
$s .= $idxoptions[$this->upperName]; $s .= $idxoptions[$this->upperName];
if ( is_array($flds) ) if ( is_array($flds) )
$flds = implode(', ',$flds); $flds = implode(', ',$flds);
$s .= '(' . $flds . ')'; $s .= '(' . $flds . ')';
$sql[] = $s; $sql[] = $s;
return $sql; return $sql;
} }
function _DropAutoIncrement($tabname) function _DropAutoIncrement($tabname)
{ {
return false; return false;
} }
function _TableSQL($tabname,$lines,$pkey,$tableoptions) function _TableSQL($tabname,$lines,$pkey,$tableoptions)
{ {
$sql = array(); $sql = array();
if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) { if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
$sql[] = sprintf($this->dropTable,$tabname); $sql[] = sprintf($this->dropTable,$tabname);
if ($this->autoIncrement) { if ($this->autoIncrement) {
@ -676,19 +676,19 @@ class ADODB_DataDict {
$s .= ",\n PRIMARY KEY ("; $s .= ",\n PRIMARY KEY (";
$s .= implode(", ",$pkey).")"; $s .= implode(", ",$pkey).")";
} }
if (isset($tableoptions['CONSTRAINTS'])) if (isset($tableoptions['CONSTRAINTS']))
$s .= "\n".$tableoptions['CONSTRAINTS']; $s .= "\n".$tableoptions['CONSTRAINTS'];
if (isset($tableoptions[$this->upperName.'_CONSTRAINTS'])) if (isset($tableoptions[$this->upperName.'_CONSTRAINTS']))
$s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS']; $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
$s .= "\n)"; $s .= "\n)";
if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName]; if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
$sql[] = $s; $sql[] = $s;
return $sql; return $sql;
} }
/* /*
GENERATE TRIGGERS IF NEEDED GENERATE TRIGGERS IF NEEDED
used when table has auto-incrementing field that is emulated using triggers used when table has auto-incrementing field that is emulated using triggers
@ -697,7 +697,7 @@ class ADODB_DataDict {
{ {
return array(); return array();
} }
/* /*
Sanitize options, so that array elements with no keys are promoted to keys Sanitize options, so that array elements with no keys are promoted to keys
*/ */
@ -711,34 +711,34 @@ class ADODB_DataDict {
} }
return $newopts; return $newopts;
} }
/* /*
"Florian Buzin [ easywe ]" <florian.buzin#easywe.de> "Florian Buzin [ easywe ]" <florian.buzin#easywe.de>
This function changes/adds new fields to your table. You don't This function changes/adds new fields to your table. You don't
have to know if the col is new or not. It will check on its own. have to know if the col is new or not. It will check on its own.
*/ */
function ChangeTableSQL($tablename, $flds, $tableoptions = false) function ChangeTableSQL($tablename, $flds, $tableoptions = false)
{ {
global $ADODB_FETCH_MODE; global $ADODB_FETCH_MODE;
$save = $ADODB_FETCH_MODE; $save = $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false); if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false);
// check table exists // check table exists
$save_handler = $this->connection->raiseErrorFn; $save_handler = $this->connection->raiseErrorFn;
$this->connection->raiseErrorFn = ''; $this->connection->raiseErrorFn = '';
$cols = $this->MetaColumns($tablename); $cols = $this->MetaColumns($tablename);
$this->connection->raiseErrorFn = $save_handler; $this->connection->raiseErrorFn = $save_handler;
if (isset($savem)) $this->connection->SetFetchMode($savem); if (isset($savem)) $this->connection->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save; $ADODB_FETCH_MODE = $save;
if ( empty($cols)) { if ( empty($cols)) {
return $this->CreateTableSQL($tablename, $flds, $tableoptions); return $this->CreateTableSQL($tablename, $flds, $tableoptions);
} }
if (is_array($flds)) { if (is_array($flds)) {
// Cycle through the update fields, comparing // Cycle through the update fields, comparing
// existing fields to fields to update. // existing fields to fields to update.
@ -757,11 +757,11 @@ class ADODB_DataDict {
} }
} else { } else {
$holdflds[$k] = $v; $holdflds[$k] = $v;
} }
} }
$flds = $holdflds; $flds = $holdflds;
} }
// already exists, alter table instead // already exists, alter table instead
list($lines,$pkey) = $this->_GenFields($flds); list($lines,$pkey) = $this->_GenFields($flds);
@ -770,18 +770,18 @@ class ADODB_DataDict {
foreach ( $lines as $id => $v ) { foreach ( $lines as $id => $v ) {
if ( isset($cols[$id]) && is_object($cols[$id]) ) { if ( isset($cols[$id]) && is_object($cols[$id]) ) {
$flds = Lens_ParseArgs($v,','); $flds = Lens_ParseArgs($v,',');
// We are trying to change the size of the field, if not allowed, simply ignore the request. // 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; if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) continue;
$sql[] = $alter . $this->alterCol . ' ' . $v; $sql[] = $alter . $this->alterCol . ' ' . $v;
} else { } else {
$sql[] = $alter . $this->addCol . ' ' . $v; $sql[] = $alter . $this->addCol . ' ' . $v;
} }
} }
return $sql; return $sql;
} }
} // class } // class

View File

@ -2,12 +2,12 @@
/** /**
V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved. V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license. Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses, Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence. the BSD license will take precedence.
Set tabs to 4 for best viewing. Set tabs to 4 for best viewing.
*/ */
// security - hide paths // security - hide paths
@ -18,10 +18,10 @@ class ADODB2_mysql extends ADODB_DataDict {
var $alterCol = ' MODIFY COLUMN'; var $alterCol = ' MODIFY COLUMN';
var $alterTableAddIndex = true; var $alterTableAddIndex = true;
var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later var $dropTable = 'DROP TABLE IF EXISTS %s'; // requires mysql 3.22 or later
var $dropIndex = 'DROP INDEX %s ON %s'; var $dropIndex = 'DROP INDEX %s ON %s';
var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition! var $renameColumn = 'ALTER TABLE %s CHANGE COLUMN %s %s %s'; // needs column-definition!
function MetaType($t,$len=-1,$fieldobj=false) function MetaType($t,$len=-1,$fieldobj=false)
{ {
if (is_object($t)) { if (is_object($t)) {
@ -30,43 +30,43 @@ class ADODB2_mysql extends ADODB_DataDict {
$len = $fieldobj->max_length; $len = $fieldobj->max_length;
} }
$is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment; $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->auto_increment;
$len = -1; // mysql max_length is not accurate $len = -1; // mysql max_length is not accurate
switch (strtoupper($t)) { switch (strtoupper($t)) {
case 'STRING': case 'STRING':
case 'CHAR': case 'CHAR':
case 'VARCHAR': case 'VARCHAR':
case 'TINYBLOB': case 'TINYBLOB':
case 'TINYTEXT': case 'TINYTEXT':
case 'ENUM': case 'ENUM':
case 'SET': case 'SET':
if ($len <= $this->blobSize) return 'C'; if ($len <= $this->blobSize) return 'C';
case 'TEXT': case 'TEXT':
case 'LONGTEXT': case 'LONGTEXT':
case 'MEDIUMTEXT': case 'MEDIUMTEXT':
return 'X'; return 'X';
// php_mysql extension always returns 'blob' even if 'text' // php_mysql extension always returns 'blob' even if 'text'
// so we have to check whether binary... // so we have to check whether binary...
case 'IMAGE': case 'IMAGE':
case 'LONGBLOB': case 'LONGBLOB':
case 'BLOB': case 'BLOB':
case 'MEDIUMBLOB': case 'MEDIUMBLOB':
return !empty($fieldobj->binary) ? 'B' : 'X'; return !empty($fieldobj->binary) ? 'B' : 'X';
case 'YEAR': case 'YEAR':
case 'DATE': return 'D'; case 'DATE': return 'D';
case 'TIME': case 'TIME':
case 'DATETIME': case 'DATETIME':
case 'TIMESTAMP': return 'T'; case 'TIMESTAMP': return 'T';
case 'FLOAT': case 'FLOAT':
case 'DOUBLE': case 'DOUBLE':
return 'F'; return 'F';
case 'INT': case 'INT':
case 'INTEGER': return $is_serial ? 'R' : 'I'; case 'INTEGER': return $is_serial ? 'R' : 'I';
case 'TINYINT': return $is_serial ? 'R' : 'I1'; case 'TINYINT': return $is_serial ? 'R' : 'I1';
case 'SMALLINT': return $is_serial ? 'R' : 'I2'; case 'SMALLINT': return $is_serial ? 'R' : 'I2';
@ -82,33 +82,33 @@ class ADODB2_mysql extends ADODB_DataDict {
case 'C': return 'VARCHAR'; case 'C': return 'VARCHAR';
case 'XL':return 'LONGTEXT'; case 'XL':return 'LONGTEXT';
case 'X': return 'TEXT'; case 'X': return 'TEXT';
case 'C2': return 'VARCHAR'; case 'C2': return 'VARCHAR';
case 'X2': return 'LONGTEXT'; case 'X2': return 'LONGTEXT';
case 'B': return 'LONGBLOB'; case 'B': return 'LONGBLOB';
case 'D': return 'DATE'; case 'D': return 'DATE';
case 'T': return 'DATETIME'; case 'T': return 'DATETIME';
case 'L': return 'TINYINT'; case 'L': return 'TINYINT';
case 'R': case 'R':
case 'I4': case 'I4':
case 'I': return 'INTEGER'; case 'I': return 'INTEGER';
case 'I1': return 'TINYINT'; case 'I1': return 'TINYINT';
case 'I2': return 'SMALLINT'; case 'I2': return 'SMALLINT';
case 'I8': return 'BIGINT'; case 'I8': return 'BIGINT';
case 'F': return 'DOUBLE'; case 'F': return 'DOUBLE';
case 'N': return 'NUMERIC'; case 'N': return 'NUMERIC';
default: default:
return $meta; return $meta;
} }
} }
// return string must begin with space // return string must begin with space
function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned) function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
{ {
$suffix = ''; $suffix = '';
if ($funsigned) $suffix .= ' UNSIGNED'; if ($funsigned) $suffix .= ' UNSIGNED';
if ($fnotnull) $suffix .= ' NOT NULL'; if ($fnotnull) $suffix .= ' NOT NULL';
@ -117,7 +117,7 @@ class ADODB2_mysql extends ADODB_DataDict {
if ($fconstraint) $suffix .= ' '.$fconstraint; if ($fconstraint) $suffix .= ' '.$fconstraint;
return $suffix; return $suffix;
} }
/* /*
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
[table_options] [select_statement] [table_options] [select_statement]
@ -133,16 +133,16 @@ class ADODB2_mysql extends ADODB_DataDict {
[reference_definition] [reference_definition]
or CHECK (expr) or CHECK (expr)
*/ */
/* /*
CREATE [UNIQUE|FULLTEXT] INDEX index_name CREATE [UNIQUE|FULLTEXT] INDEX index_name
ON tbl_name (col_name[(length)],... ) ON tbl_name (col_name[(length)],... )
*/ */
function _IndexSQL($idxname, $tabname, $flds, $idxoptions) function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
{ {
$sql = array(); $sql = array();
if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) { if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname"; if ($this->alterTableAddIndex) $sql[] = "ALTER TABLE $tabname DROP INDEX $idxname";
else $sql[] = sprintf($this->dropIndex, $idxname, $tabname); else $sql[] = sprintf($this->dropIndex, $idxname, $tabname);
@ -150,11 +150,11 @@ class ADODB2_mysql extends ADODB_DataDict {
if ( isset($idxoptions['DROP']) ) if ( isset($idxoptions['DROP']) )
return $sql; return $sql;
} }
if ( empty ($flds) ) { if ( empty ($flds) ) {
return $sql; return $sql;
} }
if (isset($idxoptions['FULLTEXT'])) { if (isset($idxoptions['FULLTEXT'])) {
$unique = ' FULLTEXT'; $unique = ' FULLTEXT';
} elseif (isset($idxoptions['UNIQUE'])) { } elseif (isset($idxoptions['UNIQUE'])) {
@ -162,19 +162,19 @@ class ADODB2_mysql extends ADODB_DataDict {
} else { } else {
$unique = ''; $unique = '';
} }
if ( is_array($flds) ) $flds = implode(', ',$flds); if ( is_array($flds) ) $flds = implode(', ',$flds);
if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname "; if ($this->alterTableAddIndex) $s = "ALTER TABLE $tabname ADD $unique INDEX $idxname ";
else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname; else $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname;
$s .= ' (' . $flds . ')'; $s .= ' (' . $flds . ')';
if ( isset($idxoptions[$this->upperName]) ) if ( isset($idxoptions[$this->upperName]) )
$s .= $idxoptions[$this->upperName]; $s .= $idxoptions[$this->upperName];
$sql[] = $s; $sql[] = $s;
return $sql; return $sql;
} }
} }

View File

@ -2,26 +2,26 @@
/** /**
V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved. V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license. Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses, Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence. the BSD license will take precedence.
Set tabs to 4 for best viewing. Set tabs to 4 for best viewing.
*/ */
// security - hide paths // security - hide paths
if (!defined('ADODB_DIR')) die(); if (!defined('ADODB_DIR')) die();
class ADODB2_postgres extends ADODB_DataDict { class ADODB2_postgres extends ADODB_DataDict {
var $databaseType = 'postgres'; var $databaseType = 'postgres';
var $seqField = false; var $seqField = false;
var $seqPrefix = 'SEQ_'; var $seqPrefix = 'SEQ_';
var $addCol = ' ADD COLUMN'; var $addCol = ' ADD COLUMN';
var $quote = '"'; var $quote = '"';
var $renameTable = 'ALTER TABLE %s RENAME TO %s'; // at least since 7.1 var $renameTable = 'ALTER TABLE %s RENAME TO %s'; // at least since 7.1
function MetaType($t,$len=-1,$fieldobj=false) function MetaType($t,$len=-1,$fieldobj=false)
{ {
if (is_object($t)) { if (is_object($t)) {
@ -29,9 +29,9 @@ class ADODB2_postgres extends ADODB_DataDict {
$t = $fieldobj->type; $t = $fieldobj->type;
$len = $fieldobj->max_length; $len = $fieldobj->max_length;
} }
$is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->unique && $is_serial = is_object($fieldobj) && $fieldobj->primary_key && $fieldobj->unique &&
$fieldobj->has_default && substr($fieldobj->default_value,0,8) == 'nextval('; $fieldobj->has_default && substr($fieldobj->default_value,0,8) == 'nextval(';
switch (strtoupper($t)) { switch (strtoupper($t)) {
case 'INTERVAL': case 'INTERVAL':
case 'CHAR': case 'CHAR':
@ -40,83 +40,83 @@ class ADODB2_postgres extends ADODB_DataDict {
case 'NAME': case 'NAME':
case 'BPCHAR': case 'BPCHAR':
if ($len <= $this->blobSize) return 'C'; if ($len <= $this->blobSize) return 'C';
case 'TEXT': case 'TEXT':
return 'X'; return 'X';
case 'IMAGE': // user defined type case 'IMAGE': // user defined type
case 'BLOB': // user defined type case 'BLOB': // user defined type
case 'BIT': // This is a bit string, not a single bit, so don't return 'L' case 'BIT': // This is a bit string, not a single bit, so don't return 'L'
case 'VARBIT': case 'VARBIT':
case 'BYTEA': case 'BYTEA':
return 'B'; return 'B';
case 'BOOL': case 'BOOL':
case 'BOOLEAN': case 'BOOLEAN':
return 'L'; return 'L';
case 'DATE': case 'DATE':
return 'D'; return 'D';
case 'TIME': case 'TIME':
case 'DATETIME': case 'DATETIME':
case 'TIMESTAMP': case 'TIMESTAMP':
case 'TIMESTAMPTZ': case 'TIMESTAMPTZ':
return 'T'; return 'T';
case 'INTEGER': return !$is_serial ? 'I' : 'R'; case 'INTEGER': return !$is_serial ? 'I' : 'R';
case 'SMALLINT': case 'SMALLINT':
case 'INT2': return !$is_serial ? 'I2' : 'R'; case 'INT2': return !$is_serial ? 'I2' : 'R';
case 'INT4': return !$is_serial ? 'I4' : 'R'; case 'INT4': return !$is_serial ? 'I4' : 'R';
case 'BIGINT': case 'BIGINT':
case 'INT8': return !$is_serial ? 'I8' : 'R'; case 'INT8': return !$is_serial ? 'I8' : 'R';
case 'OID': case 'OID':
case 'SERIAL': case 'SERIAL':
return 'R'; return 'R';
case 'FLOAT4': case 'FLOAT4':
case 'FLOAT8': case 'FLOAT8':
case 'DOUBLE PRECISION': case 'DOUBLE PRECISION':
case 'REAL': case 'REAL':
return 'F'; return 'F';
default: default:
return 'N'; return 'N';
} }
} }
function ActualType($meta) function ActualType($meta)
{ {
switch($meta) { switch($meta) {
case 'C': return 'VARCHAR'; case 'C': return 'VARCHAR';
case 'XL': case 'XL':
case 'X': return 'TEXT'; case 'X': return 'TEXT';
case 'C2': return 'VARCHAR'; case 'C2': return 'VARCHAR';
case 'X2': return 'TEXT'; case 'X2': return 'TEXT';
case 'B': return 'BYTEA'; case 'B': return 'BYTEA';
case 'D': return 'DATE'; case 'D': return 'DATE';
case 'T': return 'TIMESTAMP'; case 'T': return 'TIMESTAMP';
case 'L': return 'BOOLEAN'; case 'L': return 'BOOLEAN';
case 'I': return 'INTEGER'; case 'I': return 'INTEGER';
case 'I1': return 'SMALLINT'; case 'I1': return 'SMALLINT';
case 'I2': return 'INT2'; case 'I2': return 'INT2';
case 'I4': return 'INT4'; case 'I4': return 'INT4';
case 'I8': return 'INT8'; case 'I8': return 'INT8';
case 'F': return 'FLOAT8'; case 'F': return 'FLOAT8';
case 'N': return 'NUMERIC'; case 'N': return 'NUMERIC';
default: default:
return $meta; return $meta;
} }
} }
/** /**
* Adding a new Column * Adding a new Column
* *
* reimplementation of the default function as postgres does NOT allow to set the default in the same statement * reimplementation of the default function as postgres does NOT allow to set the default in the same statement
* *
@ -139,7 +139,7 @@ class ADODB2_postgres extends ADODB_DataDict {
$sql[] = $alter . str_replace('DEFAULT '.$default,'',$v); $sql[] = $alter . str_replace('DEFAULT '.$default,'',$v);
$sql[] = 'UPDATE '.$tabname.' SET '.$colname.'='.$default; $sql[] = 'UPDATE '.$tabname.' SET '.$colname.'='.$default;
$sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default; $sql[] = 'ALTER TABLE '.$tabname.' ALTER COLUMN '.$colname.' SET DEFAULT ' . $default;
} else { } else {
$sql[] = $alter . $v; $sql[] = $alter . $v;
} }
if ($not_null) { if ($not_null) {
@ -149,7 +149,7 @@ class ADODB2_postgres extends ADODB_DataDict {
} }
return $sql; return $sql;
} }
/** /**
* Change the definition of one column * Change the definition of one column
* *
@ -169,7 +169,7 @@ class ADODB2_postgres extends ADODB_DataDict {
} }
return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions); return $this->_recreate_copy_table($tabname,False,$tableflds,$tableoptions);
} }
/** /**
* Drop one column * Drop one column
* *
@ -193,7 +193,7 @@ class ADODB2_postgres extends ADODB_DataDict {
} }
return $this->_recreate_copy_table($tabname,$flds,$tableflds,$tableoptions); return $this->_recreate_copy_table($tabname,$flds,$tableflds,$tableoptions);
} }
/** /**
* Save the content into a temp. table, drop and recreate the original table and copy the content back in * Save the content into a temp. table, drop and recreate the original table and copy the content back in
* *
@ -213,7 +213,7 @@ class ADODB2_postgres extends ADODB_DataDict {
foreach(($meta=$this->MetaColumns($tabname)) as $fld) { foreach(($meta=$this->MetaColumns($tabname)) as $fld) {
if (!$dropflds || !in_array($fld->name,$dropflds)) { if (!$dropflds || !in_array($fld->name,$dropflds)) {
// we need to explicit convert varchar to a number to be able to do an AlterColumn of a char column to a nummeric one // we need to explicit convert varchar to a number to be able to do an AlterColumn of a char column to a nummeric one
if (preg_match('/'.$fld->name.' (I|I2|I4|I8|N|F)/i',$tableflds,$matches) && if (preg_match('/'.$fld->name.' (I|I2|I4|I8|N|F)/i',$tableflds,$matches) &&
in_array($fld->type,array('varchar','char','text','bytea'))) { in_array($fld->type,array('varchar','char','text','bytea'))) {
$copyflds[] = "to_number($fld->name,'S9999999999999D99')"; $copyflds[] = "to_number($fld->name,'S9999999999999D99')";
} elseif (preg_match('/'.$fld->name.' ([\w]+)/i',$tableflds,$matches) && } elseif (preg_match('/'.$fld->name.' ([\w]+)/i',$tableflds,$matches) &&
@ -229,7 +229,7 @@ class ADODB2_postgres extends ADODB_DataDict {
$copyflds[] = $fld->name; $copyflds[] = $fld->name;
} }
// identify the sequence name and the fld its on // identify the sequence name and the fld its on
if ($fld->primary_key && $fld->has_default && if ($fld->primary_key && $fld->has_default &&
preg_match("/nextval\('([^']+)'::(text|regclass)\)/",$fld->default_value,$matches)) { preg_match("/nextval\('([^']+)'::(text|regclass)\)/",$fld->default_value,$matches)) {
$seq_name = $matches[1]; $seq_name = $matches[1];
$seq_fld = $fld->name; $seq_fld = $fld->name;
@ -237,7 +237,7 @@ class ADODB2_postgres extends ADODB_DataDict {
} }
} }
$copyflds = implode(', ',$copyflds); $copyflds = implode(', ',$copyflds);
$tempname = $tabname.'_tmp'; $tempname = $tabname.'_tmp';
$aSql[] = 'BEGIN'; // we use a transaction, to make sure not to loose the content of the table $aSql[] = 'BEGIN'; // we use a transaction, to make sure not to loose the content of the table
$aSql[] = "SELECT * INTO TEMPORARY TABLE $tempname FROM $tabname"; $aSql[] = "SELECT * INTO TEMPORARY TABLE $tempname FROM $tabname";
@ -260,19 +260,19 @@ class ADODB2_postgres extends ADODB_DataDict {
$aSql[] = 'COMMIT'; $aSql[] = 'COMMIT';
return $aSql; return $aSql;
} }
function DropTableSQL($tabname) function DropTableSQL($tabname)
{ {
$sql = ADODB_DataDict::DropTableSQL($tabname); $sql = ADODB_DataDict::DropTableSQL($tabname);
$drop_seq = $this->_DropAutoIncrement($tabname); $drop_seq = $this->_DropAutoIncrement($tabname);
if ($drop_seq) $sql[] = $drop_seq; if ($drop_seq) $sql[] = $drop_seq;
return $sql; return $sql;
} }
// return string must begin with space // return string must begin with space
function _CreateSuffix($fname, &$ftype, $fnotnull,$fdefault,$fautoinc,$fconstraint) function _CreateSuffix($fname, &$ftype, $fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
{ {
if ($fautoinc) { if ($fautoinc) {
$ftype = 'SERIAL'; $ftype = 'SERIAL';
@ -284,7 +284,7 @@ class ADODB2_postgres extends ADODB_DataDict {
if ($fconstraint) $suffix .= ' '.$fconstraint; if ($fconstraint) $suffix .= ' '.$fconstraint;
return $suffix; return $suffix;
} }
// search for a sequece for the given table (asumes the seqence-name contains the table-name!) // search for a sequece for the given table (asumes the seqence-name contains the table-name!)
// if yes return sql to drop it // if yes return sql to drop it
// this is still necessary if postgres < 7.3 or the SERIAL was created on an earlier version!!! // this is still necessary if postgres < 7.3 or the SERIAL was created on an earlier version!!!
@ -300,7 +300,7 @@ class ADODB2_postgres extends ADODB_DataDict {
} }
return "DROP SEQUENCE ".$seq; return "DROP SEQUENCE ".$seq;
} }
/* /*
CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
{ column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ] { column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ]
@ -324,8 +324,8 @@ class ADODB2_postgres extends ADODB_DataDict {
[ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] } [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
*/ */
/* /*
CREATE [ UNIQUE ] INDEX index_name ON table CREATE [ UNIQUE ] INDEX index_name ON table
[ USING acc_method ] ( column [ ops_name ] [, ...] ) [ USING acc_method ] ( column [ ops_name ] [, ...] )
@ -337,32 +337,32 @@ CREATE [ UNIQUE ] INDEX index_name ON table
function _IndexSQL($idxname, $tabname, $flds, $idxoptions) function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
{ {
$sql = array(); $sql = array();
if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) { if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
$sql[] = sprintf ($this->dropIndex, $idxname, $tabname); $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
if ( isset($idxoptions['DROP']) ) if ( isset($idxoptions['DROP']) )
return $sql; return $sql;
} }
if ( empty ($flds) ) { if ( empty ($flds) ) {
return $sql; return $sql;
} }
$unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : ''; $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
$s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' '; $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
if (isset($idxoptions['HASH'])) if (isset($idxoptions['HASH']))
$s .= 'USING HASH '; $s .= 'USING HASH ';
if ( isset($idxoptions[$this->upperName]) ) if ( isset($idxoptions[$this->upperName]) )
$s .= $idxoptions[$this->upperName]; $s .= $idxoptions[$this->upperName];
if ( is_array($flds) ) if ( is_array($flds) )
$flds = implode(', ',$flds); $flds = implode(', ',$flds);
$s .= '(' . $flds . ')'; $s .= '(' . $flds . ')';
$sql[] = $s; $sql[] = $s;
return $sql; return $sql;
} }
} }