adodb-csv.inc.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /*
  3. @version v5.20.17 31-Mar-2020
  4. @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
  5. @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
  6. Released under both BSD license and Lesser GPL library license.
  7. Whenever there is any discrepancy between the two licenses,
  8. the BSD license will take precedence.
  9. Set tabs to 4.
  10. Currently unsupported: MetaDatabases, MetaTables and MetaColumns, and also inputarr in Execute.
  11. Native types have been converted to MetaTypes.
  12. Transactions not supported yet.
  13. Limitation of url length. For IIS, see MaxClientRequestBuffer registry value.
  14. http://support.microsoft.com/default.aspx?scid=kb;en-us;260694
  15. */
  16. // security - hide paths
  17. if (!defined('ADODB_DIR')) die();
  18. if (! defined("_ADODB_CSV_LAYER")) {
  19. define("_ADODB_CSV_LAYER", 1 );
  20. include_once(ADODB_DIR.'/adodb-csvlib.inc.php');
  21. class ADODB_csv extends ADOConnection {
  22. var $databaseType = 'csv';
  23. var $databaseProvider = 'csv';
  24. var $hasInsertID = true;
  25. var $hasAffectedRows = true;
  26. var $fmtTimeStamp = "'Y-m-d H:i:s'";
  27. var $_affectedrows=0;
  28. var $_insertid=0;
  29. var $_url;
  30. var $replaceQuote = "''"; // string to use to replace quotes
  31. var $hasTransactions = false;
  32. var $_errorNo = false;
  33. function __construct()
  34. {
  35. }
  36. function _insertid()
  37. {
  38. return $this->_insertid;
  39. }
  40. function _affectedrows()
  41. {
  42. return $this->_affectedrows;
  43. }
  44. function MetaDatabases()
  45. {
  46. return false;
  47. }
  48. // returns true or false
  49. function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
  50. {
  51. if (strtolower(substr($argHostname,0,7)) !== 'http://') return false;
  52. $this->_url = $argHostname;
  53. return true;
  54. }
  55. // returns true or false
  56. function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  57. {
  58. if (strtolower(substr($argHostname,0,7)) !== 'http://') return false;
  59. $this->_url = $argHostname;
  60. return true;
  61. }
  62. function MetaColumns($table, $normalize=true)
  63. {
  64. return false;
  65. }
  66. // parameters use PostgreSQL convention, not MySQL
  67. function SelectLimit($sql, $nrows = -1, $offset = -1, $inputarr = false, $secs2cache = 0)
  68. {
  69. global $ADODB_FETCH_MODE;
  70. $nrows = (int) $nrows;
  71. $offset = (int) $offset;
  72. $url = $this->_url.'?sql='.urlencode($sql)."&nrows=$nrows&fetch=".
  73. (($this->fetchMode !== false)?$this->fetchMode : $ADODB_FETCH_MODE).
  74. "&offset=$offset";
  75. $err = false;
  76. $rs = csv2rs($url,$err,false);
  77. if ($this->debug) print "$url<br><i>$err</i><br>";
  78. $at = strpos($err,'::::');
  79. if ($at === false) {
  80. $this->_errorMsg = $err;
  81. $this->_errorNo = (integer)$err;
  82. } else {
  83. $this->_errorMsg = substr($err,$at+4,1024);
  84. $this->_errorNo = -9999;
  85. }
  86. if ($this->_errorNo)
  87. if ($fn = $this->raiseErrorFn) {
  88. $fn($this->databaseType,'EXECUTE',$this->ErrorNo(),$this->ErrorMsg(),$sql,'');
  89. }
  90. if (is_object($rs)) {
  91. $rs->databaseType='csv';
  92. $rs->fetchMode = ($this->fetchMode !== false) ? $this->fetchMode : $ADODB_FETCH_MODE;
  93. $rs->connection = $this;
  94. }
  95. return $rs;
  96. }
  97. // returns queryID or false
  98. function _Execute($sql,$inputarr=false)
  99. {
  100. global $ADODB_FETCH_MODE;
  101. if (!$this->_bindInputArray && $inputarr) {
  102. $sqlarr = explode('?',$sql);
  103. $sql = '';
  104. $i = 0;
  105. foreach($inputarr as $v) {
  106. $sql .= $sqlarr[$i];
  107. if (gettype($v) == 'string')
  108. $sql .= $this->qstr($v);
  109. else if ($v === null)
  110. $sql .= 'NULL';
  111. else
  112. $sql .= $v;
  113. $i += 1;
  114. }
  115. $sql .= $sqlarr[$i];
  116. if ($i+1 != sizeof($sqlarr))
  117. print "Input Array does not match ?: ".htmlspecialchars($sql);
  118. $inputarr = false;
  119. }
  120. $url = $this->_url.'?sql='.urlencode($sql)."&fetch=".
  121. (($this->fetchMode !== false)?$this->fetchMode : $ADODB_FETCH_MODE);
  122. $err = false;
  123. $rs = csv2rs($url,$err,false);
  124. if ($this->debug) print urldecode($url)."<br><i>$err</i><br>";
  125. $at = strpos($err,'::::');
  126. if ($at === false) {
  127. $this->_errorMsg = $err;
  128. $this->_errorNo = (integer)$err;
  129. } else {
  130. $this->_errorMsg = substr($err,$at+4,1024);
  131. $this->_errorNo = -9999;
  132. }
  133. if ($this->_errorNo)
  134. if ($fn = $this->raiseErrorFn) {
  135. $fn($this->databaseType,'EXECUTE',$this->ErrorNo(),$this->ErrorMsg(),$sql,$inputarr);
  136. }
  137. if (is_object($rs)) {
  138. $rs->fetchMode = ($this->fetchMode !== false) ? $this->fetchMode : $ADODB_FETCH_MODE;
  139. $this->_affectedrows = $rs->affectedrows;
  140. $this->_insertid = $rs->insertid;
  141. $rs->databaseType='csv';
  142. $rs->connection = $this;
  143. }
  144. return $rs;
  145. }
  146. /* Returns: the last error message from previous database operation */
  147. function ErrorMsg()
  148. {
  149. return $this->_errorMsg;
  150. }
  151. /* Returns: the last error number from previous database operation */
  152. function ErrorNo()
  153. {
  154. return $this->_errorNo;
  155. }
  156. // returns true or false
  157. function _close()
  158. {
  159. return true;
  160. }
  161. } // class
  162. class ADORecordset_csv extends ADORecordset {
  163. function __construct($id,$mode=false)
  164. {
  165. parent::__construct($id,$mode);
  166. }
  167. function _close()
  168. {
  169. return true;
  170. }
  171. }
  172. } // define