datadict-mssql.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. V4.60 24 Jan 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
  4. Released under both BSD license and Lesser GPL library license.
  5. Whenever there is any discrepancy between the two licenses,
  6. the BSD license will take precedence.
  7. Set tabs to 4 for best viewing.
  8. */
  9. // security - hide paths
  10. if (!defined('ADODB_DIR')) die();
  11. class ADODB2_mssql extends ADODB_DataDict {
  12. var $databaseType = 'mssql';
  13. var $dropIndex = 'DROP INDEX %2$s.%1$s';
  14. var $renameTable = "EXEC sp_rename '%s','%s'";
  15. var $renameColumn = "EXEC sp_rename '%s.%s','%s'";
  16. //var $alterCol = ' ALTER COLUMN ';
  17. function MetaType($t,$len=-1,$fieldobj=false)
  18. {
  19. if (is_object($t)) {
  20. $fieldobj = $t;
  21. $t = $fieldobj->type;
  22. $len = $fieldobj->max_length;
  23. }
  24. $len = -1; // mysql max_length is not accurate
  25. switch (strtoupper($t)) {
  26. case 'R':
  27. case 'INT':
  28. case 'INTEGER': return 'I';
  29. case 'BIT':
  30. case 'TINYINT': return 'I1';
  31. case 'SMALLINT': return 'I2';
  32. case 'BIGINT': return 'I8';
  33. case 'REAL':
  34. case 'FLOAT': return 'F';
  35. default: return parent::MetaType($t,$len,$fieldobj);
  36. }
  37. }
  38. function ActualType($meta)
  39. {
  40. switch(strtoupper($meta)) {
  41. case 'C': return 'VARCHAR';
  42. case 'XL':
  43. case 'X': return 'TEXT';
  44. case 'C2': return 'NVARCHAR';
  45. case 'X2': return 'NTEXT';
  46. case 'B': return 'IMAGE';
  47. case 'D': return 'DATETIME';
  48. case 'T': return 'DATETIME';
  49. case 'L': return 'BIT';
  50. case 'R':
  51. case 'I': return 'INT';
  52. case 'I1': return 'TINYINT';
  53. case 'I2': return 'SMALLINT';
  54. case 'I4': return 'INT';
  55. case 'I8': return 'BIGINT';
  56. case 'F': return 'REAL';
  57. case 'N': return 'NUMERIC';
  58. default:
  59. return $meta;
  60. }
  61. }
  62. function AddColumnSQL($tabname, $flds)
  63. {
  64. $tabname = $this->TableName ($tabname);
  65. $f = array();
  66. list($lines,$pkey) = $this->_GenFields($flds);
  67. $s = "ALTER TABLE $tabname $this->addCol";
  68. foreach($lines as $v) {
  69. $f[] = "\n $v";
  70. }
  71. $s .= implode(', ',$f);
  72. $sql[] = $s;
  73. return $sql;
  74. }
  75. /*
  76. function AlterColumnSQL($tabname, $flds)
  77. {
  78. $tabname = $this->TableName ($tabname);
  79. $sql = array();
  80. list($lines,$pkey) = $this->_GenFields($flds);
  81. foreach($lines as $v) {
  82. $sql[] = "ALTER TABLE $tabname $this->alterCol $v";
  83. }
  84. return $sql;
  85. }
  86. */
  87. function DropColumnSQL($tabname, $flds)
  88. {
  89. $tabname = $this->TableName ($tabname);
  90. if (!is_array($flds))
  91. $flds = explode(',',$flds);
  92. $f = array();
  93. $s = 'ALTER TABLE ' . $tabname;
  94. foreach($flds as $v) {
  95. $f[] = "\n$this->dropCol ".$this->NameQuote($v);
  96. }
  97. $s .= implode(', ',$f);
  98. $sql[] = $s;
  99. return $sql;
  100. }
  101. // return string must begin with space
  102. function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint)
  103. {
  104. $suffix = '';
  105. if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
  106. if ($fautoinc) $suffix .= ' IDENTITY(1,1)';
  107. if ($fnotnull) $suffix .= ' NOT NULL';
  108. else if ($suffix == '') $suffix .= ' NULL';
  109. if ($fconstraint) $suffix .= ' '.$fconstraint;
  110. return $suffix;
  111. }
  112. /*
  113. CREATE TABLE
  114. [ database_name.[ owner ] . | owner. ] table_name
  115. ( { < column_definition >
  116. | column_name AS computed_column_expression
  117. | < table_constraint > ::= [ CONSTRAINT constraint_name ] }
  118. | [ { PRIMARY KEY | UNIQUE } [ ,...n ]
  119. )
  120. [ ON { filegroup | DEFAULT } ]
  121. [ TEXTIMAGE_ON { filegroup | DEFAULT } ]
  122. < column_definition > ::= { column_name data_type }
  123. [ COLLATE < collation_name > ]
  124. [ [ DEFAULT constant_expression ]
  125. | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ]
  126. ]
  127. [ ROWGUIDCOL]
  128. [ < column_constraint > ] [ ...n ]
  129. < column_constraint > ::= [ CONSTRAINT constraint_name ]
  130. { [ NULL | NOT NULL ]
  131. | [ { PRIMARY KEY | UNIQUE }
  132. [ CLUSTERED | NONCLUSTERED ]
  133. [ WITH FILLFACTOR = fillfactor ]
  134. [ON {filegroup | DEFAULT} ] ]
  135. ]
  136. | [ [ FOREIGN KEY ]
  137. REFERENCES ref_table [ ( ref_column ) ]
  138. [ ON DELETE { CASCADE | NO ACTION } ]
  139. [ ON UPDATE { CASCADE | NO ACTION } ]
  140. [ NOT FOR REPLICATION ]
  141. ]
  142. | CHECK [ NOT FOR REPLICATION ]
  143. ( logical_expression )
  144. }
  145. < table_constraint > ::= [ CONSTRAINT constraint_name ]
  146. { [ { PRIMARY KEY | UNIQUE }
  147. [ CLUSTERED | NONCLUSTERED ]
  148. { ( column [ ASC | DESC ] [ ,...n ] ) }
  149. [ WITH FILLFACTOR = fillfactor ]
  150. [ ON { filegroup | DEFAULT } ]
  151. ]
  152. | FOREIGN KEY
  153. [ ( column [ ,...n ] ) ]
  154. REFERENCES ref_table [ ( ref_column [ ,...n ] ) ]
  155. [ ON DELETE { CASCADE | NO ACTION } ]
  156. [ ON UPDATE { CASCADE | NO ACTION } ]
  157. [ NOT FOR REPLICATION ]
  158. | CHECK [ NOT FOR REPLICATION ]
  159. ( search_conditions )
  160. }
  161. */
  162. /*
  163. CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
  164. ON { table | view } ( column [ ASC | DESC ] [ ,...n ] )
  165. [ WITH < index_option > [ ,...n] ]
  166. [ ON filegroup ]
  167. < index_option > :: =
  168. { PAD_INDEX |
  169. FILLFACTOR = fillfactor |
  170. IGNORE_DUP_KEY |
  171. DROP_EXISTING |
  172. STATISTICS_NORECOMPUTE |
  173. SORT_IN_TEMPDB
  174. }
  175. */
  176. function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
  177. {
  178. $sql = array();
  179. if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
  180. $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
  181. if ( isset($idxoptions['DROP']) )
  182. return $sql;
  183. }
  184. if ( empty ($flds) ) {
  185. return $sql;
  186. }
  187. $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
  188. $clustered = isset($idxoptions['CLUSTERED']) ? ' CLUSTERED' : '';
  189. if ( is_array($flds) )
  190. $flds = implode(', ',$flds);
  191. $s = 'CREATE' . $unique . $clustered . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
  192. if ( isset($idxoptions[$this->upperName]) )
  193. $s .= $idxoptions[$this->upperName];
  194. $sql[] = $s;
  195. return $sql;
  196. }
  197. function _GetSize($ftype, $ty, $fsize, $fprec)
  198. {
  199. switch ($ftype) {
  200. case 'INT':
  201. case 'SMALLINT':
  202. case 'TINYINT':
  203. case 'BIGINT':
  204. return $ftype;
  205. }
  206. if ($ty == 'T') return $ftype;
  207. return parent::_GetSize($ftype, $ty, $fsize, $fprec);
  208. }
  209. }
  210. ?>