adodb-ado.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. Microsoft ADO data driver. Requires ADO. Works only on MS Windows.
  10. */
  11. // security - hide paths
  12. if (!defined('ADODB_DIR')) die();
  13. define("_ADODB_ADO_LAYER", 1 );
  14. /*--------------------------------------------------------------------------------------
  15. --------------------------------------------------------------------------------------*/
  16. class ADODB_ado extends ADOConnection {
  17. var $databaseType = "ado";
  18. var $_bindInputArray = false;
  19. var $fmtDate = "'Y-m-d'";
  20. var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  21. var $replaceQuote = "''"; // string to use to replace quotes
  22. var $dataProvider = "ado";
  23. var $hasAffectedRows = true;
  24. var $adoParameterType = 201; // 201 = long varchar, 203=long wide varchar, 205 = long varbinary
  25. var $_affectedRows = false;
  26. var $_thisTransactions;
  27. var $_cursor_type = 3; // 3=adOpenStatic,0=adOpenForwardOnly,1=adOpenKeyset,2=adOpenDynamic
  28. var $_cursor_location = 3; // 2=adUseServer, 3 = adUseClient;
  29. var $_lock_type = -1;
  30. var $_execute_option = -1;
  31. var $poorAffectedRows = true;
  32. var $charPage;
  33. function ADODB_ado()
  34. {
  35. $this->_affectedRows = new VARIANT;
  36. }
  37. function ServerInfo()
  38. {
  39. if (!empty($this->_connectionID)) $desc = $this->_connectionID->provider;
  40. return array('description' => $desc, 'version' => '');
  41. }
  42. function _affectedrows()
  43. {
  44. if (PHP_VERSION >= 5) return $this->_affectedRows;
  45. return $this->_affectedRows->value;
  46. }
  47. // you can also pass a connection string like this:
  48. //
  49. // $DB->Connect('USER ID=sa;PASSWORD=pwd;SERVER=mangrove;DATABASE=ai',false,false,'SQLOLEDB');
  50. function _connect($argHostname, $argUsername, $argPassword, $argProvider= 'MSDASQL')
  51. {
  52. $u = 'UID';
  53. $p = 'PWD';
  54. if (!empty($this->charPage))
  55. $dbc = new COM('ADODB.Connection',null,$this->charPage);
  56. else
  57. $dbc = new COM('ADODB.Connection');
  58. if (! $dbc) return false;
  59. /* special support if provider is mssql or access */
  60. if ($argProvider=='mssql') {
  61. $u = 'User Id'; //User parameter name for OLEDB
  62. $p = 'Password';
  63. $argProvider = "SQLOLEDB"; // SQL Server Provider
  64. // not yet
  65. //if ($argDatabasename) $argHostname .= ";Initial Catalog=$argDatabasename";
  66. //use trusted conection for SQL if username not specified
  67. if (!$argUsername) $argHostname .= ";Trusted_Connection=Yes";
  68. } else if ($argProvider=='access')
  69. $argProvider = "Microsoft.Jet.OLEDB.4.0"; // Microsoft Jet Provider
  70. if ($argProvider) $dbc->Provider = $argProvider;
  71. if ($argUsername) $argHostname .= ";$u=$argUsername";
  72. if ($argPassword)$argHostname .= ";$p=$argPassword";
  73. if ($this->debug) ADOConnection::outp( "Host=".$argHostname."<BR>\n version=$dbc->version");
  74. // @ added below for php 4.0.1 and earlier
  75. @$dbc->Open((string) $argHostname);
  76. $this->_connectionID = $dbc;
  77. $dbc->CursorLocation = $this->_cursor_location;
  78. return $dbc->State > 0;
  79. }
  80. // returns true or false
  81. function _pconnect($argHostname, $argUsername, $argPassword, $argProvider='MSDASQL')
  82. {
  83. return $this->_connect($argHostname,$argUsername,$argPassword,$argProvider);
  84. }
  85. /*
  86. adSchemaCatalogs = 1,
  87. adSchemaCharacterSets = 2,
  88. adSchemaCollations = 3,
  89. adSchemaColumns = 4,
  90. adSchemaCheckConstraints = 5,
  91. adSchemaConstraintColumnUsage = 6,
  92. adSchemaConstraintTableUsage = 7,
  93. adSchemaKeyColumnUsage = 8,
  94. adSchemaReferentialContraints = 9,
  95. adSchemaTableConstraints = 10,
  96. adSchemaColumnsDomainUsage = 11,
  97. adSchemaIndexes = 12,
  98. adSchemaColumnPrivileges = 13,
  99. adSchemaTablePrivileges = 14,
  100. adSchemaUsagePrivileges = 15,
  101. adSchemaProcedures = 16,
  102. adSchemaSchemata = 17,
  103. adSchemaSQLLanguages = 18,
  104. adSchemaStatistics = 19,
  105. adSchemaTables = 20,
  106. adSchemaTranslations = 21,
  107. adSchemaProviderTypes = 22,
  108. adSchemaViews = 23,
  109. adSchemaViewColumnUsage = 24,
  110. adSchemaViewTableUsage = 25,
  111. adSchemaProcedureParameters = 26,
  112. adSchemaForeignKeys = 27,
  113. adSchemaPrimaryKeys = 28,
  114. adSchemaProcedureColumns = 29,
  115. adSchemaDBInfoKeywords = 30,
  116. adSchemaDBInfoLiterals = 31,
  117. adSchemaCubes = 32,
  118. adSchemaDimensions = 33,
  119. adSchemaHierarchies = 34,
  120. adSchemaLevels = 35,
  121. adSchemaMeasures = 36,
  122. adSchemaProperties = 37,
  123. adSchemaMembers = 38
  124. */
  125. function &MetaTables()
  126. {
  127. $arr= array();
  128. $dbc = $this->_connectionID;
  129. $adors=@$dbc->OpenSchema(20);//tables
  130. if ($adors){
  131. $f = $adors->Fields(2);//table/view name
  132. $t = $adors->Fields(3);//table type
  133. while (!$adors->EOF){
  134. $tt=substr($t->value,0,6);
  135. if ($tt!='SYSTEM' && $tt !='ACCESS')
  136. $arr[]=$f->value;
  137. //print $f->value . ' ' . $t->value.'<br>';
  138. $adors->MoveNext();
  139. }
  140. $adors->Close();
  141. }
  142. return $arr;
  143. }
  144. function &MetaColumns($table)
  145. {
  146. $table = strtoupper($table);
  147. $arr = array();
  148. $dbc = $this->_connectionID;
  149. $adors=@$dbc->OpenSchema(4);//tables
  150. if ($adors){
  151. $t = $adors->Fields(2);//table/view name
  152. while (!$adors->EOF){
  153. if (strtoupper($t->Value) == $table) {
  154. $fld = new ADOFieldObject();
  155. $c = $adors->Fields(3);
  156. $fld->name = $c->Value;
  157. $fld->type = 'CHAR'; // cannot discover type in ADO!
  158. $fld->max_length = -1;
  159. $arr[strtoupper($fld->name)]=$fld;
  160. }
  161. $adors->MoveNext();
  162. }
  163. $adors->Close();
  164. }
  165. $false = false;
  166. return empty($arr) ? $false : $arr;
  167. }
  168. /* returns queryID or false */
  169. function &_query($sql,$inputarr=false)
  170. {
  171. $dbc = $this->_connectionID;
  172. $false = false;
  173. // return rs
  174. if ($inputarr) {
  175. if (!empty($this->charPage))
  176. $oCmd = new COM('ADODB.Command',null,$this->charPage);
  177. else
  178. $oCmd = new COM('ADODB.Command');
  179. $oCmd->ActiveConnection = $dbc;
  180. $oCmd->CommandText = $sql;
  181. $oCmd->CommandType = 1;
  182. foreach($inputarr as $val) {
  183. // name, type, direction 1 = input, len,
  184. $this->adoParameterType = 130;
  185. $p = $oCmd->CreateParameter('name',$this->adoParameterType,1,strlen($val),$val);
  186. //print $p->Type.' '.$p->value;
  187. $oCmd->Parameters->Append($p);
  188. }
  189. $p = false;
  190. $rs = $oCmd->Execute();
  191. $e = $dbc->Errors;
  192. if ($dbc->Errors->Count > 0) return $false;
  193. return $rs;
  194. }
  195. $rs = @$dbc->Execute($sql,$this->_affectedRows, $this->_execute_option);
  196. if ($dbc->Errors->Count > 0) return $false;
  197. if (! $rs) return $false;
  198. if ($rs->State == 0) {
  199. $true = true;
  200. return $true; // 0 = adStateClosed means no records returned
  201. }
  202. return $rs;
  203. }
  204. function BeginTrans()
  205. {
  206. if ($this->transOff) return true;
  207. if (isset($this->_thisTransactions))
  208. if (!$this->_thisTransactions) return false;
  209. else {
  210. $o = $this->_connectionID->Properties("Transaction DDL");
  211. $this->_thisTransactions = $o ? true : false;
  212. if (!$o) return false;
  213. }
  214. @$this->_connectionID->BeginTrans();
  215. $this->transCnt += 1;
  216. return true;
  217. }
  218. function CommitTrans($ok=true)
  219. {
  220. if (!$ok) return $this->RollbackTrans();
  221. if ($this->transOff) return true;
  222. @$this->_connectionID->CommitTrans();
  223. if ($this->transCnt) @$this->transCnt -= 1;
  224. return true;
  225. }
  226. function RollbackTrans() {
  227. if ($this->transOff) return true;
  228. @$this->_connectionID->RollbackTrans();
  229. if ($this->transCnt) @$this->transCnt -= 1;
  230. return true;
  231. }
  232. /* Returns: the last error message from previous database operation */
  233. function ErrorMsg()
  234. {
  235. $errc = $this->_connectionID->Errors;
  236. if ($errc->Count == 0) return '';
  237. $err = $errc->Item($errc->Count-1);
  238. return $err->Description;
  239. }
  240. function ErrorNo()
  241. {
  242. $errc = $this->_connectionID->Errors;
  243. if ($errc->Count == 0) return 0;
  244. $err = $errc->Item($errc->Count-1);
  245. return $err->NativeError;
  246. }
  247. // returns true or false
  248. function _close()
  249. {
  250. if ($this->_connectionID) $this->_connectionID->Close();
  251. $this->_connectionID = false;
  252. return true;
  253. }
  254. }
  255. /*--------------------------------------------------------------------------------------
  256. Class Name: Recordset
  257. --------------------------------------------------------------------------------------*/
  258. class ADORecordSet_ado extends ADORecordSet {
  259. var $bind = false;
  260. var $databaseType = "ado";
  261. var $dataProvider = "ado";
  262. var $_tarr = false; // caches the types
  263. var $_flds; // and field objects
  264. var $canSeek = true;
  265. var $hideErrors = true;
  266. function ADORecordSet_ado($id,$mode=false)
  267. {
  268. if ($mode === false) {
  269. global $ADODB_FETCH_MODE;
  270. $mode = $ADODB_FETCH_MODE;
  271. }
  272. $this->fetchMode = $mode;
  273. return $this->ADORecordSet($id,$mode);
  274. }
  275. // returns the field object
  276. function FetchField($fieldOffset = -1) {
  277. $off=$fieldOffset+1; // offsets begin at 1
  278. $o= new ADOFieldObject();
  279. $rs = $this->_queryID;
  280. $f = $rs->Fields($fieldOffset);
  281. $o->name = $f->Name;
  282. $t = $f->Type;
  283. $o->type = $this->MetaType($t);
  284. $o->max_length = $f->DefinedSize;
  285. $o->ado_type = $t;
  286. //print "off=$off name=$o->name type=$o->type len=$o->max_length<br>";
  287. return $o;
  288. }
  289. /* Use associative array to get fields array */
  290. function Fields($colname)
  291. {
  292. if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
  293. if (!$this->bind) {
  294. $this->bind = array();
  295. for ($i=0; $i < $this->_numOfFields; $i++) {
  296. $o = $this->FetchField($i);
  297. $this->bind[strtoupper($o->name)] = $i;
  298. }
  299. }
  300. return $this->fields[$this->bind[strtoupper($colname)]];
  301. }
  302. function _initrs()
  303. {
  304. $rs = $this->_queryID;
  305. $this->_numOfRows = $rs->RecordCount;
  306. $f = $rs->Fields;
  307. $this->_numOfFields = $f->Count;
  308. }
  309. // should only be used to move forward as we normally use forward-only cursors
  310. function _seek($row)
  311. {
  312. $rs = $this->_queryID;
  313. // absoluteposition doesn't work -- my maths is wrong ?
  314. // $rs->AbsolutePosition->$row-2;
  315. // return true;
  316. if ($this->_currentRow > $row) return false;
  317. @$rs->Move((integer)$row - $this->_currentRow-1); //adBookmarkFirst
  318. return true;
  319. }
  320. /*
  321. OLEDB types
  322. enum DBTYPEENUM
  323. { DBTYPE_EMPTY = 0,
  324. DBTYPE_NULL = 1,
  325. DBTYPE_I2 = 2,
  326. DBTYPE_I4 = 3,
  327. DBTYPE_R4 = 4,
  328. DBTYPE_R8 = 5,
  329. DBTYPE_CY = 6,
  330. DBTYPE_DATE = 7,
  331. DBTYPE_BSTR = 8,
  332. DBTYPE_IDISPATCH = 9,
  333. DBTYPE_ERROR = 10,
  334. DBTYPE_BOOL = 11,
  335. DBTYPE_VARIANT = 12,
  336. DBTYPE_IUNKNOWN = 13,
  337. DBTYPE_DECIMAL = 14,
  338. DBTYPE_UI1 = 17,
  339. DBTYPE_ARRAY = 0x2000,
  340. DBTYPE_BYREF = 0x4000,
  341. DBTYPE_I1 = 16,
  342. DBTYPE_UI2 = 18,
  343. DBTYPE_UI4 = 19,
  344. DBTYPE_I8 = 20,
  345. DBTYPE_UI8 = 21,
  346. DBTYPE_GUID = 72,
  347. DBTYPE_VECTOR = 0x1000,
  348. DBTYPE_RESERVED = 0x8000,
  349. DBTYPE_BYTES = 128,
  350. DBTYPE_STR = 129,
  351. DBTYPE_WSTR = 130,
  352. DBTYPE_NUMERIC = 131,
  353. DBTYPE_UDT = 132,
  354. DBTYPE_DBDATE = 133,
  355. DBTYPE_DBTIME = 134,
  356. DBTYPE_DBTIMESTAMP = 135
  357. ADO Types
  358. adEmpty = 0,
  359. adTinyInt = 16,
  360. adSmallInt = 2,
  361. adInteger = 3,
  362. adBigInt = 20,
  363. adUnsignedTinyInt = 17,
  364. adUnsignedSmallInt = 18,
  365. adUnsignedInt = 19,
  366. adUnsignedBigInt = 21,
  367. adSingle = 4,
  368. adDouble = 5,
  369. adCurrency = 6,
  370. adDecimal = 14,
  371. adNumeric = 131,
  372. adBoolean = 11,
  373. adError = 10,
  374. adUserDefined = 132,
  375. adVariant = 12,
  376. adIDispatch = 9,
  377. adIUnknown = 13,
  378. adGUID = 72,
  379. adDate = 7,
  380. adDBDate = 133,
  381. adDBTime = 134,
  382. adDBTimeStamp = 135,
  383. adBSTR = 8,
  384. adChar = 129,
  385. adVarChar = 200,
  386. adLongVarChar = 201,
  387. adWChar = 130,
  388. adVarWChar = 202,
  389. adLongVarWChar = 203,
  390. adBinary = 128,
  391. adVarBinary = 204,
  392. adLongVarBinary = 205,
  393. adChapter = 136,
  394. adFileTime = 64,
  395. adDBFileTime = 137,
  396. adPropVariant = 138,
  397. adVarNumeric = 139
  398. */
  399. function MetaType($t,$len=-1,$fieldobj=false)
  400. {
  401. if (is_object($t)) {
  402. $fieldobj = $t;
  403. $t = $fieldobj->type;
  404. $len = $fieldobj->max_length;
  405. }
  406. if (!is_numeric($t)) return $t;
  407. switch ($t) {
  408. case 0:
  409. case 12: // variant
  410. case 8: // bstr
  411. case 129: //char
  412. case 130: //wc
  413. case 200: // varc
  414. case 202:// varWC
  415. case 128: // bin
  416. case 204: // varBin
  417. case 72: // guid
  418. if ($len <= $this->blobSize) return 'C';
  419. case 201:
  420. case 203:
  421. return 'X';
  422. case 128:
  423. case 204:
  424. case 205:
  425. return 'B';
  426. case 7:
  427. case 133: return 'D';
  428. case 134:
  429. case 135: return 'T';
  430. case 11: return 'L';
  431. case 16:// adTinyInt = 16,
  432. case 2://adSmallInt = 2,
  433. case 3://adInteger = 3,
  434. case 4://adBigInt = 20,
  435. case 17://adUnsignedTinyInt = 17,
  436. case 18://adUnsignedSmallInt = 18,
  437. case 19://adUnsignedInt = 19,
  438. case 20://adUnsignedBigInt = 21,
  439. return 'I';
  440. default: return 'N';
  441. }
  442. }
  443. // time stamp not supported yet
  444. function _fetch()
  445. {
  446. $rs = $this->_queryID;
  447. if (!$rs or $rs->EOF) {
  448. $this->fields = false;
  449. return false;
  450. }
  451. $this->fields = array();
  452. if (!$this->_tarr) {
  453. $tarr = array();
  454. $flds = array();
  455. for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
  456. $f = $rs->Fields($i);
  457. $flds[] = $f;
  458. $tarr[] = $f->Type;
  459. }
  460. // bind types and flds only once
  461. $this->_tarr = $tarr;
  462. $this->_flds = $flds;
  463. }
  464. $t = reset($this->_tarr);
  465. $f = reset($this->_flds);
  466. if ($this->hideErrors) $olde = error_reporting(E_ERROR|E_CORE_ERROR);// sometimes $f->value be null
  467. for ($i=0,$max = $this->_numOfFields; $i < $max; $i++) {
  468. //echo "<p>",$t,' ';var_dump($f->value); echo '</p>';
  469. switch($t) {
  470. case 135: // timestamp
  471. if (!strlen((string)$f->value)) $this->fields[] = false;
  472. else {
  473. if (!is_numeric($f->value)) $val = variant_date_to_timestamp($f->value);
  474. else $val = $f->value;
  475. $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
  476. }
  477. break;
  478. case 133:// A date value (yyyymmdd)
  479. if ($val = $f->value) {
  480. $this->fields[] = substr($val,0,4).'-'.substr($val,4,2).'-'.substr($val,6,2);
  481. } else
  482. $this->fields[] = false;
  483. break;
  484. case 7: // adDate
  485. if (!strlen((string)$f->value)) $this->fields[] = false;
  486. else {
  487. if (!is_numeric($f->value)) $val = variant_date_to_timestamp($f->value);
  488. else $val = $f->value;
  489. if (($val % 86400) == 0) $this->fields[] = adodb_date('Y-m-d',$val);
  490. else $this->fields[] = adodb_date('Y-m-d H:i:s',$val);
  491. }
  492. break;
  493. case 1: // null
  494. $this->fields[] = false;
  495. break;
  496. case 6: // currency is not supported properly;
  497. ADOConnection::outp( '<b>'.$f->Name.': currency type not supported by PHP</b>');
  498. $this->fields[] = (float) $f->value;
  499. break;
  500. default:
  501. $this->fields[] = $f->value;
  502. break;
  503. }
  504. //print " $f->value $t, ";
  505. $f = next($this->_flds);
  506. $t = next($this->_tarr);
  507. } // for
  508. if ($this->hideErrors) error_reporting($olde);
  509. @$rs->MoveNext(); // @ needed for some versions of PHP!
  510. if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  511. $this->fields = &$this->GetRowAssoc(ADODB_ASSOC_CASE);
  512. }
  513. return true;
  514. }
  515. function NextRecordSet()
  516. {
  517. $rs = $this->_queryID;
  518. $this->_queryID = $rs->NextRecordSet();
  519. //$this->_queryID = $this->_QueryId->NextRecordSet();
  520. if ($this->_queryID == null) return false;
  521. $this->_currentRow = -1;
  522. $this->_currentPage = -1;
  523. $this->bind = false;
  524. $this->fields = false;
  525. $this->_flds = false;
  526. $this->_tarr = false;
  527. $this->_inited = false;
  528. $this->Init();
  529. return true;
  530. }
  531. function _close() {
  532. $this->_flds = false;
  533. @$this->_queryID->Close();// by Pete Dishman (peterd@telephonetics.co.uk)
  534. $this->_queryID = false;
  535. }
  536. }
  537. ?>