Changeset 34 for trunk/phpgwapi/inc/adodb/adodb-datadict.inc.php
- Timestamp:
- 06/29/07 15:17:46 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpgwapi/inc/adodb/adodb-datadict.inc.php
r2 r34 2 2 3 3 /** 4 V4. 51 29 July 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.4 V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved. 5 5 Released under both BSD license and Lesser GPL library license. 6 6 Whenever there is any discrepancy between the two licenses, … … 23 23 function Lens_ParseTest() 24 24 { 25 $str = "`zcol ACOL` NUMBER(32,2) DEFAULT 'The \"cow\" (and Jim''s dog) jumps over the moon' PRIMARY, INTI INT AUTO DEFAULT 0 ";25 $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"; 26 26 print "<p>$str</p>"; 27 27 $a= Lens_ParseArgs($str); … … 30 30 print "</pre>"; 31 31 } 32 32 33 33 34 if (!function_exists('ctype_alnum')) { … … 61 62 $max = strlen($args); 62 63 $quoted = false; 64 $tokarr = array(); 63 65 64 66 while ($pos < $max) { … … 154 156 $pos += 1; 155 157 } 158 if ($intoken) $tokens[$stmtno][] = implode('',$tokarr); 156 159 157 160 return $tokens; … … 163 166 var $debug = false; 164 167 var $dropTable = 'DROP TABLE %s'; 168 var $renameTable = 'RENAME TABLE %s TO %s'; 165 169 var $dropIndex = 'DROP INDEX %s'; 166 170 var $addCol = ' ADD'; 167 171 var $alterCol = ' ALTER COLUMN'; 168 172 var $dropCol = ' DROP COLUMN'; 173 var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s'; // table, old-column, new-column, column-definitions (not used by default) 169 174 var $nameRegex = '\w'; 175 var $nameRegexBrackets = 'a-zA-Z0-9_\(\)'; 170 176 var $schema = false; 171 177 var $serverInfo = array(); 172 178 var $autoIncrement = false; 173 179 var $dataProvider; 180 var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changetablesql 174 181 var $blobSize = 100; /// any varchar/char field this size or greater is treated as a blob 175 182 /// in other words, we use a text area for editting. … … 185 192 } 186 193 187 function &MetaTables() 188 { 194 function MetaTables() 195 { 196 if (!$this->connection->IsConnected()) return array(); 189 197 return $this->connection->MetaTables(); 190 198 } 191 199 192 function &MetaColumns($tab, $upper=true, $schema=false) 193 { 200 function MetaColumns($tab, $upper=true, $schema=false) 201 { 202 if (!$this->connection->IsConnected()) return array(); 194 203 return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema); 195 204 } 196 205 197 function &MetaPrimaryKeys($tab,$owner=false,$intkey=false) 198 { 206 function MetaPrimaryKeys($tab,$owner=false,$intkey=false) 207 { 208 if (!$this->connection->IsConnected()) return array(); 199 209 return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey); 200 210 } 201 211 202 function &MetaIndexes($table, $primary = false, $owner = false) 203 { 212 function MetaIndexes($table, $primary = false, $owner = false) 213 { 214 if (!$this->connection->IsConnected()) return array(); 204 215 return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner); 205 216 } 206 217 218 207 219 function MetaType($t,$len=-1,$fieldobj=false) 208 { 209 return ADORecordSet::MetaType($t,$len,$fieldobj); 210 } 211 212 function NameQuote($name = NULL) 220 { 221 static $typeMap = array( 222 'VARCHAR' => 'C', 223 'VARCHAR2' => 'C', 224 'CHAR' => 'C', 225 'C' => 'C', 226 'STRING' => 'C', 227 'NCHAR' => 'C', 228 'NVARCHAR' => 'C', 229 'VARYING' => 'C', 230 'BPCHAR' => 'C', 231 'CHARACTER' => 'C', 232 'INTERVAL' => 'C', # Postgres 233 'MACADDR' => 'C', # postgres 234 ## 235 'LONGCHAR' => 'X', 236 'TEXT' => 'X', 237 'NTEXT' => 'X', 238 'M' => 'X', 239 'X' => 'X', 240 'CLOB' => 'X', 241 'NCLOB' => 'X', 242 'LVARCHAR' => 'X', 243 ## 244 'BLOB' => 'B', 245 'IMAGE' => 'B', 246 'BINARY' => 'B', 247 'VARBINARY' => 'B', 248 'LONGBINARY' => 'B', 249 'B' => 'B', 250 ## 251 'YEAR' => 'D', // mysql 252 'DATE' => 'D', 253 'D' => 'D', 254 ## 255 'UNIQUEIDENTIFIER' => 'C', # MS SQL Server 256 ## 257 'TIME' => 'T', 258 'TIMESTAMP' => 'T', 259 'DATETIME' => 'T', 260 'TIMESTAMPTZ' => 'T', 261 'T' => 'T', 262 'TIMESTAMP WITHOUT TIME ZONE' => 'T', // postgresql 263 ## 264 'BOOL' => 'L', 265 'BOOLEAN' => 'L', 266 'BIT' => 'L', 267 'L' => 'L', 268 ## 269 'COUNTER' => 'R', 270 'R' => 'R', 271 'SERIAL' => 'R', // ifx 272 'INT IDENTITY' => 'R', 273 ## 274 'INT' => 'I', 275 'INT2' => 'I', 276 'INT4' => 'I', 277 'INT8' => 'I', 278 'INTEGER' => 'I', 279 'INTEGER UNSIGNED' => 'I', 280 'SHORT' => 'I', 281 'TINYINT' => 'I', 282 'SMALLINT' => 'I', 283 'I' => 'I', 284 ## 285 'LONG' => 'N', // interbase is numeric, oci8 is blob 286 'BIGINT' => 'N', // this is bigger than PHP 32-bit integers 287 'DECIMAL' => 'N', 288 'DEC' => 'N', 289 'REAL' => 'N', 290 'DOUBLE' => 'N', 291 'DOUBLE PRECISION' => 'N', 292 'SMALLFLOAT' => 'N', 293 'FLOAT' => 'N', 294 'NUMBER' => 'N', 295 'NUM' => 'N', 296 'NUMERIC' => 'N', 297 'MONEY' => 'N', 298 299 ## informix 9.2 300 'SQLINT' => 'I', 301 'SQLSERIAL' => 'I', 302 'SQLSMINT' => 'I', 303 'SQLSMFLOAT' => 'N', 304 'SQLFLOAT' => 'N', 305 'SQLMONEY' => 'N', 306 'SQLDECIMAL' => 'N', 307 'SQLDATE' => 'D', 308 'SQLVCHAR' => 'C', 309 'SQLCHAR' => 'C', 310 'SQLDTIME' => 'T', 311 'SQLINTERVAL' => 'N', 312 'SQLBYTES' => 'B', 313 'SQLTEXT' => 'X', 314 ## informix 10 315 "SQLINT8" => 'I8', 316 "SQLSERIAL8" => 'I8', 317 "SQLNCHAR" => 'C', 318 "SQLNVCHAR" => 'C', 319 "SQLLVARCHAR" => 'X', 320 "SQLBOOL" => 'L' 321 ); 322 323 if (!$this->connection->IsConnected()) { 324 $t = strtoupper($t); 325 if (isset($typeMap[$t])) return $typeMap[$t]; 326 return 'N'; 327 } 328 return $this->connection->MetaType($t,$len,$fieldobj); 329 } 330 331 function NameQuote($name = NULL,$allowBrackets=false) 213 332 { 214 333 if (!is_string($name)) { … … 230 349 231 350 // if name contains special characters, quote it 232 if ( !preg_match('/^[' . $this->nameRegex . ']+$/', $name) ) { 351 $regex = ($allowBrackets) ? $this->nameRegexBrackets : $this->nameRegex; 352 353 if ( !preg_match('/^[' . $regex . ']+$/', $name) ) { 233 354 return $quote . $name . $quote; 234 355 } … … 265 386 } 266 387 267 /* 388 /** 268 389 Returns the actual type given a character code. 269 390 … … 311 432 312 433 foreach($flds as $key => $fld) { 313 $flds[$key] = $this->NameQuote($fld); 434 # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32) 435 $flds[$key] = $this->NameQuote($fld,$allowBrackets=true); 314 436 } 315 437 … … 331 453 $tabname = $this->TableName ($tabname); 332 454 $sql = array(); 333 list($lines,$pkey) = $this->_GenFields($flds); 455 list($lines,$pkey,$idxs) = $this->_GenFields($flds); 456 // genfields can return FALSE at times 457 if ($lines == null) $lines = array(); 334 458 $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' '; 335 459 foreach($lines as $v) { 336 460 $sql[] = $alter . $v; 337 461 } 462 if (is_array($idxs)) { 463 foreach($idxs as $idx => $idxdef) { 464 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); 465 $sql = array_merge($sql, $sql_idxs); 466 } 467 } 338 468 return $sql; 339 469 } 340 470 341 function AlterColumnSQL($tabname, $flds) 471 /** 472 * Change the definition of one column 473 * 474 * As some DBM's can't do that on there own, you need to supply the complete defintion of the new table, 475 * to allow, recreating the table and copying the content over to the new table 476 * @param string $tabname table-name 477 * @param string $flds column-name and type for the changed column 478 * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default '' 479 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default '' 480 * @return array with SQL strings 481 */ 482 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') 342 483 { 343 484 $tabname = $this->TableName ($tabname); 344 485 $sql = array(); 345 list($lines,$pkey) = $this->_GenFields($flds); 486 list($lines,$pkey,$idxs) = $this->_GenFields($flds); 487 // genfields can return FALSE at times 488 if ($lines == null) $lines = array(); 346 489 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' '; 347 490 foreach($lines as $v) { 348 491 $sql[] = $alter . $v; 349 492 } 493 if (is_array($idxs)) { 494 foreach($idxs as $idx => $idxdef) { 495 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); 496 $sql = array_merge($sql, $sql_idxs); 497 } 498 499 } 350 500 return $sql; 351 501 } 352 502 353 function DropColumnSQL($tabname, $flds) 503 /** 504 * Rename one column 505 * 506 * Some DBM's can only do this together with changeing the type of the column (even if that stays the same, eg. mysql) 507 * @param string $tabname table-name 508 * @param string $oldcolumn column-name to be renamed 509 * @param string $newcolumn new column-name 510 * @param string $flds='' complete column-defintion-string like for AddColumnSQL, only used by mysql atm., default='' 511 * @return array with SQL strings 512 */ 513 function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='') 514 { 515 $tabname = $this->TableName ($tabname); 516 if ($flds) { 517 list($lines,$pkey,$idxs) = $this->_GenFields($flds); 518 // genfields can return FALSE at times 519 if ($lines == null) $lines = array(); 520 list(,$first) = each($lines); 521 list(,$column_def) = split("[\t ]+",$first,2); 522 } 523 return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def)); 524 } 525 526 /** 527 * Drop one column 528 * 529 * Some DBM's can't do that on there own, you need to supply the complete defintion of the new table, 530 * to allow, recreating the table and copying the content over to the new table 531 * @param string $tabname table-name 532 * @param string $flds column-name and type for the changed column 533 * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default '' 534 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default '' 535 * @return array with SQL strings 536 */ 537 function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') 354 538 { 355 539 $tabname = $this->TableName ($tabname); … … 368 552 } 369 553 370 /* 554 function RenameTableSQL($tabname,$newname) 555 { 556 return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname))); 557 } 558 559 /** 371 560 Generate the SQL to create table. Returns an array of sql strings. 372 561 */ 373 function CreateTableSQL($tabname, $flds, $tableoptions= false)374 { 375 if (!$tableoptions) $tableoptions = array();376 377 list($lines,$pkey) = $this->_GenFields($flds);562 function CreateTableSQL($tabname, $flds, $tableoptions=array()) 563 { 564 list($lines,$pkey,$idxs) = $this->_GenFields($flds, true); 565 // genfields can return FALSE at times 566 if ($lines == null) $lines = array(); 378 567 379 568 $taboptions = $this->_Options($tableoptions); … … 381 570 $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions); 382 571 572 // ggiunta - 2006/10/12 - KLUDGE: 573 // if we are on autoincrement, and table options includes REPLACE, the 574 // autoincrement sequence has already been dropped on table creation sql, so 575 // we avoid passing REPLACE to trigger creation code. This prevents 576 // creating sql that double-drops the sequence 577 if ($this->autoIncrement && isset($taboptions['REPLACE'])) 578 unset($taboptions['REPLACE']); 383 579 $tsql = $this->_Triggers($tabname,$taboptions); 384 580 foreach($tsql as $s) $sql[] = $s; 385 581 582 if (is_array($idxs)) { 583 foreach($idxs as $idx => $idxdef) { 584 $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); 585 $sql = array_merge($sql, $sql_idxs); 586 } 587 } 588 386 589 return $sql; 387 590 } 388 591 389 function _GenFields($flds )592 function _GenFields($flds,$widespacing=false) 390 593 { 391 594 if (is_string($flds)) { … … 399 602 foreach($f0 as $token) { 400 603 switch (strtoupper($token)) { 604 case 'INDEX': 605 $f1['INDEX'] = ''; 606 // fall through intentionally 401 607 case 'CONSTRAINT': 402 608 case 'DEFAULT': … … 410 616 } 411 617 } 618 // 'index' token without a name means single column index: name it after column 619 if (array_key_exists('INDEX', $f1) && $f1['INDEX'] == '') { 620 $f1['INDEX'] = isset($f0['NAME']) ? $f0['NAME'] : $f0[0]; 621 // check if column name used to create an index name was quoted 622 if (($f1['INDEX'][0] == '"' || $f1['INDEX'][0] == "'" || $f1['INDEX'][0] == "`") && 623 ($f1['INDEX'][0] == substr($f1['INDEX'], -1))) { 624 $f1['INDEX'] = $f1['INDEX'][0].'idx_'.substr($f1['INDEX'], 1, -1).$f1['INDEX'][0]; 625 } 626 else 627 $f1['INDEX'] = 'idx_'.$f1['INDEX']; 628 } 629 // reset it, so we don't get next field 1st token as INDEX... 630 $hasparam = false; 631 412 632 $flds[] = $f1; 413 633 … … 417 637 $lines = array(); 418 638 $pkey = array(); 639 $idxs = array(); 419 640 foreach($flds as $fld) { 420 641 $fld = _array_change_key_case($fld); 421 642 422 643 $fname = false; 423 644 $fdefault = false; … … 433 654 $fnotnull = false; 434 655 $funsigned = false; 656 $findex = ''; 657 $funiqueindex = false; 435 658 436 659 //----------------- … … 458 681 case 'AUTO': $fautoinc = true; $fnotnull = true; break; 459 682 case 'KEY': 460 case 'PRIMARY': $fprimary = $v; $fnotnull = true; break; 683 // a primary key col can be non unique in itself (if key spans many cols...) 684 case 'PRIMARY': $fprimary = $v; $fnotnull = true; /*$funiqueindex = true;*/ break; 461 685 case 'DEF': 462 686 case 'DEFAULT': $fdefault = $v; break; … … 466 690 case 'DEFTIMESTAMP': $fdefts = $v; break; 467 691 case 'CONSTRAINT': $fconstraint = $v; break; 692 // let INDEX keyword create a 'very standard' index on column 693 case 'INDEX': $findex = $v; break; 694 case 'UNIQUE': $funiqueindex = true; break; 468 695 } //switch 469 696 } // foreach $fld … … 495 722 if ($ty == 'X') $fdefault = false; 496 723 724 // build list of indexes 725 if ($findex != '') { 726 if (array_key_exists($findex, $idxs)) { 727 $idxs[$findex]['cols'][] = ($fname); 728 if (in_array('UNIQUE', $idxs[$findex]['opts']) != $funiqueindex) { 729 if ($this->debug) ADOConnection::outp("Index $findex defined once UNIQUE and once not"); 730 } 731 if ($funiqueindex && !in_array('UNIQUE', $idxs[$findex]['opts'])) 732 $idxs[$findex]['opts'][] = 'UNIQUE'; 733 } 734 else 735 { 736 $idxs[$findex] = array(); 737 $idxs[$findex]['cols'] = array($fname); 738 if ($funiqueindex) 739 $idxs[$findex]['opts'] = array('UNIQUE'); 740 else 741 $idxs[$findex]['opts'] = array(); 742 } 743 } 744 497 745 //-------------------- 498 746 // CONSTRUCT FIELD SQL … … 509 757 $fdefault = $this->connection->sysDate; 510 758 } 511 } else if ( strlen($fdefault) && !$fnoquote)759 } else if ($fdefault !== false && !$fnoquote) { 512 760 if ($ty == 'C' or $ty == 'X' or 513 ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) 761 ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault))) { 762 763 if (($ty == 'D' || $ty == 'T') && strtolower($fdefault) != 'null') { 764 // convert default date into database-aware code 765 if ($ty == 'T') 766 { 767 $fdefault = $this->connection->DBTimeStamp($fdefault); 768 } 769 else 770 { 771 $fdefault = $this->connection->DBDate($fdefault); 772 } 773 } 774 else 514 775 if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ') 515 776 $fdefault = trim($fdefault); 516 777 else if (strtolower($fdefault) != 'null') 517 778 $fdefault = $this->connection->qstr($fdefault); 779 } 780 } 518 781 $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned); 519 782 520 $fname = str_pad($fname,16); 783 // add index creation 784 if ($widespacing) $fname = str_pad($fname,24); 785 786 // check for field names appearing twice 787 if (array_key_exists($fid, $lines)) { 788 ADOConnection::outp("Field '$fname' defined twice"); 789 } 790 521 791 $lines[$fid] = $fname.' '.$ftype.$suffix; 522 792 … … 524 794 } // foreach $flds 525 795 526 return array($lines,$pkey); 527 } 528 /* 796 return array($lines,$pkey,$idxs); 797 } 798 799 /** 529 800 GENERATE THE SIZE PART OF THE DATATYPE 530 801 $ftype is the actual type … … 619 890 } 620 891 621 /* 892 /** 622 893 GENERATE TRIGGERS IF NEEDED 623 894 used when table has auto-incrementing field that is emulated using triggers … … 628 899 } 629 900 630 /* 901 /** 631 902 Sanitize options, so that array elements with no keys are promoted to keys 632 903 */ … … 642 913 } 643 914 644 /* 645 "Florian Buzin [ easywe ]" <florian.buzin @easywe.de>915 /** 916 "Florian Buzin [ easywe ]" <florian.buzin#easywe.de> 646 917 647 918 This function changes/adds new fields to your table. You don't … … 650 921 function ChangeTableSQL($tablename, $flds, $tableoptions = false) 651 922 { 923 global $ADODB_FETCH_MODE; 924 925 $save = $ADODB_FETCH_MODE; 926 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 927 if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false); 928 652 929 // check table exists 653 $cols = &$this->MetaColumns($tablename); 930 $save_handler = $this->connection->raiseErrorFn; 931 $this->connection->raiseErrorFn = ''; 932 $cols = $this->MetaColumns($tablename); 933 $this->connection->raiseErrorFn = $save_handler; 934 935 if (isset($savem)) $this->connection->SetFetchMode($savem); 936 $ADODB_FETCH_MODE = $save; 937 654 938 if ( empty($cols)) { 655 939 return $this->CreateTableSQL($tablename, $flds, $tableoptions); 656 940 } 657 941 942 if (is_array($flds)) { 943 // Cycle through the update fields, comparing 944 // existing fields to fields to update. 945 // if the Metatype and size is exactly the 946 // same, ignore - by Mark Newham 947 $holdflds = array(); 948 foreach($flds as $k=>$v) { 949 if ( isset($cols[$k]) && is_object($cols[$k]) ) { 950 // If already not allowing nulls, then don't change 951 $obj = $cols[$k]; 952 if (isset($obj->not_null) && $obj->not_null) 953 $v = str_replace('NOT NULL','',$v); 954 955 $c = $cols[$k]; 956 $ml = $c->max_length; 957 $mt = $this->MetaType($c->type,$ml); 958 if ($ml == -1) $ml = ''; 959 if ($mt == 'X') $ml = $v['SIZE']; 960 if (($mt != $v['TYPE']) || $ml != $v['SIZE']) { 961 $holdflds[$k] = $v; 962 } 963 } else { 964 $holdflds[$k] = $v; 965 } 966 } 967 $flds = $holdflds; 968 } 969 970 658 971 // already exists, alter table instead 659 list($lines,$pkey) = $this->_GenFields($flds); 972 list($lines,$pkey,$idxs) = $this->_GenFields($flds); 973 // genfields can return FALSE at times 974 if ($lines == null) $lines = array(); 660 975 $alter = 'ALTER TABLE ' . $this->TableName($tablename); 661 976 $sql = array(); 662 977 663 978 foreach ( $lines as $id => $v ) { 664 979 if ( isset($cols[$id]) && is_object($cols[$id]) ) { 980 981 $flds = Lens_ParseArgs($v,','); 982 983 // We are trying to change the size of the field, if not allowed, simply ignore the request. 984 if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) { 985 echo "<h3>$this->alterCol cannot be changed to $flds currently</h3>"; 986 continue; 987 } 665 988 $sql[] = $alter . $this->alterCol . ' ' . $v; 666 989 } else {
Note: See TracChangeset
for help on using the changeset viewer.