using System;
using System.Collections.Generic;
using Migrator.Framework;
namespace Migrator.Providers
{
///
/// This is basically a just a helper base class
/// per-database implementors may want to override ColumnSql
///
public class ColumnPropertiesMapper
{
protected Dialect dialect;
/// The SQL type
protected string type;
/// The name of the column
protected string name;
///
/// the type of the column
///
protected string columnSql;
///
/// Sql if This column is Indexed
///
protected bool indexed = false;
///
/// Sql if this column has a default value
///
protected object defaultVal;
public ColumnPropertiesMapper(Dialect dialect, string type)
{
this.dialect = dialect;
this.type = type;
}
///
/// The sql for this column, override in database-specific implementation classes
///
public virtual string ColumnSql
{
get { return columnSql; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public object Default
{
get { return defaultVal; }
set { defaultVal = value; }
}
public string QuotedName
{
get { return dialect.Quote(Name); }
}
public string IndexSql
{
get
{
if (dialect.SupportsIndex && indexed)
return String.Format("INDEX({0})", dialect.Quote(name));
return null;
}
}
public void MapColumnProperties(Column column)
{
Name = column.Name;
indexed = PropertySelected(column.ColumnProperty, ColumnProperty.Indexed);
List vals = new List();
vals.Add(dialect.ColumnNameNeedsQuote ? QuotedName : Name);
vals.Add(type);
if (! dialect.IdentityNeedsType)
AddValueIfSelected(column, ColumnProperty.Identity, vals);
AddValueIfSelected(column, ColumnProperty.Unsigned, vals);
if (! PropertySelected(column.ColumnProperty, ColumnProperty.PrimaryKey) || dialect.NeedsNotNullForIdentity)
AddValueIfSelected(column, ColumnProperty.NotNull, vals);
AddValueIfSelected(column, ColumnProperty.PrimaryKey, vals);
if (dialect.IdentityNeedsType)
AddValueIfSelected(column, ColumnProperty.Identity, vals);
AddValueIfSelected(column, ColumnProperty.Unique, vals);
AddValueIfSelected(column, ColumnProperty.ForeignKey, vals);
if (column.DefaultValue != null)
vals.Add(dialect.Default(column.DefaultValue));
columnSql = String.Join(" ", vals.ToArray());
}
private void AddValueIfSelected(Column column, ColumnProperty property, ICollection vals)
{
if (PropertySelected(column.ColumnProperty, property))
vals.Add(dialect.SqlForProperty(property));
}
public static bool PropertySelected(ColumnProperty source, ColumnProperty comparison)
{
return (source & comparison) == comparison;
}
}
}