sqlsrv.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. require_once 'MDB2/Driver/Reverse/Common.php';
  46. /**
  47. * MDB2 MSSQL driver for the schema reverse engineering module
  48. *
  49. * @package MDB2
  50. * @category Database
  51. * @author Lukas Smith <smith@dybnet.de>
  52. * @author Lorenzo Alberton <l.alberton@quipo.it>
  53. */
  54. class MDB2_Driver_Reverse_sqlsrv extends MDB2_Driver_Reverse_Common
  55. {
  56. // {{{ getTableFieldDefinition()
  57. /**
  58. * Get the structure of a field into an array
  59. *
  60. * @param string $table_name name of table that should be used in method
  61. * @param string $field_name name of field that should be used in method
  62. * @return mixed data array on success, a MDB2 error on failure
  63. * @access public
  64. */
  65. function getTableFieldDefinition($table_name, $field_name)
  66. {
  67. $db = $this->getDBInstance();
  68. if (MDB2::isError($db)) {
  69. return $db;
  70. }
  71. $result = $db->loadModule('Datatype', null, true);
  72. if (MDB2::isError($result)) {
  73. return $result;
  74. }
  75. list($schema, $table) = $this->splitTableSchema($table_name);
  76. $table = $db->quoteIdentifier($table, true);
  77. $fldname = $db->quoteIdentifier($field_name, true);
  78. $query = "SELECT t.table_name,
  79. c.column_name 'name',
  80. c.data_type 'type',
  81. CASE c.is_nullable WHEN 'YES' THEN 1 ELSE 0 END AS 'is_nullable',
  82. c.column_default,
  83. c.character_maximum_length 'length',
  84. c.numeric_precision,
  85. c.numeric_scale,
  86. c.character_set_name,
  87. c.collation_name
  88. FROM INFORMATION_SCHEMA.TABLES t,
  89. INFORMATION_SCHEMA.COLUMNS c
  90. WHERE t.table_name = c.table_name
  91. AND t.table_name = '$table'
  92. AND c.column_name = '$fldname'";
  93. if (!empty($schema)) {
  94. $query .= " AND t.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
  95. }
  96. $query .= ' ORDER BY t.table_name';
  97. $column = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC);
  98. if (MDB2::isError($column)) {
  99. return $column;
  100. }
  101. if (empty($column)) {
  102. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  103. 'it was not specified an existing table column', __FUNCTION__);
  104. }
  105. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  106. if ($db->options['field_case'] == CASE_LOWER) {
  107. $column['name'] = strtolower($column['name']);
  108. } else {
  109. $column['name'] = strtoupper($column['name']);
  110. }
  111. } else {
  112. $column = array_change_key_case($column, $db->options['field_case']);
  113. }
  114. $mapped_datatype = $db->datatype->mapNativeDatatype($column);
  115. if (MDB2::isError($mapped_datatype)) {
  116. return $mapped_datatype;
  117. }
  118. list($types, $length, $unsigned, $fixed) = $mapped_datatype;
  119. $notnull = true;
  120. if ($column['is_nullable']) {
  121. $notnull = false;
  122. }
  123. $default = false;
  124. if (array_key_exists('column_default', $column)) {
  125. $default = $column['column_default'];
  126. if ((null === $default) && $notnull) {
  127. $default = '';
  128. } elseif (strlen($default) > 4
  129. && substr($default, 0, 1) == '('
  130. && substr($default, -1, 1) == ')'
  131. ) {
  132. //mssql wraps the default value in parentheses: "((1234))", "(NULL)"
  133. $default = trim($default, '()');
  134. if ($default == 'NULL') {
  135. $default = null;
  136. }
  137. }
  138. }
  139. $definition[0] = array(
  140. 'notnull' => $notnull,
  141. 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
  142. );
  143. if (null !== $length) {
  144. $definition[0]['length'] = $length;
  145. }
  146. if (null !== $unsigned) {
  147. $definition[0]['unsigned'] = $unsigned;
  148. }
  149. if (null !== $fixed) {
  150. $definition[0]['fixed'] = $fixed;
  151. }
  152. if ($default !== false) {
  153. $definition[0]['default'] = $default;
  154. }
  155. foreach ($types as $key => $type) {
  156. $definition[$key] = $definition[0];
  157. if ($type == 'clob' || $type == 'blob') {
  158. unset($definition[$key]['default']);
  159. }
  160. $definition[$key]['type'] = $type;
  161. $definition[$key]['mdb2type'] = $type;
  162. }
  163. return $definition;
  164. }
  165. // }}}
  166. // {{{ getTableIndexDefinition()
  167. /**
  168. * Get the structure of an index into an array
  169. *
  170. * @param string $table_name name of table that should be used in method
  171. * @param string $index_name name of index that should be used in method
  172. * @return mixed data array on success, a MDB2 error on failure
  173. * @access public
  174. */
  175. function getTableIndexDefinition($table_name, $index_name)
  176. {
  177. $db = $this->getDBInstance();
  178. if (MDB2::isError($db)) {
  179. return $db;
  180. }
  181. list($schema, $table) = $this->splitTableSchema($table_name);
  182. $table = $db->quoteIdentifier($table, true);
  183. //$idxname = $db->quoteIdentifier($index_name, true);
  184. $query = "SELECT OBJECT_NAME(i.id) tablename,
  185. i.name indexname,
  186. c.name field_name,
  187. CASE INDEXKEY_PROPERTY(i.id, i.indid, ik.keyno, 'IsDescending')
  188. WHEN 1 THEN 'DESC' ELSE 'ASC'
  189. END 'collation',
  190. ik.keyno 'position'
  191. FROM sysindexes i
  192. JOIN sysindexkeys ik ON ik.id = i.id AND ik.indid = i.indid
  193. JOIN syscolumns c ON c.id = ik.id AND c.colid = ik.colid
  194. WHERE OBJECT_NAME(i.id) = '$table'
  195. AND i.name = '%s'
  196. AND NOT EXISTS (
  197. SELECT *
  198. FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
  199. WHERE k.table_name = OBJECT_NAME(i.id)
  200. AND k.constraint_name = i.name";
  201. if (!empty($schema)) {
  202. $query .= " AND k.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
  203. }
  204. $query .= ')
  205. ORDER BY tablename, indexname, ik.keyno';
  206. $index_name_mdb2 = $db->getIndexName($index_name);
  207. $result = $db->queryRow(sprintf($query, $index_name_mdb2));
  208. if (!MDB2::isError($result) && (null !== $result)) {
  209. // apply 'idxname_format' only if the query succeeded, otherwise
  210. // fallback to the given $index_name, without transformation
  211. $index_name = $index_name_mdb2;
  212. }
  213. $result = $db->query(sprintf($query, $index_name));
  214. if (MDB2::isError($result)) {
  215. return $result;
  216. }
  217. $definition = array();
  218. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  219. $column_name = $row['field_name'];
  220. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  221. if ($db->options['field_case'] == CASE_LOWER) {
  222. $column_name = strtolower($column_name);
  223. } else {
  224. $column_name = strtoupper($column_name);
  225. }
  226. }
  227. $definition['fields'][$column_name] = array(
  228. 'position' => (int)$row['position'],
  229. );
  230. if (!empty($row['collation'])) {
  231. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
  232. ? 'ascending' : 'descending');
  233. }
  234. }
  235. $result->free();
  236. if (empty($definition['fields'])) {
  237. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  238. 'it was not specified an existing table index', __FUNCTION__);
  239. }
  240. return $definition;
  241. }
  242. // }}}
  243. // {{{ getTableConstraintDefinition()
  244. /**
  245. * Get the structure of a constraint into an array
  246. *
  247. * @param string $table_name name of table that should be used in method
  248. * @param string $constraint_name name of constraint that should be used in method
  249. * @return mixed data array on success, a MDB2 error on failure
  250. * @access public
  251. */
  252. function getTableConstraintDefinition($table_name, $constraint_name)
  253. {
  254. $db = $this->getDBInstance();
  255. if (MDB2::isError($db)) {
  256. return $db;
  257. }
  258. list($schema, $table) = $this->splitTableSchema($table_name);
  259. $table = $db->quoteIdentifier($table, true);
  260. $query = "SELECT k.table_name,
  261. k.column_name field_name,
  262. CASE c.constraint_type WHEN 'PRIMARY KEY' THEN 1 ELSE 0 END 'primary',
  263. CASE c.constraint_type WHEN 'UNIQUE' THEN 1 ELSE 0 END 'unique',
  264. CASE c.constraint_type WHEN 'FOREIGN KEY' THEN 1 ELSE 0 END 'foreign',
  265. CASE c.constraint_type WHEN 'CHECK' THEN 1 ELSE 0 END 'check',
  266. CASE c.is_deferrable WHEN 'NO' THEN 0 ELSE 1 END 'deferrable',
  267. CASE c.initially_deferred WHEN 'NO' THEN 0 ELSE 1 END 'initiallydeferred',
  268. rc.match_option 'match',
  269. rc.update_rule 'onupdate',
  270. rc.delete_rule 'ondelete',
  271. kcu.table_name 'references_table',
  272. kcu.column_name 'references_field',
  273. k.ordinal_position 'field_position'
  274. FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
  275. LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
  276. ON k.table_name = c.table_name
  277. AND k.table_schema = c.table_schema
  278. AND k.table_catalog = c.table_catalog
  279. AND k.constraint_catalog = c.constraint_catalog
  280. AND k.constraint_name = c.constraint_name
  281. LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
  282. ON rc.constraint_schema = c.constraint_schema
  283. AND rc.constraint_catalog = c.constraint_catalog
  284. AND rc.constraint_name = c.constraint_name
  285. LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
  286. ON rc.unique_constraint_schema = kcu.constraint_schema
  287. AND rc.unique_constraint_catalog = kcu.constraint_catalog
  288. AND rc.unique_constraint_name = kcu.constraint_name
  289. AND k.ordinal_position = kcu.ordinal_position
  290. WHERE k.constraint_catalog = DB_NAME()
  291. AND k.table_name = '$table'
  292. AND k.constraint_name = '%s'";
  293. if (!empty($schema)) {
  294. $query .= " AND k.table_schema = '" .$db->quoteIdentifier($schema, true) ."'";
  295. }
  296. $query .= ' ORDER BY k.constraint_name,
  297. k.ordinal_position';
  298. $constraint_name_mdb2 = $db->getIndexName($constraint_name);
  299. $result = $db->queryRow(sprintf($query, $constraint_name_mdb2));
  300. if (!MDB2::isError($result) && (null !== $result)) {
  301. // apply 'idxname_format' only if the query succeeded, otherwise
  302. // fallback to the given $index_name, without transformation
  303. $constraint_name = $constraint_name_mdb2;
  304. }
  305. $result = $db->query(sprintf($query, $constraint_name));
  306. if (MDB2::isError($result)) {
  307. return $result;
  308. }
  309. $definition = array(
  310. 'fields' => array()
  311. );
  312. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  313. $row = array_change_key_case($row, CASE_LOWER);
  314. $column_name = $row['field_name'];
  315. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  316. if ($db->options['field_case'] == CASE_LOWER) {
  317. $column_name = strtolower($column_name);
  318. } else {
  319. $column_name = strtoupper($column_name);
  320. }
  321. }
  322. $definition['fields'][$column_name] = array(
  323. 'position' => (int)$row['field_position']
  324. );
  325. if ($row['foreign']) {
  326. $ref_column_name = $row['references_field'];
  327. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  328. if ($db->options['field_case'] == CASE_LOWER) {
  329. $ref_column_name = strtolower($ref_column_name);
  330. } else {
  331. $ref_column_name = strtoupper($ref_column_name);
  332. }
  333. }
  334. $definition['references']['table'] = $row['references_table'];
  335. $definition['references']['fields'][$ref_column_name] = array(
  336. 'position' => (int)$row['field_position']
  337. );
  338. }
  339. //collation?!?
  340. /*
  341. if (!empty($row['collation'])) {
  342. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
  343. ? 'ascending' : 'descending');
  344. }
  345. */
  346. $lastrow = $row;
  347. // otherwise $row is no longer usable on exit from loop
  348. }
  349. $result->free();
  350. if (empty($definition['fields'])) {
  351. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  352. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  353. }
  354. $definition['primary'] = (boolean)$lastrow['primary'];
  355. $definition['unique'] = (boolean)$lastrow['unique'];
  356. $definition['foreign'] = (boolean)$lastrow['foreign'];
  357. $definition['check'] = (boolean)$lastrow['check'];
  358. $definition['deferrable'] = (boolean)$lastrow['deferrable'];
  359. $definition['initiallydeferred'] = (boolean)$lastrow['initiallydeferred'];
  360. $definition['onupdate'] = $lastrow['onupdate'];
  361. $definition['ondelete'] = $lastrow['ondelete'];
  362. $definition['match'] = $lastrow['match'];
  363. return $definition;
  364. }
  365. // }}}
  366. // {{{ getTriggerDefinition()
  367. /**
  368. * Get the structure of a trigger into an array
  369. *
  370. * EXPERIMENTAL
  371. *
  372. * WARNING: this function is experimental and may change the returned value
  373. * at any time until labelled as non-experimental
  374. *
  375. * @param string $trigger name of trigger that should be used in method
  376. * @return mixed data array on success, a MDB2 error on failure
  377. * @access public
  378. */
  379. function getTriggerDefinition($trigger)
  380. {
  381. $db = $this->getDBInstance();
  382. if (MDB2::isError($db)) {
  383. return $db;
  384. }
  385. $query = "SELECT sys1.name trigger_name,
  386. sys2.name table_name,
  387. c.text trigger_body,
  388. c.encrypted is_encripted,
  389. CASE
  390. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsTriggerDisabled') = 1
  391. THEN 0 ELSE 1
  392. END trigger_enabled,
  393. CASE
  394. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsertTrigger') = 1
  395. THEN 'INSERT'
  396. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsUpdateTrigger') = 1
  397. THEN 'UPDATE'
  398. WHEN OBJECTPROPERTY(sys1.id, 'ExecIsDeleteTrigger') = 1
  399. THEN 'DELETE'
  400. END trigger_event,
  401. CASE WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsteadOfTrigger') = 1
  402. THEN 'INSTEAD OF' ELSE 'AFTER'
  403. END trigger_type,
  404. '' trigger_comment
  405. FROM sysobjects sys1
  406. JOIN sysobjects sys2 ON sys1.parent_obj = sys2.id
  407. JOIN syscomments c ON sys1.id = c.id
  408. WHERE sys1.xtype = 'TR'
  409. AND sys1.name = ". $db->quote($trigger, 'text');
  410. $types = array(
  411. 'trigger_name' => 'text',
  412. 'table_name' => 'text',
  413. 'trigger_body' => 'text',
  414. 'trigger_type' => 'text',
  415. 'trigger_event' => 'text',
  416. 'trigger_comment' => 'text',
  417. 'trigger_enabled' => 'boolean',
  418. 'is_encripted' => 'boolean',
  419. );
  420. $def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
  421. if (MDB2::isError($def)) {
  422. return $def;
  423. }
  424. $trg_body = $db->queryCol('EXEC sp_helptext '. $db->quote($trigger, 'text'), 'text');
  425. if (!MDB2::isError($trg_body)) {
  426. $def['trigger_body'] = implode(' ', $trg_body);
  427. }
  428. return $def;
  429. }
  430. // }}}
  431. // {{{ tableInfo()
  432. /**
  433. * Returns information about a table or a result set
  434. *
  435. * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  436. * is a table name.
  437. *
  438. * @param object|string $result MDB2_result object from a query or a
  439. * string containing the name of a table.
  440. * While this also accepts a query result
  441. * resource identifier, this behavior is
  442. * deprecated.
  443. * @param int $mode a valid tableInfo mode
  444. *
  445. * @return array an associative array with the information requested.
  446. * A MDB2_Error object on failure.
  447. *
  448. * @see MDB2_Driver_Common::tableInfo()
  449. */
  450. function tableInfo($result, $mode = null)
  451. {
  452. if (is_string($result)) {
  453. return parent::tableInfo($result, $mode);
  454. }
  455. $db = $this->getDBInstance();
  456. if (MDB2::isError($db)) {
  457. return $db;
  458. }
  459. $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;
  460. if (!is_resource($resource)) {
  461. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  462. 'Could not generate result resource', __FUNCTION__);
  463. }
  464. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  465. if ($db->options['field_case'] == CASE_LOWER) {
  466. $case_func = 'strtolower';
  467. } else {
  468. $case_func = 'strtoupper';
  469. }
  470. } else {
  471. $case_func = 'strval';
  472. }
  473. $meta = @sqlsrv_field_metadata($resource);
  474. $count = count($meta);
  475. $res = array();
  476. if ($mode) {
  477. $res['num_fields'] = $count;
  478. }
  479. $db->loadModule('Datatype', null, true);
  480. for ($i = 0; $i < $count; $i++) {
  481. $res[$i] = array(
  482. 'table' => '',
  483. 'name' => $case_func($meta[$i]['Name']),
  484. 'type' => $meta[$i]['Type'],
  485. 'length' => $meta[$i]['Size'],
  486. 'numeric_precision' => $meta[$i]['Precision'],
  487. 'numeric_scale' => $meta[$i]['Scale'],
  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. ?>