PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/MDB2/Driver/Manager/mssql.php

https://github.com/chregu/fluxcms
PHP | 364 lines | 134 code | 17 blank | 213 comment | 22 complexity | d85f2b0fc1fa8d2bd4ceb4bb02c6eb48 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 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. // | Author: Frank M. Kromann <frank@kromann.info> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. //
  47. require_once 'MDB2/Driver/Manager/Common.php';
  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. */
  55. class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
  56. {
  57. // }}}
  58. // {{{ createDatabase()
  59. /**
  60. * create a new database
  61. *
  62. * @param string $name name of the database that should be created
  63. * @return mixed MDB2_OK on success, a MDB2 error on failure
  64. * @access public
  65. */
  66. function createDatabase($name)
  67. {
  68. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  69. $database_device = (isset($db->options['database_device'])) ? $db->options['database_device'] : 'DEFAULT';
  70. $database_size = (isset($db->options['database_size'])) ? '='.$db->options['database_size'] : '';
  71. return $db->standaloneQuery("CREATE DATABASE $name ON $database_device$database_size");
  72. }
  73. // }}}
  74. // {{{ dropDatabase()
  75. /**
  76. * drop an existing database
  77. *
  78. * @param string $name name of the database that should be dropped
  79. * @return mixed MDB2_OK on success, a MDB2 error on failure
  80. * @access public
  81. */
  82. function dropDatabase($name)
  83. {
  84. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  85. return $db->standaloneQuery("DROP DATABASE $name");
  86. }
  87. // }}}
  88. // {{{ alterTable()
  89. /**
  90. * alter an existing table
  91. *
  92. * @param string $name name of the table that is intended to be changed.
  93. * @param array $changes associative array that contains the details of each type
  94. * of change that is intended to be performed. The types of
  95. * changes that are currently supported are defined as follows:
  96. *
  97. * name
  98. *
  99. * New name for the table.
  100. *
  101. * added_fields
  102. *
  103. * Associative array with the names of fields to be added as
  104. * indexes of the array. The value of each entry of the array
  105. * should be set to another associative array with the properties
  106. * of the fields to be added. The properties of the fields should
  107. * be the same as defined by the Metabase parser.
  108. *
  109. * Additionally, there should be an entry named Declaration that
  110. * is expected to contain the portion of the field declaration already
  111. * in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  112. *
  113. * removed_fields
  114. *
  115. * Associative array with the names of fields to be removed as indexes
  116. * of the array. Currently the values assigned to each entry are ignored.
  117. * An empty array should be used for future compatibility.
  118. *
  119. * renamed_fields
  120. *
  121. * Associative array with the names of fields to be renamed as indexes
  122. * of the array. The value of each entry of the array should be set to
  123. * another associative array with the entry named name with the new
  124. * field name and the entry named Declaration that is expected to contain
  125. * the portion of the field declaration already in DBMS specific SQL code
  126. * as it is used in the CREATE TABLE statement.
  127. *
  128. * changed_fields
  129. *
  130. * Associative array with the names of the fields to be changed as indexes
  131. * of the array. Keep in mind that if it is intended to change either the
  132. * name of a field and any other properties, the changed_fields array entries
  133. * should have the new names of the fields as array indexes.
  134. *
  135. * The value of each entry of the array should be set to another associative
  136. * array with the properties of the fields to that are meant to be changed as
  137. * array entries. These entries should be assigned to the new values of the
  138. * respective properties. The properties of the fields should be the same
  139. * as defined by the Metabase parser.
  140. *
  141. * If the default property is meant to be added, removed or changed, there
  142. * should also be an entry with index ChangedDefault assigned to 1. Similarly,
  143. * if the notnull constraint is to be added or removed, there should also be
  144. * an entry with index ChangedNotNull assigned to 1.
  145. *
  146. * Additionally, there should be an entry named Declaration that is expected
  147. * to contain the portion of the field changed declaration already in DBMS
  148. * specific SQL code as it is used in the CREATE TABLE statement.
  149. * Example
  150. * array(
  151. * 'name' => 'userlist',
  152. * 'added_fields' => array(
  153. * 'quota' => array(
  154. * 'type' => 'integer',
  155. * 'unsigned' => 1
  156. * 'declaration' => 'quota INT'
  157. * )
  158. * ),
  159. * 'removed_fields' => array(
  160. * 'file_limit' => array(),
  161. * 'time_limit' => array()
  162. * ),
  163. * 'changed_fields' => array(
  164. * 'gender' => array(
  165. * 'default' => 'M',
  166. * 'change_default' => 1,
  167. * 'declaration' => "gender CHAR(1) DEFAULT 'M'"
  168. * )
  169. * ),
  170. * 'renamed_fields' => array(
  171. * 'sex' => array(
  172. * 'name' => 'gender',
  173. * 'declaration' => "gender CHAR(1) DEFAULT 'M'"
  174. * )
  175. * )
  176. * )
  177. *
  178. * @param boolean $check indicates whether the function should just check if the DBMS driver
  179. * can perform the requested table alterations if the value is true or
  180. * actually perform them otherwise.
  181. * @access public
  182. *
  183. * @return mixed MDB2_OK on success, a MDB2 error on failure
  184. */
  185. function alterTable($name, $changes, $check)
  186. {
  187. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  188. if ($check) {
  189. foreach ($changes as $change_name => $change) {
  190. switch ($change_name) {
  191. case 'added_fields':
  192. break;
  193. case 'removed_fields':
  194. case 'name':
  195. case 'renamed_fields':
  196. case 'changed_fields':
  197. default:
  198. return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  199. 'alterTable: change type "'.$change_name.'" not yet supported');
  200. }
  201. }
  202. return MDB2_OK;
  203. } else {
  204. if (isset($changes[$change = 'removed_fields'])
  205. || isset($changes[$change = 'name'])
  206. || isset($changes[$change = 'renamed_fields'])
  207. || isset($changes[$change = 'changed_fields'])
  208. ) {
  209. return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  210. 'alterTable: change type "'.$change.'" is not supported by the server"');
  211. }
  212. $query = '';
  213. if (isset($changes['added_fields'])) {
  214. if ($query) {
  215. $query.= ', ';
  216. }
  217. $query.= 'ADD ';
  218. $fields = $changes['added_fields'];
  219. foreach ($fields as $field) {
  220. if ($query) {
  221. $query.= ', ';
  222. }
  223. $query.= $field['declaration'];
  224. }
  225. }
  226. return $query ? $db->query("ALTER TABLE $name $query") : MDB2_OK;
  227. }
  228. }
  229. // }}}
  230. // {{{ listTables()
  231. /**
  232. * list all tables in the current database
  233. *
  234. * @return mixed data array on success, a MDB error on failure
  235. * @access public
  236. */
  237. function listTables()
  238. {
  239. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  240. $query = 'EXECUTE sp_tables @table_type = "\'TABLE\'"';
  241. $table_names = $db->queryCol($query, null, 2);
  242. if (MDB2::isError($table_names)) {
  243. return $table_names;
  244. }
  245. $tables = array();
  246. for ($i = 0, $j = count($table_names); $i <$j; ++$i) {
  247. if (!$this->_isSequenceName($db, $table_names[$i])) {
  248. $tables[] = $table_names[$i];
  249. }
  250. }
  251. return $tables;
  252. }
  253. // }}}
  254. // {{{ listTableFields()
  255. /**
  256. * list all fields in a tables in the current database
  257. *
  258. * @param string $table name of table that should be used in method
  259. * @return mixed data array on success, a MDB error on failure
  260. * @access public
  261. */
  262. function listTableFields($table)
  263. {
  264. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  265. $result = $db->query("SELECT * FROM $table");
  266. if (MDB2::isError($result)) {
  267. return $result;
  268. }
  269. $columns = $result->getColumnNames();
  270. $result->free();
  271. if (MDB2::isError($columns)) {
  272. return $columns;
  273. }
  274. return array_flip($columns);
  275. }
  276. // }}}
  277. // {{{ listTableIndexes()
  278. /**
  279. * list all indexes in a table
  280. *
  281. * @param string $table name of table that should be used in method
  282. * @return mixed data array on success, a MDB error on failure
  283. * @access public
  284. */
  285. function listTableIndexes($table)
  286. {
  287. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  288. $key_name = 'INDEX_NAME';
  289. $pk_name = 'PK_NAME';
  290. if ($db->options['portability'] & MDB2_PORTABILITY_LOWERCASE) {
  291. $key_name = strtolower($key_name);
  292. $pk_name = strtolower($pk_name);
  293. }
  294. $query = "EXEC sp_statistics @table_name='$table'";
  295. $indexes_all = $db->query($query, 'text', $key_name);
  296. if (MDB2::isError($indexes_all)) {
  297. return $indexes_all;
  298. }
  299. $query = "EXEC sp_pkeys @table_name='$table'";
  300. $pk_all = $db->queryCol($query, 'text', $pk_name);
  301. $found = $indexes = array();
  302. for ($index = 0, $j = count($indexes_all); $index < $j; ++$index) {
  303. if (!in_array($indexes_all[$index], $pk_all)
  304. && $indexes_all[$index] != null
  305. && !isset($found[$indexes_all[$index]])
  306. ) {
  307. $indexes[] = $indexes_all[$index];
  308. $found[$indexes_all[$index]] = 1;
  309. }
  310. }
  311. return $indexes;
  312. }
  313. // }}}
  314. // {{{ createSequence()
  315. /**
  316. * create sequence
  317. *
  318. * @param string $seq_name name of the sequence to be created
  319. * @param string $start start value of the sequence; default is 1
  320. * @return mixed MDB2_OK on success, a MDB2 error on failure
  321. * @access public
  322. */
  323. function createSequence($seq_name, $start = 1)
  324. {
  325. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  326. $sequence_name = $db->getSequenceName($seq_name);
  327. $query = "CREATE TABLE $sequence_name (".$db->options['seqname_col_name']
  328. ." INT NOT NULL IDENTITY($start,1) PRIMARY KEY CLUSTERED)";
  329. return $db->query($query);
  330. }
  331. // }}}
  332. // {{{ dropSequence()
  333. /**
  334. * drop existing sequence
  335. *
  336. * @param string $seq_name name of the sequence to be dropped
  337. * @return mixed MDB2_OK on success, a MDB2 error on failure
  338. * @access public
  339. */
  340. function dropSequence($seq_name)
  341. {
  342. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  343. $sequence_name = $db->getSequenceName($seq_name);
  344. return $db->Query("DROP TABLE $sequence_name");
  345. }
  346. }
  347. ?>