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