sqlsrv.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Authors: Frank M. Kromann <frank@kromann.info> |
  43. // | David Coallier <davidc@php.net> |
  44. // | Lorenzo Alberton <l.alberton@quipo.it> |
  45. // +----------------------------------------------------------------------+
  46. require_once 'MDB2/Driver/Manager/Common.php';
  47. // {{{ class MDB2_Driver_Manager_sqlsrv
  48. /**
  49. * MDB2 MSSQL driver for the management modules
  50. *
  51. * @package MDB2
  52. * @category Database
  53. * @author Frank M. Kromann <frank@kromann.info>
  54. * @author David Coallier <davidc@php.net>
  55. * @author Lorenzo Alberton <l.alberton@quipo.it>
  56. */
  57. class MDB2_Driver_Manager_sqlsrv extends MDB2_Driver_Manager_Common
  58. {
  59. // {{{ createDatabase()
  60. /**
  61. * create a new database
  62. *
  63. * @param string $name name of the database that should be created
  64. * @param array $options array with collation info
  65. *
  66. * @return mixed MDB2_OK on success, a MDB2 error on failure
  67. * @access public
  68. */
  69. function createDatabase($name, $options = array())
  70. {
  71. $db = $this->getDBInstance();
  72. if (MDB2::isError($db)) {
  73. return $db;
  74. }
  75. $name = $db->quoteIdentifier($name, true);
  76. $query = "CREATE DATABASE $name";
  77. if ($db->options['database_device']) {
  78. $query.= ' ON '.$db->options['database_device'];
  79. $query.= $db->options['database_size'] ? '=' .
  80. $db->options['database_size'] : '';
  81. }
  82. if (!empty($options['collation'])) {
  83. $query .= ' COLLATE ' . $options['collation'];
  84. }
  85. return $db->standaloneQuery($query, null, true);
  86. }
  87. // }}}
  88. // {{{ alterDatabase()
  89. /**
  90. * alter an existing database
  91. *
  92. * @param string $name name of the database that is intended to be changed
  93. * @param array $options array with name, collation info
  94. *
  95. * @return mixed MDB2_OK on success, a MDB2 error on failure
  96. * @access public
  97. */
  98. function alterDatabase($name, $options = array())
  99. {
  100. $db = $this->getDBInstance();
  101. if (MDB2::isError($db)) {
  102. return $db;
  103. }
  104. $query = '';
  105. if (!empty($options['name'])) {
  106. $query .= ' MODIFY NAME = ' .$db->quoteIdentifier($options['name'], true);
  107. }
  108. if (!empty($options['collation'])) {
  109. $query .= ' COLLATE ' . $options['collation'];
  110. }
  111. if (!empty($query)) {
  112. $query = 'ALTER DATABASE '. $db->quoteIdentifier($name, true) . $query;
  113. return $db->standaloneQuery($query, null, true);
  114. }
  115. return MDB2_OK;
  116. }
  117. // }}}
  118. // {{{ dropDatabase()
  119. /**
  120. * drop an existing database
  121. *
  122. * @param string $name name of the database that should be dropped
  123. *
  124. * @return mixed MDB2_OK on success, a MDB2 error on failure
  125. * @access public
  126. */
  127. function dropDatabase($name)
  128. {
  129. $db = $this->getDBInstance();
  130. if (MDB2::isError($db)) {
  131. return $db;
  132. }
  133. $name = $db->quoteIdentifier($name, true);
  134. return $db->standaloneQuery("DROP DATABASE $name", null, true);
  135. }
  136. // }}}
  137. // {{{ dropTable()
  138. /**
  139. * drop an existing table
  140. *
  141. * @param string $name name of the table that should be dropped
  142. * @return mixed MDB2_OK on success, a MDB2 error on failure
  143. * @access public
  144. */
  145. function dropTable($name)
  146. {
  147. $db = $this->getDBInstance();
  148. if (MDB2::isError($db)) {
  149. return $db;
  150. }
  151. $name = $db->quoteIdentifier($name, true);
  152. $result = $db->exec("IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='$name') DROP TABLE $name");
  153. if (MDB2::isError($result)) {
  154. return $result;
  155. }
  156. return MDB2_OK;
  157. }
  158. // }}}
  159. // {{{ _getTemporaryTableQuery()
  160. /**
  161. * Override the parent method.
  162. *
  163. * @return string The string required to be placed between "CREATE" and "TABLE"
  164. * to generate a temporary table, if possible.
  165. */
  166. function _getTemporaryTableQuery()
  167. {
  168. return '';
  169. }
  170. // }}}
  171. // {{{ _getAdvancedFKOptions()
  172. /**
  173. * Return the FOREIGN KEY query section dealing with non-standard options
  174. * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  175. *
  176. * @param array $definition
  177. *
  178. * @return string
  179. * @access protected
  180. */
  181. function _getAdvancedFKOptions($definition)
  182. {
  183. $query = '';
  184. if (!empty($definition['onupdate'])) {
  185. $query .= ' ON UPDATE '.$definition['onupdate'];
  186. }
  187. if (!empty($definition['ondelete'])) {
  188. $query .= ' ON DELETE '.$definition['ondelete'];
  189. }
  190. return $query;
  191. }
  192. // }}}
  193. // {{{ createTable()
  194. /**
  195. * create a new table
  196. *
  197. * @param string $name Name of the database that should be created
  198. * @param array $fields Associative array that contains the definition of each field of the new table
  199. * The indexes of the array entries are the names of the fields of the table an
  200. * the array entry values are associative arrays like those that are meant to be
  201. * passed with the field definitions to get[Type]Declaration() functions.
  202. *
  203. * Example
  204. * array(
  205. *
  206. * 'id' => array(
  207. * 'type' => 'integer',
  208. * 'unsigned' => 1,
  209. * 'notnull' => 1,
  210. * 'default' => 0,
  211. * ),
  212. * 'name' => array(
  213. * 'type' => 'text',
  214. * 'length' => 12,
  215. * ),
  216. * 'description' => array(
  217. * 'type' => 'text',
  218. * 'length' => 12,
  219. * )
  220. * );
  221. * @param array $options An associative array of table options:
  222. * array(
  223. * 'comment' => 'Foo',
  224. * 'temporary' => true|false,
  225. * );
  226. *
  227. * @return mixed MDB2_OK on success, a MDB2 error on failure
  228. * @access public
  229. */
  230. function createTable($name, $fields, $options = array())
  231. {
  232. /*if (!empty($options['temporary'])) {
  233. $name = '#'.$name;//would make subsequent calls fail because it would go out ot scope and be destroyed already
  234. }*/
  235. return parent::createTable($name, $fields, $options);
  236. }
  237. // }}}
  238. // {{{ truncateTable()
  239. /**
  240. * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
  241. * it falls back to a DELETE FROM TABLE query)
  242. *
  243. * @param string $name name of the table that should be truncated
  244. * @return mixed MDB2_OK on success, a MDB2 error on failure
  245. * @access public
  246. */
  247. function truncateTable($name)
  248. {
  249. $db = $this->getDBInstance();
  250. if (MDB2::isError($db)) {
  251. return $db;
  252. }
  253. $name = $db->quoteIdentifier($name, true);
  254. $result = $db->exec("TRUNCATE TABLE $name");
  255. if (MDB2::isError($result)) {
  256. return $result;
  257. }
  258. return MDB2_OK;
  259. }
  260. // }}}
  261. // {{{ vacuum()
  262. /**
  263. * Optimize (vacuum) all the tables in the db (or only the specified table)
  264. * and optionally run ANALYZE.
  265. *
  266. * @param string $table table name (all the tables if empty)
  267. * @param array $options an array with driver-specific options:
  268. * - timeout [int] (in seconds) [mssql-only]
  269. * - analyze [boolean] [pgsql and mysql]
  270. * - full [boolean] [pgsql-only]
  271. * - freeze [boolean] [pgsql-only]
  272. *
  273. * NB: you have to run the NSControl Create utility to enable VACUUM
  274. *
  275. * @return mixed MDB2_OK success, a MDB2 error on failure
  276. * @access public
  277. */
  278. function vacuum($table = null, $options = array())
  279. {
  280. $db = $this->getDBInstance();
  281. if (MDB2::isError($db)) {
  282. return $db;
  283. }
  284. $timeout = isset($options['timeout']) ? (int)$options['timeout'] : 300;
  285. $query = 'NSControl Create';
  286. $result = $db->exec($query);
  287. if (MDB2::isError($result)) {
  288. return $result;
  289. }
  290. $result = $db->exec('EXEC NSVacuum '.$timeout);
  291. if (MDB2::isError($result)) {
  292. return $result;
  293. }
  294. return MDB2_OK;
  295. }
  296. // }}}
  297. // {{{ alterTable()
  298. /**
  299. * alter an existing table
  300. *
  301. * @param string $name name of the table that is intended to be changed.
  302. * @param array $changes associative array that contains the details of each type
  303. * of change that is intended to be performed. The types of
  304. * changes that are currently supported are defined as follows:
  305. *
  306. * name
  307. *
  308. * New name for the table.
  309. *
  310. * add
  311. *
  312. * Associative array with the names of fields to be added as
  313. * indexes of the array. The value of each entry of the array
  314. * should be set to another associative array with the properties
  315. * of the fields to be added. The properties of the fields should
  316. * be the same as defined by the MDB2 parser.
  317. *
  318. *
  319. * remove
  320. *
  321. * Associative array with the names of fields to be removed as indexes
  322. * of the array. Currently the values assigned to each entry are ignored.
  323. * An empty array should be used for future compatibility.
  324. *
  325. * rename
  326. *
  327. * Associative array with the names of fields to be renamed as indexes
  328. * of the array. The value of each entry of the array should be set to
  329. * another associative array with the entry named name with the new
  330. * field name and the entry named Declaration that is expected to contain
  331. * the portion of the field declaration already in DBMS specific SQL code
  332. * as it is used in the CREATE TABLE statement.
  333. *
  334. * change
  335. *
  336. * Associative array with the names of the fields to be changed as indexes
  337. * of the array. Keep in mind that if it is intended to change either the
  338. * name of a field and any other properties, the change array entries
  339. * should have the new names of the fields as array indexes.
  340. *
  341. * The value of each entry of the array should be set to another associative
  342. * array with the properties of the fields to that are meant to be changed as
  343. * array entries. These entries should be assigned to the new values of the
  344. * respective properties. The properties of the fields should be the same
  345. * as defined by the MDB2 parser.
  346. *
  347. * Example
  348. * array(
  349. * 'name' => 'userlist',
  350. * 'add' => array(
  351. * 'quota' => array(
  352. * 'type' => 'integer',
  353. * 'unsigned' => 1
  354. * )
  355. * ),
  356. * 'remove' => array(
  357. * 'file_limit' => array(),
  358. * 'time_limit' => array()
  359. * ),
  360. * 'change' => array(
  361. * 'name' => array(
  362. * 'length' => '20',
  363. * 'definition' => array(
  364. * 'type' => 'text',
  365. * 'length' => 20,
  366. * ),
  367. * )
  368. * ),
  369. * 'rename' => array(
  370. * 'sex' => array(
  371. * 'name' => 'gender',
  372. * 'definition' => array(
  373. * 'type' => 'text',
  374. * 'length' => 1,
  375. * 'default' => 'M',
  376. * ),
  377. * )
  378. * )
  379. * )
  380. *
  381. * @param boolean $check indicates whether the function should just check if the DBMS driver
  382. * can perform the requested table alterations if the value is true or
  383. * actually perform them otherwise.
  384. *
  385. * @return mixed MDB2_OK on success, a MDB2 error on failure
  386. * @access public
  387. */
  388. function alterTable($name, $changes, $check)
  389. {
  390. $db = $this->getDBInstance();
  391. if (MDB2::isError($db)) {
  392. return $db;
  393. }
  394. $name_quoted = $db->quoteIdentifier($name, true);
  395. foreach ($changes as $change_name => $change) {
  396. switch ($change_name) {
  397. case 'remove':
  398. case 'rename':
  399. case 'add':
  400. case 'change':
  401. case 'name':
  402. break;
  403. default:
  404. return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  405. 'change type "'.$change_name.'" not yet supported', __FUNCTION__);
  406. }
  407. }
  408. if ($check) {
  409. return MDB2_OK;
  410. }
  411. $idxname_format = $db->getOption('idxname_format');
  412. $db->setOption('idxname_format', '%s');
  413. if (!empty($changes['remove']) && is_array($changes['remove'])) {
  414. $result = $this->_dropConflictingIndices($name, array_keys($changes['remove']));
  415. if (MDB2::isError($result)) {
  416. $db->setOption('idxname_format', $idxname_format);
  417. return $result;
  418. }
  419. $result = $this->_dropConflictingConstraints($name, array_keys($changes['remove']));
  420. if (MDB2::isError($result)) {
  421. $db->setOption('idxname_format', $idxname_format);
  422. return $result;
  423. }
  424. $query = '';
  425. foreach ($changes['remove'] as $field_name => $field) {
  426. if ($query) {
  427. $query.= ', ';
  428. }
  429. $field_name = $db->quoteIdentifier($field_name, true);
  430. $query.= 'COLUMN ' . $field_name;
  431. }
  432. $result = $db->exec("ALTER TABLE $name_quoted DROP $query");
  433. if (MDB2::isError($result)) {
  434. $db->setOption('idxname_format', $idxname_format);
  435. return $result;
  436. }
  437. }
  438. if (!empty($changes['rename']) && is_array($changes['rename'])) {
  439. foreach ($changes['rename'] as $field_name => $field) {
  440. $field_name = $db->quoteIdentifier($field_name, true);
  441. $result = $db->exec("sp_rename '$name_quoted.$field_name', '".$field['name']."', 'COLUMN'");
  442. if (MDB2::isError($result)) {
  443. $db->setOption('idxname_format', $idxname_format);
  444. return $result;
  445. }
  446. }
  447. }
  448. if (!empty($changes['add']) && is_array($changes['add'])) {
  449. $query = '';
  450. foreach ($changes['add'] as $field_name => $field) {
  451. if ($query) {
  452. $query.= ', ';
  453. } else {
  454. $query.= 'ADD ';
  455. }
  456. $query.= $db->getDeclaration($field['type'], $field_name, $field);
  457. }
  458. $result = $db->exec("ALTER TABLE $name_quoted $query");
  459. if (MDB2::isError($result)) {
  460. $db->setOption('idxname_format', $idxname_format);
  461. return $result;
  462. }
  463. }
  464. $dropped_indices = array();
  465. $dropped_constraints = array();
  466. if (!empty($changes['change']) && is_array($changes['change'])) {
  467. $dropped = $this->_dropConflictingIndices($name, array_keys($changes['change']));
  468. if (MDB2::isError($dropped)) {
  469. $db->setOption('idxname_format', $idxname_format);
  470. return $dropped;
  471. }
  472. $dropped_indices = array_merge($dropped_indices, $dropped);
  473. $dropped = $this->_dropConflictingConstraints($name, array_keys($changes['change']));
  474. if (MDB2::isError($dropped)) {
  475. $db->setOption('idxname_format', $idxname_format);
  476. return $dropped;
  477. }
  478. $dropped_constraints = array_merge($dropped_constraints, $dropped);
  479. foreach ($changes['change'] as $field_name => $field) {
  480. //MSSQL doesn't allow multiple ALTER COLUMNs in one query
  481. $query = 'ALTER COLUMN ';
  482. //MSSQL doesn't allow changing the DEFAULT value of a field in altering mode
  483. if (array_key_exists('default', $field['definition'])) {
  484. unset($field['definition']['default']);
  485. }
  486. $query .= $db->getDeclaration($field['definition']['type'], $field_name, $field['definition']);
  487. $result = $db->exec("ALTER TABLE $name_quoted $query");
  488. if (MDB2::isError($result)) {
  489. $db->setOption('idxname_format', $idxname_format);
  490. return $result;
  491. }
  492. }
  493. }
  494. // restore the dropped conflicting indices and constraints
  495. foreach ($dropped_indices as $index_name => $index) {
  496. $result = $this->createIndex($name, $index_name, $index);
  497. if (MDB2::isError($result)) {
  498. $db->setOption('idxname_format', $idxname_format);
  499. return $result;
  500. }
  501. }
  502. foreach ($dropped_constraints as $constraint_name => $constraint) {
  503. $result = $this->createConstraint($name, $constraint_name, $constraint);
  504. if (MDB2::isError($result)) {
  505. $db->setOption('idxname_format', $idxname_format);
  506. return $result;
  507. }
  508. }
  509. $db->setOption('idxname_format', $idxname_format);
  510. if (!empty($changes['name'])) {
  511. $new_name = $db->quoteIdentifier($changes['name'], true);
  512. $result = $db->exec("sp_rename '$name_quoted', '$new_name'");
  513. if (MDB2::isError($result)) {
  514. return $result;
  515. }
  516. }
  517. return MDB2_OK;
  518. }
  519. // }}}
  520. // {{{ _dropConflictingIndices()
  521. /**
  522. * Drop the indices that prevent a successful ALTER TABLE action
  523. *
  524. * @param string $table table name
  525. * @param array $fields array of names of the fields affected by the change
  526. *
  527. * @return array dropped indices definitions
  528. */
  529. function _dropConflictingIndices($table, $fields)
  530. {
  531. $db = $this->getDBInstance();
  532. if (MDB2::isError($db)) {
  533. return $db;
  534. }
  535. $dropped = array();
  536. $index_names = $this->listTableIndexes($table);
  537. if (MDB2::isError($index_names)) {
  538. return $index_names;
  539. }
  540. $db->loadModule('Reverse');
  541. $indexes = array();
  542. foreach ($index_names as $index_name) {
  543. $idx_def = $db->reverse->getTableIndexDefinition($table, $index_name);
  544. if (!MDB2::isError($idx_def)) {
  545. $indexes[$index_name] = $idx_def;
  546. }
  547. }
  548. foreach ($fields as $field_name) {
  549. foreach ($indexes as $index_name => $index) {
  550. if (!isset($dropped[$index_name]) && array_key_exists($field_name, $index['fields'])) {
  551. $dropped[$index_name] = $index;
  552. $result = $this->dropIndex($table, $index_name);
  553. if (MDB2::isError($result)) {
  554. return $result;
  555. }
  556. }
  557. }
  558. }
  559. return $dropped;
  560. }
  561. // }}}
  562. // {{{ _dropConflictingConstraints()
  563. /**
  564. * Drop the constraints that prevent a successful ALTER TABLE action
  565. *
  566. * @param string $table table name
  567. * @param array $fields array of names of the fields affected by the change
  568. *
  569. * @return array dropped constraints definitions
  570. */
  571. function _dropConflictingConstraints($table, $fields)
  572. {
  573. $db = $this->getDBInstance();
  574. if (MDB2::isError($db)) {
  575. return $db;
  576. }
  577. $dropped = array();
  578. $constraint_names = $this->listTableConstraints($table);
  579. if (MDB2::isError($constraint_names)) {
  580. return $constraint_names;
  581. }
  582. $db->loadModule('Reverse');
  583. $constraints = array();
  584. foreach ($constraint_names as $constraint_name) {
  585. $cons_def = $db->reverse->getTableConstraintDefinition($table, $constraint_name);
  586. if (!MDB2::isError($cons_def)) {
  587. $constraints[$constraint_name] = $cons_def;
  588. }
  589. }
  590. foreach ($fields as $field_name) {
  591. foreach ($constraints as $constraint_name => $constraint) {
  592. if (!isset($dropped[$constraint_name]) && array_key_exists($field_name, $constraint['fields'])) {
  593. $dropped[$constraint_name] = $constraint;
  594. $result = $this->dropConstraint($table, $constraint_name);
  595. if (MDB2::isError($result)) {
  596. return $result;
  597. }
  598. }
  599. }
  600. // also drop implicit DEFAULT constraints
  601. $default = $this->_getTableFieldDefaultConstraint($table, $field_name);
  602. if (!MDB2::isError($default) && !empty($default)) {
  603. $result = $this->dropConstraint($table, $default);
  604. if (MDB2::isError($result)) {
  605. return $result;
  606. }
  607. }
  608. }
  609. return $dropped;
  610. }
  611. // }}}
  612. // {{{ _getTableFieldDefaultConstraint()
  613. /**
  614. * Get the default constraint for a table field
  615. *
  616. * @param string $table name of table that should be used in method
  617. * @param string $field name of field that should be used in method
  618. *
  619. * @return mixed name of default constraint on success, a MDB2 error on failure
  620. * @access private
  621. */
  622. function _getTableFieldDefaultConstraint($table, $field)
  623. {
  624. $db = $this->getDBInstance();
  625. if (MDB2::isError($db)) {
  626. return $db;
  627. }
  628. $table = $db->quoteIdentifier($table, true);
  629. $field = $db->quote($field, 'text');
  630. $query = "SELECT OBJECT_NAME(syscolumns.cdefault)
  631. FROM syscolumns
  632. WHERE syscolumns.id = object_id('$table')
  633. AND syscolumns.name = $field
  634. AND syscolumns.cdefault <> 0";
  635. return $db->queryOne($query);
  636. }
  637. // }}}
  638. // {{{ listTables()
  639. /**
  640. * list all tables in the current database
  641. *
  642. * @return mixed array of table names on success, a MDB2 error on failure
  643. * @access public
  644. */
  645. function listTables()
  646. {
  647. $db = $this->getDBInstance();
  648. if (MDB2::isError($db)) {
  649. return $db;
  650. }
  651. $query = 'EXEC sp_tables @table_type = "\'TABLE\'"';
  652. $table_names = $db->queryCol($query, null, 2);
  653. if (MDB2::isError($table_names)) {
  654. return $table_names;
  655. }
  656. $result = array();
  657. foreach ($table_names as $table_name) {
  658. if (!$this->_fixSequenceName($table_name, true)) {
  659. $result[] = $table_name;
  660. }
  661. }
  662. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  663. $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  664. 'strtolower' : 'strtoupper'), $result);
  665. }
  666. return $result;
  667. }
  668. // }}}
  669. // {{{ listTableFields()
  670. /**
  671. * list all fields in a table in the current database
  672. *
  673. * @param string $table name of table that should be used in method
  674. *
  675. * @return mixed array of field names on success, a MDB2 error on failure
  676. * @access public
  677. */
  678. function listTableFields($table)
  679. {
  680. $db = $this->getDBInstance();
  681. if (MDB2::isError($db)) {
  682. return $db;
  683. }
  684. $table = $db->quoteIdentifier($table, true);
  685. $columns = $db->queryCol("SELECT c.name
  686. FROM syscolumns c
  687. LEFT JOIN sysobjects o ON c.id = o.id
  688. WHERE o.name = '$table'");
  689. if (MDB2::isError($columns)) {
  690. return $columns;
  691. }
  692. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  693. $columns = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $columns);
  694. }
  695. return $columns;
  696. }
  697. // }}}
  698. // {{{ listTableIndexes()
  699. /**
  700. * list all indexes in a table
  701. *
  702. * @param string $table name of table that should be used in method
  703. *
  704. * @return mixed array of index names on success, a MDB2 error on failure
  705. * @access public
  706. */
  707. function listTableIndexes($table)
  708. {
  709. $db = $this->getDBInstance();
  710. if (MDB2::isError($db)) {
  711. return $db;
  712. }
  713. $key_name = 'INDEX_NAME';
  714. $pk_name = 'PK_NAME';
  715. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  716. if ($db->options['field_case'] == CASE_LOWER) {
  717. $key_name = strtolower($key_name);
  718. $pk_name = strtolower($pk_name);
  719. } else {
  720. $key_name = strtoupper($key_name);
  721. $pk_name = strtoupper($pk_name);
  722. }
  723. }
  724. $table = $db->quote($table, 'text');
  725. $query = "EXEC sp_statistics @table_name=$table";
  726. $indexes = $db->queryCol($query, 'text', $key_name);
  727. if (MDB2::isError($indexes)) {
  728. return $indexes;
  729. }
  730. $query = "EXEC sp_pkeys @table_name=$table";
  731. $pk_all = $db->queryCol($query, 'text', $pk_name);
  732. $result = array();
  733. foreach ($indexes as $index) {
  734. if (!in_array($index, $pk_all) && ($index = $this->_fixIndexName($index))) {
  735. $result[$index] = true;
  736. }
  737. }
  738. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  739. $result = array_change_key_case($result, $db->options['field_case']);
  740. }
  741. return array_keys($result);
  742. }
  743. // }}}
  744. // {{{ listDatabases()
  745. /**
  746. * list all databases
  747. *
  748. * @return mixed array of database names on success, a MDB2 error on failure
  749. * @access public
  750. */
  751. function listDatabases()
  752. {
  753. $db = $this->getDBInstance();
  754. if (MDB2::isError($db)) {
  755. return $db;
  756. }
  757. $result = $db->queryCol('SELECT name FROM sys.databases');
  758. if (MDB2::isError($result)) {
  759. return $result;
  760. }
  761. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  762. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  763. }
  764. return $result;
  765. }
  766. // }}}
  767. // {{{ listUsers()
  768. /**
  769. * list all users
  770. *
  771. * @return mixed array of user names on success, a MDB2 error on failure
  772. * @access public
  773. */
  774. function listUsers()
  775. {
  776. $db = $this->getDBInstance();
  777. if (MDB2::isError($db)) {
  778. return $db;
  779. }
  780. $result = $db->queryCol('SELECT DISTINCT loginame FROM master..sysprocesses');
  781. if (MDB2::isError($result) || empty($result)) {
  782. return $result;
  783. }
  784. foreach (array_keys($result) as $k) {
  785. $result[$k] = trim($result[$k]);
  786. }
  787. return $result;
  788. }
  789. // }}}
  790. // {{{ listFunctions()
  791. /**
  792. * list all functions in the current database
  793. *
  794. * @return mixed array of function names on success, a MDB2 error on failure
  795. * @access public
  796. */
  797. function listFunctions()
  798. {
  799. $db = $this->getDBInstance();
  800. if (MDB2::isError($db)) {
  801. return $db;
  802. }
  803. $query = "SELECT name
  804. FROM sysobjects
  805. WHERE objectproperty(id, N'IsMSShipped') = 0
  806. AND (objectproperty(id, N'IsTableFunction') = 1
  807. OR objectproperty(id, N'IsScalarFunction') = 1)";
  808. /*
  809. SELECT ROUTINE_NAME
  810. FROM INFORMATION_SCHEMA.ROUTINES
  811. WHERE ROUTINE_TYPE = 'FUNCTION'
  812. */
  813. $result = $db->queryCol($query);
  814. if (MDB2::isError($result)) {
  815. return $result;
  816. }
  817. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  818. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  819. }
  820. return $result;
  821. }
  822. // }}}
  823. // {{{ listTableTriggers()
  824. /**
  825. * list all triggers in the database that reference a given table
  826. *
  827. * @param string table for which all referenced triggers should be found
  828. *
  829. * @return mixed array of trigger names on success, otherwise, false which
  830. * could be a db error if the db is not instantiated or could
  831. * be the results of the error that occured during the
  832. * querying of the sysobject module.
  833. * @access public
  834. */
  835. function listTableTriggers($table = null)
  836. {
  837. $db = $this->getDBInstance();
  838. if (MDB2::isError($db)) {
  839. return $db;
  840. }
  841. $table = $db->quote($table, 'text');
  842. $query = "SELECT o.name
  843. FROM sysobjects o
  844. WHERE xtype = 'TR'
  845. AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0";
  846. if (null !== $table) {
  847. $query .= " AND object_name(parent_obj) = $table";
  848. }
  849. $result = $db->queryCol($query);
  850. if (MDB2::isError($result)) {
  851. return $result;
  852. }
  853. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE &&
  854. $db->options['field_case'] == CASE_LOWER)
  855. {
  856. $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  857. 'strtolower' : 'strtoupper'), $result);
  858. }
  859. return $result;
  860. }
  861. // }}}
  862. // {{{ listViews()
  863. /**
  864. * list all views in the current database
  865. *
  866. * @param string database, the current is default
  867. *
  868. * @return mixed array of view names on success, a MDB2 error on failure
  869. * @access public
  870. */
  871. function listViews()
  872. {
  873. $db = $this->getDBInstance();
  874. if (MDB2::isError($db)) {
  875. return $db;
  876. }
  877. $query = "SELECT name
  878. FROM sysobjects
  879. WHERE xtype = 'V'";
  880. /*
  881. SELECT *
  882. FROM sysobjects
  883. WHERE objectproperty(id, N'IsMSShipped') = 0
  884. AND objectproperty(id, N'IsView') = 1
  885. */
  886. $result = $db->queryCol($query);
  887. if (MDB2::isError($result)) {
  888. return $result;
  889. }
  890. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE &&
  891. $db->options['field_case'] == CASE_LOWER)
  892. {
  893. $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  894. 'strtolower' : 'strtoupper'), $result);
  895. }
  896. return $result;
  897. }
  898. // }}}
  899. // {{{ dropIndex()
  900. /**
  901. * drop existing index
  902. *
  903. * @param string $table name of table that should be used in method
  904. * @param string $name name of the index to be dropped
  905. *
  906. * @return mixed MDB2_OK on success, a MDB2 error on failure
  907. * @access public
  908. */
  909. function dropIndex($table, $name)
  910. {
  911. $db = $this->getDBInstance();
  912. if (MDB2::isError($db)) {
  913. return $db;
  914. }
  915. $table = $db->quoteIdentifier($table, true);
  916. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  917. $result = $db->exec("DROP INDEX $table.$name");
  918. if (MDB2::isError($result)) {
  919. return $result;
  920. }
  921. return MDB2_OK;
  922. }
  923. // }}}
  924. // {{{ listTableConstraints()
  925. /**
  926. * list all constraints in a table
  927. *
  928. * @param string $table name of table that should be used in method
  929. *
  930. * @return mixed array of constraint names on success, a MDB2 error on failure
  931. * @access public
  932. */
  933. function listTableConstraints($table)
  934. {
  935. $db = $this->getDBInstance();
  936. if (MDB2::isError($db)) {
  937. return $db;
  938. }
  939. $query = "SELECT c.constraint_name
  940. FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
  941. WHERE c.constraint_catalog = DB_NAME()
  942. AND c.table_name = '$table'";
  943. $constraints = $db->queryCol($query);
  944. if (MDB2::isError($constraints)) {
  945. return $constraints;
  946. }
  947. $result = array();
  948. foreach ($constraints as $constraint) {
  949. $constraint = $this->_fixIndexName($constraint);
  950. if (!empty($constraint)) {
  951. $result[$constraint] = true;
  952. }
  953. }
  954. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  955. $result = array_change_key_case($result, $db->options['field_case']);
  956. }
  957. return array_keys($result);
  958. }
  959. // }}}
  960. // {{{
  961. /**
  962. * Create a basic SQL query for a new table creation
  963. *
  964. * @param string $name Name of the database that should be created
  965. * @param array $fields Associative array that contains the definition of each field of the new table
  966. * @param array $options An associative array of table options
  967. * Supported options are:
  968. * 'primary' An array of column names in the array keys
  969. * that form the primary key of the table
  970. * 'temporary' If true, creates the table as a temporary table
  971. * @return mixed string The SQL query on success, or MDB2 error on failure
  972. * @see createTable()
  973. */
  974. function _getCreateTableQuery($name, $fields, $options = array())
  975. {
  976. $db = $this->getDBInstance();
  977. if (MDB2::isError($db)) {
  978. return $db;
  979. }
  980. if (!$name) {
  981. return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
  982. 'no valid table name specified', __FUNCTION__);
  983. }
  984. if (empty($fields)) {
  985. return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
  986. 'no fields specified for table "'.$name.'"', __FUNCTION__);
  987. }
  988. $query_fields = $this->getFieldDeclarationList($fields);
  989. if (MDB2::isError($query_fields)) {
  990. return $query_fields;
  991. }
  992. /*Removed since you can't get the PK name from Schema here, will result in a redefinition of PK index error
  993. if (!empty($options['primary'])) {
  994. $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
  995. }*/
  996. $name = $db->quoteIdentifier($name, true);
  997. $result = 'CREATE ';
  998. if (!empty($options['temporary']) && $options['temporary']) {
  999. $result .= $this->_getTemporaryTableQuery() . ' ';
  1000. }
  1001. $result .= "TABLE $name ($query_fields)";
  1002. return $result;
  1003. }
  1004. // }}}
  1005. // {{{ createSequence()
  1006. /**
  1007. * create sequence
  1008. *
  1009. * @param string $seq_name name of the sequence to be created
  1010. * @param string $start start value of the sequence; default is 1
  1011. *
  1012. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1013. * @access public
  1014. */
  1015. function createSequence($seq_name, $start = 1)
  1016. {
  1017. $db = $this->getDBInstance();
  1018. if (MDB2::isError($db)) {
  1019. return $db;
  1020. }
  1021. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  1022. $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
  1023. $query = "CREATE TABLE $sequence_name ($seqcol_name " .
  1024. "INT PRIMARY KEY CLUSTERED IDENTITY($start,1) NOT NULL)";
  1025. $res = $db->exec($query);
  1026. if (MDB2::isError($res)) {
  1027. return $res;
  1028. }
  1029. $query = "SET IDENTITY_INSERT $sequence_name ON ".
  1030. "INSERT INTO $sequence_name ($seqcol_name) VALUES ($start)";
  1031. $res = $db->exec($query);
  1032. if (!MDB2::isError($res)) {
  1033. return MDB2_OK;
  1034. }
  1035. $result = $db->exec("DROP TABLE $sequence_name");
  1036. if (MDB2::isError($result)) {
  1037. return $db->raiseError($result, null, null,
  1038. 'could not drop inconsistent sequence table', __FUNCTION__);
  1039. }
  1040. return $db->raiseError($res, null, null,
  1041. 'could not create sequence table', __FUNCTION__);
  1042. }
  1043. // }}}
  1044. // {{{ dropSequence()
  1045. /**
  1046. * This function drops an existing sequence
  1047. *
  1048. * @param string $seq_name name of the sequence to be dropped
  1049. *
  1050. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1051. * @access public
  1052. */
  1053. function dropSequence($seq_name)
  1054. {
  1055. $db = $this->getDBInstance();
  1056. if (MDB2::isError($db)) {
  1057. return $db;
  1058. }
  1059. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  1060. $result = $db->exec("DROP TABLE $sequence_name");
  1061. if (MDB2::isError($result)) {
  1062. return $result;
  1063. }
  1064. return MDB2_OK;
  1065. }
  1066. // }}}
  1067. // {{{ listSequences()
  1068. /**
  1069. * list all sequences in the current database
  1070. *
  1071. * @return mixed array of sequence names on success, a MDB2 error on failure
  1072. * @access public
  1073. */
  1074. function listSequences()
  1075. {
  1076. $db = $this->getDBInstance();
  1077. if (MDB2::isError($db)) {
  1078. return $db;
  1079. }
  1080. $query = "SELECT name FROM sysobjects WHERE xtype = 'U'";
  1081. $table_names = $db->queryCol($query);
  1082. if (MDB2::isError($table_names)) {
  1083. return $table_names;
  1084. }
  1085. $result = array();
  1086. foreach ($table_names as $table_name) {
  1087. if ($sqn = $this->_fixSequenceName($table_name, true)) {
  1088. $result[] = $sqn;
  1089. }
  1090. }
  1091. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  1092. $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  1093. 'strtolower' : 'strtoupper'), $result);
  1094. }
  1095. return $result;
  1096. }
  1097. // }}}
  1098. /**
  1099. * New OPENX method to check table name according to specifications:
  1100. * http://msdn.microsoft.com/en-us/library/aa258255(SQL.80).aspx
  1101. *
  1102. * Table names must conform to the rules for identifiers. The combination of owner.table_name
  1103. * must be unique within the database. table_name can contain a maximum of 128 characters,
  1104. * except for local temporary table names (names prefixed with a single number sign (#)) that
  1105. * cannot exceed 116 characters.
  1106. *
  1107. * @param string $name table name to check
  1108. * @return true if name is correct and PEAR error on failure
  1109. */
  1110. function validateTableName($name)
  1111. {
  1112. // Table name maximum length is 128
  1113. if (strlen($name) > 128) {
  1114. return PEAR::raiseError(
  1115. 'SQL Server table names are limited to 128 characters in length');
  1116. }
  1117. return true;
  1118. }
  1119. /**
  1120. * New OpenX method
  1121. *
  1122. * @param string $table
  1123. * @return array
  1124. */
  1125. function getTableStatus($table)
  1126. {
  1127. $db = $this->getDBInstance();
  1128. if (MDB2::isError($db)) {
  1129. return $db;
  1130. }
  1131. $query = "exec sp_spaceused '{$table}'";
  1132. $result = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  1133. if (MDB2::isError($result))
  1134. {
  1135. return array();
  1136. }
  1137. $result[0]['data_length'] = (isset($result[0]['data'])) ? $result[0]['data'] : 0;
  1138. $result[0]['data_free'] = (isset($result[0]['unused'])) ? $result[0]['unused'] : 0;
  1139. //data_length,rows,auto_increment,data_free
  1140. $query = "SELECT IDENT_CURRENT ('{$table}') + IDENT_INCR ('{$table}') AS auto_increment";
  1141. $resultIdentity = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  1142. $result[0]['auto_increment'] = (isset($resultIdentity[0]['auto_increment'])) ? $resultIdentity[0]['auto_increment'] : 0;
  1143. return $result;
  1144. }
  1145. function checkTable($tableName)
  1146. {
  1147. $db = $this->getDBInstance();
  1148. if (MDB2::isError($db)) {
  1149. return $db;
  1150. }
  1151. $query = 'CHECK TABLE '.$tableName;
  1152. $result = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
  1153. if (MDB2::isError($result))
  1154. {
  1155. return array('msg_text' => $result->getUserInfo());
  1156. }
  1157. return $result;
  1158. }
  1159. /**
  1160. * New OPENX method to check database name according to specifications:
  1161. * Mysql specification: http://dev.mysql.com/doc/refman/4.1/en/identifiers.html
  1162. * Mysql specification: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
  1163. * For 4.0, 4.1, 5.0 seem to be the same
  1164. *
  1165. * @param string $name database name to check
  1166. * @return true in name is correct and PEAR error on failure
  1167. */
  1168. function validateDatabaseName($name)
  1169. {
  1170. return $this->_validateEntityName($name, 'Database');
  1171. }
  1172. /**
  1173. * New OPENX method to check entity name according to specifications:
  1174. * Mysql specification: http://dev.mysql.com/doc/refman/4.1/en/identifiers.html
  1175. * Mysql specification: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
  1176. * For 4.0, 4.1, 5.0 seem to be the same
  1177. *
  1178. * There are some restrictions on the characters that may appear in identifiers:
  1179. * - No identifier can contain ASCII 0 (0x00) or a byte with a value of 255.
  1180. * - Before MySQL 4.1, identifier quote characters should not be used in identifiers.
  1181. * - Database, table, and column names should not end with space characters.
  1182. * - Database and table names cannot contain "/", "\", ".", or characters that are not allowed in filenames.
  1183. *
  1184. * Table/Database name maximum length:
  1185. * - 64
  1186. *
  1187. * @param string $name table name to check
  1188. * @param string $entityType
  1189. *
  1190. * @return true if name is correct and PEAR error on failure
  1191. */
  1192. function _validateEntityName($name, $entityType)
  1193. {
  1194. // Table name maximum length is 64
  1195. if (strlen($name) > 64) {
  1196. return PEAR::raiseError(
  1197. $entityType.' names are limited to 64 characters in length');
  1198. }
  1199. // Database, table, and column names should not end with space characters.
  1200. // Extended for leading and ending spaces
  1201. if ($name != trim($name)) {
  1202. return PEAR::raiseError(
  1203. $entityType.' names should not start or end with space characters');
  1204. }
  1205. // No identifier can contain ASCII 0 (0x00) or a byte with a value of 255.
  1206. if (preg_match( '/([\x00\xff])/', $name)) {
  1207. return PEAR::raiseError(
  1208. $entityType.' names cannot contain ASCII 0 (0x00) or a byte with a value of 255');
  1209. }
  1210. //Before MySQL 4.1, identifier quote characters should not be used in identifiers.
  1211. //we actually extend that and disallow quoting at all
  1212. if (preg_match( '/(\\\\|\/|\.|\"|\\\'| |\\(|\\)|\\:|\\;)/', $name)) {
  1213. return PEAR::raiseError(
  1214. $entityType.' names cannot contain "/", "\\", ".", or characters that are not allowed in filenames');
  1215. }
  1216. return true;
  1217. }
  1218. // {{{ createConstraint()
  1219. /**
  1220. * create a constraint on a table
  1221. *
  1222. * @param string $table name of the table on which the constraint is to be created
  1223. * @param string $name name of the constraint to be created
  1224. * @param array $definition associative array that defines properties of the constraint to be created.
  1225. * Currently, only one property named FIELDS is supported. This property
  1226. * is also an associative with the names of the constraint fields as array
  1227. * constraints. Each entry of this array is set to another type of associative
  1228. * array that specifies properties of the constraint that are specific to
  1229. * each field.
  1230. *
  1231. * Example
  1232. * array(
  1233. * 'fields' => array(
  1234. * 'user_name' => array(),
  1235. * 'last_login' => array()
  1236. * )
  1237. * )
  1238. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1239. * @access public
  1240. */
  1241. function createConstraint($table, $name, $definition)
  1242. {
  1243. $db = $this->getDBInstance();
  1244. if (MDB2::isError($db)) {
  1245. return $db;
  1246. }
  1247. $table = $db->quoteIdentifier($table, true);
  1248. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  1249. if (!empty($definition['primary']) && empty($definition['unique'])) {
  1250. $query = "ALTER TABLE $table ADD CONSTRAINT $name";
  1251. if (!empty($definition['primary'])) {
  1252. $query.= ' PRIMARY KEY';
  1253. } elseif (!empty($definition['unique'])) {
  1254. $query.= ' UNIQUE';
  1255. }
  1256. } elseif (!empty($definition['unique'])) {
  1257. $query = "CREATE UNIQUE NONCLUSTERED INDEX $name ON $table";
  1258. } elseif (!empty($definition['foreign'])) {
  1259. $query = "ALTER TABLE $table ADD CONSTRAINT $name FOREIGN KEY";
  1260. }
  1261. $fields = array();
  1262. foreach (array_keys($definition['fields']) as $field) {
  1263. $fields[] = $db->quoteIdentifier($field, true);
  1264. }
  1265. $query .= ' ('. implode(', ', $fields) . ')';
  1266. //deals with NULL values and UNIQUE indexes, this solution is only available in SQL Server 2008
  1267. //https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=299229
  1268. if (!empty($definition['unique']) && empty($definition['primary'])) {
  1269. for($i=0;$i<count($fields);$i++) $fields[$i] .= ' is NOT NULL';
  1270. $query .= ' WHERE '. implode(' AND ', $fields);
  1271. }
  1272. if (!empty($definition['foreign'])) {
  1273. $query.= ' REFERENCES ' . $db->quoteIdentifier($definition['references']['table'], true);
  1274. $referenced_fields = array();
  1275. foreach (array_keys($definition['references']['fields']) as $field) {
  1276. $referenced_fields[] = $db->quoteIdentifier($field, true);
  1277. }
  1278. $query .= ' ('. implode(', ', $referenced_fields) . ')';
  1279. $query .= $this->_getAdvancedFKOptions($definition);
  1280. }
  1281. $result = $db->exec($query);
  1282. if (MDB2::isError($result)) {
  1283. return $result;
  1284. }
  1285. return MDB2_OK;
  1286. }
  1287. // }}}
  1288. }
  1289. // }}}
  1290. ?>