Source for file adodb-csvlib.inc.php
Documentation is available at adodb-csvlib.inc.php
global $ADODB_INCLUDED_CSV;
V4.97 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence. See License.txt.
Set tabs to 4 for best viewing.
Latest version is available at http://adodb.sourceforge.net
Library for CSV serialization. This is used by the csv/proxy driver and is the
CacheExecute() serialization format.
Format documented at http://php.weblogs.com/ADODB_CSV
* convert a recordset into special format
* @param rs the recordset
* @return the CSV formated data
$max = ($rs) ? $rs->FieldCount() : 0;
if ($max <= 0 || $rs->dataProvider == 'empty') { // is insert/update/delete
$sql .= ','. $conn->Affected_Rows();
$sql .= ','. $conn->Insert_ID();
$text = "====-1,0,$sql\n";
$tt = ($rs->timeCreated) ? $rs->timeCreated : time();
## changed format from ====0 to ====1
$line = "====1,$tt,$sql\n";
if ($rs->databaseType == 'array') {
for($i= 0; $i < $max; $i++ ) {
$o = & $rs->FetchField($i);
$savefetch = isset ($rs->adodbFetchMode) ? $rs->adodbFetchMode : $rs->fetchMode;
$class = $rs->connection->arrayClass;
$rs2->oldProvider = $rs->dataProvider;
$rs2->InitArrayFields($rows,$flds);
$rs2->fetchMode = $savefetch;
* Open CSV file and convert it into Data.
* @param url file/ftp/http url
* @param err returns the error message
* @param timeout dispose if recordset has been alive for $timeout secs
* @return recordset, or false if error occured. If no
* error occurred in sql INSERT/UPDATE/DELETE,
* empty recordset is returned
function &csv2rs($url,&$err,$timeout= 0, $rsclass= 'ADORecordSet_array')
$err = $url. ' file/URL not found';
if ($meta = fgetcsv($fp, 32000, ",")) {
// check if error message
if (strncmp($meta[0],'****',4) === 0) {
// $meta[0] is -1 means return an empty recordset
// $meta[1] contains a time
if (strncmp($meta[0], '====',4) === 0) {
if ($meta[0] == "====-1") {
$err = "Corrupt first line for format -1";
$err = " Illegal Timeout $timeout ";
$rs = new $rsclass($val= true);
$rs->timeCreated = $meta[1];
$rs->affectedrows = (integer) $meta[3];
$rs->insertid = $meta[4];
# Under high volume loads, we want only 1 thread/process to _write_file
# so that we don't have 50 processes queueing to write the same data.
# We use probabilistic timeout, ahead of time.
# -4 sec before timeout, give processes 1/32 chance of timing out
# -2 sec before timeout, give processes 1/16 chance of timing out
# -1 sec after timeout give processes 1/4 chance of timing out
# +0 sec after timeout, give processes 100% chance of timing out
$tdiff = (integer) ( $meta[1]+ $timeout - time());
if ((rand() & 31) == 0) {
if ((rand() & 15) == 0) {
} // if check flush cache
//================================================
// new cache format - use serialize extensively...
if ($meta[0] === '====1') {
$text = fread($fp,$MAXSIZE);
while ($txt = fread($fp,$MAXSIZE)) {
$err = "Unable to unserialize recordset";
//echo htmlspecialchars($text),' !--END--!<p>';
$err = "Unexpected EOF 1";
// Get Column definitions
$fld->max_length = $o2[2];
$err = "Recordset had unexpected EOF 2";
while ($txt = fread($fp,$MAXSIZE)) {
$err = "Recordset had unexpected EOF (in serialized recordset)";
$rs->InitArrayFields($arr,$flds);
* Save a file $filename and its $contents (normally for caching) with file locking
* Returns true if ok, false if fopen/fwrite error, 0 if rename error (eg. file is locked)
# http://www.php.net/bugs.php?id=9203 Bug that flock fails on Windows
# So to simulate locking, we assume that rename is an atomic operation.
# First we delete $filename, then we create a $tempfile write to it and
# rename to the desired $filename. If the rename works, then we successfully
# modified the file exclusively.
# What a stupid need - having to simulate locking.
# 1. $tempfile name is not unique -- very very low
# 2. unlink($filename) fails -- ok, rename will fail
# 3. adodb reads stale file because unlink fails -- ok, $rs timeout occurs
# 4. another process creates $filename between unlink() and rename() -- ok, rename() fails and cache updated
if (strncmp(PHP_OS,'WIN',3) === 0) {
// skip the decimal place
// getmypid() actually returns 0 on Win98 - never mind!
if (!($fd = @fopen($tmpname,'w'))) return false;
if (fwrite($fd,$contents)) $ok = true;
if (!@rename($tmpname,$filename)) {
if (!($fd = @fopen($filename, 'a'))) return false;
if (fwrite( $fd, $contents )) $ok = true;
|