| [ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 3 /** 4 V4.65 22 July 2005 (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved. 5 Released under both BSD license and Lesser GPL library license. 6 Whenever there is any discrepancy between the two licenses, 7 the BSD license will take precedence. 8 9 Set tabs to 4 for best viewing. 10 11 */ 12 13 // security - hide paths 14 if (!defined('ADODB_DIR')) die(); 15 16 class ADODB2_mssql extends ADODB_DataDict { 17 var $databaseType = 'mssql'; 18 var $dropIndex = 'DROP INDEX %2$s.%1$s'; 19 var $renameTable = "EXEC sp_rename '%s','%s'"; 20 var $renameColumn = "EXEC sp_rename '%s.%s','%s'"; 21 22 var $typeX = 'TEXT'; ## Alternatively, set it to VARCHAR(4000) 23 var $typeXL = 'TEXT'; 24 25 //var $alterCol = ' ALTER COLUMN '; 26 27 function MetaType($t,$len=-1,$fieldobj=false) 28 { 29 if (is_object($t)) { 30 $fieldobj = $t; 31 $t = $fieldobj->type; 32 $len = $fieldobj->max_length; 33 } 34 35 $len = -1; // mysql max_length is not accurate 36 switch (strtoupper($t)) { 37 case 'R': 38 case 'INT': 39 case 'INTEGER': return 'I'; 40 case 'BIT': 41 case 'TINYINT': return 'I1'; 42 case 'SMALLINT': return 'I2'; 43 case 'BIGINT': return 'I8'; 44 45 case 'REAL': 46 case 'FLOAT': return 'F'; 47 default: return parent::MetaType($t,$len,$fieldobj); 48 } 49 } 50 51 function ActualType($meta) 52 { 53 switch(strtoupper($meta)) { 54 55 case 'C': return 'VARCHAR'; 56 case 'XL': return (isset($this)) ? $this->typeXL : 'TEXT'; 57 case 'X': return (isset($this)) ? $this->typeX : 'TEXT'; ## could be varchar(8000), but we want compat with oracle 58 case 'C2': return 'NVARCHAR'; 59 case 'X2': return 'NTEXT'; 60 61 case 'B': return 'IMAGE'; 62 63 case 'D': return 'DATETIME'; 64 case 'T': return 'DATETIME'; 65 case 'L': return 'BIT'; 66 67 case 'R': 68 case 'I': return 'INT'; 69 case 'I1': return 'TINYINT'; 70 case 'I2': return 'SMALLINT'; 71 case 'I4': return 'INT'; 72 case 'I8': return 'BIGINT'; 73 74 case 'F': return 'REAL'; 75 case 'N': return 'NUMERIC'; 76 default: 77 return $meta; 78 } 79 } 80 81 82 function AddColumnSQL($tabname, $flds) 83 { 84 $tabname = $this->TableName ($tabname); 85 $f = array(); 86 list($lines,$pkey) = $this->_GenFields($flds); 87 $s = "ALTER TABLE $tabname $this->addCol"; 88 foreach($lines as $v) { 89 $f[] = "\n $v"; 90 } 91 $s .= implode(', ',$f); 92 $sql[] = $s; 93 return $sql; 94 } 95 96 /** 97 * query the name of a default constrain 98 * 99 * @param string $tabname table-name 100 * @param string $colname column-name 101 * @return boolean/string name of the default constrain or false if there's none 102 */ 103 function _get_default_name($tabname,$colname) 104 { 105 return $this->connection->GetOne("SELECT sysobjects.name FROM sysobjects JOIN syscolumns ON syscolumns.id=parent_obj AND info=colorder ". 106 "WHERE syscolumns.name='$colname' AND sysobjects.xtype='D' AND parent_obj=(SELECT id FROM sysobjects WHERE name='$tableoptions' and type='U')"); 107 } 108 109 /** 110 * Change the definition of one column 111 * 112 * Reimplemented because MSSQL need a different handling for DEFAULT, it cant be in an ALTER COLUMN 113 * 114 * @param string $tabname table-name 115 * @param string $flds column-name and type for the changed column 116 * @param string $tableflds='' complete defintion of the new table, eg. for postgres, default '' 117 * @param array/string $tableoptions='' options for the new table see CreateTableSQL, default '' 118 * @return array with SQL strings 119 */ 120 function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') 121 { 122 $tabname = $this->TableName ($tabname); 123 $sql = array(); 124 list($lines,$pkey) = $this->_GenFields($flds); 125 $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' '; 126 foreach($lines as $v) { 127 list($colname) = explode(' ',$v); 128 // drop a default if it exists 129 $def_name = $this->_get_default_name($tabname,$colname); 130 $sql[] = "EXEC sp_unbindefault '$tabname.$colname'"; 131 $sql[] = "DROP DEFAULT $def_name"; 132 if (preg_match("/DEFAULT ([0-9]+|'[^']*')/",$lines,$matches)) { 133 $sql[] = $alter . str_replace("DEFAULT $matches[1]",'',$v); 134 $sql[] = "ALTER TABLE $tabname ADD CONSTRAINT def_{$tabname}_$colname DEFAULT $matches[1] FOR $colname"; 135 } else 136 $sql[] = $alter . $v; 137 } 138 return $sql; 139 } 140 141 function DropColumnSQL($tabname, $flds) 142 { 143 $tabname = $this->TableName ($tabname); 144 if (!is_array($flds)) 145 $flds = explode(',',$flds); 146 $f = array(); 147 $s = 'ALTER TABLE ' . $tabname; 148 foreach($flds as $v) { 149 $f[] = "\n$this->dropCol ".$this->NameQuote($v); 150 } 151 $s .= implode(', ',$f); 152 $sql[] = $s; 153 return $sql; 154 } 155 156 // return string must begin with space 157 function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint) 158 { 159 $suffix = ''; 160 if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault"; 161 if ($fautoinc) $suffix .= ' IDENTITY(1,1)'; 162 if ($fnotnull) $suffix .= ' NOT NULL'; 163 else if ($suffix == '') $suffix .= ' NULL'; 164 if ($fconstraint) $suffix .= ' '.$fconstraint; 165 return $suffix; 166 } 167 168 /* 169 CREATE TABLE 170 [ database_name.[ owner ] . | owner. ] table_name 171 ( { < column_definition > 172 | column_name AS computed_column_expression 173 | < table_constraint > ::= [ CONSTRAINT constraint_name ] } 174 175 | [ { PRIMARY KEY | UNIQUE } [ ,...n ] 176 ) 177 178 [ ON { filegroup | DEFAULT } ] 179 [ TEXTIMAGE_ON { filegroup | DEFAULT } ] 180 181 < column_definition > ::= { column_name data_type } 182 [ COLLATE < collation_name > ] 183 [ [ DEFAULT constant_expression ] 184 | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ] 185 ] 186 [ ROWGUIDCOL] 187 [ < column_constraint > ] [ ...n ] 188 189 < column_constraint > ::= [ CONSTRAINT constraint_name ] 190 { [ NULL | NOT NULL ] 191 | [ { PRIMARY KEY | UNIQUE } 192 [ CLUSTERED | NONCLUSTERED ] 193 [ WITH FILLFACTOR = fillfactor ] 194 [ON {filegroup | DEFAULT} ] ] 195 ] 196 | [ [ FOREIGN KEY ] 197 REFERENCES ref_table [ ( ref_column ) ] 198 [ ON DELETE { CASCADE | NO ACTION } ] 199 [ ON UPDATE { CASCADE | NO ACTION } ] 200 [ NOT FOR REPLICATION ] 201 ] 202 | CHECK [ NOT FOR REPLICATION ] 203 ( logical_expression ) 204 } 205 206 < table_constraint > ::= [ CONSTRAINT constraint_name ] 207 { [ { PRIMARY KEY | UNIQUE } 208 [ CLUSTERED | NONCLUSTERED ] 209 { ( column [ ASC | DESC ] [ ,...n ] ) } 210 [ WITH FILLFACTOR = fillfactor ] 211 [ ON { filegroup | DEFAULT } ] 212 ] 213 | FOREIGN KEY 214 [ ( column [ ,...n ] ) ] 215 REFERENCES ref_table [ ( ref_column [ ,...n ] ) ] 216 [ ON DELETE { CASCADE | NO ACTION } ] 217 [ ON UPDATE { CASCADE | NO ACTION } ] 218 [ NOT FOR REPLICATION ] 219 | CHECK [ NOT FOR REPLICATION ] 220 ( search_conditions ) 221 } 222 223 224 */ 225 226 /* 227 CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name 228 ON { table | view } ( column [ ASC | DESC ] [ ,...n ] ) 229 [ WITH < index_option > [ ,...n] ] 230 [ ON filegroup ] 231 < index_option > :: = 232 { PAD_INDEX | 233 FILLFACTOR = fillfactor | 234 IGNORE_DUP_KEY | 235 DROP_EXISTING | 236 STATISTICS_NORECOMPUTE | 237 SORT_IN_TEMPDB 238 } 239 */ 240 function _IndexSQL($idxname, $tabname, $flds, $idxoptions) 241 { 242 $sql = array(); 243 244 if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) { 245 $sql[] = sprintf ($this->dropIndex, $idxname, $tabname); 246 if ( isset($idxoptions['DROP']) ) 247 return $sql; 248 } 249 250 if ( empty ($flds) ) { 251 return $sql; 252 } 253 254 $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : ''; 255 $clustered = isset($idxoptions['CLUSTERED']) ? ' CLUSTERED' : ''; 256 257 if ( is_array($flds) ) 258 $flds = implode(', ',$flds); 259 $s = 'CREATE' . $unique . $clustered . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')'; 260 261 if ( isset($idxoptions[$this->upperName]) ) 262 $s .= $idxoptions[$this->upperName]; 263 264 265 $sql[] = $s; 266 267 return $sql; 268 } 269 270 271 function _GetSize($ftype, $ty, $fsize, $fprec) 272 { 273 switch ($ftype) { 274 case 'INT': 275 case 'SMALLINT': 276 case 'TINYINT': 277 case 'BIGINT': 278 return $ftype; 279 } 280 if ($ty == 'T') return $ftype; 281 return parent::_GetSize($ftype, $ty, $fsize, $fprec); 282 283 } 284 } 285 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |