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