16 if (!defined(
'ADODB_DIR')) die();
18 if (!defined(
'_ADODB_ODBC_LAYER')) {
19 include(ADODB_DIR.
"/drivers/adodb-odbc.inc.php");
29 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'))";
31 "select c.name,t.name,c.length,c.isnullable, c.status,
32 (case when c.xusertype=61 then 0 else c.xprec end),
33 (case when c.xusertype=61 then 0 else c.xscale end)
34 from syscolumns c join systypes t on t.xusertype=c.xusertype join sysobjects o on o.id=c.id where o.name='%s'";
45 var
$connectStmt =
'SET CONCAT_NULL_YIELDS_NULL OFF'; # When SET CONCAT_NULL_YIELDS_NULL is ON,
46 # concatenating a null value with a string yields a NULL result
57 global $ADODB_FETCH_MODE;
58 $save = $ADODB_FETCH_MODE;
59 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
60 $row = $this->GetRow(
"execute sp_server_info 2");
61 $ADODB_FETCH_MODE = $save;
62 if (!is_array($row))
return false;
63 $arr[
'description'] = $row[2];
64 $arr[
'version'] = ADOConnection::_findvers($arr[
'description']);
70 return " ISNULL($field, $ifNull) ";
80 return $this->GetOne($this->identitySQL);
86 global $ADODB_FETCH_MODE;
88 $save = $ADODB_FETCH_MODE;
89 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
90 $table = $this->qstr(strtoupper($table));
93 "select object_name(constid) as constraint_name,
94 col_name(fkeyid, fkey) as column_name,
95 object_name(rkeyid) as referenced_table_name,
96 col_name(rkeyid, rkey) as referenced_column_name
98 where upper(object_name(fkeyid)) = $table
99 order by constraint_name, referenced_table_name, keyno";
101 $constraints = $this->GetArray($sql);
103 $ADODB_FETCH_MODE = $save;
106 foreach($constraints as $constr) {
108 $arr[$constr[0]][$constr[2]][] = $constr[1].
'='.$constr[3];
110 if (!$arr)
return false;
114 foreach($arr as $k => $v) {
115 foreach($v as
$a => $b) {
116 if ($upper)
$a = strtoupper(
$a);
123 function MetaTables($ttype=
false,$showSchema=
false,$mask=
false)
127 $mask = $this->qstr($mask);
128 $this->metaTablesSQL .=
" AND name like $mask";
130 $ret = ADOConnection::MetaTables($ttype,$showSchema);
133 $this->metaTablesSQL = $save;
141 $this->_findschema($table,$schema);
143 $dbName = $this->database;
144 $this->SelectDB($schema);
146 global $ADODB_FETCH_MODE;
147 $save = $ADODB_FETCH_MODE;
148 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
150 if ($this->fetchMode !==
false) $savem = $this->SetFetchMode(
false);
151 $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
154 $this->SelectDB($dbName);
157 if (isset($savem)) $this->SetFetchMode($savem);
158 $ADODB_FETCH_MODE = $save;
159 if (!is_object($rs)) {
166 $fld =
new ADOFieldObject();
167 $fld->name = $rs->fields[0];
168 $fld->type = $rs->fields[1];
170 $fld->not_null = (!$rs->fields[3]);
171 $fld->auto_increment = ($rs->fields[4] == 128);
174 if (isset($rs->fields[5]) && $rs->fields[5]) {
175 if ($rs->fields[5]>0) $fld->max_length = $rs->fields[5];
176 $fld->scale = $rs->fields[6];
177 if ($fld->scale>0) $fld->max_length += 1;
179 $fld->max_length = $rs->fields[2];
182 if ($save == ADODB_FETCH_NUM) {
185 $retarr[strtoupper($fld->name)] = $fld;
198 $table = $this->qstr($table);
200 $sql =
"SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno,
201 CASE WHEN I.indid BETWEEN 1 AND 254 AND (I.status & 2048 = 2048 OR I.Status = 16402 AND O.XType = 'V') THEN 1 ELSE 0 END AS IsPK,
202 CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique
203 FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id
204 INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid
205 INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid
206 WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND O.Name LIKE $table
207 ORDER BY O.name, I.Name, K.keyno";
209 global $ADODB_FETCH_MODE;
210 $save = $ADODB_FETCH_MODE;
211 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
212 if ($this->fetchMode !== FALSE) {
213 $savem = $this->SetFetchMode(FALSE);
216 $rs = $this->Execute($sql);
218 $this->SetFetchMode($savem);
220 $ADODB_FETCH_MODE = $save;
222 if (!is_object($rs)) {
227 while ($row = $rs->FetchRow()) {
228 if (!$primary && $row[5])
continue;
230 $indexes[$row[0]][
'unique'] = $row[6];
231 $indexes[$row[0]][
'columns'][] = $row[1];
238 if (is_string($sql)) $sql = str_replace(
'||',
'+',$sql);
244 $this->_transmode = $transaction_mode;
245 if (empty($transaction_mode)) {
246 $this->Execute(
'SET TRANSACTION ISOLATION LEVEL READ COMMITTED');
249 if (!stristr($transaction_mode,
'isolation')) $transaction_mode =
'ISOLATION LEVEL '.$transaction_mode;
250 $this->Execute(
"SET TRANSACTION ".$transaction_mode);
257 global $ADODB_FETCH_MODE;
260 $this->_findschema($table,$schema);
262 if ($schema) $schema =
"and k.table_catalog like '$schema%'";
264 $sql =
"select distinct k.column_name,ordinal_position from information_schema.key_column_usage k,
265 information_schema.table_constraints tc
266 where tc.constraint_name = k.constraint_name and tc.constraint_type =
267 'PRIMARY KEY' and k.table_name = '$table' $schema order by ordinal_position ";
269 $savem = $ADODB_FETCH_MODE;
270 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
271 $a = $this->GetCol($sql);
272 $ADODB_FETCH_MODE = $savem;
274 if (
$a &&
sizeof(
$a)>0)
return $a;
279 function SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=
false,$secs2cache=0)
281 if ($nrows > 0 && $offset <= 0) {
283 '/(^\s*select\s+(distinctrow|distinct)?)/i',
'\\1 '.$this->hasTop.
" $nrows ",$sql);
284 $rs = $this->Execute($sql,$inputarr);
286 $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
298 for ($i=0; $i < $len; $i++) {
304 $s .=
"datename(yyyy,$col)";
307 $s .=
"convert(char(3),$col,0)";
310 $s .=
"replace(str(month($col),2),' ','0')";
314 $s .=
"datename(quarter,$col)";
318 $s .=
"replace(str(day($col),2),' ','0')";
321 $s .=
"substring(convert(char(14),$col,0),13,2)";
325 $s .=
"replace(str(datepart(hh,$col),2),' ','0')";
329 $s .=
"replace(str(datepart(mi,$col),2),' ','0')";
332 $s .=
"replace(str(datepart(ss,$col),2),' ','0')";
336 $s .=
"substring(convert(char(19),$col,0),18,2)";
342 $ch = substr($fmt,$i,1);
344 $s .= $this->qstr($ch);