datadict-mssqlnative.inc.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. @version v5.20.17 31-Mar-2020
  4. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  5. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 4 for best viewing.
  10. */
  11. /*
  12. In ADOdb, named quotes for MS SQL Server use ". From the MSSQL Docs:
  13. Note Delimiters are for identifiers only. Delimiters cannot be used for keywords,
  14. whether or not they are marked as reserved in SQL Server.
  15. Quoted identifiers are delimited by double quotation marks ("):
  16. SELECT * FROM "Blanks in Table Name"
  17. Bracketed identifiers are delimited by brackets ([ ]):
  18. SELECT * FROM [Blanks In Table Name]
  19. Quoted identifiers are valid only when the QUOTED_IDENTIFIER option is set to ON. By default,
  20. the Microsoft OLE DB Provider for SQL Server and SQL Server ODBC driver set QUOTED_IDENTIFIER ON
  21. when they connect.
  22. In Transact-SQL, the option can be set at various levels using SET QUOTED_IDENTIFIER,
  23. the quoted identifier option of sp_dboption, or the user options option of sp_configure.
  24. When SET ANSI_DEFAULTS is ON, SET QUOTED_IDENTIFIER is enabled.
  25. Syntax
  26. SET QUOTED_IDENTIFIER { ON | OFF }
  27. */
  28. // security - hide paths
  29. if (!defined('ADODB_DIR')) die();
  30. class ADODB2_mssqlnative extends ADODB_DataDict {
  31. var $databaseType = 'mssqlnative';
  32. var $dropIndex = 'DROP INDEX %1$s ON %2$s';
  33. var $renameTable = "EXEC sp_rename '%s','%s'";
  34. var $renameColumn = "EXEC sp_rename '%s.%s','%s'";
  35. var $typeX = 'TEXT'; ## Alternatively, set it to VARCHAR(4000)
  36. var $typeXL = 'TEXT';
  37. //var $alterCol = ' ALTER COLUMN ';
  38. function MetaType($t,$len=-1,$fieldobj=false)
  39. {
  40. if (is_object($t)) {
  41. $fieldobj = $t;
  42. $t = $fieldobj->type;
  43. $len = $fieldobj->max_length;
  44. }
  45. $_typeConversion = array(
  46. -155 => 'D',
  47. 93 => 'D',
  48. -154 => 'D',
  49. -2 => 'D',
  50. 91 => 'D',
  51. 12 => 'C',
  52. 1 => 'C',
  53. -9 => 'C',
  54. -8 => 'C',
  55. -7 => 'L',
  56. -6 => 'I2',
  57. -5 => 'I8',
  58. -11 => 'I',
  59. 4 => 'I',
  60. 5 => 'I4',
  61. -1 => 'X',
  62. -10 => 'X',
  63. 2 => 'N',
  64. 3 => 'N',
  65. 6 => 'N',
  66. 7 => 'N',
  67. -152 => 'X',
  68. -151 => 'X',
  69. -4 => 'X',
  70. -3 => 'X'
  71. );
  72. return $_typeConversion($t);
  73. }
  74. function ActualType($meta)
  75. {
  76. $DATE_TYPE = 'DATETIME';
  77. switch(strtoupper($meta)) {
  78. case 'C': return 'VARCHAR';
  79. case 'XL': return (isset($this)) ? $this->typeXL : 'TEXT';
  80. case 'X': return (isset($this)) ? $this->typeX : 'TEXT'; ## could be varchar(8000), but we want compat with oracle
  81. case 'C2': return 'NVARCHAR';
  82. case 'X2': return 'NTEXT';
  83. case 'B': return 'IMAGE';
  84. case 'D': return $DATE_TYPE;
  85. case 'T': return 'TIME';
  86. case 'L': return 'BIT';
  87. case 'R':
  88. case 'I': return 'INT';
  89. case 'I1': return 'TINYINT';
  90. case 'I2': return 'SMALLINT';
  91. case 'I4': return 'INT';
  92. case 'I8': return 'BIGINT';
  93. case 'F': return 'REAL';
  94. case 'N': return 'NUMERIC';
  95. default:
  96. print "RETURN $meta";
  97. return $meta;
  98. }
  99. }
  100. function AddColumnSQL($tabname, $flds)
  101. {
  102. $tabname = $this->TableName ($tabname);
  103. $f = array();
  104. list($lines,$pkey) = $this->_GenFields($flds);
  105. $s = "ALTER TABLE $tabname $this->addCol";
  106. foreach($lines as $v) {
  107. $f[] = "\n $v";
  108. }
  109. $s .= implode(', ',$f);
  110. $sql[] = $s;
  111. return $sql;
  112. }
  113. function DefaultConstraintname($tabname, $colname)
  114. {
  115. $constraintname = false;
  116. $rs = $this->connection->Execute(
  117. "SELECT name FROM sys.default_constraints
  118. WHERE object_name(parent_object_id) = '$tabname'
  119. AND col_name(parent_object_id, parent_column_id) = '$colname'"
  120. );
  121. if ( is_object($rs) ) {
  122. $row = $rs->FetchRow();
  123. $constraintname = $row['name'];
  124. }
  125. return $constraintname;
  126. }
  127. function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
  128. {
  129. $tabname = $this->TableName ($tabname);
  130. $sql = array();
  131. list($lines,$pkey,$idxs) = $this->_GenFields($flds);
  132. $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' ';
  133. foreach($lines as $v) {
  134. $not_null = false;
  135. if ($not_null = preg_match('/NOT NULL/i',$v)) {
  136. $v = preg_replace('/NOT NULL/i','',$v);
  137. }
  138. if (preg_match('/^([^ ]+) .*DEFAULT (\'[^\']+\'|\"[^\"]+\"|[^ ]+)/',$v,$matches)) {
  139. list(,$colname,$default) = $matches;
  140. $v = preg_replace('/^' . preg_quote($colname) . '\s/', '', $v);
  141. $t = trim(str_replace('DEFAULT '.$default,'',$v));
  142. if ( $constraintname = $this->DefaultConstraintname($tabname,$colname) ) {
  143. $sql[] = 'ALTER TABLE '.$tabname.' DROP CONSTRAINT '. $constraintname;
  144. }
  145. if ($not_null) {
  146. $sql[] = $alter . $colname . ' ' . $t . ' NOT NULL';
  147. } else {
  148. $sql[] = $alter . $colname . ' ' . $t ;
  149. }
  150. $sql[] = 'ALTER TABLE ' . $tabname
  151. . ' ADD CONSTRAINT DF__' . $tabname . '__' . $colname . '__' . dechex(rand())
  152. . ' DEFAULT ' . $default . ' FOR ' . $colname;
  153. } else {
  154. $colname = strtok($v," ");
  155. if ( $constraintname = $this->DefaultConstraintname($tabname,$colname) ) {
  156. $sql[] = 'ALTER TABLE '.$tabname.' DROP CONSTRAINT '. $constraintname;
  157. }
  158. if ($not_null) {
  159. $sql[] = $alter . $v . ' NOT NULL';
  160. } else {
  161. $sql[] = $alter . $v;
  162. }
  163. }
  164. }
  165. if (is_array($idxs)) {
  166. foreach($idxs as $idx => $idxdef) {
  167. $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']);
  168. $sql = array_merge($sql, $sql_idxs);
  169. }
  170. }
  171. return $sql;
  172. }
  173. /**
  174. * Drop a column, syntax is ALTER TABLE table DROP COLUMN column,column
  175. *
  176. * @param string $tabname Table Name
  177. * @param string[] $flds One, or an array of Fields To Drop
  178. * @param string $tableflds Throwaway value to make the function match the parent
  179. * @param string $tableoptions Throway value to make the function match the parent
  180. *
  181. * @return string The SQL necessary to drop the column
  182. */
  183. function DropColumnSQL($tabname, $flds, $tableflds='',$tableoptions='')
  184. {
  185. $tabname = $this->TableName ($tabname);
  186. if (!is_array($flds))
  187. $flds = explode(',',$flds);
  188. $f = array();
  189. $s = 'ALTER TABLE ' . $tabname;
  190. foreach($flds as $v) {
  191. if ( $constraintname = $this->DefaultConstraintname($tabname,$v) ) {
  192. $sql[] = 'ALTER TABLE ' . $tabname . ' DROP CONSTRAINT ' . $constraintname;
  193. }
  194. $f[] = ' DROP COLUMN ' . $this->NameQuote($v);
  195. }
  196. $s .= implode(', ',$f);
  197. $sql[] = $s;
  198. return $sql;
  199. }
  200. // return string must begin with space
  201. function _CreateSuffix($fname,&$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint,$funsigned)
  202. {
  203. $suffix = '';
  204. if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault";
  205. if ($fautoinc) $suffix .= ' IDENTITY(1,1)';
  206. if ($fnotnull) $suffix .= ' NOT NULL';
  207. else if ($suffix == '') $suffix .= ' NULL';
  208. if ($fconstraint) $suffix .= ' '.$fconstraint;
  209. return $suffix;
  210. }
  211. /*
  212. CREATE TABLE
  213. [ database_name.[ owner ] . | owner. ] table_name
  214. ( { < column_definition >
  215. | column_name AS computed_column_expression
  216. | < table_constraint > ::= [ CONSTRAINT constraint_name ] }
  217. | [ { PRIMARY KEY | UNIQUE } [ ,...n ]
  218. )
  219. [ ON { filegroup | DEFAULT } ]
  220. [ TEXTIMAGE_ON { filegroup | DEFAULT } ]
  221. < column_definition > ::= { column_name data_type }
  222. [ COLLATE < collation_name > ]
  223. [ [ DEFAULT constant_expression ]
  224. | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ]
  225. ]
  226. [ ROWGUIDCOL]
  227. [ < column_constraint > ] [ ...n ]
  228. < column_constraint > ::= [ CONSTRAINT constraint_name ]
  229. { [ NULL | NOT NULL ]
  230. | [ { PRIMARY KEY | UNIQUE }
  231. [ CLUSTERED | NONCLUSTERED ]
  232. [ WITH FILLFACTOR = fillfactor ]
  233. [ON {filegroup | DEFAULT} ] ]
  234. ]
  235. | [ [ FOREIGN KEY ]
  236. REFERENCES ref_table [ ( ref_column ) ]
  237. [ ON DELETE { CASCADE | NO ACTION } ]
  238. [ ON UPDATE { CASCADE | NO ACTION } ]
  239. [ NOT FOR REPLICATION ]
  240. ]
  241. | CHECK [ NOT FOR REPLICATION ]
  242. ( logical_expression )
  243. }
  244. < table_constraint > ::= [ CONSTRAINT constraint_name ]
  245. { [ { PRIMARY KEY | UNIQUE }
  246. [ CLUSTERED | NONCLUSTERED ]
  247. { ( column [ ASC | DESC ] [ ,...n ] ) }
  248. [ WITH FILLFACTOR = fillfactor ]
  249. [ ON { filegroup | DEFAULT } ]
  250. ]
  251. | FOREIGN KEY
  252. [ ( column [ ,...n ] ) ]
  253. REFERENCES ref_table [ ( ref_column [ ,...n ] ) ]
  254. [ ON DELETE { CASCADE | NO ACTION } ]
  255. [ ON UPDATE { CASCADE | NO ACTION } ]
  256. [ NOT FOR REPLICATION ]
  257. | CHECK [ NOT FOR REPLICATION ]
  258. ( search_conditions )
  259. }
  260. */
  261. /*
  262. CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
  263. ON { table | view } ( column [ ASC | DESC ] [ ,...n ] )
  264. [ WITH < index_option > [ ,...n] ]
  265. [ ON filegroup ]
  266. < index_option > :: =
  267. { PAD_INDEX |
  268. FILLFACTOR = fillfactor |
  269. IGNORE_DUP_KEY |
  270. DROP_EXISTING |
  271. STATISTICS_NORECOMPUTE |
  272. SORT_IN_TEMPDB
  273. }
  274. */
  275. function _IndexSQL($idxname, $tabname, $flds, $idxoptions)
  276. {
  277. $sql = array();
  278. if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) {
  279. $sql[] = sprintf ($this->dropIndex, $idxname, $tabname);
  280. if ( isset($idxoptions['DROP']) )
  281. return $sql;
  282. }
  283. if ( empty ($flds) ) {
  284. return $sql;
  285. }
  286. $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : '';
  287. $clustered = isset($idxoptions['CLUSTERED']) ? ' CLUSTERED' : '';
  288. if ( is_array($flds) )
  289. $flds = implode(', ',$flds);
  290. $s = 'CREATE' . $unique . $clustered . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')';
  291. if ( isset($idxoptions[$this->upperName]) )
  292. $s .= $idxoptions[$this->upperName];
  293. $sql[] = $s;
  294. return $sql;
  295. }
  296. function _GetSize($ftype, $ty, $fsize, $fprec)
  297. {
  298. switch ($ftype) {
  299. case 'INT':
  300. case 'SMALLINT':
  301. case 'TINYINT':
  302. case 'BIGINT':
  303. return $ftype;
  304. }
  305. if ($ty == 'T') return $ftype;
  306. return parent::_GetSize($ftype, $ty, $fsize, $fprec);
  307. }
  308. }