Class CsvWORM
#include <CsvWorm.h >
const string& filename |
a path in the file system where the CSV file can be found. The file does not request the .csv extension as long as it is in CSV format. |
Instantiate this class - start by reading first line of CSV db to get the list of fields. Note that header case does not matter for searches, but results are always returned using the header case from the CSV file, regardless of field case in the query.
Return value: (an instance of this class)
Return the filename for this CSV file
Return value: the filename
Should be called after instantiating to ensure this is a valid CSV db
Return value: true if CSV file found and with the correct format; false if CSV file not found or incorrect format
HashArray& fields |
A simple array of field names. Although no field name checking is performed, I strongly urge you to use the naming convention of a database you someday hope to migrate to. |
Create a CSV database with the fields provided.
Note: if DB/file already exists, this fails
An id field must be present. If absent, it will be added as the first field, "Id".
Return value: false - the current CSV database already has headers or the CSV file is not writable; true - headers accepted and written to the CSV file
string field |
the name of the field to find (case ignored) |
Returns false if field not found. Return field id (column pos) of named field.
Return value: 1 for field not found; 0+ for field found, position returned
Return ordered list of fields for this DB with proper case.
Return value: A simple array of field names.
HashArray criteria |
a hashed array of column indices (not field names) - starting at 0 - with values; the values are the values to match. All criteria must match. |
HashArray& result |
|
HashArrayArray& rows |
|
bool multiples |
false if you seek the first match, true if you seek all matches Default value: false |
bool exact |
true if exact case match, false if case insensitive Default value: true |
Find row(s) that match the criteria provided. This is not as user friendly as either findRecord() or findRecords() below. Nor does it error check. This is meant to be fast.
Return value: If fslse, A hashed array in result with all key-value pairs where the keys are field names from the matching row. If true, an array of hashed arrays. Each hashed array has all key-value pairs wherethe keys are field names from each matching row.
string field1 |
the name (case ignored) of the first field criteria |
const string& value1 |
the value of the first field criteria |
string field2 |
(optional) the name (case ignored) of the second field criteria Default value: "" |
const string& value2 |
(optional) the value of the first second criteria Default value: "" |
string field3 |
(optional) the name (case ignored) of the third field criteria Default value: "" |
const string& value3 |
(optional) the value of the first third criteria Default value: "" |
Locate a single record (stop search after finding match) given one, two, or three fields of criteria. The matching record must match all criteria provided, but does not require matching case.
Return value: A hashed array with all key-value pairs where the keys are field names from the matching row.
string field1 |
the name (case ignored) of the first field criteria |
const string& value1 |
the value of the first field criteria |
string field2 |
(optional) the name (case ignored) of the second field criteria Default value: "" |
const string& value2 |
(optional) the value of the first second criteria Default value: "" |
string field3 |
(optional) the name (case ignored) of the third field criteria Default value: "" |
const string& value3 |
(optional) the value of the first third criteria Default value: "" |
Locate a single record (stop search after finding match) given one, two, or three fields of criteria. The matching record must match all criteria provided, but and requires matching case.
Return value: A hashed array with all key-value pairs where the keys are field names from the matching row.
string field1 |
the name (case ignored) of the first field criteria |
const string& value1 |
the value of the first field criteria |
string field2 |
(optional) the name (case ignored) of the second field criteria Default value: "" |
const string& value2 |
(optional) the value of the first second criteria Default value: "" |
string field3 |
(optional) the name (case ignored) of the third field criteria Default value: "" |
const string& value3 |
(optional) the value of the first third criteria Default value: "" |
Locate a possibly multiple records given one, two, or three fields of criteria. The matching records must match all criteria provided, but does not require matching case.
Return value: An array of hashed arrays. Each hashed array has all key-value pairs where the keys are field names from each matching row.
string field1 |
the name (case ignored) of the first field criteria |
const string& value1 |
the value of the first field criteria |
string field2 |
(optional) the name (case ignored) of the second field criteria Default value: "" |
const string& value2 |
(optional) the value of the first second criteria Default value: "" |
string field3 |
(optional) the name (case ignored) of the third field criteria Default value: "" |
const string& value3 |
(optional) the value of the first third criteria Default value: "" |
Locate a possibly multiple records given one, two, or three fields of criteria. The matching records must match all criteria provided and requires matching case.
Return value: An array of hashed arrays. Each hashed array has all key-value pairs where the keys are field names from each matching row.
int id |
the record id to find |
Locate a CSV record by the field "Id' equal to provided $id. If there is no Id field in the CSV data, then the first line of data is assumed to be Id=1, the second line of data is Id=2, etc.
Return value: A hashed array with all key-value pairs where the keys are field names from the matching row. Empty array if not match found.
Get the number of headers in use
Return value: header count
int id |
the row id to fetch |
Given a row id, return the field values.
Return value: a HashArray of field/value pairs
int id |
the row id to fetch |
string field |
the field name |
Given a row id and field name, return its value as a string
Return value: the field value
int id |
the row id to fetch |
string field |
the field name |
Given a row id and field name, return its value as a int
Return value: the field value
int id |
the row id to fetch |
string field |
the field name |
Given a row id and field name, return its value as boolean
Return value: the field value
HashArray& values |
a hashed array where the keys are field names |
Append another record to the database. Missing field values are assumed blank.
Return value: 0 on failure, the record number on success(1+)
const HashArray& row |
a db row as returned by findRecord(). |
FILE* outf |
Default value: stdout |
Debugging utility - output a single row from the database
Return value: An HTML table for a single row of data.
const HashArrayArray& rows |
An array of rows, as returned by findRecords(). |
FILE* outf |
output stream where data will printed to Default value: stdout |
Debugging utility - output multiple rows from the database
Return value: An HTML table for a single row of data.
Return all records and field values
Return number of header field names found
FILE* f |
file to read from |
Read a CSV row from the file.
Return value: the values read
FILE* f |
file to write to |
const HashArray& fields |
values to write |
Write a CSV row to the file
Return value: number of bytes output
bool dbLoaded |
Flag: has data been loaded from a file |
string csvfilename |
Filename of data loaded |
HashArray fields |
Field names |
HashArray fieldsOrig |
Field names |
string values |
|
int nextIdvalue |
Next row record id to use |
HashArray& row |
an indexed array |
Change an array of field indices to a hashed array using field names
Return value: A hashed array (using field names)
bool multiples |
find multiple matches, if present |
bool exact |
must match case exactly |
HashArray& result |
search results for one row |
HashArrayArray& rows |
search results for multiple rows |
string field1 |
the name (case ignored) of the first field criteria |
const string& value1 |
the value of the first field criteria |
string field2 |
(optional) the name (case ignored) of the second field criteria Default value: "" |
const string& value2 |
(optional) the value of the first second criteria Default value: "" |
string field3 |
(optional) the name (case ignored) of the third field criteria Default value: "" |
const string& value3 |
(optional) the value of the first third criteria Default value: "" |
Process the inputs for the find*Record*() methods.
Locate record(s) given one, two, or three fields of criteria.
The matching record must match all criteria provided.
Return value: If true, then result is a hashed array with all key-value pairs where the keys are field names from the matching row.
Get next Id value for an appended record. Each record needs a unique id. We issue them in sequence; we need to know the next available sequence number.
Return value: An integer, which is the next unique id for a db row
Load the first line of a file - the header with field names