C# Class Azavea.Open.DAO.CSV.CsvDaLayer

Data layer that implements reading/writing/modifying CSV files. NOTE: Updates/Deletes will require re-writing the file, so expect some churn if you're doing a lot of those. Inserts will be appended to the end of the file.
Inheritance: Azavea.Open.DAO.Unqueryable.UnqueryableDaLayer
Mostra file Open project: azavea/net-dao-csv Class Usage Examples

Protected Properties

Property Type Description
_connDesc CsvDescriptor

Public Methods

Method Description
CoerceType ( Type desiredType, object input ) : object

Overridden to handle the case of converting an empty string to a non-string datatype. It's a slow check because you have to trim, and check type, etc, which is why it isn't in the base class. But CSV files apparently frequently use "" for blank numerical values which causes the base class implementation to error out.

CsvDaLayer ( CsvDescriptor connDesc ) : System

Instantiates the data access layer with the connection descriptor for the DB.

Delete ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit ) : int

Deletes a data object record using the mapping and criteria for what's deleted.

ExecuteQuery ( ITransaction transaction, ClassMapping mapping, IDaQuery query, DataReaderDelegate invokeMe, Hashtable parameters ) : void

Executes a query and invokes a method with a DataReader of results.

GetCount ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit, ICollection groupExpressions ) : List

Gets a count of records for the given criteria, aggregated by the given grouping expressions. This matches "GROUP BY" behavior in SQL.

GetCount ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit ) : int

Gets a count of records for the given criteria.

GetLastAutoGeneratedId ( ITransaction transaction, ClassMapping mapping, string idCol ) : object

Finds the last generated id number for a column.

GetNextSequenceValue ( ITransaction transaction, string sequenceName ) : int

Gets the next id number from a sequence in the data source.

Insert ( ITransaction transaction, ClassMapping mapping, object>.IDictionary propValues ) : int

Inserts a data object record using the "table" and a list of column/value pairs.

InsertBatch ( ITransaction transaction, ClassMapping mapping, object>.List propValueDictionaries ) : void

Inserts a list of data object records of the same type.

Truncate ( ClassMapping mapping ) : void

Blanks the file, leaving nothing but the header row (if there is one).

Update ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit, object>.IDictionary propValues ) : int

Updates a data object record using the "table" and a list of column/value pairs.

UpdateBatch ( ITransaction transaction, ClassMapping mapping, List criteriaList, object>.List propValueDictionaries ) : void

Updates a list of data object records of the same type. NOTE: At the moment this just loops calling Update().

Protected Methods

Method Description
DoneWithReader ( StreamReader reader ) : void

Returns the reader for closing (or not, if we were configured with it).

DoneWithWriter ( WriterInfo info ) : void

Returns the writer for closing (or not, if we were configured with it).

GetReader ( ClassMapping mapping ) : StreamReader

Gets a valid StreamReader that can be used to read CSV data. If we were configured with a StreamReader, it may be that one. If we are accessing a file we will open a new reader. You should not close it yourself, instead you should call DoneWithReader when you are done with it.

GetWriter ( ClassMapping mapping, bool append ) : WriterInfo

Gets a valid TextWriter that can be used to output CSV data. If we were configured with a TextWriter, it may be that one. If we are accessing a file we will open a new writer. You should not close it yourself, instead you should call DoneWithWriter when you are done with it.

UseNamedColumns ( ClassMapping mapping ) : bool

Checks whether this mapping is using column indexes or named columns. If named columns, returns true (and there must be a header row). If numerically indexed columns, returns false (we assume there is not a header row).

Private Methods

Method Description
GetFileName ( ClassMapping mapping ) : string
MakeDataRow ( ClassMapping mapping, object>.IDictionary values ) : string

Creates a new row for the CSV file. Will create blanks (",,") if using numeric indexing and the numbers are not consecutive.

MakeHeaderRow ( ClassMapping mapping ) : string

Creates the first row of the CSV file, the header row.

QuoteValue ( object val ) : string

Uses the OutputQuoteLevel on the connection descriptor plus the value itself to determine if we need to quote it, and if so quote it. Also converts the value to a string, and if it is already a string, escapes internal quotes in it.

Method Details

CoerceType() public method

Overridden to handle the case of converting an empty string to a non-string datatype. It's a slow check because you have to trim, and check type, etc, which is why it isn't in the base class. But CSV files apparently frequently use "" for blank numerical values which causes the base class implementation to error out.
public CoerceType ( Type desiredType, object input ) : object
desiredType System.Type Type we need the value to be.
input object Input value, may or may not already be the right type.
return object

CsvDaLayer() public method

Instantiates the data access layer with the connection descriptor for the DB.
public CsvDaLayer ( CsvDescriptor connDesc ) : System
connDesc CsvDescriptor The connection descriptor that is being used by this FastDaoLayer.
return System

Delete() public method

Deletes a data object record using the mapping and criteria for what's deleted.
public Delete ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit ) : int
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The mapping of the table from which to delete.
crit Azavea.Open.DAO.Criteria.DaoCriteria Criteria for deletion. NOTE: Only the expressions are observed, /// other things (like "order" or start / limit) are ignored. /// WARNING: A null or empty (no expression) criteria will /// delete ALL records!
return int

DoneWithReader() protected method

Returns the reader for closing (or not, if we were configured with it).
protected DoneWithReader ( StreamReader reader ) : void
reader System.IO.StreamReader The StreamReader obtained from a GetReader call.
return void

DoneWithWriter() protected method

Returns the writer for closing (or not, if we were configured with it).
protected DoneWithWriter ( WriterInfo info ) : void
info WriterInfo The WriterInfo obtained from a GetWriter call.
return void

ExecuteQuery() public method

Executes a query and invokes a method with a DataReader of results.
public ExecuteQuery ( ITransaction transaction, ClassMapping mapping, IDaQuery query, DataReaderDelegate invokeMe, Hashtable parameters ) : void
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping Class mapping for the table we're querying against. Optional, /// but not all columns may be properly typed if it is null.
query IDaQuery The query to execute, should have come from CreateQuery.
invokeMe DataReaderDelegate The method to invoke with the IDataReader results.
parameters System.Collections.Hashtable A hashtable containing any values that need to be persisted through invoked method. /// The list of objects from the query will be placed here.
return void

GetCount() public method

Gets a count of records for the given criteria, aggregated by the given grouping expressions. This matches "GROUP BY" behavior in SQL.
public GetCount ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit, ICollection groupExpressions ) : List
transaction ITransaction The transaction to do this as part of.
mapping ClassMapping The mapping of the table for which to build the query string.
crit Azavea.Open.DAO.Criteria.DaoCriteria The criteria to use for "where" comparisons.
groupExpressions ICollection The fields/expressions to aggregate on when counting.
return List

GetCount() public method

Gets a count of records for the given criteria.
public GetCount ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit ) : int
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The mapping of the table for which to build the query string.
crit Azavea.Open.DAO.Criteria.DaoCriteria The criteria to use for "where" comparisons.
return int

GetLastAutoGeneratedId() public method

Finds the last generated id number for a column.
public GetLastAutoGeneratedId ( ITransaction transaction, ClassMapping mapping, string idCol ) : object
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The class mapping for the table being queried.
idCol string The ID column for which to find the last-generated ID.
return object

GetNextSequenceValue() public method

Gets the next id number from a sequence in the data source.
public GetNextSequenceValue ( ITransaction transaction, string sequenceName ) : int
transaction ITransaction Should be null, transactions are not supported.
sequenceName string The name of the sequence.
return int

GetReader() protected method

Gets a valid StreamReader that can be used to read CSV data. If we were configured with a StreamReader, it may be that one. If we are accessing a file we will open a new reader. You should not close it yourself, instead you should call DoneWithReader when you are done with it.
protected GetReader ( ClassMapping mapping ) : StreamReader
mapping ClassMapping The mapping for the object we intend to read.
return System.IO.StreamReader

GetWriter() protected method

Gets a valid TextWriter that can be used to output CSV data. If we were configured with a TextWriter, it may be that one. If we are accessing a file we will open a new writer. You should not close it yourself, instead you should call DoneWithWriter when you are done with it.
protected GetWriter ( ClassMapping mapping, bool append ) : WriterInfo
mapping ClassMapping The mapping for the object we intend to write.
append bool Whether to append to, or overwrite, the file if it exists. /// If it does not exist, this parameter doesn't matter and a new /// file is created.
return WriterInfo

Insert() public method

Inserts a data object record using the "table" and a list of column/value pairs.
public Insert ( ITransaction transaction, ClassMapping mapping, object>.IDictionary propValues ) : int
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The mapping of the table or other data container we're dealing with.
propValues object>.IDictionary A dictionary of "column"/value pairs for the object to insert.
return int

InsertBatch() public method

Inserts a list of data object records of the same type.
public InsertBatch ( ITransaction transaction, ClassMapping mapping, object>.List propValueDictionaries ) : void
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The mapping of the table or other data container we're dealing with.
propValueDictionaries object>.List A list of dictionaries of column/value pairs. /// Each item in the list should represent the dictionary of column/value pairs for /// each respective object being inserted.
return void

Truncate() public method

Blanks the file, leaving nothing but the header row (if there is one).
public Truncate ( ClassMapping mapping ) : void
mapping ClassMapping
return void

Update() public method

Updates a data object record using the "table" and a list of column/value pairs.
public Update ( ITransaction transaction, ClassMapping mapping, DaoCriteria crit, object>.IDictionary propValues ) : int
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The mapping of the table or other data container we're dealing with.
crit Azavea.Open.DAO.Criteria.DaoCriteria All records matching this criteria will be updated per the dictionary of /// values.
propValues object>.IDictionary A dictionary of column/value pairs for all non-ID columns to be updated.
return int

UpdateBatch() public method

Updates a list of data object records of the same type. NOTE: At the moment this just loops calling Update().
public UpdateBatch ( ITransaction transaction, ClassMapping mapping, List criteriaList, object>.List propValueDictionaries ) : void
transaction ITransaction Should be null, transactions are not supported.
mapping ClassMapping The mapping of the table or other data container we're dealing with.
criteriaList List A list of DaoCriteria. /// Each item in the list should represent the criteria for /// rows that will be updated per the accompanying dictionary.
propValueDictionaries object>.List A list of dictionaries of column/value pairs. /// Each item in the list should represent the dictionary of non-ID column/value pairs for /// each respective object being updated.
return void

UseNamedColumns() protected static method

Checks whether this mapping is using column indexes or named columns. If named columns, returns true (and there must be a header row). If numerically indexed columns, returns false (we assume there is not a header row).
protected static UseNamedColumns ( ClassMapping mapping ) : bool
mapping ClassMapping Mapping for this file.
return bool

Property Details

_connDesc protected_oe property

We want to treat it as a CSV descriptor rather than a generic connection descriptor.
protected CsvDescriptor,Azavea.Open.DAO.CSV _connDesc
return CsvDescriptor