adodb-odbc_mssql.inc.php 7.3 KB

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