/* Copyright (C) 2008 - 2011 Jordan Marr
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see . */
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Data.Common;
namespace Marr.Data.Mapping
{
///
/// This class contains a list of column mappings.
/// It also provides various methods to filter the collection.
///
public class ColumnMapCollection : List
{
#region - Filters -
public ColumnMap GetByColumnName(string columnName)
{
return Find(m => m.ColumnInfo.Name == columnName);
}
public ColumnMap GetByFieldName(string fieldName)
{
return Find(m => m.FieldName == fieldName);
}
///
/// Iterates through all fields marked as return values.
///
public IEnumerable ReturnValues
{
get
{
foreach (ColumnMap map in this)
if (map.ColumnInfo.ReturnValue)
yield return map;
}
}
///
/// Iterates through all fields that are not return values.
///
public ColumnMapCollection NonReturnValues
{
get
{
ColumnMapCollection collection = new ColumnMapCollection();
foreach (ColumnMap map in this)
if (!map.ColumnInfo.ReturnValue)
collection.Add(map);
return collection;
}
}
///
/// Iterates through all fields marked as Output parameters or InputOutput.
///
public IEnumerable OutputFields
{
get
{
foreach (ColumnMap map in this)
if (map.ColumnInfo.ParamDirection == ParameterDirection.InputOutput ||
map.ColumnInfo.ParamDirection == ParameterDirection.Output)
yield return map;
}
}
///
/// Iterates through all fields marked as primary keys.
///
public ColumnMapCollection PrimaryKeys
{
get
{
ColumnMapCollection keys = new ColumnMapCollection();
foreach (ColumnMap map in this)
if (map.ColumnInfo.IsPrimaryKey)
keys.Add(map);
return keys;
}
}
///
/// Parses and orders the parameters from the query text.
/// Filters the list of mapped columns to match the parameters found in the sql query.
/// All parameters starting with the '@' or ':' symbol are matched and returned.
///
/// The command and parameters that are being parsed.
/// A list of mapped columns that are present in the sql statement as parameters.
public ColumnMapCollection OrderParameters(DbCommand command)
{
if (command.CommandType == CommandType.Text && Count > 0)
{
string commandTypeString = command.GetType().ToString();
if (commandTypeString.Contains("Oracle") || commandTypeString.Contains("OleDb"))
{
ColumnMapCollection columns = new ColumnMapCollection();
// Find all @Parameters contained in the sql statement
string paramPrefix = commandTypeString.Contains("Oracle") ? ":" : "@";
string regexString = string.Format(@"{0}[\w-]+", paramPrefix);
Regex regex = new Regex(regexString);
foreach (Match m in regex.Matches(command.CommandText))
{
ColumnMap matchingColumn = Find(c => string.Concat(paramPrefix, c.ColumnInfo.Name.ToLower()) == m.Value.ToLower());
if (matchingColumn != null)
columns.Add(matchingColumn);
}
return columns;
}
}
return this;
}
#endregion
#region - Actions -
///
/// Set's each column's altname as the given prefix + the column name.
/// Ex:
/// Original column name: "ID"
/// Passed in prefix: "PRODUCT_"
/// Generated AltName: "PRODUCT_ID"
///
/// The given prefix.
///
public ColumnMapCollection PrefixAltNames(string prefix)
{
ForEach(c => c.ColumnInfo.AltName = c.ColumnInfo.Name.Insert(0, prefix));
return this;
}
///
/// Set's each column's altname as the column name + the given prefix.
/// Ex:
/// Original column name: "ID"
/// Passed in suffix: "_PRODUCT"
/// Generated AltName: "ID_PRODUCT"
///
///
///
public ColumnMapCollection SuffixAltNames(string suffix)
{
ForEach(c => c.ColumnInfo.AltName = c.ColumnInfo.Name + suffix);
return this;
}
#endregion
}
}