RE: [PEAR-DEV] [new] File_CSV class avaible
| From: | Alan Richmond | Date: | Sun, 21 Jul 2002 03:24:50 +0000 |
| Subject: | RE: [PEAR-DEV] [new] File_CSV class avaible | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-7809@lists.php.net to get a copy of this message | ||
> I just want to announce that the new class File_CSV is
avaible. As the
> PS.- Someone liking to develop a CSV driver for PEAR DB?
;-)>
I think I have the beginnings of one already. I just
subscribed to this list today and found this message in the
archive a couple of minutes ago. I was planning to lurk for
a while before submitting a new driver for PEAR DB, but I
guess I'll jump right in.
I wrote a driver called querysim and it does just that, it
simulates the resultset of a select query. It's based on
the ColdFusion custom tag of the same name at
www.halhelms.com. (I'm a longtime CF programmer teaching
myself PHP these last few weeks. This is only my second
piece of PHP code, so no laughing. :) ) It can read inline
QuerySim text as well as external files, and because it has
configureable delimiters, by default, it can also read
simple CSVs. I haven't looked at File_CSV closly yet but it
could replace my file reader and parser. My parser is just
getting bigger and bigger to handle 'weird' cases, and one
of the things on my to do list is to add quoted field
support, which you have. Anyway, the more I thought about
it, the more I thought the parser should live in it's own
driver. And here there has been a class all along that just
needs a wrapper.
I've also had grand visions of adding an SQL parser and
being able to read and write to CSV style files.
I am interested in working on this.
-Alan
More info on QuerySim, if you're wondering, "Why would you
want to do that?":
www.halhelms.com/index.cfm?fuseaction=newsletters.halsteve&i
ssue=16
This is how it works (from the querysim Fusedoc):
<note author="Alan Richmond">
QuerySims are especially useful when following
the FLiP methodology developed for Fusebox framework
application development but you don't have to be using
Fusebox or FLiP to use QuerySims!
This file should be placed in the /pear/DB
directory.
QuerySim Text Syntax:
The first line is a comma delimited list of
query columns.
Second (and any subsequent lines) are
pipe-delimited data used to populate the query.
An empty data column is denoted by the
string 'null' or an empty column. The value stored in the
recordset is actually a zero length string, '', rather than
a true NULL. (Should this be a NULL?)
A carriage return/line feed denotes an
end-of-line.
All delimiters are configurable through
setOption() or as connect() options.
For instance:
userID,firstName,lastName,userGroups
100|Stan|Cox|33
would return a recordset with 1 row containing 4
columns of information about Stan Cox.
To access an external file use the following
syntax:
querysim:///filename
^^^note: *3* forward slashes before
file path!
These will also work:
querysim:///../../../webserver.log
querysim:///c:/netlogs/webserver.log
^ ^note: windows file
delimiters can be forward or back slashes.
When calling an external file the text in
query() is ignored. However, if the parameter isn't passed
at all a warning is raised by PEAR DB. Use an empty string
or a dummy string to prevent a warning from being thrown:
$conn->query('');
$conn->query('read from file');
============================================================
===========
Inline usage example:
require_once 'DB.php';
$dsn = 'querysim';
$conn = DB::connect($dsn);
if (DB::isError($conn)){
die ('Cannot connect: ' . $conn->getMessage() . '\n');
}
$user = $conn->query('
userID,firstName,lastName65,userGroups
100|Stan|Cox|33
102|Hal|Helms|22
103|Bert|Dawson|11
');
printf("Result contains %d rows and %d columns\n<br /><br
/>\n", $user->numRows(), $user->numCols());
//Note that you may return ordered or associative results as
well as single rows (key based now (0=>Stan), should it be
row number based (1=>Stan)?)
while ($row = $user->fetchRow(DB_FETCHMODE_ASSOC)) {
printf("%d, %s %s, %s\n<br />\n", $row['userID'],
$row['firstName'], $row['lastName'], $row['userGroups']);
}//end while
$user->free();
============================================================
===========
External file usage example (only showing lines that are
different than inline example):
$dsn = 'querysim:///c:/weblogs/webserver.log';
$conn = DB::connect($dsn, array('persistent'=>true,
'dataDelim'=>','));
$user = $conn->query('read from file');
============================================================
===========
Resources:
PEAR DB pear.php.net
Fusebox/FLiP/ www.fusebox.org
Fusedoc www.halhelms.com
www.grokfusebox.com
www.secretagents.com
PHP Fusebox bombusbee.com
QuerySim
www.halhelms.com/index.cfm?fuseaction=newsletters.halsteve&i
ssue=16
</note>