[2] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | V4.51 29 July 2004 (c) 2000-2004 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 | |
---|
| 19 | var $dropIndex = 'DROP INDEX %2$s.%1$s'; |
---|
| 20 | |
---|
| 21 | function MetaType($t,$len=-1,$fieldobj=false) |
---|
| 22 | { |
---|
| 23 | if (is_object($t)) { |
---|
| 24 | $fieldobj = $t; |
---|
| 25 | $t = $fieldobj->type; |
---|
| 26 | $len = $fieldobj->max_length; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | $len = -1; // mysql max_length is not accurate |
---|
| 30 | switch (strtoupper($t)) { |
---|
| 31 | |
---|
| 32 | case 'INT': |
---|
| 33 | case 'INTEGER': return 'I'; |
---|
| 34 | case 'BIT': |
---|
| 35 | case 'TINYINT': return 'I1'; |
---|
| 36 | case 'SMALLINT': return 'I2'; |
---|
| 37 | case 'BIGINT': return 'I8'; |
---|
| 38 | |
---|
| 39 | case 'REAL': |
---|
| 40 | case 'FLOAT': return 'F'; |
---|
| 41 | default: return parent::MetaType($t,$len,$fieldobj); |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | function ActualType($meta) |
---|
| 46 | { |
---|
| 47 | switch(strtoupper($meta)) { |
---|
| 48 | case 'C': return 'VARCHAR'; |
---|
| 49 | case 'XL': |
---|
| 50 | case 'X': return 'TEXT'; |
---|
| 51 | |
---|
| 52 | case 'C2': return 'NVARCHAR'; |
---|
| 53 | case 'X2': return 'NTEXT'; |
---|
| 54 | |
---|
| 55 | case 'B': return 'IMAGE'; |
---|
| 56 | |
---|
| 57 | case 'D': return 'DATETIME'; |
---|
| 58 | case 'T': return 'DATETIME'; |
---|
| 59 | case 'L': return 'BIT'; |
---|
| 60 | |
---|
| 61 | case 'I': return 'INT'; |
---|
| 62 | case 'I1': return 'TINYINT'; |
---|
| 63 | case 'I2': return 'SMALLINT'; |
---|
| 64 | case 'I4': return 'INT'; |
---|
| 65 | case 'I8': return 'BIGINT'; |
---|
| 66 | |
---|
| 67 | case 'F': return 'REAL'; |
---|
| 68 | case 'N': return 'NUMERIC'; |
---|
| 69 | default: |
---|
| 70 | return $meta; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | function AddColumnSQL($tabname, $flds) |
---|
| 76 | { |
---|
| 77 | $tabname = $this->TableName ($tabname); |
---|
| 78 | $f = array(); |
---|
| 79 | list($lines,$pkey) = $this->_GenFields($flds); |
---|
| 80 | $s = "ALTER TABLE $tabname $this->addCol"; |
---|
| 81 | foreach($lines as $v) { |
---|
| 82 | $f[] = "\n $v"; |
---|
| 83 | } |
---|
| 84 | $s .= implode(',',$f); |
---|
| 85 | $sql[] = $s; |
---|
| 86 | return $sql; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /* |
---|
| 90 | function AlterColumnSQL($tabname, $flds) |
---|
| 91 | { |
---|
| 92 | $tabname = $this->TableName ($tabname); |
---|
| 93 | $sql = array(); |
---|
| 94 | list($lines,$pkey) = $this->_GenFields($flds); |
---|
| 95 | foreach($lines as $v) { |
---|
| 96 | $sql[] = "ALTER TABLE $tabname $this->alterCol $v"; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | return $sql; |
---|
| 100 | } |
---|
| 101 | */ |
---|
| 102 | |
---|
| 103 | function DropColumnSQL($tabname, $flds) |
---|
| 104 | { |
---|
| 105 | $tabname = $this->TableName ($tabname); |
---|
| 106 | if (!is_array($flds)) |
---|
| 107 | $flds = explode(',',$flds); |
---|
| 108 | $f = array(); |
---|
| 109 | $s = 'ALTER TABLE ' . $tabname; |
---|
| 110 | foreach($flds as $v) { |
---|
| 111 | $f[] = "\n$this->dropCol $v"; |
---|
| 112 | } |
---|
| 113 | $s .= implode(',',$f); |
---|
| 114 | $sql[] = $s; |
---|
| 115 | return $sql; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // return string must begin with space |
---|
| 119 | function _CreateSuffix($fname,$ftype,$fnotnull,$fdefault,$fautoinc,$fconstraint) |
---|
| 120 | { |
---|
| 121 | $suffix = ''; |
---|
| 122 | if (strlen($fdefault)) $suffix .= " DEFAULT $fdefault"; |
---|
| 123 | if ($fautoinc) $suffix .= ' IDENTITY(1,1)'; |
---|
| 124 | if ($fnotnull) $suffix .= ' NOT NULL'; |
---|
| 125 | else if ($suffix == '') $suffix .= ' NULL'; |
---|
| 126 | if ($fconstraint) $suffix .= ' '.$fconstraint; |
---|
| 127 | return $suffix; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* |
---|
| 131 | CREATE TABLE |
---|
| 132 | [ database_name.[ owner ] . | owner. ] table_name |
---|
| 133 | ( { < column_definition > |
---|
| 134 | | column_name AS computed_column_expression |
---|
| 135 | | < table_constraint > ::= [ CONSTRAINT constraint_name ] } |
---|
| 136 | |
---|
| 137 | | [ { PRIMARY KEY | UNIQUE } [ ,...n ] |
---|
| 138 | ) |
---|
| 139 | |
---|
| 140 | [ ON { filegroup | DEFAULT } ] |
---|
| 141 | [ TEXTIMAGE_ON { filegroup | DEFAULT } ] |
---|
| 142 | |
---|
| 143 | < column_definition > ::= { column_name data_type } |
---|
| 144 | [ COLLATE < collation_name > ] |
---|
| 145 | [ [ DEFAULT constant_expression ] |
---|
| 146 | | [ IDENTITY [ ( seed , increment ) [ NOT FOR REPLICATION ] ] ] |
---|
| 147 | ] |
---|
| 148 | [ ROWGUIDCOL] |
---|
| 149 | [ < column_constraint > ] [ ...n ] |
---|
| 150 | |
---|
| 151 | < column_constraint > ::= [ CONSTRAINT constraint_name ] |
---|
| 152 | { [ NULL | NOT NULL ] |
---|
| 153 | | [ { PRIMARY KEY | UNIQUE } |
---|
| 154 | [ CLUSTERED | NONCLUSTERED ] |
---|
| 155 | [ WITH FILLFACTOR = fillfactor ] |
---|
| 156 | [ON {filegroup | DEFAULT} ] ] |
---|
| 157 | ] |
---|
| 158 | | [ [ FOREIGN KEY ] |
---|
| 159 | REFERENCES ref_table [ ( ref_column ) ] |
---|
| 160 | [ ON DELETE { CASCADE | NO ACTION } ] |
---|
| 161 | [ ON UPDATE { CASCADE | NO ACTION } ] |
---|
| 162 | [ NOT FOR REPLICATION ] |
---|
| 163 | ] |
---|
| 164 | | CHECK [ NOT FOR REPLICATION ] |
---|
| 165 | ( logical_expression ) |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | < table_constraint > ::= [ CONSTRAINT constraint_name ] |
---|
| 169 | { [ { PRIMARY KEY | UNIQUE } |
---|
| 170 | [ CLUSTERED | NONCLUSTERED ] |
---|
| 171 | { ( column [ ASC | DESC ] [ ,...n ] ) } |
---|
| 172 | [ WITH FILLFACTOR = fillfactor ] |
---|
| 173 | [ ON { filegroup | DEFAULT } ] |
---|
| 174 | ] |
---|
| 175 | | FOREIGN KEY |
---|
| 176 | [ ( column [ ,...n ] ) ] |
---|
| 177 | REFERENCES ref_table [ ( ref_column [ ,...n ] ) ] |
---|
| 178 | [ ON DELETE { CASCADE | NO ACTION } ] |
---|
| 179 | [ ON UPDATE { CASCADE | NO ACTION } ] |
---|
| 180 | [ NOT FOR REPLICATION ] |
---|
| 181 | | CHECK [ NOT FOR REPLICATION ] |
---|
| 182 | ( search_conditions ) |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | */ |
---|
| 187 | |
---|
| 188 | /* |
---|
| 189 | CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name |
---|
| 190 | ON { table | view } ( column [ ASC | DESC ] [ ,...n ] ) |
---|
| 191 | [ WITH < index_option > [ ,...n] ] |
---|
| 192 | [ ON filegroup ] |
---|
| 193 | < index_option > :: = |
---|
| 194 | { PAD_INDEX | |
---|
| 195 | FILLFACTOR = fillfactor | |
---|
| 196 | IGNORE_DUP_KEY | |
---|
| 197 | DROP_EXISTING | |
---|
| 198 | STATISTICS_NORECOMPUTE | |
---|
| 199 | SORT_IN_TEMPDB |
---|
| 200 | } |
---|
| 201 | */ |
---|
| 202 | function _IndexSQL($idxname, $tabname, $flds, $idxoptions) |
---|
| 203 | { |
---|
| 204 | $sql = array(); |
---|
| 205 | |
---|
| 206 | if ( isset($idxoptions['REPLACE']) || isset($idxoptions['DROP']) ) { |
---|
| 207 | $sql[] = sprintf ($this->dropIndex, $idxname, $tabname); |
---|
| 208 | if ( isset($idxoptions['DROP']) ) |
---|
| 209 | return $sql; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | if ( empty ($flds) ) { |
---|
| 213 | return $sql; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | $unique = isset($idxoptions['UNIQUE']) ? ' UNIQUE' : ''; |
---|
| 217 | $clustered = isset($idxoptions['CLUSTERED']) ? ' CLUSTERED' : ''; |
---|
| 218 | |
---|
| 219 | if ( is_array($flds) ) |
---|
| 220 | $flds = implode(', ',$flds); |
---|
| 221 | $s = 'CREATE' . $unique . $clustered . ' INDEX ' . $idxname . ' ON ' . $tabname . ' (' . $flds . ')'; |
---|
| 222 | |
---|
| 223 | if ( isset($idxoptions[$this->upperName]) ) |
---|
| 224 | $s .= $idxoptions[$this->upperName]; |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | $sql[] = $s; |
---|
| 228 | |
---|
| 229 | return $sql; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | function _GetSize($ftype, $ty, $fsize, $fprec) |
---|
| 234 | { |
---|
| 235 | switch ($ftype) { |
---|
| 236 | case 'INT': |
---|
| 237 | case 'SMALLINT': |
---|
| 238 | case 'TINYINT': |
---|
| 239 | case 'BIGINT': |
---|
| 240 | return $ftype; |
---|
| 241 | } |
---|
| 242 | if (strlen($fsize) && $ty != 'X' && $ty != 'B' && strpos($ftype,'(') === false) { |
---|
| 243 | $ftype .= "(".$fsize; |
---|
| 244 | if (strlen($fprec)) $ftype .= ",".$fprec; |
---|
| 245 | $ftype .= ')'; |
---|
| 246 | } |
---|
| 247 | return $ftype; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | ?> |
---|