mssql.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith, Frank M. Kromann, Lorenzo Alberton |
  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: Lukas Smith <smith@pooteeweet.org> |
  43. // | Lorenzo Alberton <l.alberton@quipo.it> |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id$
  47. //
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49. /**
  50. * MDB2 MSSQL driver for the schema reverse engineering module
  51. *
  52. * @package MDB2
  53. * @category Database
  54. * @author Lukas Smith <smith@dybnet.de>
  55. * @author Lorenzo Alberton <l.alberton@quipo.it>
  56. */
  57. class MDB2_Driver_Reverse_mssql extends MDB2_Driver_Reverse_Common
  58. {
  59. // {{{ getTableFieldDefinition()
  60. /**
  61. * Get the structure of a field into an array
  62. *
  63. * @param string $table_name name of table that should be used in method
  64. * @param string $field_name name of field that should be used in method
  65. * @return mixed data array on success, a MDB2 error on failure
  66. * @access public
  67. */
  68. function getTableFieldDefinition($table_name, $field_name)
  69. {
  70. $db = $this->getDBInstance();
  71. if (MDB2::isError($db)) {
  72. return $db;
  73. }
  74. $result = $db->loadModule('Datatype', null, true);
  75. if (MDB2::isError($result)) {
  76. return $result;
  77. }
  78. list($schema, $table) = $this->splitTableSchema($table_name);
  79. $table = $db->quoteIdentifier($table, true);
  80. $fldname = $db->quoteIdentifier($field_name, true);
  81. $query = "SELECT t.table_name,
  82. c.column_name 'name',
  83. c.data_type 'type',
  84. CASE c.is_nullable WHEN 'YES' THEN 1 ELSE 0 END AS 'is_nullable',
  85. c.column_default,
  86. c.character_maximum_length 'length',
  87. c.numeric_precision,
  88. c.numeric_scale,
  89. c.character_set_name,
  90. c.collation_name
  91. FROM INFORMATION_SCHEMA.TABLES t,
  92. INFORMATION_SCHEMA.COLUMNS c
  93. WHERE t.table_name = c.table_name
  94. AND t.table_name = '$table'
  95. AND c.column_name = '$fldname'";
  96. if (!empty($schema)) {
  97. $query .= " AND t.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
  98. }
  99. $query .= ' ORDER BY t.table_name';
  100. $column = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
  101. if (MDB2::isError($column)) {
  102. return $column;
  103. }
  104. if (empty($column)) {
  105. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  106. 'it was not specified an existing table column', __FUNCTION__);
  107. }
  108. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  109. if ($db->options['field_case'] == CASE_LOWER) {
  110. $column['name'] = strtolower($column['name']);
  111. } else {
  112. $column['name'] = strtoupper($column['name']);
  113. }
  114. } else {
  115. $column = array_change_key_case($column, $db->options['field_case']);
  116. }
  117. $mapped_datatype = $db->datatype->mapNativeDatatype($column);
  118. if (MDB2::isError($mapped_datatype)) {
  119. return $mapped_datatype;
  120. }
  121. list($types, $length, $unsigned, $fixed) = $mapped_datatype;
  122. $notnull = true;
  123. if ($column['is_nullable']) {
  124. $notnull = false;
  125. }
  126. $default = false;
  127. if (array_key_exists('column_default', $column)) {
  128. $default = $column['column_default'];
  129. if ((null === $default) && $notnull) {
  130. $default = '';
  131. } elseif (strlen($default) > 4
  132. && substr($default, 0, 1) == '('
  133. && substr($default, -1, 1) == ')'
  134. ) {
  135. //mssql wraps the default value in parentheses: "((1234))", "(NULL)"
  136. $default = trim($default, '()');
  137. if ($default == 'NULL') {
  138. $default = null;
  139. }
  140. }
  141. }
  142. $definition[0] = array(
  143. 'notnull' => $notnull,
  144. 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
  145. );
  146. if (null !== $length) {
  147. $definition[0]['length'] = $length;
  148. }
  149. if (null !== $unsigned) {
  150. $definition[0]['unsigned'] = $unsigned;
  151. }
  152. if (null !== $fixed) {
  153. $definition[0]['fixed'] = $fixed;
  154. }
  155. if (false !== $default) {
  156. $definition[0]['default'] = $default;
  157. }
  158. foreach ($types as $key => $type) {
  159. $definition[$key] = $definition[0];
  160. if ($type == 'clob' || $type == 'blob') {
  161. unset($definition[$key]['default']);
  162. }
  163. $definition[$key]['type'] = $type;
  164. $definition[$key]['mdb2type'] = $type;
  165. }
  166. return $definition;
  167. }
  168. // }}}
  169. // {{{ getTableIndexDefinition()
  170. /**
  171. * Get the structure of an index into an array
  172. *
  173. * @param string $table_name name of table that should be used in method
  174. * @param string $index_name name of index that should be used in method
  175. * @return mixed data array on success, a MDB2 error on failure
  176. * @access public
  177. */
  178. function getTableIndexDefinition($table_name, $index_name)
  179. {
  180. $db = $this->getDBInstance();
  181. if (MDB2::isError($db)) {
  182. return $db;
  183. }
  184. list($schema, $table) = $this->splitTableSchema($table_name);
  185. $table = $db->quoteIdentifier($table, true);
  186. //$idxname = $db->quoteIdentifier($index_name, true);
  187. $query = "SELECT OBJECT_NAME(i.id) tablename,
  188. i.name indexname,
  189. c.name field_name,
  190. CASE INDEXKEY_PROPERTY(i.id, i.indid, ik.keyno, 'IsDescending')
  191. WHEN 1 THEN 'DESC' ELSE 'ASC'
  192. END 'collation',
  193. ik.keyno 'position'
  194. FROM sysindexes i
  195. JOIN sysindexkeys ik ON ik.id = i.id AND ik.indid = i.indid
  196. JOIN syscolumns c ON c.id = ik.id AND c.colid = ik.colid
  197. WHERE OBJECT_NAME(i.id) = '$table'
  198. AND i.name = '%s'
  199. AND NOT EXISTS (
  200. SELECT *
  201. FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
  202. WHERE k.table_name = OBJECT_NAME(i.id)
  203. AND k.constraint_name = i.name";
  204. if (!empty($schema)) {
  205. $query .= " AND k.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
  206. }
  207. $query .= ')
  208. ORDER BY tablename, indexname, ik.keyno';
  209. $index_name_mdb2 = $db->getIndexName($index_name);
  210. $result = $db->queryRow(sprintf($query, $index_name_mdb2));
  211. if (!MDB2::isError($result) && (null !== $result)) {
  212. // apply 'idxname_format' only if the query succeeded, otherwise
  213. // fallback to the given $index_name, without transformation
  214. $index_name = $index_name_mdb2;
  215. }
  216. $result = $db->query(sprintf($query, $index_name));
  217. if (MDB2::isError($result)) {
  218. return $result;
  219. }
  220. $definition = array();
  221. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  222. $column_name = $row['field_name'];
  223. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  224. if ($db->options['field_case'] == CASE_LOWER) {
  225. $column_name = strtolower($column_name);
  226. } else {
  227. $column_name = strtoupper($column_name);
  228. }
  229. }
  230. $definition['fields'][$column_name] = array(
  231. 'position' => (int)$row['position'],
  232. );
  233. if (!empty($row['collation'])) {
  234. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
  235. ? 'ascending' : 'descending');
  236. }
  237. }
  238. $result->free();
  239. if (empty($definition['fields'])) {
  240. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  241. 'it was not specified an existing table index', __FUNCTION__);
  242. }
  243. return $definition;
  244. }
  245. // }}}
  246. // {{{ getTableConstraintDefinition()
  247. /**
  248. * Get the structure of a constraint into an array
  249. *
  250. * @param string $table_name name of table that should be used in method
  251. * @param string $constraint_name name of constraint that should be used in method
  252. * @return mixed data array on success, a MDB2 error on failure
  253. * @access public
  254. */
  255. function getTableConstraintDefinition($table_name, $constraint_name)
  256. {
  257. $db = $this->getDBInstance();
  258. if (MDB2::isError($db)) {
  259. return $db;
  260. }
  261. list($schema, $table) = $this->splitTableSchema($table_name);
  262. $table = $db->quoteIdentifier($table, true);
  263. $query = "SELECT k.table_name,
  264. k.column_name field_name,
  265. CASE c.constraint_type WHEN 'PRIMARY KEY' THEN 1 ELSE 0 END 'primary',
  266. CASE c.constraint_type WHEN 'UNIQUE' THEN 1 ELSE 0 END 'unique',
  267. CASE c.constraint_type WHEN 'FOREIGN KEY' THEN 1 ELSE 0 END 'foreign',
  268. CASE c.constraint_type WHEN 'CHECK' THEN 1 ELSE 0 END 'check',
  269. CASE c.is_deferrable WHEN 'NO' THEN 0 ELSE 1 END 'deferrable',
  270. CASE c.initially_deferred WHEN 'NO' THEN 0 ELSE 1 END 'initiallydeferred',
  271. rc.match_option 'match',
  272. rc.update_rule 'onupdate',
  273. rc.delete_rule 'ondelete',
  274. kcu.table_name 'references_table',
  275. kcu.column_name 'references_field',
  276. k.ordinal_position 'field_position'
  277. FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
  278. LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
  279. ON k.table_name = c.table_name
  280. AND k.table_schema = c.table_schema
  281. AND k.table_catalog = c.table_catalog
  282. AND k.constraint_catalog = c.constraint_catalog
  283. AND k.constraint_name = c.constraint_name
  284. LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
  285. ON rc.constraint_schema = c.constraint_schema
  286. AND rc.constraint_catalog = c.constraint_catalog
  287. AND rc.constraint_name = c.constraint_name
  288. LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
  289. ON rc.unique_constraint_schema = kcu.constraint_schema
  290. AND rc.unique_constraint_catalog = kcu.constraint_catalog
  291. AND rc.unique_constraint_name = kcu.constraint_name
  292. AND k.ordinal_position = kcu.ordinal_position
  293. WHERE k.constraint_catalog = DB_NAME()
  294. AND k.table_name = '$table'
  295. AND k.constraint_name = '%s'";
  296. if (!empty($schema)) {
  297. $query .= " AND k.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
  298. }
  299. $query .= ' ORDER BY k.constraint_name,
  300. k.ordinal_position';
  301. $constraint_name_mdb2 = $db->getIndexName($constraint_name);
  302. $result = $db->queryRow(sprintf($query, $constraint_name_mdb2));
  303. if (!MDB2::isError($result) && (null !== $result)) {
  304. // apply 'idxname_format' only if the query succeeded, otherwise
  305. // fallback to the given $index_name, without transformation
  306. $constraint_name = $constraint_name_mdb2;
  307. }
  308. $result = $db->query(sprintf($query, $constraint_name));
  309. if (MDB2::isError($result)) {
  310. return $result;
  311. }
  312. $definition = array(
  313. 'fields' => array()
  314. );
  315. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  316. $row = array_change_key_case($row, CASE_LOWER);
  317. $column_name = $row['field_name'];
  318. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  319. if ($db->options['field_case'] == CASE_LOWER) {
  320. $column_name = strtolower($column_name);
  321. } else {
  322. $column_name = strtoupper($column_name);
  323. }
  324. }
  325. $definition['fields'][$column_name] = array(
  326. 'position' => (int)$row['field_position']
  327. );
  328. if ($row['foreign']) {
  329. $ref_column_name = $row['references_field'];
  330. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  331. if ($db->options['field_case'] == CASE_LOWER) {
  332. $ref_column_name = strtolower($ref_column_name);
  333. } else {
  334. $ref_column_name = strtoupper($ref_column_name);
  335. }
  336. }
  337. $definition['references']['table'] = $row['references_table'];
  338. $definition['references']['fields'][$ref_column_name] = array(
  339. 'position' => (int)$row['field_position']
  340. );
  341. }
  342. //collation?!?
  343. /*
  344. if (!empty($row['collation'])) {
  345. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
  346. ? 'ascending' : 'descending');
  347. }
  348. */
  349. $lastrow = $row;
  350. // otherwise $row is no longer usable on exit from loop
  351. }
  352. $result->free();
  353. if (empty($definition['fields'])) {
  354. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  355. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  356. }
  357. $definition['primary'] = (boolean)$lastrow['primary'];
  358. $definition['unique'] = (boolean)$lastrow['unique'];
  359. $definition['foreign'] = (boolean)$lastrow['foreign'];
  360. $definition['check'] = (boolean)$lastrow['check'];
  361. $definition['deferrable'] = (boolean)$lastrow['deferrable'];
  362. $definition['initiallydeferred'] = (boolean)$lastrow['initiallydeferred'];
  363. $definition['onupdate'] = $lastrow['onupdate'];
  364. $definition['ondelete'] = $lastrow['ondelete'];
  365. $definition['match'] = $lastrow['match'];
  366. return $definition;
  367. }
  368. // }}}
  369. // {{{ getTriggerDefinition()
  370. /**
  371. * Get the structure of a trigger into an array
  372. *
  373. * EXPERIMENTAL
  374. *
  375. * WARNING: this function is experimental and may change the returned value
  376. * at any time until labelled as non-experimental
  377. *
  378. * @param string $trigger name of trigger that should be used in method
  379. * @return mixed data array on success, a MDB2 error on failure
  380. * @access public
  381. */
  382. function getTriggerDefinition($trigger)
  383. {
  384. $db = $this->getDBInstance();
  385. if (MDB2::isError($db)) {
  386. return $db;
  387. }
  388. $query = "SELECT sys1.name trigger_name,
  389. sys2.name table_name,
  390. c.text trigger_body,
  391. c.encrypted is_encripted,
  392. CASE
  393. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsTriggerDisabled') = 1
  394. THEN 0 ELSE 1
  395. END trigger_enabled,
  396. CASE
  397. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsertTrigger') = 1
  398. THEN 'INSERT'
  399. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsUpdateTrigger') = 1
  400. THEN 'UPDATE'
  401. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsDeleteTrigger') = 1
  402. THEN 'DELETE'
  403. END trigger_event,
  404. CASE WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsteadOfTrigger') = 1
  405. THEN 'INSTEAD OF' ELSE 'AFTER'
  406. END trigger_type,
  407. '' trigger_comment
  408. FROM sysobjects sys1
  409. JOIN sysobjects sys2 ON sys1.parent_obj = sys2.id
  410. JOIN syscomments c ON sys1.id = c.id
  411. WHERE sys1.xtype = 'TR'
  412. AND sys1.name = ". $db->quote($trigger, 'text');
  413. $types = array(
  414. 'trigger_name' => 'text',
  415. 'table_name' => 'text',
  416. 'trigger_body' => 'text',
  417. 'trigger_type' => 'text',
  418. 'trigger_event' => 'text',
  419. 'trigger_comment' => 'text',
  420. 'trigger_enabled' => 'boolean',
  421. 'is_encripted' => 'boolean',
  422. );
  423. $def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
  424. if (MDB2::isError($def)) {
  425. return $def;
  426. }
  427. $trg_body = $db->queryCol('EXEC sp_helptext '. $db->quote($trigger, 'text'), 'text');
  428. if (!MDB2::isError($trg_body)) {
  429. $def['trigger_body'] = implode(' ', $trg_body);
  430. }
  431. return $def;
  432. }
  433. // }}}
  434. // {{{ tableInfo()
  435. /**
  436. * Returns information about a table or a result set
  437. *
  438. * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  439. * is a table name.
  440. *
  441. * @param object|string $result MDB2_result object from a query or a
  442. * string containing the name of a table.
  443. * While this also accepts a query result
  444. * resource identifier, this behavior is
  445. * deprecated.
  446. * @param int $mode a valid tableInfo mode
  447. *
  448. * @return array an associative array with the information requested.
  449. * A MDB2_Error object on failure.
  450. *
  451. * @see MDB2_Driver_Common::tableInfo()
  452. */
  453. function tableInfo($result, $mode = null)
  454. {
  455. if (is_string($result)) {
  456. return parent::tableInfo($result, $mode);
  457. }
  458. $db = $this->getDBInstance();
  459. if (MDB2::isError($db)) {
  460. return $db;
  461. }
  462. $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;
  463. if (!is_resource($resource)) {
  464. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  465. 'Could not generate result resource', __FUNCTION__);
  466. }
  467. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  468. if ($db->options['field_case'] == CASE_LOWER) {
  469. $case_func = 'strtolower';
  470. } else {
  471. $case_func = 'strtoupper';
  472. }
  473. } else {
  474. $case_func = 'strval';
  475. }
  476. $count = @mssql_num_fields($resource);
  477. $res = array();
  478. if ($mode) {
  479. $res['num_fields'] = $count;
  480. }
  481. $db->loadModule('Datatype', null, true);
  482. for ($i = 0; $i < $count; $i++) {
  483. $res[$i] = array(
  484. 'table' => '',
  485. 'name' => $case_func(@mssql_field_name($resource, $i)),
  486. 'type' => @mssql_field_type($resource, $i),
  487. 'length' => @mssql_field_length($resource, $i),
  488. 'flags' => '',
  489. );
  490. $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);
  491. if (MDB2::isError($mdb2type_info)) {
  492. return $mdb2type_info;
  493. }
  494. $res[$i]['mdb2type'] = $mdb2type_info[0][0];
  495. if ($mode & MDB2_TABLEINFO_ORDER) {
  496. $res['order'][$res[$i]['name']] = $i;
  497. }
  498. if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
  499. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  500. }
  501. }
  502. return $res;
  503. }
  504. // }}}
  505. // {{{ _mssql_field_flags()
  506. /**
  507. * Get a column's flags
  508. *
  509. * Supports "not_null", "primary_key",
  510. * "auto_increment" (mssql identity), "timestamp" (mssql timestamp),
  511. * "unique_key" (mssql unique index, unique check or primary_key) and
  512. * "multiple_key" (multikey index)
  513. *
  514. * mssql timestamp is NOT similar to the mysql timestamp so this is maybe
  515. * not useful at all - is the behaviour of mysql_field_flags that primary
  516. * keys are alway unique? is the interpretation of multiple_key correct?
  517. *
  518. * @param string $table the table name
  519. * @param string $column the field name
  520. *
  521. * @return string the flags
  522. *
  523. * @access protected
  524. * @author Joern Barthel <j_barthel@web.de>
  525. */
  526. function _mssql_field_flags($table, $column)
  527. {
  528. $db = $this->getDBInstance();
  529. if (MDB2::isError($db)) {
  530. return $db;
  531. }
  532. static $tableName = null;
  533. static $flags = array();
  534. if ($table != $tableName) {
  535. $flags = array();
  536. $tableName = $table;
  537. // get unique and primary keys
  538. $res = $db->queryAll("EXEC SP_HELPINDEX[$table]", null, MDB2_FETCHMODE_ASSOC);
  539. foreach ($res as $val) {
  540. $val = array_change_key_case($val, CASE_LOWER);
  541. $keys = explode(', ', $val['index_keys']);
  542. if (sizeof($keys) > 1) {
  543. foreach ($keys as $key) {
  544. $this->_add_flag($flags[$key], 'multiple_key');
  545. }
  546. }
  547. if (strpos($val['index_description'], 'primary key')) {
  548. foreach ($keys as $key) {
  549. $this->_add_flag($flags[$key], 'primary_key');
  550. }
  551. } elseif (strpos($val['index_description'], 'unique')) {
  552. foreach ($keys as $key) {
  553. $this->_add_flag($flags[$key], 'unique_key');
  554. }
  555. }
  556. }
  557. // get auto_increment, not_null and timestamp
  558. $res = $db->queryAll("EXEC SP_COLUMNS[$table]", null, MDB2_FETCHMODE_ASSOC);
  559. foreach ($res as $val) {
  560. $val = array_change_key_case($val, CASE_LOWER);
  561. if ($val['nullable'] == '0') {
  562. $this->_add_flag($flags[$val['column_name']], 'not_null');
  563. }
  564. if (strpos($val['type_name'], 'identity')) {
  565. $this->_add_flag($flags[$val['column_name']], 'auto_increment');
  566. }
  567. if (strpos($val['type_name'], 'timestamp')) {
  568. $this->_add_flag($flags[$val['column_name']], 'timestamp');
  569. }
  570. }
  571. }
  572. if (!empty($flags[$column])) {
  573. return(implode(' ', $flags[$column]));
  574. }
  575. return '';
  576. }
  577. // }}}
  578. // {{{ _add_flag()
  579. /**
  580. * Adds a string to the flags array if the flag is not yet in there
  581. * - if there is no flag present the array is created
  582. *
  583. * @param array &$array the reference to the flag-array
  584. * @param string $value the flag value
  585. *
  586. * @return void
  587. *
  588. * @access protected
  589. * @author Joern Barthel <j_barthel@web.de>
  590. */
  591. function _add_flag(&$array, $value)
  592. {
  593. if (!is_array($array)) {
  594. $array = array($value);
  595. } elseif (!in_array($value, $array)) {
  596. array_push($array, $value);
  597. }
  598. }
  599. // }}}
  600. }
  601. ?>