PageRenderTime 1139ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/interface/main/calendar/pnadodb/drivers/adodb-ado.inc.php

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