PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/include/phpgacl/adodb/adodb-datadict.inc.php

https://github.com/radicaldesigns/amp
PHP | 784 lines | 764 code | 4 blank | 16 comment | 1 complexity | a6be7d428595bc711f1bce30e740dc67 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. V4.92a 29 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved.
  4. Released under both BSD license and Lesser GPL library license.
  5. Whenever there is any discrepancy between the two licenses,
  6. the BSD license will take precedence.
  7. Set tabs to 4 for best viewing.
  8. DOCUMENTATION:
  9. See adodb/tests/test-datadict.php for docs and examples.
  10. */
  11. /*
  12. Test script for parser
  13. */
  14. // security - hide paths
  15. if (!defined('ADODB_DIR')) die();
  16. function Lens_ParseTest()
  17. {
  18. $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";
  19. print "<p>$str</p>";
  20. $a= Lens_ParseArgs($str);
  21. print "<pre>";
  22. print_r($a);
  23. print "</pre>";
  24. }
  25. if (!function_exists('ctype_alnum')) {
  26. function ctype_alnum($text) {
  27. return preg_match('/^[a-z0-9]*$/i', $text);
  28. }
  29. }
  30. //Lens_ParseTest();
  31. /**
  32. Parse arguments, treat "text" (text) and 'text' as quotation marks.
  33. To escape, use "" or '' or ))
  34. Will read in "abc def" sans quotes, as: abc def
  35. Same with 'abc def'.
  36. However if `abc def`, then will read in as `abc def`
  37. @param endstmtchar Character that indicates end of statement
  38. @param tokenchars Include the following characters in tokens apart from A-Z and 0-9
  39. @returns 2 dimensional array containing parsed tokens.
  40. */
  41. function Lens_ParseArgs($args,$endstmtchar=',',$tokenchars='_.-')
  42. {
  43. $pos = 0;
  44. $intoken = false;
  45. $stmtno = 0;
  46. $endquote = false;
  47. $tokens = array();
  48. $tokens[$stmtno] = array();
  49. $max = strlen($args);
  50. $quoted = false;
  51. $tokarr = array();
  52. while ($pos < $max) {
  53. $ch = substr($args,$pos,1);
  54. switch($ch) {
  55. case ' ':
  56. case "\t":
  57. case "\n":
  58. case "\r":
  59. if (!$quoted) {
  60. if ($intoken) {
  61. $intoken = false;
  62. $tokens[$stmtno][] = implode('',$tokarr);
  63. }
  64. break;
  65. }
  66. $tokarr[] = $ch;
  67. break;
  68. case '`':
  69. if ($intoken) $tokarr[] = $ch;
  70. case '(':
  71. case ')':
  72. case '"':
  73. case "'":
  74. if ($intoken) {
  75. if (empty($endquote)) {
  76. $tokens[$stmtno][] = implode('',$tokarr);
  77. if ($ch == '(') $endquote = ')';
  78. else $endquote = $ch;
  79. $quoted = true;
  80. $intoken = true;
  81. $tokarr = array();
  82. } else if ($endquote == $ch) {
  83. $ch2 = substr($args,$pos+1,1);
  84. if ($ch2 == $endquote) {
  85. $pos += 1;
  86. $tokarr[] = $ch2;
  87. } else {
  88. $quoted = false;
  89. $intoken = false;
  90. $tokens[$stmtno][] = implode('',$tokarr);
  91. $endquote = '';
  92. }
  93. } else
  94. $tokarr[] = $ch;
  95. }else {
  96. if ($ch == '(') $endquote = ')';
  97. else $endquote = $ch;
  98. $quoted = true;
  99. $intoken = true;
  100. $tokarr = array();
  101. if ($ch == '`') $tokarr[] = '`';
  102. }
  103. break;
  104. default:
  105. if (!$intoken) {
  106. if ($ch == $endstmtchar) {
  107. $stmtno += 1;
  108. $tokens[$stmtno] = array();
  109. break;
  110. }
  111. $intoken = true;
  112. $quoted = false;
  113. $endquote = false;
  114. $tokarr = array();
  115. }
  116. if ($quoted) $tokarr[] = $ch;
  117. else if (ctype_alnum($ch) || strpos($tokenchars,$ch) !== false) $tokarr[] = $ch;
  118. else {
  119. if ($ch == $endstmtchar) {
  120. $tokens[$stmtno][] = implode('',$tokarr);
  121. $stmtno += 1;
  122. $tokens[$stmtno] = array();
  123. $intoken = false;
  124. $tokarr = array();
  125. break;
  126. }
  127. $tokens[$stmtno][] = implode('',$tokarr);
  128. $tokens[$stmtno][] = $ch;
  129. $intoken = false;
  130. }
  131. }
  132. $pos += 1;
  133. }
  134. if ($intoken) $tokens[$stmtno][] = implode('',$tokarr);
  135. return $tokens;
  136. }
  137. class ADODB_DataDict {
  138. var $connection;
  139. var $debug = false;
  140. var $dropTable = 'DROP TABLE %s';
  141. var $renameTable = 'RENAME TABLE %s TO %s';
  142. var $dropIndex = 'DROP INDEX %s';
  143. var $addCol = ' ADD';
  144. var $alterCol = ' ALTER COLUMN';
  145. var $dropCol = ' DROP COLUMN';
  146. var $renameColumn = 'ALTER TABLE %s RENAME COLUMN %s TO %s'; // table, old-column, new-column, column-definitions (not used by default)
  147. var $nameRegex = '\w';
  148. var $nameRegexBrackets = 'a-zA-Z0-9_\(\)';
  149. var $schema = false;
  150. var $serverInfo = array();
  151. var $autoIncrement = false;
  152. var $dataProvider;
  153. var $invalidResizeTypes4 = array('CLOB','BLOB','TEXT','DATE','TIME'); // for changetablesql
  154. var $blobSize = 100; /// any varchar/char field this size or greater is treated as a blob
  155. /// in other words, we use a text area for editting.
  156. function GetCommentSQL($table,$col)
  157. {
  158. return false;
  159. }
  160. function SetCommentSQL($table,$col,$cmt)
  161. {
  162. return false;
  163. }
  164. function MetaTables()
  165. {
  166. if (!$this->connection->IsConnected()) return array();
  167. return $this->connection->MetaTables();
  168. }
  169. function MetaColumns($tab, $upper=true, $schema=false)
  170. {
  171. if (!$this->connection->IsConnected()) return array();
  172. return $this->connection->MetaColumns($this->TableName($tab), $upper, $schema);
  173. }
  174. function MetaPrimaryKeys($tab,$owner=false,$intkey=false)
  175. {
  176. if (!$this->connection->IsConnected()) return array();
  177. return $this->connection->MetaPrimaryKeys($this->TableName($tab), $owner, $intkey);
  178. }
  179. function MetaIndexes($table, $primary = false, $owner = false)
  180. {
  181. if (!$this->connection->IsConnected()) return array();
  182. return $this->connection->MetaIndexes($this->TableName($table), $primary, $owner);
  183. }
  184. function MetaType($t,$len=-1,$fieldobj=false)
  185. {
  186. return ADORecordSet::MetaType($t,$len,$fieldobj);
  187. }
  188. function NameQuote($name = NULL,$allowBrackets=false)
  189. {
  190. if (!is_string($name)) {
  191. return FALSE;
  192. }
  193. $name = trim($name);
  194. if ( !is_object($this->connection) ) {
  195. return $name;
  196. }
  197. $quote = $this->connection->nameQuote;
  198. // if name is of the form `name`, quote it
  199. if ( preg_match('/^`(.+)`$/', $name, $matches) ) {
  200. return $quote . $matches[1] . $quote;
  201. }
  202. // if name contains special characters, quote it
  203. $regex = ($allowBrackets) ? $this->nameRegexBrackets : $this->nameRegex;
  204. if ( !preg_match('/^[' . $regex . ']+$/', $name) ) {
  205. return $quote . $name . $quote;
  206. }
  207. return $name;
  208. }
  209. function TableName($name)
  210. {
  211. if ( $this->schema ) {
  212. return $this->NameQuote($this->schema) .'.'. $this->NameQuote($name);
  213. }
  214. return $this->NameQuote($name);
  215. }
  216. // Executes the sql array returned by GetTableSQL and GetIndexSQL
  217. function ExecuteSQLArray($sql, $continueOnError = true)
  218. {
  219. $rez = 2;
  220. $conn = &$this->connection;
  221. $saved = $conn->debug;
  222. foreach($sql as $line) {
  223. if ($this->debug) $conn->debug = true;
  224. $ok = $conn->Execute($line);
  225. $conn->debug = $saved;
  226. if (!$ok) {
  227. if ($this->debug) ADOConnection::outp($conn->ErrorMsg());
  228. if (!$continueOnError) return 0;
  229. $rez = 1;
  230. }
  231. }
  232. return $rez;
  233. }
  234. /*
  235. Returns the actual type given a character code.
  236. C: varchar
  237. X: CLOB (character large object) or largest varchar size if CLOB is not supported
  238. C2: Multibyte varchar
  239. X2: Multibyte CLOB
  240. B: BLOB (binary large object)
  241. D: Date
  242. T: Date-time
  243. L: Integer field suitable for storing booleans (0 or 1)
  244. I: Integer
  245. F: Floating point number
  246. N: Numeric or decimal number
  247. */
  248. function ActualType($meta)
  249. {
  250. return $meta;
  251. }
  252. function CreateDatabase($dbname,$options=false)
  253. {
  254. $options = $this->_Options($options);
  255. $sql = array();
  256. $s = 'CREATE DATABASE ' . $this->NameQuote($dbname);
  257. if (isset($options[$this->upperName]))
  258. $s .= ' '.$options[$this->upperName];
  259. $sql[] = $s;
  260. return $sql;
  261. }
  262. /*
  263. Generates the SQL to create index. Returns an array of sql strings.
  264. */
  265. function CreateIndexSQL($idxname, $tabname, $flds, $idxoptions = false)
  266. {
  267. if (!is_array($flds)) {
  268. $flds = explode(',',$flds);
  269. }
  270. foreach($flds as $key => $fld) {
  271. # some indexes can use partial fields, eg. index first 32 chars of "name" with NAME(32)
  272. $flds[$key] = $this->NameQuote($fld,$allowBrackets=true);
  273. }
  274. return $this->_IndexSQL($this->NameQuote($idxname), $this->TableName($tabname), $flds, $this->_Options($idxoptions));
  275. }
  276. function DropIndexSQL ($idxname, $tabname = NULL)
  277. {
  278. return array(sprintf($this->dropIndex, $this->NameQuote($idxname), $this->TableName($tabname)));
  279. }
  280. function SetSchema($schema)
  281. {
  282. $this->schema = $schema;
  283. }
  284. function AddColumnSQL($tabname, $flds)
  285. {
  286. $tabname = $this->TableName ($tabname);
  287. $sql = array();
  288. list($lines,$pkey) = $this->_GenFields($flds);
  289. $alter = 'ALTER TABLE ' . $tabname . $this->addCol . ' ';
  290. foreach($lines as $v) {
  291. $sql[] = $alter . $v;
  292. }
  293. return $sql;
  294. }
  295. /**
  296. * Change the definition of one column
  297. *
  298. * As some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
  299. * to allow, recreating the table and copying the content over to the new table
  300. * @param string $tabname table-name
  301. * @param string $flds column-name and type for the changed column
  302. * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
  303. * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
  304. * @return array with SQL strings
  305. */
  306. function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
  307. {
  308. $tabname = $this->TableName ($tabname);
  309. $sql = array();
  310. list($lines,$pkey) = $this->_GenFields($flds);
  311. $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
  312. foreach($lines as $v) {
  313. $sql[] = $alter . $v;
  314. }
  315. return $sql;
  316. }
  317. /**
  318. * Rename one column
  319. *
  320. * Some DBM's can only do this together with changeing the type of the column (even if that stays the same, eg. mysql)
  321. * @param string $tabname table-name
  322. * @param string $oldcolumn column-name to be renamed
  323. * @param string $newcolumn new column-name
  324. * @param string $flds='' complete column-defintion-string like for AddColumnSQL, only used by mysql atm., default=''
  325. * @return array with SQL strings
  326. */
  327. function RenameColumnSQL($tabname,$oldcolumn,$newcolumn,$flds='')
  328. {
  329. $tabname = $this->TableName ($tabname);
  330. if ($flds) {
  331. list($lines,$pkey) = $this->_GenFields($flds);
  332. list(,$first) = each($lines);
  333. list(,$column_def) = split("[\t ]+",$first,2);
  334. }
  335. return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def));
  336. }
  337. /**
  338. * Drop one column
  339. *
  340. * Some DBM's can't do that on there own, you need to supply the complete defintion of the new table,
  341. * to allow, recreating the table and copying the content over to the new table
  342. * @param string $tabname table-name
  343. * @param string $flds column-name and type for the changed column
  344. * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default ''
  345. * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default ''
  346. * @return array with SQL strings
  347. */
  348. function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
  349. {
  350. $tabname = $this->TableName ($tabname);
  351. if (!is_array($flds)) $flds = explode(',',$flds);
  352. $sql = array();
  353. $alter = 'ALTER TABLE ' . $tabname . $this->dropCol . ' ';
  354. foreach($flds as $v) {
  355. $sql[] = $alter . $this->NameQuote($v);
  356. }
  357. return $sql;
  358. }
  359. function DropTableSQL($tabname)
  360. {
  361. return array (sprintf($this->dropTable, $this->TableName($tabname)));
  362. }
  363. function RenameTableSQL($tabname,$newname)
  364. {
  365. return array (sprintf($this->renameTable, $this->TableName($tabname),$this->TableName($newname)));
  366. }
  367. /*
  368. Generate the SQL to create table. Returns an array of sql strings.
  369. */
  370. function CreateTableSQL($tabname, $flds, $tableoptions=false)
  371. {
  372. if (!$tableoptions) $tableoptions = array();
  373. list($lines,$pkey) = $this->_GenFields($flds, true);
  374. $taboptions = $this->_Options($tableoptions);
  375. $tabname = $this->TableName ($tabname);
  376. $sql = $this->_TableSQL($tabname,$lines,$pkey,$taboptions);
  377. $tsql = $this->_Triggers($tabname,$taboptions);
  378. foreach($tsql as $s) $sql[] = $s;
  379. return $sql;
  380. }
  381. function _GenFields($flds,$widespacing=false)
  382. {
  383. if (is_string($flds)) {
  384. $padding = ' ';
  385. $txt = $flds.$padding;
  386. $flds = array();
  387. $flds0 = Lens_ParseArgs($txt,',');
  388. $hasparam = false;
  389. foreach($flds0 as $f0) {
  390. $f1 = array();
  391. foreach($f0 as $token) {
  392. switch (strtoupper($token)) {
  393. case 'CONSTRAINT':
  394. case 'DEFAULT':
  395. $hasparam = $token;
  396. break;
  397. default:
  398. if ($hasparam) $f1[$hasparam] = $token;
  399. else $f1[] = $token;
  400. $hasparam = false;
  401. break;
  402. }
  403. }
  404. $flds[] = $f1;
  405. }
  406. }
  407. $this->autoIncrement = false;
  408. $lines = array();
  409. $pkey = array();
  410. foreach($flds as $fld) {
  411. $fld = _array_change_key_case($fld);
  412. $fname = false;
  413. $fdefault = false;
  414. $fautoinc = false;
  415. $ftype = false;
  416. $fsize = false;
  417. $fprec = false;
  418. $fprimary = false;
  419. $fnoquote = false;
  420. $fdefts = false;
  421. $fdefdate = false;
  422. $fconstraint = false;
  423. $fnotnull = false;
  424. $funsigned = false;
  425. //-----------------
  426. // Parse attributes
  427. foreach($fld as $attr => $v) {
  428. if ($attr == 2 && is_numeric($v)) $attr = 'SIZE';
  429. else if (is_numeric($attr) && $attr > 1 && !is_numeric($v)) $attr = strtoupper($v);
  430. switch($attr) {
  431. case '0':
  432. case 'NAME': $fname = $v; break;
  433. case '1':
  434. case 'TYPE': $ty = $v; $ftype = $this->ActualType(strtoupper($v)); break;
  435. case 'SIZE':
  436. $dotat = strpos($v,'.'); if ($dotat === false) $dotat = strpos($v,',');
  437. if ($dotat === false) $fsize = $v;
  438. else {
  439. $fsize = substr($v,0,$dotat);
  440. $fprec = substr($v,$dotat+1);
  441. }
  442. break;
  443. case 'UNSIGNED': $funsigned = true; break;
  444. case 'AUTOINCREMENT':
  445. case 'AUTO': $fautoinc = true; $fnotnull = true; break;
  446. case 'KEY':
  447. case 'PRIMARY': $fprimary = $v; $fnotnull = true; break;
  448. case 'DEF':
  449. case 'DEFAULT': $fdefault = $v; break;
  450. case 'NOTNULL': $fnotnull = $v; break;
  451. case 'NOQUOTE': $fnoquote = $v; break;
  452. case 'DEFDATE': $fdefdate = $v; break;
  453. case 'DEFTIMESTAMP': $fdefts = $v; break;
  454. case 'CONSTRAINT': $fconstraint = $v; break;
  455. } //switch
  456. } // foreach $fld
  457. //--------------------
  458. // VALIDATE FIELD INFO
  459. if (!strlen($fname)) {
  460. if ($this->debug) ADOConnection::outp("Undefined NAME");
  461. return false;
  462. }
  463. $fid = strtoupper(preg_replace('/^`(.+)`$/', '$1', $fname));
  464. $fname = $this->NameQuote($fname);
  465. if (!strlen($ftype)) {
  466. if ($this->debug) ADOConnection::outp("Undefined TYPE for field '$fname'");
  467. return false;
  468. } else {
  469. $ftype = strtoupper($ftype);
  470. }
  471. $ftype = $this->_GetSize($ftype, $ty, $fsize, $fprec);
  472. if ($ty == 'X' || $ty == 'X2' || $ty == 'B') $fnotnull = false; // some blob types do not accept nulls
  473. if ($fprimary) $pkey[] = $fname;
  474. // some databases do not allow blobs to have defaults
  475. if ($ty == 'X') $fdefault = false;
  476. //--------------------
  477. // CONSTRUCT FIELD SQL
  478. if ($fdefts) {
  479. if (substr($this->connection->databaseType,0,5) == 'mysql') {
  480. $ftype = 'TIMESTAMP';
  481. } else {
  482. $fdefault = $this->connection->sysTimeStamp;
  483. }
  484. } else if ($fdefdate) {
  485. if (substr($this->connection->databaseType,0,5) == 'mysql') {
  486. $ftype = 'TIMESTAMP';
  487. } else {
  488. $fdefault = $this->connection->sysDate;
  489. }
  490. } else if ($fdefault !== false && !$fnoquote)
  491. if ($ty == 'C' or $ty == 'X' or
  492. ( substr($fdefault,0,1) != "'" && !is_numeric($fdefault)))
  493. if (strlen($fdefault) != 1 && substr($fdefault,0,1) == ' ' && substr($fdefault,strlen($fdefault)-1) == ' ')
  494. $fdefault = trim($fdefault);
  495. else if (strtolower($fdefault) != 'null')
  496. $fdefault = $this->connection->qstr($fdefault);
  497. $suffix = $this->_CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned);
  498. if ($widespacing) $fname = str_pad($fname,24);
  499. $lines[$fid] = $fname.' '.$ftype.$suffix;
  500. if ($fautoinc) $this->autoIncrement = true;
  501. } // foreach $flds
  502. return array($lines,$pkey);
  503. }
  504. /*
  505. GENERATE THE SIZE PART OF THE DATATYPE
  506. $ftype is the actual type
  507. $ty is the type defined originally in the DDL
  508. */
  509. function _GetSize($ftype, $ty, $fsize, $fprec)
  510. {
  511. if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) {
  512. $ftype .= "(".$fsize;
  513. if (strlen($fprec)) $ftype .= ",".$fprec;
  514. $ftype .= ')';
  515. }
  516. return $ftype;
  517. }
  518. // return string must begin with space
  519. function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
  520. {
  521. $suffix = '';
  522. if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
  523. if ($fnotnull) $suffix .= ' NOT NULL';
  524. if ($fconstraint) $suffix .= ' '.$fconstraint;
  525. return $suffix;
  526. }
  527. function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
  528. {
  529. $sql = array();
  530. if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
  531. $sql[] = sprintf ($this->dropIndex, $idxname);
  532. if ( isset($idxoptions['DROP']) )
  533. return $sql;
  534. }
  535. if ( empty ($flds) ) {
  536. return $sql;
  537. }
  538. $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
  539. $s = 'CREATE' . $unique . ' INDEX ' . $idxname . ' ON ' . $tabname . ' ';
  540. if ( isset($idxoptions[$this->upperName]) )
  541. $s .= $idxoptions[$this->upperName];
  542. if ( is_array($flds) )
  543. $flds = implode(', ',$flds);
  544. $s .= '(' . $flds . ')';
  545. $sql[] = $s;
  546. return $sql;
  547. }
  548. function _DropAutoIncrement($tabname)
  549. {
  550. return false;
  551. }
  552. function _TableSQL($tabname,$lines,$pkey,$tableoptions)
  553. {
  554. $sql = array();
  555. if (isset($tableoptions['REPLACE']) || isset ($tableoptions['DROP'])) {
  556. $sql[] = sprintf($this->dropTable,$tabname);
  557. if ($this->autoIncrement) {
  558. $sInc = $this->_DropAutoIncrement($tabname);
  559. if ($sInc) $sql[] = $sInc;
  560. }
  561. if ( isset ($tableoptions['DROP']) ) {
  562. return $sql;
  563. }
  564. }
  565. $s = "CREATE TABLE $tabname (\n";
  566. $s .= implode(",\n", $lines);
  567. if (sizeof($pkey)>0) {
  568. $s .= ",\n PRIMARY KEY (";
  569. $s .= implode(", ",$pkey).")";
  570. }
  571. if (isset($tableoptions['CONSTRAINTS']))
  572. $s .= "\n".$tableoptions['CONSTRAINTS'];
  573. if (isset($tableoptions[$this->upperName.'_CONSTRAINTS']))
  574. $s .= "\n".$tableoptions[$this->upperName.'_CONSTRAINTS'];
  575. $s .= "\n)";
  576. if (isset($tableoptions[$this->upperName])) $s .= $tableoptions[$this->upperName];
  577. $sql[] = $s;
  578. return $sql;
  579. }
  580. /*
  581. GENERATE TRIGGERS IF NEEDED
  582. used when table has auto-incrementing field that is emulated using triggers
  583. */
  584. function _Triggers($tabname,$taboptions)
  585. {
  586. return array();
  587. }
  588. /*
  589. Sanitize options, so that array elements with no keys are promoted to keys
  590. */
  591. function _Options($opts)
  592. {
  593. if (!is_array($opts)) return array();
  594. $newopts = array();
  595. foreach($opts as $k => $v) {
  596. if (is_numeric($k)) $newopts[strtoupper($v)] = $v;
  597. else $newopts[strtoupper($k)] = $v;
  598. }
  599. return $newopts;
  600. }
  601. /*
  602. "Florian Buzin [ easywe ]" <florian.buzin#easywe.de>
  603. This function changes/adds new fields to your table. You don't
  604. have to know if the col is new or not. It will check on its own.
  605. */
  606. function ChangeTableSQL($tablename, $flds, $tableoptions = false)
  607. {
  608. global $ADODB_FETCH_MODE;
  609. $save = $ADODB_FETCH_MODE;
  610. $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
  611. if ($this->connection->fetchMode !== false) $savem = $this->connection->SetFetchMode(false);
  612. // check table exists
  613. $save_handler = $this->connection->raiseErrorFn;
  614. $this->connection->raiseErrorFn = '';
  615. $cols = $this->MetaColumns($tablename);
  616. $this->connection->raiseErrorFn = $save_handler;
  617. if (isset($savem)) $this->connection->SetFetchMode($savem);
  618. $ADODB_FETCH_MODE = $save;
  619. if ( empty($cols)) {
  620. return $this->CreateTableSQL($tablename, $flds, $tableoptions);
  621. }
  622. if (is_array($flds)) {
  623. // Cycle through the update fields, comparing
  624. // existing fields to fields to update.
  625. // if the Metatype and size is exactly the
  626. // same, ignore - by Mark Newham
  627. $holdflds = array();
  628. foreach($flds as $k=>$v) {
  629. if ( isset($cols[$k]) && is_object($cols[$k]) ) {
  630. // If already not allowing nulls, then don't change
  631. $obj = $cols[$k];
  632. if (isset($obj->not_null) && $obj->not_null)
  633. $v = str_replace('NOT NULL','',$v);
  634. $c = $cols[$k];
  635. $ml = $c->max_length;
  636. $mt = $this->MetaType($c->type,$ml);
  637. if ($ml == -1) $ml = '';
  638. if ($mt == 'X') $ml = $v['SIZE'];
  639. if (($mt != $v['TYPE']) || $ml != $v['SIZE']) {
  640. $holdflds[$k] = $v;
  641. }
  642. } else {
  643. $holdflds[$k] = $v;
  644. }
  645. }
  646. $flds = $holdflds;
  647. }
  648. // already exists, alter table instead
  649. list($lines,$pkey) = $this->_GenFields($flds);
  650. $alter = 'ALTER TABLE ' . $this->TableName($tablename);
  651. $sql = array();
  652. foreach ( $lines as $id => $v ) {
  653. if ( isset($cols[$id]) && is_object($cols[$id]) ) {
  654. $flds = Lens_ParseArgs($v,',');
  655. // We are trying to change the size of the field, if not allowed, simply ignore the request.
  656. if ($flds && in_array(strtoupper(substr($flds[0][1],0,4)),$this->invalidResizeTypes4)) continue;
  657. $sql[] = $alter . $this->alterCol . ' ' . $v;
  658. } else {
  659. $sql[] = $alter . $this->addCol . ' ' . $v;
  660. }
  661. }
  662. return $sql;
  663. }
  664. } // class
  665. ?>