Class CsvWORM

Index Home MAE > MAE Architecture > MAE Data Types > Class Index > Class CsvWORM

Public Methods
CsvWORM(filename)
inline const string& getFilename() const
bool valid()
bool create(fields)
int getFieldId(field)
HashArray getFieldNames()
int findMatch(criteria, result, rows, multiples, exact) const
HashArray findRecord(field1, value1, field2, value2, field3, value3) const
HashArray findExactRecord(field1, value1, field2, value2, field3, value3) const
HashArrayArray findRecords(field1, value1, field2, value2, field3, value3) const
HashArrayArray findExactRecords(field1, value1, field2, value2, field3, value3) const
HashArray findRecordById(id)
inline int getFieldCount()
inline HashArray get(id)
const string& get(id, field)
int getInt(id, field)
bool getBool(id, field)
int insertRecord(values)
static void dumpRow(row, outf)
static void dumpRows(rows, outf)
HashArrayArray findAll() const
inline int getHeaderCount() const
static HashArray fgetcsv(f)
static int fputcsv(f, fields)
Private Properties
Private Methods
HashArray rowIndicesToFields(row) const
int internalFind(multiples, exact, result, rows, field1, value1, field2, value2, field3, value3) const
int nextId()
void loadFields()

#include <CsvWorm.h >

Public Methods

CsvWORM(filename)

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)

inline const string& getFilename() const

Return the filename for this CSV file

Return value: the filename

bool valid()

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

bool create(fields)

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

int getFieldId(field)

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

HashArray getFieldNames()

Return ordered list of fields for this DB with proper case.

Return value: A simple array of field names.

int findMatch(criteria, result, rows, multiples, exact) const

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.

HashArray findRecord(field1, value1, field2, value2, field3, value3) const

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.

HashArray findExactRecord(field1, value1, field2, value2, field3, value3) const

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.

HashArrayArray findRecords(field1, value1, field2, value2, field3, value3) const

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.

HashArrayArray findExactRecords(field1, value1, field2, value2, field3, value3) const

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.

HashArray findRecordById(id)

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.

inline int getFieldCount()

Get the number of headers in use

Return value: header count

inline HashArray get(id)

int id

the row id to fetch


Given a row id, return the field values.

Return value: a HashArray of field/value pairs

const string& get(id, field)

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 getInt(id, field)

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

bool getBool(id, field)

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

int insertRecord(values)

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+)

static void dumpRow(row, outf)

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.

static void dumpRows(rows, outf)

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.

HashArrayArray findAll() const

Return all records and field values

inline int getHeaderCount() const

Return number of header field names found

static HashArray fgetcsv(f)

FILE* f

file to read from


Read a CSV row from the file.

Return value: the values read

static int fputcsv(f, fields)

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

Private Properties

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


Private Methods

HashArray rowIndicesToFields(row) const

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)

int internalFind(multiples, exact, result, rows, field1, value1, field2, value2, field3, value3) const

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.

int nextId()

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

void loadFields()

Load the first line of a file - the header with field names