]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-odbc_mssql.inc.php
extra_empty_lines
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-odbc_mssql.inc.php
1 <?php
2 /*
3 V4.22 15 Apr 2004  (c) 2000-2004 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   Latest version is available at http://php.weblogs.com/
10
11   MSSQL support via ODBC. Requires ODBC. Works on Windows and Unix.
12   For Unix configuration, see http://phpbuilder.com/columns/alberto20000919.php3
13 */
14
15 if (!defined('_ADODB_ODBC_LAYER')) {
16     include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
17 }
18
19 class  ADODB_odbc_mssql extends ADODB_odbc {
20     var $databaseType = 'odbc_mssql';
21     var $fmtDate = "'Y-m-d'";
22     var $fmtTimeStamp = "'Y-m-d h:i:sA'";
23     var $_bindInputArray = true;
24     var $metaTablesSQL="select name,case when type='U' then 'T' else 'V' end from sysobjects where (type='U' or type='V') and (name not in ('sysallocations','syscolumns','syscomments','sysdepends','sysfilegroups','sysfiles','sysfiles1','sysforeignkeys','sysfulltextcatalogs','sysindexes','sysindexkeys','sysmembers','sysobjects','syspermissions','sysprotects','sysreferences','systypes','sysusers','sysalternates','sysconstraints','syssegments','REFERENTIAL_CONSTRAINTS','CHECK_CONSTRAINTS','CONSTRAINT_TABLE_USAGE','CONSTRAINT_COLUMN_USAGE','VIEWS','VIEW_TABLE_USAGE','VIEW_COLUMN_USAGE','SCHEMATA','TABLES','TABLE_CONSTRAINTS','TABLE_PRIVILEGES','COLUMNS','COLUMN_DOMAIN_USAGE','COLUMN_PRIVILEGES','DOMAINS','DOMAIN_CONSTRAINTS','KEY_COLUMN_USAGE'))";
25     var $metaColumnsSQL = "select c.name,t.name,c.length from syscolumns c join systypes t on t.xusertype=c.xusertype join sysobjects o on o.id=c.id where o.name='%s'";
26     var $hasTop = 'top';                // support mssql/interbase SELECT TOP 10 * FROM TABLE
27     var $sysDate = 'GetDate()';
28     var $sysTimeStamp = 'GetDate()';
29     var $leftOuter = '*=';
30     var $rightOuter = '=*';
31     var $upperCase = 'upper';
32     var $substr = 'substring';
33     var $length = 'len';
34     var $ansiOuter = true; // for mssql7 or later
35     var $identitySQL = 'select @@IDENTITY'; // 'select SCOPE_IDENTITY'; # for mssql 2000
36     var $hasInsertID = true;
37     var $connectStmt = 'SET CONCAT_NULL_YIELDS_NULL OFF'; # When SET CONCAT_NULL_YIELDS_NULL is ON,
38                                                           # concatenating a null value with a string yields a NULL result
39
40     function ADODB_odbc_mssql()
41     {
42         $this->ADODB_odbc();
43         $this->curmode = SQL_CUR_USE_ODBC;
44     }
45
46     // crashes php...
47     function ServerInfo()
48     {
49     global $ADODB_FETCH_MODE;
50         $save = $ADODB_FETCH_MODE;
51         $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
52         $row = $this->GetRow("execute sp_server_info 2");
53         $ADODB_FETCH_MODE = $save;
54         if (!is_array($row)) return false;
55         $arr['description'] = $row[2];
56         $arr['version'] = ADOConnection::_findvers($arr['description']);
57         return $arr;
58     }
59
60     function IfNull( $field, $ifNull )
61     {
62         return " ISNULL($field, $ifNull) "; // if MS SQL Server
63     }
64
65     function _insertid()
66     {
67     // SCOPE_IDENTITY()
68     // Returns the last IDENTITY value inserted into an IDENTITY column in
69     // the same scope. A scope is a module -- a stored procedure, trigger,
70     // function, or batch. Thus, two statements are in the same scope if
71     // they are in the same stored procedure, function, or batch.
72             return $this->GetOne($this->identitySQL);
73     }
74
75     function MetaForeignKeys($table, $owner=false, $upper=false)
76     {
77     global $ADODB_FETCH_MODE;
78
79         $save = $ADODB_FETCH_MODE;
80         $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
81         $table = $this->qstr(strtoupper($table));
82
83         $sql =
84 "select object_name(constid) as constraint_name,
85     col_name(fkeyid, fkey) as column_name,
86     object_name(rkeyid) as referenced_table_name,
87        col_name(rkeyid, rkey) as referenced_column_name
88 from sysforeignkeys
89 where upper(object_name(fkeyid)) = $table
90 order by constraint_name, referenced_table_name, keyno";
91
92         $constraints =& $this->GetArray($sql);
93
94         $ADODB_FETCH_MODE = $save;
95
96         $arr = false;
97         foreach($constraints as $constr) {
98             //print_r($constr);
99             $arr[$constr[0]][$constr[2]][] = $constr[1].'='.$constr[3];
100         }
101         if (!$arr) return false;
102
103         $arr2 = false;
104
105         foreach($arr as $k => $v) {
106             foreach($v as $a => $b) {
107                 if ($upper) $a = strtoupper($a);
108                 $arr2[$a] = $b;
109             }
110         }
111         return $arr2;
112     }
113
114     function &MetaTables($ttype=false,$showSchema=false,$mask=false)
115     {
116         if ($mask) {$this->debug=1;
117             $save = $this->metaTablesSQL;
118             $mask = $this->qstr($mask);
119             $this->metaTablesSQL .= " AND name like $mask";
120         }
121         $ret =& ADOConnection::MetaTables($ttype,$showSchema);
122
123         if ($mask) {
124             $this->metaTablesSQL = $save;
125         }
126         return $ret;
127     }
128
129     function &MetaColumns($table)
130     {
131         return ADOConnection::MetaColumns($table);
132     }
133
134     function _query($sql,$inputarr)
135     {
136         if (is_string($sql)) $sql = str_replace('||','+',$sql);
137         return ADODB_odbc::_query($sql,$inputarr);
138     }
139
140     // "Stein-Aksel Basma" <basma@accelero.no>
141     // tested with MSSQL 2000
142     function &MetaPrimaryKeys($table)
143     {
144         $sql = "select k.column_name from information_schema.key_column_usage k,
145         information_schema.table_constraints tc
146         where tc.constraint_name = k.constraint_name and tc.constraint_type =
147         'PRIMARY KEY' and k.table_name = '$table'";
148
149         $a = $this->GetCol($sql);
150         if ($a && sizeof($a)>0) return $a;
151         return false;
152     }
153
154     function &SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
155     {
156         if ($nrows > 0 && $offset <= 0) {
157             $sql = preg_replace(
158                 '/(^\s*select\s+(distinctrow|distinct)?)/i','\\1 '.$this->hasTop." $nrows ",$sql);
159             $rs =& $this->Execute($sql,$inputarr);
160         } else
161             $rs =& ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
162
163         return $rs;
164     }
165
166     // Format date column in sql string given an input format that understands Y M D
167     function SQLDate($fmt, $col=false)
168     {
169         if (!$col) $col = $this->sysTimeStamp;
170         $s = '';
171
172         $len = strlen($fmt);
173         for ($i=0; $i < $len; $i++) {
174             if ($s) $s .= '+';
175             $ch = $fmt[$i];
176             switch($ch) {
177             case 'Y':
178             case 'y':
179                 $s .= "datename(yyyy,$col)";
180                 break;
181             case 'M':
182                 $s .= "convert(char(3),$col,0)";
183                 break;
184             case 'm':
185                 $s .= "replace(str(month($col),2),' ','0')";
186                 break;
187             case 'Q':
188             case 'q':
189                 $s .= "datename(quarter,$col)";
190                 break;
191             case 'D':
192             case 'd':
193                 $s .= "replace(str(day($col),2),' ','0')";
194                 break;
195             case 'h':
196                 $s .= "substring(convert(char(14),$col,0),13,2)";
197                 break;
198
199             case 'H':
200                 $s .= "replace(str(datepart(hh,$col),2),' ','0')";
201                 break;
202
203             case 'i':
204                 $s .= "replace(str(datepart(mi,$col),2),' ','0')";
205                 break;
206             case 's':
207                 $s .= "replace(str(datepart(ss,$col),2),' ','0')";
208                 break;
209             case 'a':
210             case 'A':
211                 $s .= "substring(convert(char(19),$col,0),18,2)";
212                 break;
213
214             default:
215                 if ($ch == '\\') {
216                     $i++;
217                     $ch = substr($fmt,$i,1);
218                 }
219                 $s .= $this->qstr($ch);
220                 break;
221             }
222         }
223         return $s;
224     }
225
226 }
227
228 class  ADORecordSet_odbc_mssql extends ADORecordSet_odbc {
229
230     var $databaseType = 'odbc_mssql';
231
232     function ADORecordSet_odbc_mssql($id,$mode=false)
233     {
234         return $this->ADORecordSet_odbc($id,$mode);
235     }
236 }
237 ?>